Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Re-cleaned error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
tabeckers committed Nov 19, 2020
1 parent 67d8814 commit 2d029d2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 71 deletions.
6 changes: 2 additions & 4 deletions Swabbr.Core/Interfaces/Services/INotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ public interface INotificationService
/// Send a vlog record request notification.
/// </summary>
/// <param name="userId">User id to notify.</param>
/// <param name="livestreamId">The livestream id.</param>
/// <param name="pars">The recording parameters.</param>
Task NotifyVlogRecordRequestAsync(Guid userId, Guid livestreamId, ParametersRecordVlog pars);
Task NotifyVlogRecordRequestAsync(Guid userId, ParametersRecordVlog pars);

/// <summary>
/// Send a vlog record timeout notification.
Expand All @@ -34,9 +33,8 @@ public interface INotificationService
/// Notify all followers of a user that the user is live.
/// </summary>
/// <param name="userId">User that is live.</param>
/// <param name="livestreamId">The livestream id.</param>
/// <param name="pars">The livestream parameters.</param>
Task NotifyFollowersProfileLiveAsync(Guid userId, Guid livestreamId, ParametersFollowedProfileLive pars);
Task NotifyFollowersProfileLiveAsync(Guid userId, ParametersFollowedProfileLive pars);

/// <summary>
/// Notify all followers of a user that a new vlog was posted.
Expand Down
42 changes: 6 additions & 36 deletions Swabbr.Core/Notifications/NotificationBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Swabbr.Core.Extensions;
using Swabbr.Core.Notifications.JsonWrappers;
using Swabbr.Core.Utility;
using Swabbr.Core.Notifications.JsonWrappers;
using System;

