mirror of
https://github.com/bitwarden/server
synced 2025-12-18 09:13:19 +00:00
[PM-10600] Push notification creation to affected clients (#4923)
* PM-10600: Notification push notification * PM-10600: Sending to specific client types for relay push notifications * PM-10600: Sending to specific client types for other clients * PM-10600: Send push notification on notification creation * PM-10600: Explicit group names * PM-10600: Id typos * PM-10600: Revert global push notifications * PM-10600: Added DeviceType claim * PM-10600: Sent to organization typo * PM-10600: UT coverage * PM-10600: Small refactor, UTs coverage * PM-10600: UTs coverage * PM-10600: Startup fix * PM-10600: Test fix * PM-10600: Required attribute, organization group for push notification fix * PM-10600: UT coverage * PM-10600: Fix Mobile devices not registering to organization push notifications We only register devices for organization push notifications when the organization is being created. This does not work, since we have a use case (Notification Center) of delivering notifications to all users of organization. This fixes it, by adding the organization id tag when device registers for push notifications. * PM-10600: Unit Test coverage for NotificationHubPushRegistrationService Fixed IFeatureService substitute mocking for Android tests. Added user part of organization test with organizationId tags expectation. * PM-10600: Unit Tests fix to NotificationHubPushRegistrationService after merge conflict * PM-10600: Organization push notifications not sending to mobile device from self-hosted. Self-hosted instance uses relay to register the mobile device against Bitwarden Cloud Api. Only the self-hosted server knows client's organization membership, which means it needs to pass in the organization id's information to the relay. Similarly, for Bitwarden Cloud, the organizaton id will come directly from the server. * PM-10600: Fix self-hosted organization notification not being received by mobile device. When mobile device registers on self-hosted through the relay, every single id, like user id, device id and now organization id needs to be prefixed with the installation id. This have been missing in the PushController that handles this for organization id. * PM-10600: Broken NotificationsController integration test Device type is now part of JWT access token, so the notification center results in the integration test are now scoped to client type web and all. * PM-10600: Merge conflicts fix * merge conflict fix
This commit is contained in:
@@ -2,36 +2,26 @@
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Platform.Push;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.Azure.NotificationHubs;
|
||||
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;
|
||||
|
||||
public NotificationHubPushRegistrationService(
|
||||
IInstallationDeviceRepository installationDeviceRepository,
|
||||
GlobalSettings globalSettings,
|
||||
INotificationHubPool notificationHubPool,
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<NotificationHubPushRegistrationService> logger)
|
||||
INotificationHubPool notificationHubPool)
|
||||
{
|
||||
_installationDeviceRepository = installationDeviceRepository;
|
||||
_globalSettings = globalSettings;
|
||||
_notificationHubPool = notificationHubPool;
|
||||
_serviceProvider = serviceProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
|
||||
string identifier, DeviceType type)
|
||||
string identifier, DeviceType type, IEnumerable<string> organizationIds)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(pushToken))
|
||||
{
|
||||
@@ -45,16 +35,21 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
Templates = new Dictionary<string, InstallationTemplate>()
|
||||
};
|
||||
|
||||
installation.Tags = new List<string>
|
||||
{
|
||||
$"userId:{userId}"
|
||||
};
|
||||
var clientType = DeviceTypes.ToClientType(type);
|
||||
|
||||
installation.Tags = new List<string> { $"userId:{userId}", $"clientType:{clientType}" };
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(identifier))
|
||||
{
|
||||
installation.Tags.Add("deviceIdentifier:" + identifier);
|
||||
}
|
||||
|
||||
var organizationIdsList = organizationIds.ToList();
|
||||
foreach (var organizationId in organizationIdsList)
|
||||
{
|
||||
installation.Tags.Add($"organizationId:{organizationId}");
|
||||
}
|
||||
|
||||
string payloadTemplate = null, messageTemplate = null, badgeMessageTemplate = null;
|
||||
switch (type)
|
||||
{
|
||||
@@ -84,10 +79,12 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
break;
|
||||
}
|
||||
|
||||
BuildInstallationTemplate(installation, "payload", payloadTemplate, userId, identifier);
|
||||
BuildInstallationTemplate(installation, "message", messageTemplate, userId, identifier);
|
||||
BuildInstallationTemplate(installation, "payload", payloadTemplate, userId, identifier, clientType,
|
||||
organizationIdsList);
|
||||
BuildInstallationTemplate(installation, "message", messageTemplate, userId, identifier, clientType,
|
||||
organizationIdsList);
|
||||
BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate,
|
||||
userId, identifier);
|
||||
userId, identifier, clientType, organizationIdsList);
|
||||
|
||||
await ClientFor(GetComb(deviceId)).CreateOrUpdateInstallationAsync(installation);
|
||||
if (InstallationDeviceEntity.IsInstallationDeviceId(deviceId))
|
||||
@@ -97,7 +94,7 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
}
|
||||
|
||||
private void BuildInstallationTemplate(Installation installation, string templateId, string templateBody,
|
||||
string userId, string identifier)
|
||||
string userId, string identifier, ClientType clientType, List<string> organizationIds)
|
||||
{
|
||||
if (templateBody == null)
|
||||
{
|
||||
@@ -111,8 +108,7 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
Body = templateBody,
|
||||
Tags = new List<string>
|
||||
{
|
||||
fullTemplateId,
|
||||
$"{fullTemplateId}_userId:{userId}"
|
||||
fullTemplateId, $"{fullTemplateId}_userId:{userId}", $"clientType:{clientType}"
|
||||
}
|
||||
};
|
||||
|
||||
@@ -121,6 +117,11 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
template.Tags.Add($"{fullTemplateId}_deviceIdentifier:{identifier}");
|
||||
}
|
||||
|
||||
foreach (var organizationId in organizationIds)
|
||||
{
|
||||
template.Tags.Add($"organizationId:{organizationId}");
|
||||
}
|
||||
|
||||
installation.Templates.Add(fullTemplateId, template);
|
||||
}
|
||||
|
||||
@@ -197,7 +198,7 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
}
|
||||
}
|
||||
|
||||
private NotificationHubClient ClientFor(Guid deviceId)
|
||||
private INotificationHubClient ClientFor(Guid deviceId)
|
||||
{
|
||||
return _notificationHubPool.ClientFor(deviceId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user