Skip to content

Commit

Permalink
PM-10600: Small refactor, UTs coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mzieniukbw committed Oct 22, 2024
1 parent ab370a3 commit cf8b91e
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/Core/NotificationHub/INotificationHubPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Bit.Core.NotificationHub;

public interface INotificationHubPool
{
NotificationHubClient ClientFor(Guid comb);
INotificationHubClient ClientFor(Guid comb);
INotificationHubProxy AllClients { get; }
}
2 changes: 1 addition & 1 deletion src/Core/NotificationHub/NotificationHubPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private List<NotificationHubConnection> FilterInvalidHubs(IEnumerable<GlobalSett
/// <param name="comb"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException">Thrown when no notification hub is found for a given comb.</exception>
public NotificationHubClient ClientFor(Guid comb)
public INotificationHubClient ClientFor(Guid comb)
{
var possibleConnections = _connections.Where(c => c.RegistrationEnabled(comb)).ToArray();
if (possibleConnections.Length == 0)
Expand Down
36 changes: 11 additions & 25 deletions src/Core/NotificationHub/NotificationHubPushRegistrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,25 @@
using Bit.Core.Models.Data;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using Microsoft.Azure.NotificationHubs;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Bit.Core.NotificationHub;

public class NotificationHubPushRegistrationService : IPushRegistrationService
{
private readonly IInstallationDeviceRepository _installationDeviceRepository;
private readonly GlobalSettings _globalSettings;
private readonly INotificationHubPool _notificationHubPool;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<NotificationHubPushRegistrationService> _logger;
private readonly IFeatureService _featureService;

public NotificationHubPushRegistrationService(
IInstallationDeviceRepository installationDeviceRepository,
GlobalSettings globalSettings,
INotificationHubPool notificationHubPool,
IServiceProvider serviceProvider,
ILogger<NotificationHubPushRegistrationService> logger)
IFeatureService featureService)
{
_installationDeviceRepository = installationDeviceRepository;
_globalSettings = globalSettings;
_notificationHubPool = notificationHubPool;
_serviceProvider = serviceProvider;
_logger = logger;
_featureService = featureService;
}

public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
Expand Down Expand Up @@ -60,8 +51,7 @@ public async Task CreateOrUpdateRegistrationAsync(string pushToken, string devic
switch (type)
{
case DeviceType.Android:
var featureService = _serviceProvider.GetRequiredService<IFeatureService>();
if (featureService.IsEnabled(FeatureFlagKeys.AnhFcmv1Migration))
if (_featureService.IsEnabled(FeatureFlagKeys.AnhFcmv1Migration))
{
payloadTemplate = "{\"message\":{\"data\":{\"type\":\"$(type)\",\"payload\":\"$(payload)\"}}}";
messageTemplate = "{\"message\":{\"data\":{\"type\":\"$(type)\"}," +
Expand Down Expand Up @@ -196,7 +186,8 @@ private async Task PatchTagsForUserDevicesAsync(IEnumerable<string> deviceIds, U
{
try
{
await ClientFor(GetComb(deviceId)).PatchInstallationAsync(deviceId, new List<PartialUpdateOperation> { operation });
await ClientFor(GetComb(deviceId))
.PatchInstallationAsync(deviceId, new List<PartialUpdateOperation> { operation });
}
catch (Exception e) when (e.InnerException == null || !e.InnerException.Message.Contains("(404) Not Found"))
{
Expand All @@ -205,29 +196,24 @@ private async Task PatchTagsForUserDevicesAsync(IEnumerable<string> deviceIds, U
}
}

private NotificationHubClient ClientFor(Guid deviceId)
private INotificationHubClient ClientFor(Guid deviceId)
{
return _notificationHubPool.ClientFor(deviceId);
}

private Guid GetComb(string deviceId)
{
var deviceIdString = deviceId;
InstallationDeviceEntity installationDeviceEntity;
Guid deviceIdGuid;
if (InstallationDeviceEntity.TryParse(deviceIdString, out installationDeviceEntity))
if (InstallationDeviceEntity.TryParse(deviceId, out var installationDeviceEntity))
{
// Strip off the installation id (PartitionId). RowKey is the ID in the Installation's table.
deviceIdString = installationDeviceEntity.RowKey;
deviceId = installationDeviceEntity.RowKey;
}

if (Guid.TryParse(deviceIdString, out deviceIdGuid))
{
}
else
if (!Guid.TryParse(deviceId, out var deviceIdGuid))
{
throw new Exception($"Invalid device id {deviceId}.");
}

return deviceIdGuid;
}
}
Loading

0 comments on commit cf8b91e

Please sign in to comment.