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

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

205 lines
6.5 KiB
C#
Raw Normal View History

2018-08-02 17:23:37 -04:00
using System;
using System.Collections.Generic;
2018-08-02 17:23:37 -04:00
using System.Threading.Tasks;
2020-01-10 08:33:13 -05:00
using Azure.Storage.Queues;
using Bit.Core.Context;
2018-08-02 17:23:37 -04:00
using Bit.Core.Enums;
using Bit.Core.Models;
using Bit.Core.Models.Table;
using Bit.Core.Settings;
2018-08-02 17:23:37 -04:00
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
2018-08-02 17:23:37 -04:00
namespace Bit.Core.Services
{
public class AzureQueuePushNotificationService : IPushNotificationService
{
2020-01-10 08:33:13 -05:00
private readonly QueueClient _queueClient;
2018-08-02 17:23:37 -04:00
private readonly GlobalSettings _globalSettings;
private readonly IHttpContextAccessor _httpContextAccessor;
private JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
public AzureQueuePushNotificationService(
GlobalSettings globalSettings,
IHttpContextAccessor httpContextAccessor)
{
2020-01-10 08:33:13 -05:00
_queueClient = new QueueClient(globalSettings.Notifications.ConnectionString, "notifications");
2018-08-02 17:23:37 -04:00
_globalSettings = globalSettings;
_httpContextAccessor = httpContextAccessor;
}
public async Task PushSyncCipherCreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
2018-08-02 17:23:37 -04:00
{
await PushCipherAsync(cipher, PushType.SyncCipherCreate, collectionIds);
2018-08-02 17:23:37 -04:00
}
public async Task PushSyncCipherUpdateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
2018-08-02 17:23:37 -04:00
{
await PushCipherAsync(cipher, PushType.SyncCipherUpdate, collectionIds);
2018-08-02 17:23:37 -04:00
}
public async Task PushSyncCipherDeleteAsync(Cipher cipher)
{
await PushCipherAsync(cipher, PushType.SyncLoginDelete, null);
2018-08-02 17:23:37 -04:00
}
private async Task PushCipherAsync(Cipher cipher, PushType type, IEnumerable<Guid> collectionIds)
2018-08-02 17:23:37 -04:00
{
if (cipher.OrganizationId.HasValue)
2018-08-02 17:23:37 -04:00
{
var message = new SyncCipherPushNotification
{
Id = cipher.Id,
OrganizationId = cipher.OrganizationId,
RevisionDate = cipher.RevisionDate,
CollectionIds = collectionIds,
2018-08-02 17:23:37 -04:00
};
await SendMessageAsync(type, message, true);
}
else if (cipher.UserId.HasValue)
2018-08-02 17:23:37 -04:00
{
var message = new SyncCipherPushNotification
{
Id = cipher.Id,
UserId = cipher.UserId,
RevisionDate = cipher.RevisionDate,
};
await SendMessageAsync(type, message, true);
}
}
public async Task PushSyncFolderCreateAsync(Folder folder)
{
await PushFolderAsync(folder, PushType.SyncFolderCreate);
}
public async Task PushSyncFolderUpdateAsync(Folder folder)
{
await PushFolderAsync(folder, PushType.SyncFolderUpdate);
}
public async Task PushSyncFolderDeleteAsync(Folder folder)
{
await PushFolderAsync(folder, PushType.SyncFolderDelete);
}
private async Task PushFolderAsync(Folder folder, PushType type)
{
var message = new SyncFolderPushNotification
{
Id = folder.Id,
UserId = folder.UserId,
RevisionDate = folder.RevisionDate
};
await SendMessageAsync(type, message, true);
}
public async Task PushSyncCiphersAsync(Guid userId)
{
2018-08-28 08:22:49 -04:00
await PushUserAsync(userId, PushType.SyncCiphers);
2018-08-02 17:23:37 -04:00
}
public async Task PushSyncVaultAsync(Guid userId)
{
2018-08-28 08:22:49 -04:00
await PushUserAsync(userId, PushType.SyncVault);
2018-08-02 17:23:37 -04:00
}
public async Task PushSyncOrgKeysAsync(Guid userId)
{
2018-08-28 08:22:49 -04:00
await PushUserAsync(userId, PushType.SyncOrgKeys);
2018-08-02 17:23:37 -04:00
}
public async Task PushSyncSettingsAsync(Guid userId)
{
2018-08-28 08:22:49 -04:00
await PushUserAsync(userId, PushType.SyncSettings);
2018-08-02 17:23:37 -04:00
}
2018-08-28 08:22:49 -04:00
public async Task PushLogOutAsync(Guid userId)
2018-08-02 17:23:37 -04:00
{
2018-08-28 08:22:49 -04:00
await PushUserAsync(userId, PushType.LogOut);
}
private async Task PushUserAsync(Guid userId, PushType type)
{
var message = new UserPushNotification
2018-08-02 17:23:37 -04:00
{
UserId = userId,
Date = DateTime.UtcNow
};
await SendMessageAsync(type, message, false);
}
public async Task PushSyncSendCreateAsync(Send send)
{
await PushSendAsync(send, PushType.SyncSendCreate);
}
public async Task PushSyncSendUpdateAsync(Send send)
{
await PushSendAsync(send, PushType.SyncSendUpdate);
}
public async Task PushSyncSendDeleteAsync(Send send)
{
await PushSendAsync(send, PushType.SyncSendDelete);
}
private async Task PushSendAsync(Send send, PushType type)
{
if (send.UserId.HasValue)
{
var message = new SyncSendPushNotification
{
Id = send.Id,
UserId = send.UserId.Value,
RevisionDate = send.RevisionDate
};
await SendMessageAsync(type, message, true);
}
}
2018-08-02 17:23:37 -04:00
private async Task SendMessageAsync<T>(PushType type, T payload, bool excludeCurrentContext)
{
var contextId = GetContextIdentifier(excludeCurrentContext);
2019-03-19 00:39:03 -04:00
var message = JsonConvert.SerializeObject(new PushNotificationData<T>(type, payload, contextId),
2018-08-02 17:23:37 -04:00
_jsonSettings);
2020-01-10 08:33:13 -05:00
await _queueClient.SendMessageAsync(message);
2018-08-02 17:23:37 -04:00
}
private string GetContextIdentifier(bool excludeCurrentContext)
{
if (!excludeCurrentContext)
2018-08-02 17:23:37 -04:00
{
return null;
}
var currentContext = _httpContextAccessor?.HttpContext?.
RequestServices.GetService(typeof(ICurrentContext)) as ICurrentContext;
2018-08-02 17:23:37 -04:00
return currentContext?.DeviceIdentifier;
}
2019-03-19 00:39:03 -04:00
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier,
string deviceId = null)
2018-08-02 17:23:37 -04:00
{
2018-08-02 21:03:04 -04:00
// Noop
return Task.FromResult(0);
2018-08-02 17:23:37 -04:00
}
2019-03-19 00:39:03 -04:00
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
string deviceId = null)
2018-08-02 17:23:37 -04:00
{
2018-08-02 21:03:04 -04:00
// Noop
return Task.FromResult(0);
2018-08-02 17:23:37 -04:00
}
}
}