namespace Swabbr.Core.Notifications
Expand All @@ -23,18 +21,12 @@ public static class NotificationBuilder
/// <param name="liveVlogId">The corresponding vlog.</param>
/// <returns>Notification object.</returns>
public static SwabbrNotification BuildFollowedProfileLive(Guid liveUserId, Guid livestreamId, Guid liveVlogId)
{
liveUserId.ThrowIfNullOrEmpty();
livestreamId.ThrowIfNullOrEmpty();
liveVlogId.ThrowIfNullOrEmpty();

return new SwabbrNotification(NotificationAction.FollowedProfileLive, new ParametersFollowedProfileLive
=> new SwabbrNotification(NotificationAction.FollowedProfileLive, new ParametersFollowedProfileLive
{
LiveLivestreamId = livestreamId,
LiveUserId = liveUserId,
LiveVlogId = liveVlogId
}, title: DefaultTitle, message: DefaultTitle);
}

/// <summary>
/// Build a notification for indicating that a followed
Expand All @@ -44,16 +36,11 @@ public static SwabbrNotification BuildFollowedProfileLive(Guid liveUserId, Guid
/// <param name="vlogOwnerUserId">The vlog owner.</param>
/// <returns>Notification object.</returns>
public static SwabbrNotification BuildFollowedProfileVlogPosted(Guid vlogId, Guid vlogOwnerUserId)
{
vlogId.ThrowIfNullOrEmpty();
vlogOwnerUserId.ThrowIfNullOrEmpty();

return new SwabbrNotification(NotificationAction.FollowedProfileVlogPosted, new ParametersFollowedProfileVlogPosted
=> new SwabbrNotification(NotificationAction.FollowedProfileVlogPosted, new ParametersFollowedProfileVlogPosted
{
VlogId = vlogId,
VlogOwnerUserId = vlogOwnerUserId
}, title: DefaultTitle, message: DefaultTitle);
}

/// <summary>
/// Build a notification for indicating that a user
Expand All @@ -65,20 +52,13 @@ public static SwabbrNotification BuildFollowedProfileVlogPosted(Guid vlogId, Gui
/// <param name="requestTimeout">The timeout of the request.</param>
/// <returns>Notification object.</returns>
public static SwabbrNotification BuildRecordVlog(Guid livestreamId, Guid vlogId, DateTimeOffset requestMoment, TimeSpan requestTimeout)
{
livestreamId.ThrowIfNullOrEmpty();
vlogId.ThrowIfNullOrEmpty();
requestMoment.ThrowIfNullOrEmpty();
// TODO Timespan nullcheck

return new SwabbrNotification(NotificationAction.VlogRecordRequest, new ParametersRecordVlog
=> new SwabbrNotification(NotificationAction.VlogRecordRequest, new ParametersRecordVlog
{
LivestreamId = livestreamId,
RequestMoment = requestMoment,
RequestTimeout = requestTimeout,
VlogId = vlogId
}, title: DefaultTitle, message: DefaultTitle);
}

/// <summary>
/// Build a notification for indicating that a vlog
Expand All @@ -88,16 +68,11 @@ public static SwabbrNotification BuildRecordVlog(Guid livestreamId, Guid vlogId,
/// <param name="userThatLikedId">The user that liked.</param>
/// <returns>Notification object.</returns>
public static SwabbrNotification BuildVlogGainedLike(Guid vlogId, Guid userThatLikedId)
{
vlogId.ThrowIfNullOrEmpty();
userThatLikedId.ThrowIfNullOrEmpty();

return new SwabbrNotification(NotificationAction.VlogGainedLikes, new ParametersVlogGainedLike
=> new SwabbrNotification(NotificationAction.VlogGainedLikes, new ParametersVlogGainedLike
{
UserThatLikedId = userThatLikedId,
VlogId = vlogId
}, title: DefaultTitle, message: DefaultTitle);
}

/// <summary>
/// Build a notification for indicating that a user
Expand All @@ -107,15 +82,10 @@ public static SwabbrNotification BuildVlogGainedLike(Guid vlogId, Guid userThatL
/// <param name="reactionId">The reaction id.</param>
/// <returns>Notification object.</returns>
public static SwabbrNotification BuildVlogNewReaction(Guid vlogId, Guid reactionId)
{
vlogId.ThrowIfNullOrEmpty();
reactionId.ThrowIfNullOrEmpty();

return new SwabbrNotification(NotificationAction.VlogNewReaction, new ParametersVlogNewReaction
=> new SwabbrNotification(NotificationAction.VlogNewReaction, new ParametersVlogNewReaction
{
ReactionId = reactionId,
VlogId = vlogId
}, title: DefaultTitle, message: DefaultTitle);
}
}
}
2 changes: 1 addition & 1 deletion Swabbr.Core/Services/UserStreamingHandlingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task OnUserConnectedToLivestreamAsync(Guid userId, Guid livestreamI
await _livestreamingService.OnUserConnectedToLivestreamAsync(livestreamId, userId).ConfigureAwait(false);

// Notify all followers
await _notificationService.NotifyFollowersProfileLiveAsync(userId, livestreamId, new ParametersFollowedProfileLive
await _notificationService.NotifyFollowersProfileLiveAsync(userId, new ParametersFollowedProfileLive
{
LiveLivestreamId = livestreamId,
LiveUserId = userId,
Expand Down
2 changes: 1 addition & 1 deletion Swabbr.Core/Services/VlogTriggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task ProcessVlogTriggerForUserAsync(Guid userId, DateTimeOffset tri

// Process notifications
var parameters = await _livestreamingService.GetParametersRecordVlogAsync(livestream.Id, triggerMinute).ConfigureAwait(false);
await _notificationService.NotifyVlogRecordRequestAsync(userId, livestream.Id, parameters).ConfigureAwait(false);
await _notificationService.NotifyVlogRecordRequestAsync(userId, parameters).ConfigureAwait(false);

// First commit, then log success
scope.Complete();
Expand Down
21 changes: 4 additions & 17 deletions Swabbr.Infrastructure/Notifications/NotificationClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
using Swabbr.Core.Entities;
using Swabbr.Core.Enums;
using Swabbr.Core.Exceptions;
using Swabbr.Core.Extensions;
using Swabbr.Core.Notifications;
using Swabbr.Core.Utility;
using Swabbr.Infrastructure.Configuration;
using Swabbr.Infrastructure.Notifications.JsonExtraction;
using System;
Expand Down Expand Up @@ -80,7 +78,7 @@ internal async Task<bool> IsServiceAvailableAsync()
/// <param name="internalRegistration">Our internal registration object.</param>
internal async Task<NotificationRegistration> RegisterAsync(NotificationRegistration internalRegistration)
{
if (internalRegistration == null)
if (internalRegistration is null)
{
throw new ArgumentNullException(nameof(internalRegistration));
}
Expand Down Expand Up @@ -111,7 +109,7 @@ internal async Task<NotificationRegistration> RegisterAsync(NotificationRegistra
/// <param name="internalRegistration">Internal registration object.</param>
internal async Task UnregisterAsync(NotificationRegistration internalRegistration)
{
if (internalRegistration == null)
if (internalRegistration is null)
{
throw new ArgumentNullException(nameof(internalRegistration));
}
Expand Down Expand Up @@ -147,8 +145,6 @@ internal async Task UnregisterAsync(NotificationRegistration internalRegistratio
/// <param name="notification">The notification object.</param>
internal async Task SendNotificationAsync(Guid userId, PushNotificationPlatform platform, SwabbrNotification notification)
{
userId.ThrowIfNullOrEmpty();

switch (platform)
{
case PushNotificationPlatform.APNS:
Expand All @@ -173,15 +169,11 @@ internal async Task SendNotificationAsync(Guid userId, PushNotificationPlatform
/// <returns>Notification hub registration.</returns>
private static RegistrationDescription ExtractForCreation(NotificationRegistration notificationRegistration)
{
if (notificationRegistration == null)
if (notificationRegistration is null)
{
throw new ArgumentNullException(nameof(notificationRegistration));
}

// Prevent an invalid registration from being created
notificationRegistration.UserId.ThrowIfNullOrEmpty();
notificationRegistration.Handle.ThrowIfNullOrEmpty();

// Use the user id as tag (as recommended by Azure Notification Hub docs)
var tags = new List<string> { notificationRegistration.UserId.ToString() };

Expand All @@ -203,11 +195,6 @@ private static RegistrationDescription ExtractForCreation(NotificationRegistrati
/// </remarks>
/// <param name="userId">The internal user id to check.</param>
private async Task<bool> IsRegisteredAsync(Guid userId)
{
// Avoid checking for empty registrations
userId.ThrowIfNullOrEmpty();

return (await _hubClient.GetRegistrationsByTagAsync(userId.ToString(), 0).ConfigureAwait(false)).Any();
}
=> (await _hubClient.GetRegistrationsByTagAsync(userId.ToString(), 0).ConfigureAwait(false)).Any();
}
}
24 changes: 12 additions & 12 deletions Swabbr.Infrastructure/Notifications/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Swabbr.Core.Entities;
using Swabbr.Core.Enums;
using Swabbr.Core.Exceptions;
using Swabbr.Core.Extensions;
using Swabbr.Core.Interfaces.Repositories;
using Swabbr.Core.Interfaces.Services;
using Swabbr.Core.Notifications;
Expand Down Expand Up @@ -54,16 +53,15 @@ public virtual Task<bool> IsServiceOnlineAsync()
/// Notify all followers of a user that the user is live.
/// </summary>
/// <param name="userId">User that is live.</param>
/// <param name="livestreamId">The livestream id.</param>
/// <param name="pars">The livestream parameters.</param>
public virtual async Task NotifyFollowersProfileLiveAsync(Guid userId, Guid livestreamId, ParametersFollowedProfileLive pars)
public virtual async Task NotifyFollowersProfileLiveAsync(Guid userId, ParametersFollowedProfileLive pars)
{
if (pars == null)
if (pars is null)
{
throw new ArgumentNullException(nameof(pars));
}

_logger.LogTrace($"{nameof(NotifyFollowersProfileLiveAsync)} - Attempting notifying followers for livestream {livestreamId} from user {userId}");
_logger.LogTrace($"{nameof(NotifyFollowersProfileLiveAsync)} - Attempting notifying followers from user {userId}");

// Notify each follower individually.
var notification = NotificationBuilder.BuildFollowedProfileLive(pars.LiveUserId, pars.LiveLivestreamId, pars.LiveVlogId);
Expand All @@ -74,7 +72,7 @@ public virtual async Task NotifyFollowersProfileLiveAsync(Guid userId, Guid live
_logger.LogTrace($"{nameof(NotifyFollowersProfileLiveAsync)} - Notified user {item.UserId}");
}

_logger.LogTrace($"{nameof(NotifyFollowersProfileLiveAsync)} - Completed notifying followers for livestream {livestreamId} from user {userId}");
_logger.LogTrace($"{nameof(NotifyFollowersProfileLiveAsync)} - Completed notifying followers from user {userId}");
}

/// <summary>
Expand Down Expand Up @@ -102,21 +100,23 @@ public virtual async Task NotifyFollowersVlogPostedAsync(Guid userId, Guid vlogI
/// Send a vlog record request notification.
/// </summary>
/// <param name="userId">User id to notify.</param>
/// <param name="livestreamId">The livestream id.</param>
/// <param name="pars">The recording parameters.</param>
public virtual async Task NotifyVlogRecordRequestAsync(Guid userId, Guid livestreamId, ParametersRecordVlog pars)
public virtual async Task NotifyVlogRecordRequestAsync(Guid userId, ParametersRecordVlog pars)
{
pars.Validate();
if (pars is null)
{
throw new ArgumentNullException(nameof(pars));
}

_logger.LogTrace($"{nameof(NotifyVlogRecordRequestAsync)} - Attempting vlog record request for livestream {livestreamId} to user {userId}");
_logger.LogTrace($"{nameof(NotifyVlogRecordRequestAsync)} - Attempting vlog record request for livestream {pars.LivestreamId} to user {userId}");

if (!await _userRepository.UserExistsAsync(userId).ConfigureAwait(false)) { throw new UserNotFoundException(); }

var notification = NotificationBuilder.BuildRecordVlog(livestreamId, pars.VlogId, pars.RequestMoment, pars.RequestTimeout);
var notification = NotificationBuilder.BuildRecordVlog(pars.LivestreamId, pars.VlogId, pars.RequestMoment, pars.RequestTimeout);
var pushDetails = await _userRepository.GetPushDetailsAsync(userId).ConfigureAwait(false);
await _notificationClient.SendNotificationAsync(pushDetails.UserId, pushDetails.PushNotificationPlatform, notification).ConfigureAwait(false);

_logger.LogTrace($"{nameof(NotifyVlogRecordRequestAsync)} - Completed vlog record request for livestream {livestreamId} to user {userId}");
_logger.LogTrace($"{nameof(NotifyVlogRecordRequestAsync)} - Completed vlog record request for livestream {pars.LivestreamId} to user {userId}");
}

/// <summary>
Expand Down

0 comments on commit 2d029d2

Please sign in to comment.