Files
server/src/Core/Services/Implementations/NotificationHubPushRegistrationService.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

195 lines
6.9 KiB
C#
Raw Normal View History

using Bit.Core.Enums;
2019-03-19 00:39:03 -04:00
using Bit.Core.Models.Data;
using Bit.Core.Repositories;
using Bit.Core.Settings;
using Microsoft.Azure.NotificationHubs;
namespace Bit.Core.Services;
2022-08-29 16:06:55 -04:00
public class NotificationHubPushRegistrationService : IPushRegistrationService
{
private readonly IInstallationDeviceRepository _installationDeviceRepository;
private readonly GlobalSettings _globalSettings;
2022-08-29 16:06:55 -04:00
2017-05-26 22:52:50 -04:00
private NotificationHubClient _client = null;
2017-05-26 22:52:50 -04:00
public NotificationHubPushRegistrationService(
2019-03-19 00:39:03 -04:00
IInstallationDeviceRepository installationDeviceRepository,
GlobalSettings globalSettings)
2022-08-29 16:06:55 -04:00
{
2017-05-26 22:52:50 -04:00
_installationDeviceRepository = installationDeviceRepository;
_globalSettings = globalSettings;
2017-05-26 22:52:50 -04:00
_client = NotificationHubClient.CreateClientFromConnectionString(
_globalSettings.NotificationHub.ConnectionString,
_globalSettings.NotificationHub.HubName);
2022-08-29 16:06:55 -04:00
}
2017-05-26 22:52:50 -04:00
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
2019-03-19 00:39:03 -04:00
string identifier, DeviceType type)
2022-08-29 16:06:55 -04:00
{
2019-03-19 00:39:03 -04:00
if (string.IsNullOrWhiteSpace(pushToken))
{
return;
}
var installation = new Installation
{
2017-08-11 08:57:31 -04:00
InstallationId = deviceId,
PushChannel = pushToken,
Templates = new Dictionary<string, InstallationTemplate>()
};
2017-08-11 08:57:31 -04:00
installation.Tags = new List<string>
{
2017-08-11 08:57:31 -04:00
$"userId:{userId}"
};
2017-05-26 22:52:50 -04:00
if (!string.IsNullOrWhiteSpace(identifier))
{
installation.Tags.Add("deviceIdentifier:" + identifier);
}
string payloadTemplate = null, messageTemplate = null, badgeMessageTemplate = null;
switch (type)
{
2017-08-11 08:57:31 -04:00
case DeviceType.Android:
2017-06-02 16:52:54 -04:00
payloadTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}}";
messageTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\"}," +
"\"notification\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}}";
2022-08-29 16:06:55 -04:00
installation.Platform = NotificationPlatform.Fcm;
break;
2017-08-11 08:57:31 -04:00
case DeviceType.iOS:
2017-05-26 22:52:50 -04:00
payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}," +
"\"aps\":{\"content-available\":1}}";
2017-05-26 22:52:50 -04:00
messageTemplate = "{\"data\":{\"type\":\"#(type)\"}," +
"\"aps\":{\"alert\":\"$(message)\",\"badge\":null,\"content-available\":1}}";
badgeMessageTemplate = "{\"data\":{\"type\":\"#(type)\"}," +
"\"aps\":{\"alert\":\"$(message)\",\"badge\":\"#(badge)\",\"content-available\":1}}";
2022-08-29 16:06:55 -04:00
installation.Platform = NotificationPlatform.Apns;
break;
case DeviceType.AndroidAmazon:
2017-05-26 22:52:50 -04:00
payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}";
messageTemplate = "{\"data\":{\"type\":\"#(type)\",\"message\":\"$(message)\"}}";
2022-08-29 16:06:55 -04:00
installation.Platform = NotificationPlatform.Adm;
break;
default:
break;
}
2017-05-26 22:52:50 -04:00
BuildInstallationTemplate(installation, "payload", payloadTemplate, userId, identifier);
2019-03-19 00:39:03 -04:00
BuildInstallationTemplate(installation, "message", messageTemplate, userId, identifier);
BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate,
userId, identifier);
2017-08-11 08:57:31 -04:00
await _client.CreateOrUpdateInstallationAsync(installation);
if (InstallationDeviceEntity.IsInstallationDeviceId(deviceId))
{
2017-08-11 08:57:31 -04:00
await _installationDeviceRepository.UpsertAsync(new InstallationDeviceEntity(deviceId));
2017-05-26 22:52:50 -04:00
}
2022-08-29 16:06:55 -04:00
}
2017-05-26 22:52:50 -04:00
private void BuildInstallationTemplate(Installation installation, string templateId, string templateBody,
2017-08-11 08:57:31 -04:00
string userId, string identifier)
2022-08-29 16:06:55 -04:00
{
2017-05-26 22:52:50 -04:00
if (templateBody == null)
{
return;
2017-05-26 22:52:50 -04:00
}
2019-03-19 00:39:03 -04:00
var fullTemplateId = $"template:{templateId}";
2022-08-29 14:53:16 -04:00
var template = new InstallationTemplate
{
2017-08-11 08:57:31 -04:00
Body = templateBody,
2019-03-19 00:39:03 -04:00
Tags = new List<string>
{
2019-03-19 00:39:03 -04:00
fullTemplateId,
$"{fullTemplateId}_userId:{userId}"
}
2022-08-29 16:06:55 -04:00
};
2017-08-11 08:57:31 -04:00
if (!string.IsNullOrWhiteSpace(identifier))
2022-08-29 16:06:55 -04:00
{
2017-08-11 08:57:31 -04:00
template.Tags.Add($"{fullTemplateId}_deviceIdentifier:{identifier}");
}
2017-05-26 22:52:50 -04:00
2019-03-19 00:39:03 -04:00
installation.Templates.Add(fullTemplateId, template);
2022-08-29 16:06:55 -04:00
}
2017-08-11 08:57:31 -04:00
public async Task DeleteRegistrationAsync(string deviceId)
2022-08-29 16:06:55 -04:00
{
try
2017-05-26 22:52:50 -04:00
{
2019-03-19 00:39:03 -04:00
await _client.DeleteInstallationAsync(deviceId);
if (InstallationDeviceEntity.IsInstallationDeviceId(deviceId))
{
2019-03-19 00:39:03 -04:00
await _installationDeviceRepository.DeleteAsync(new InstallationDeviceEntity(deviceId));
}
2017-05-26 22:52:50 -04:00
}
2019-03-19 00:39:03 -04:00
catch (Exception e) when (e.InnerException == null || !e.InnerException.Message.Contains("(404) Not Found"))
2022-08-29 16:06:55 -04:00
{
throw;
}
}
2017-05-26 22:52:50 -04:00
2017-08-11 08:57:31 -04:00
public async Task AddUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
2022-08-29 16:06:55 -04:00
{
2017-08-11 08:57:31 -04:00
await PatchTagsForUserDevicesAsync(deviceIds, UpdateOperationType.Add, $"organizationId:{organizationId}");
if (deviceIds.Any() && InstallationDeviceEntity.IsInstallationDeviceId(deviceIds.First()))
2017-05-26 22:52:50 -04:00
{
2019-03-19 00:39:03 -04:00
var entities = deviceIds.Select(e => new InstallationDeviceEntity(e));
await _installationDeviceRepository.UpsertManyAsync(entities.ToList());
2017-05-26 22:52:50 -04:00
}
2022-08-29 16:06:55 -04:00
}
2017-05-26 22:52:50 -04:00
public async Task DeleteUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
2022-08-29 16:06:55 -04:00
{
2017-08-11 08:57:31 -04:00
await PatchTagsForUserDevicesAsync(deviceIds, UpdateOperationType.Remove,
$"organizationId:{organizationId}");
if (deviceIds.Any() && InstallationDeviceEntity.IsInstallationDeviceId(deviceIds.First()))
2017-05-26 22:52:50 -04:00
{
var entities = deviceIds.Select(e => new InstallationDeviceEntity(e));
2017-08-11 08:57:31 -04:00
await _installationDeviceRepository.UpsertManyAsync(entities.ToList());
}
2022-08-29 16:06:55 -04:00
}
2017-08-11 08:57:31 -04:00
2017-05-26 22:52:50 -04:00
private async Task PatchTagsForUserDevicesAsync(IEnumerable<string> deviceIds, UpdateOperationType op,
string tag)
2022-08-29 16:06:55 -04:00
{
if (!deviceIds.Any())
2017-05-26 22:52:50 -04:00
{
return;
}
var operation = new PartialUpdateOperation
2017-11-14 09:35:48 -05:00
{
Operation = op,
2017-11-14 09:35:48 -05:00
Path = "/tags"
2022-08-29 14:53:16 -04:00
};
if (op == UpdateOperationType.Add)
2017-11-14 09:35:48 -05:00
{
operation.Value = tag;
}
else if (op == UpdateOperationType.Remove)
2022-08-29 16:06:55 -04:00
{
2017-11-14 09:35:48 -05:00
operation.Path += $"/{tag}";
2022-08-29 16:06:55 -04:00
}
foreach (var id in deviceIds)
2022-08-29 16:06:55 -04:00
{
try
2017-05-26 22:52:50 -04:00
{
2017-11-14 09:35:48 -05:00
await _client.PatchInstallationAsync(id, new List<PartialUpdateOperation> { operation });
}
catch (Exception e) when (e.InnerException == null || !e.InnerException.Message.Contains("(404) Not Found"))
{
throw;
2017-05-26 22:52:50 -04:00
}
}
}
}