1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-28 13:15:12 +01:00

[PM-6977] Migrate to FCM v1 (redo) (#4046)

* revert changes again

* use IServiceProvider

* fix test
This commit is contained in:
Kyle Spearrin 2024-05-02 16:37:06 -04:00 committed by GitHub
parent b40d9ab70d
commit c045739325
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 5 deletions

View File

@ -135,6 +135,7 @@ public static class FeatureFlagKeys
public const string UnassignedItemsBanner = "unassigned-items-banner"; public const string UnassignedItemsBanner = "unassigned-items-banner";
public const string EnableDeleteProvider = "AC-1218-delete-provider"; public const string EnableDeleteProvider = "AC-1218-delete-provider";
public const string EmailVerification = "email-verification"; public const string EmailVerification = "email-verification";
public const string AnhFcmv1Migration = "anh-fcmv1-migration";
public static List<string> GetAllKeys() public static List<string> GetAllKeys()
{ {

View File

@ -3,6 +3,7 @@ using Bit.Core.Models.Data;
using Bit.Core.Repositories; using Bit.Core.Repositories;
using Bit.Core.Settings; using Bit.Core.Settings;
using Microsoft.Azure.NotificationHubs; using Microsoft.Azure.NotificationHubs;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Bit.Core.Services; namespace Bit.Core.Services;
@ -11,16 +12,19 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
{ {
private readonly IInstallationDeviceRepository _installationDeviceRepository; private readonly IInstallationDeviceRepository _installationDeviceRepository;
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<NotificationHubPushRegistrationService> _logger; private readonly ILogger<NotificationHubPushRegistrationService> _logger;
private Dictionary<NotificationHubType, NotificationHubClient> _clients = []; private Dictionary<NotificationHubType, NotificationHubClient> _clients = [];
public NotificationHubPushRegistrationService( public NotificationHubPushRegistrationService(
IInstallationDeviceRepository installationDeviceRepository, IInstallationDeviceRepository installationDeviceRepository,
GlobalSettings globalSettings, GlobalSettings globalSettings,
IServiceProvider serviceProvider,
ILogger<NotificationHubPushRegistrationService> logger) ILogger<NotificationHubPushRegistrationService> logger)
{ {
_installationDeviceRepository = installationDeviceRepository; _installationDeviceRepository = installationDeviceRepository;
_globalSettings = globalSettings; _globalSettings = globalSettings;
_serviceProvider = serviceProvider;
_logger = logger; _logger = logger;
// Is this dirty to do in the ctor? // Is this dirty to do in the ctor?
@ -72,11 +76,21 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
switch (type) switch (type)
{ {
case DeviceType.Android: case DeviceType.Android:
payloadTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}}"; var featureService = _serviceProvider.GetRequiredService<IFeatureService>();
messageTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\"}," + if (featureService.IsEnabled(FeatureFlagKeys.AnhFcmv1Migration))
"\"notification\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}}"; {
payloadTemplate = "{\"message\":{\"data\":{\"type\":\"$(type)\",\"payload\":\"$(payload)\"}}}";
installation.Platform = NotificationPlatform.Fcm; messageTemplate = "{\"message\":{\"data\":{\"type\":\"$(type)\"}," +
"\"notification\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}}";
installation.Platform = NotificationPlatform.FcmV1;
}
else
{
payloadTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}}";
messageTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\"}," +
"\"notification\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}}";
installation.Platform = NotificationPlatform.Fcm;
}
break; break;
case DeviceType.iOS: case DeviceType.iOS:
payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}," + payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}," +

View File

@ -12,18 +12,21 @@ public class NotificationHubPushRegistrationServiceTests
private readonly NotificationHubPushRegistrationService _sut; private readonly NotificationHubPushRegistrationService _sut;
private readonly IInstallationDeviceRepository _installationDeviceRepository; private readonly IInstallationDeviceRepository _installationDeviceRepository;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<NotificationHubPushRegistrationService> _logger; private readonly ILogger<NotificationHubPushRegistrationService> _logger;
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
public NotificationHubPushRegistrationServiceTests() public NotificationHubPushRegistrationServiceTests()
{ {
_installationDeviceRepository = Substitute.For<IInstallationDeviceRepository>(); _installationDeviceRepository = Substitute.For<IInstallationDeviceRepository>();
_serviceProvider = Substitute.For<IServiceProvider>();
_logger = Substitute.For<ILogger<NotificationHubPushRegistrationService>>(); _logger = Substitute.For<ILogger<NotificationHubPushRegistrationService>>();
_globalSettings = new GlobalSettings(); _globalSettings = new GlobalSettings();
_sut = new NotificationHubPushRegistrationService( _sut = new NotificationHubPushRegistrationService(
_installationDeviceRepository, _installationDeviceRepository,
_globalSettings, _globalSettings,
_serviceProvider,
_logger _logger
); );
} }