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

Commit

Permalink
Removed unnecessary checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tabeckers committed Nov 19, 2020
1 parent 13278c9 commit 67d8814
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ dotnet_diagnostic.CS1591.severity = none
dotnet_diagnostic.CA1031.severity = none

# CA1051: Do not declare visible instance fields
dotnet_code_quality.ca1051.api_surface = private, internal, protected
dotnet_diagnostic.CA1051.api_surface = private, internal, protected
24 changes: 0 additions & 24 deletions Swabbr.Core/Utility/NotificationRegistrationExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ internal static class NotificationJsonExtractor
/// <param name="platform"><see cref="PushNotificationPlatform"/></param>
/// <param name="notification"><see cref="SwabbrNotification"/></param>
/// <returns><see cref="NotificationWrapperJsonBase"/></returns>
public static NotificationWrapperJsonBase Extract(PushNotificationPlatform platform, SwabbrNotification notification)
{
if (notification == null) { throw new ArgumentNullException(nameof(notification)); }

return platform switch
public static NotificationWrapperJsonBase Extract(PushNotificationPlatform platform, SwabbrNotification notification)
=> platform switch
{
PushNotificationPlatform.APNS => FcmJsonExtractor.Extract(notification),
PushNotificationPlatform.FCM => ApnsJsonExtractor.Extract(notification),
_ => throw new InvalidOperationException(nameof(platform)),
};
}
}
}
22 changes: 13 additions & 9 deletions Swabbr.Infrastructure/Notifications/NotificationClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ internal async Task<NotificationRegistration> RegisterAsync(NotificationRegistra
{
throw new ArgumentNullException(nameof(internalRegistration));
}
internalRegistration.Handle.ThrowIfNullOrEmpty();

// Create new registration and return the converted result
var externalRegistration = await _hubClient.CreateRegistrationAsync(ExtractForCreation(internalRegistration)).ConfigureAwait(false);
Expand Down Expand Up @@ -112,8 +111,10 @@ internal async Task<NotificationRegistration> RegisterAsync(NotificationRegistra
/// <param name="internalRegistration">Internal registration object.</param>
internal async Task UnregisterAsync(NotificationRegistration internalRegistration)
{
if (internalRegistration == null) { throw new ArgumentNullException(nameof(internalRegistration)); }
internalRegistration.ThrowIfInvalid();
if (internalRegistration == null)
{
throw new ArgumentNullException(nameof(internalRegistration));
}

// Throw if no registration exists
if (!await IsRegisteredAsync(internalRegistration.UserId).ConfigureAwait(false))
Expand Down Expand Up @@ -147,11 +148,6 @@ internal async Task UnregisterAsync(NotificationRegistration internalRegistratio
internal async Task SendNotificationAsync(Guid userId, PushNotificationPlatform platform, SwabbrNotification notification)
{
userId.ThrowIfNullOrEmpty();
if (notification == null)
{
throw new ArgumentNullException(nameof(notification));
}
// TODO Validate notification

switch (platform)
{
Expand Down Expand Up @@ -181,6 +177,9 @@ private static RegistrationDescription ExtractForCreation(NotificationRegistrati
{
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)
Expand All @@ -204,6 +203,11 @@ private static RegistrationDescription ExtractForCreation(NotificationRegistrati
/// </remarks>
/// <param name="userId">The internal user id to check.</param>
private async Task<bool> IsRegisteredAsync(Guid userId)
=> (await _hubClient.GetRegistrationsByTagAsync(userId.ToString(), 0).ConfigureAwait(false)).Any();
{
// Avoid checking for empty registrations
userId.ThrowIfNullOrEmpty();

return (await _hubClient.GetRegistrationsByTagAsync(userId.ToString(), 0).ConfigureAwait(false)).Any();
}
}
}
17 changes: 0 additions & 17 deletions Swabbr.Infrastructure/Notifications/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public virtual Task<bool> IsServiceOnlineAsync()
/// <param name="pars">The livestream parameters.</param>
public virtual async Task NotifyFollowersProfileLiveAsync(Guid userId, Guid livestreamId, ParametersFollowedProfileLive pars)
{
userId.ThrowIfNullOrEmpty();
livestreamId.ThrowIfNullOrEmpty();
if (pars == null)
{
throw new ArgumentNullException(nameof(pars));
Expand All @@ -86,9 +84,6 @@ public virtual async Task NotifyFollowersProfileLiveAsync(Guid userId, Guid live
/// <param name="vlogId">The posted vlog id.</param>
public virtual async Task NotifyFollowersVlogPostedAsync(Guid userId, Guid vlogId)
{
userId.ThrowIfNullOrEmpty();
vlogId.ThrowIfNullOrEmpty();

_logger.LogTrace($"{nameof(NotifyFollowersVlogPostedAsync)} - Attempting notifying followers for posted vlog {vlogId} from user {userId}");

// Notify each follower individually.
Expand All @@ -111,8 +106,6 @@ public virtual async Task NotifyFollowersVlogPostedAsync(Guid userId, Guid vlogI
/// <param name="pars">The recording parameters.</param>
public virtual async Task NotifyVlogRecordRequestAsync(Guid userId, Guid livestreamId, ParametersRecordVlog pars)
{
userId.ThrowIfNullOrEmpty();
livestreamId.ThrowIfNullOrEmpty();
pars.Validate();

_logger.LogTrace($"{nameof(NotifyVlogRecordRequestAsync)} - Attempting vlog record request for livestream {livestreamId} to user {userId}");
Expand All @@ -135,10 +128,6 @@ public virtual async Task NotifyVlogRecordRequestAsync(Guid userId, Guid livestr
/// <param name="reactionId">The placed reaction id.</param>
public virtual async Task NotifyReactionPlacedAsync(Guid receivingUserId, Guid vlogId, Guid reactionId)
{
receivingUserId.ThrowIfNullOrEmpty();
vlogId.ThrowIfNullOrEmpty();
reactionId.ThrowIfNullOrEmpty();

_logger.LogTrace($"{nameof(NotifyReactionPlacedAsync)} - Attempting vlog reaction notification for reaction {reactionId}");

var userPushDetails = await _userRepository.GetPushDetailsAsync(receivingUserId).ConfigureAwait(false);
Expand All @@ -160,9 +149,6 @@ public virtual async Task NotifyVlogLikedAsync(Guid receivingUserId, VlogLikeId
{
throw new ArgumentNullException(nameof(vlogLikeId));
}
vlogLikeId.UserId.ThrowIfNullOrEmpty();
vlogLikeId.VlogId.ThrowIfNullOrEmpty();
receivingUserId.ThrowIfNullOrEmpty();

_logger.LogTrace($"{nameof(NotifyVlogLikedAsync)} - Attempting vlog like notification for vlog like {vlogLikeId}");

Expand All @@ -188,9 +174,6 @@ public virtual Task NotifyVlogRecordTimeoutAsync(Guid userId)
/// <param name="handle">Device handle.</param>
public virtual async Task RegisterAsync(Guid userId, PushNotificationPlatform platform, string handle)
{
userId.ThrowIfNullOrEmpty();
handle.ThrowIfNullOrEmpty();

// First clear the existing registration if it exists
var registrations = await _notificationRegistrationRepository.GetRegistrationsForUserAsync(userId).ConfigureAwait(false);
if (registrations.Any())
Expand Down

0 comments on commit 67d8814

Please sign in to comment.