mirror of
https://github.com/bitwarden/server.git
synced 2024-12-11 15:17:44 +01:00
149 lines
5.5 KiB
C#
149 lines
5.5 KiB
C#
#if NET461
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Azure.NotificationHubs;
|
|
using Bit.Core.Enums;
|
|
using System.Linq;
|
|
|
|
namespace Bit.Core.Services
|
|
{
|
|
public class NotificationHubPushRegistrationService : IPushRegistrationService
|
|
{
|
|
private readonly NotificationHubClient _client;
|
|
|
|
public NotificationHubPushRegistrationService(
|
|
GlobalSettings globalSettings)
|
|
{
|
|
_client = NotificationHubClient.CreateClientFromConnectionString(globalSettings.NotificationHub.ConnectionString,
|
|
globalSettings.NotificationHub.HubName);
|
|
}
|
|
|
|
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
|
|
string identifier, DeviceType type)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(pushToken))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var installation = new Installation
|
|
{
|
|
InstallationId = deviceId,
|
|
PushChannel = pushToken,
|
|
Templates = new Dictionary<string, InstallationTemplate>()
|
|
};
|
|
|
|
installation.Tags = new List<string>
|
|
{
|
|
$"userId:{userId}"
|
|
};
|
|
|
|
if(!string.IsNullOrWhiteSpace(identifier))
|
|
{
|
|
installation.Tags.Add("deviceIdentifier:" + identifier);
|
|
}
|
|
|
|
string payloadTemplate = null, messageTemplate = null, badgeMessageTemplate = null;
|
|
switch(type)
|
|
{
|
|
case DeviceType.Android:
|
|
payloadTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}}";
|
|
messageTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\"}," +
|
|
"\"notification\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}}";
|
|
|
|
installation.Platform = NotificationPlatform.Gcm;
|
|
break;
|
|
case DeviceType.iOS:
|
|
payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}," +
|
|
"\"aps\":{\"alert\":null,\"badge\":null,\"content-available\":1}}";
|
|
messageTemplate = "{\"data\":{\"type\":\"#(type)\"}," +
|
|
"\"aps\":{\"alert\":\"$(message)\",\"badge\":null,\"content-available\":1}}";
|
|
badgeMessageTemplate = "{\"data\":{\"type\":\"#(type)\"}," +
|
|
"\"aps\":{\"alert\":\"$(message)\",\"badge\":\"#(badge)\",\"content-available\":1}}";
|
|
|
|
installation.Platform = NotificationPlatform.Apns;
|
|
break;
|
|
case DeviceType.AndroidAmazon:
|
|
payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}";
|
|
messageTemplate = "{\"data\":{\"type\":\"#(type)\",\"message\":\"$(message)\"}}";
|
|
|
|
installation.Platform = NotificationPlatform.Adm;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
BuildInstallationTemplate(installation, "payload", payloadTemplate, userId, identifier);
|
|
BuildInstallationTemplate(installation, "message", messageTemplate, userId, identifier);
|
|
BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate, userId, identifier);
|
|
|
|
await _client.CreateOrUpdateInstallationAsync(installation);
|
|
}
|
|
|
|
private void BuildInstallationTemplate(Installation installation, string templateId, string templateBody,
|
|
string userId, string identifier)
|
|
{
|
|
if(templateBody == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var fullTemplateId = $"template:{templateId}";
|
|
|
|
var template = new InstallationTemplate
|
|
{
|
|
Body = templateBody,
|
|
Tags = new List<string>
|
|
{
|
|
fullTemplateId,
|
|
$"{fullTemplateId}_userId:{userId}"
|
|
}
|
|
};
|
|
|
|
if(!string.IsNullOrWhiteSpace(identifier))
|
|
{
|
|
template.Tags.Add($"{fullTemplateId}_deviceIdentifier:{identifier}");
|
|
}
|
|
|
|
installation.Templates.Add(fullTemplateId, template);
|
|
}
|
|
|
|
public async Task DeleteRegistrationAsync(string deviceId)
|
|
{
|
|
await _client.DeleteInstallationAsync(deviceId);
|
|
}
|
|
|
|
public async Task AddUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
|
|
{
|
|
await PatchTagsForUserDevicesAsync(deviceIds, UpdateOperationType.Add, $"organizationId:{organizationId}");
|
|
}
|
|
|
|
public async Task DeleteUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
|
|
{
|
|
await PatchTagsForUserDevicesAsync(deviceIds, UpdateOperationType.Remove,
|
|
$"organizationId:{organizationId}");
|
|
}
|
|
|
|
private async Task PatchTagsForUserDevicesAsync(IEnumerable<string> deviceIds, UpdateOperationType op, string tag)
|
|
{
|
|
if(!deviceIds.Any())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var operation = new PartialUpdateOperation
|
|
{
|
|
Operation = op,
|
|
Path = "/tags",
|
|
Value = tag
|
|
};
|
|
|
|
foreach(var id in deviceIds)
|
|
{
|
|
await _client.PatchInstallationAsync(id, new List<PartialUpdateOperation> { operation });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|