2018-08-02 17:23:37 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
|
using Bit.Core.Models.Table;
|
2019-03-19 00:39:03 -04:00
|
|
|
|
using Bit.Core.Repositories;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2019-03-19 00:39:03 -04:00
|
|
|
|
using Bit.Core.Utilities;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class MultiServicePushNotificationService : IPushNotificationService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly List<IPushNotificationService> _services = new List<IPushNotificationService>();
|
2019-03-19 22:47:53 -04:00
|
|
|
|
private readonly ILogger<MultiServicePushNotificationService> _logger;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
|
|
|
|
|
|
public MultiServicePushNotificationService(
|
2019-03-19 00:39:03 -04:00
|
|
|
|
IDeviceRepository deviceRepository,
|
|
|
|
|
|
IInstallationDeviceRepository installationDeviceRepository,
|
2018-08-02 17:23:37 -04:00
|
|
|
|
GlobalSettings globalSettings,
|
|
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2019-03-19 22:47:53 -04:00
|
|
|
|
ILogger<MultiServicePushNotificationService> logger,
|
2018-08-16 13:35:16 -04:00
|
|
|
|
ILogger<RelayPushNotificationService> relayLogger,
|
2018-08-16 13:52:11 -04:00
|
|
|
|
ILogger<NotificationsApiPushNotificationService> hubLogger)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (globalSettings.SelfHosted)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (CoreHelpers.SettingHasValue(globalSettings.PushRelayBaseUri) &&
|
2018-08-02 17:23:37 -04:00
|
|
|
|
globalSettings.Installation?.Id != null &&
|
|
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings.Installation?.Key))
|
|
|
|
|
|
{
|
2019-03-20 08:38:28 -04:00
|
|
|
|
_services.Add(new RelayPushNotificationService(deviceRepository, globalSettings,
|
2019-03-19 00:39:03 -04:00
|
|
|
|
httpContextAccessor, relayLogger));
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (CoreHelpers.SettingHasValue(globalSettings.InternalIdentityKey) &&
|
2018-08-16 13:50:41 -04:00
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings.BaseServiceUri.InternalNotifications))
|
2018-08-16 13:35:16 -04:00
|
|
|
|
{
|
2018-08-17 18:04:11 -04:00
|
|
|
|
_services.Add(new NotificationsApiPushNotificationService(
|
|
|
|
|
|
globalSettings, httpContextAccessor, hubLogger));
|
2018-08-16 13:35:16 -04:00
|
|
|
|
}
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (CoreHelpers.SettingHasValue(globalSettings.NotificationHub.ConnectionString))
|
2018-08-16 13:35:16 -04:00
|
|
|
|
{
|
2019-03-20 08:38:28 -04:00
|
|
|
|
_services.Add(new NotificationHubPushNotificationService(installationDeviceRepository,
|
2019-03-19 00:39:03 -04:00
|
|
|
|
globalSettings, httpContextAccessor));
|
2018-08-16 13:35:16 -04:00
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (CoreHelpers.SettingHasValue(globalSettings.Notifications?.ConnectionString))
|
2018-08-16 13:35:16 -04:00
|
|
|
|
{
|
2018-08-17 18:04:11 -04:00
|
|
|
|
_services.Add(new AzureQueuePushNotificationService(globalSettings, httpContextAccessor));
|
2018-08-16 13:35:16 -04:00
|
|
|
|
}
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2019-03-19 22:47:53 -04:00
|
|
|
|
_logger = logger;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 09:29:38 -04:00
|
|
|
|
public Task PushSyncCipherCreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
PushToServices((s) => s.PushSyncCipherCreateAsync(cipher, collectionIds));
|
2018-08-02 17:23:37 -04:00
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 09:29:38 -04:00
|
|
|
|
public Task PushSyncCipherUpdateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
PushToServices((s) => s.PushSyncCipherUpdateAsync(cipher, collectionIds));
|
2018-08-02 17:23:37 -04:00
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncCipherDeleteAsync(Cipher cipher)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncCipherDeleteAsync(cipher));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncFolderCreateAsync(Folder folder)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncFolderCreateAsync(folder));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncFolderUpdateAsync(Folder folder)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncFolderUpdateAsync(folder));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncFolderDeleteAsync(Folder folder)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncFolderDeleteAsync(folder));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncCiphersAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncCiphersAsync(userId));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncVaultAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncVaultAsync(userId));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncOrgKeysAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncOrgKeysAsync(userId));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncSettingsAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncSettingsAsync(userId));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-28 08:22:49 -04:00
|
|
|
|
public Task PushLogOutAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushLogOutAsync(userId));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-22 16:16:40 -05:00
|
|
|
|
public Task PushSyncSendCreateAsync(Send send)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncSendCreateAsync(send));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncSendUpdateAsync(Send send)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncSendUpdateAsync(send));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncSendDeleteAsync(Send send)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncSendDeleteAsync(send));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2019-03-19 00:39:03 -04:00
|
|
|
|
PushToServices((s) => s.SendPayloadToUserAsync(userId, type, payload, identifier, deviceId));
|
2018-08-02 21:03:04 -04:00
|
|
|
|
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
|
|
|
|
{
|
2019-03-19 00:39:03 -04:00
|
|
|
|
PushToServices((s) => s.SendPayloadToOrganizationAsync(orgId, type, payload, identifier, deviceId));
|
2018-08-02 21:03:04 -04:00
|
|
|
|
return Task.FromResult(0);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void PushToServices(Func<IPushNotificationService, Task> pushFunc)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (_services != null)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var service in _services)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
|
|
|
|
|
pushFunc(service);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|