From 2d029d20ac48d351e2d567c15c8cdb5f3b91f4ae Mon Sep 17 00:00:00 2001 From: Thomas Beckers Date: Thu, 19 Nov 2020 11:42:53 +0100 Subject: [PATCH] Re-cleaned error checking --- .../Services/INotificationService.cs | 6 +-- .../Notifications/NotificationBuilder.cs | 42 +++---------------- .../Services/UserStreamingHandlingService.cs | 2 +- Swabbr.Core/Services/VlogTriggerService.cs | 2 +- .../Notifications/NotificationClient.cs | 21 ++-------- .../Notifications/NotificationService.cs | 24 +++++------ 6 files changed, 26 insertions(+), 71 deletions(-) diff --git a/Swabbr.Core/Interfaces/Services/INotificationService.cs b/Swabbr.Core/Interfaces/Services/INotificationService.cs index 4ba26a71..67c477bd 100644 --- a/Swabbr.Core/Interfaces/Services/INotificationService.cs +++ b/Swabbr.Core/Interfaces/Services/INotificationService.cs @@ -20,9 +20,8 @@ public interface INotificationService /// Send a vlog record request notification. /// /// User id to notify. - /// The livestream id. /// The recording parameters. - Task NotifyVlogRecordRequestAsync(Guid userId, Guid livestreamId, ParametersRecordVlog pars); + Task NotifyVlogRecordRequestAsync(Guid userId, ParametersRecordVlog pars); /// /// Send a vlog record timeout notification. @@ -34,9 +33,8 @@ public interface INotificationService /// Notify all followers of a user that the user is live. /// /// User that is live. - /// The livestream id. /// The livestream parameters. - Task NotifyFollowersProfileLiveAsync(Guid userId, Guid livestreamId, ParametersFollowedProfileLive pars); + Task NotifyFollowersProfileLiveAsync(Guid userId, ParametersFollowedProfileLive pars); /// /// Notify all followers of a user that a new vlog was posted. diff --git a/Swabbr.Core/Notifications/NotificationBuilder.cs b/Swabbr.Core/Notifications/NotificationBuilder.cs index 4aec6210..3cf51c52 100644 --- a/Swabbr.Core/Notifications/NotificationBuilder.cs +++ b/Swabbr.Core/Notifications/NotificationBuilder.cs @@ -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 @@ -23,18 +21,12 @@ public static class NotificationBuilder /// The corresponding vlog. /// Notification object. 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); - } /// /// Build a notification for indicating that a followed @@ -44,16 +36,11 @@ public static SwabbrNotification BuildFollowedProfileLive(Guid liveUserId, Guid /// The vlog owner. /// Notification object. 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); - } /// /// Build a notification for indicating that a user @@ -65,20 +52,13 @@ public static SwabbrNotification BuildFollowedProfileVlogPosted(Guid vlogId, Gui /// The timeout of the request. /// Notification object. 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); - } /// /// Build a notification for indicating that a vlog @@ -88,16 +68,11 @@ public static SwabbrNotification BuildRecordVlog(Guid livestreamId, Guid vlogId, /// The user that liked. /// Notification object. 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); - } /// /// Build a notification for indicating that a user @@ -107,15 +82,10 @@ public static SwabbrNotification BuildVlogGainedLike(Guid vlogId, Guid userThatL /// The reaction id. /// Notification object. 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); - } } } diff --git a/Swabbr.Core/Services/UserStreamingHandlingService.cs b/Swabbr.Core/Services/UserStreamingHandlingService.cs index 8fb92378..df945e89 100644 --- a/Swabbr.Core/Services/UserStreamingHandlingService.cs +++ b/Swabbr.Core/Services/UserStreamingHandlingService.cs @@ -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, diff --git a/Swabbr.Core/Services/VlogTriggerService.cs b/Swabbr.Core/Services/VlogTriggerService.cs index ee198db1..5284652a 100644 --- a/Swabbr.Core/Services/VlogTriggerService.cs +++ b/Swabbr.Core/Services/VlogTriggerService.cs @@ -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(); diff --git a/Swabbr.Infrastructure/Notifications/NotificationClient.cs b/Swabbr.Infrastructure/Notifications/NotificationClient.cs index d6471c81..1da73fb8 100644 --- a/Swabbr.Infrastructure/Notifications/NotificationClient.cs +++ b/Swabbr.Infrastructure/Notifications/NotificationClient.cs @@ -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; @@ -80,7 +78,7 @@ internal async Task IsServiceAvailableAsync() /// Our internal registration object. internal async Task RegisterAsync(NotificationRegistration internalRegistration) { - if (internalRegistration == null) + if (internalRegistration is null) { throw new ArgumentNullException(nameof(internalRegistration)); } @@ -111,7 +109,7 @@ internal async Task RegisterAsync(NotificationRegistra /// Internal registration object. internal async Task UnregisterAsync(NotificationRegistration internalRegistration) { - if (internalRegistration == null) + if (internalRegistration is null) { throw new ArgumentNullException(nameof(internalRegistration)); } @@ -147,8 +145,6 @@ internal async Task UnregisterAsync(NotificationRegistration internalRegistratio /// The notification object. internal async Task SendNotificationAsync(Guid userId, PushNotificationPlatform platform, SwabbrNotification notification) { - userId.ThrowIfNullOrEmpty(); - switch (platform) { case PushNotificationPlatform.APNS: @@ -173,15 +169,11 @@ internal async Task SendNotificationAsync(Guid userId, PushNotificationPlatform /// Notification hub registration. 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 { notificationRegistration.UserId.ToString() }; @@ -203,11 +195,6 @@ private static RegistrationDescription ExtractForCreation(NotificationRegistrati /// /// The internal user id to check. private async Task 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(); } } diff --git a/Swabbr.Infrastructure/Notifications/NotificationService.cs b/Swabbr.Infrastructure/Notifications/NotificationService.cs index 51249641..95d5b8ca 100644 --- a/Swabbr.Infrastructure/Notifications/NotificationService.cs +++ b/Swabbr.Infrastructure/Notifications/NotificationService.cs @@ -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; @@ -54,16 +53,15 @@ public virtual Task IsServiceOnlineAsync() /// Notify all followers of a user that the user is live. /// /// User that is live. - /// The livestream id. /// The livestream parameters. - 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); @@ -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}"); } /// @@ -102,21 +100,23 @@ public virtual async Task NotifyFollowersVlogPostedAsync(Guid userId, Guid vlogI /// Send a vlog record request notification. /// /// User id to notify. - /// The livestream id. /// The recording parameters. - 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}"); } ///