2024-11-21 21:39:28 +00:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
using Bit.Core.Auth.Entities;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
using Bit.Core.Enums;
|
2024-10-21 14:58:57 +01:00
|
|
|
|
using Bit.Core.NotificationCenter.Entities;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2023-04-18 14:05:17 +02:00
|
|
|
|
using Bit.Core.Tools.Entities;
|
2023-03-02 13:23:38 -05:00
|
|
|
|
using Bit.Core.Vault.Entities;
|
2024-10-22 09:20:57 -07:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
public class MultiServicePushNotificationService : IPushNotificationService
|
|
|
|
|
|
{
|
2024-11-21 21:39:28 +00:00
|
|
|
|
private readonly IEnumerable<IPushNotificationService>? _services;
|
2019-03-19 22:47:53 -04:00
|
|
|
|
private readonly ILogger<MultiServicePushNotificationService> _logger;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
public MultiServicePushNotificationService(
|
2024-10-22 09:20:57 -07:00
|
|
|
|
[FromKeyedServices("implementation")] IEnumerable<IPushNotificationService> services,
|
2019-03-19 22:47:53 -04:00
|
|
|
|
ILogger<MultiServicePushNotificationService> logger,
|
2024-10-22 09:20:57 -07:00
|
|
|
|
GlobalSettings globalSettings)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2024-10-22 09:20:57 -07:00
|
|
|
|
_services = services;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
|
|
|
|
|
|
_logger = logger;
|
2024-10-22 09:20:57 -07:00
|
|
|
|
_logger.LogInformation("Hub services: {Services}", _services.Count());
|
2024-11-21 21:39:28 +00:00
|
|
|
|
globalSettings.NotificationHubPool?.NotificationHubs?.ForEach(hub =>
|
2024-10-22 09:20:57 -07:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("HubName: {HubName}, EnableSendTracing: {EnableSendTracing}, RegistrationStartDate: {RegistrationStartDate}, RegistrationEndDate: {RegistrationEndDate}", hub.HubName, hub.EnableSendTracing, hub.RegistrationStartDate, hub.RegistrationEndDate);
|
|
|
|
|
|
});
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncCipherCreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncCipherCreateAsync(cipher, collectionIds));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncCipherUpdateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncCipherUpdateAsync(cipher, collectionIds));
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-28 08:22:49 -04:00
|
|
|
|
public Task PushSyncCiphersAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncCiphersAsync(userId));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-22 16:16:40 -05:00
|
|
|
|
public Task PushSyncVaultAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncVaultAsync(userId));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-14 04:15:07 +10:00
|
|
|
|
public Task PushSyncOrganizationsAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncOrganizationsAsync(userId));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-22 16:16:40 -05:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-24 11:25:16 -05:00
|
|
|
|
public Task PushLogOutAsync(Guid userId, bool excludeCurrentContext = false)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2022-11-24 11:25:16 -05:00
|
|
|
|
PushToServices((s) => s.PushLogOutAsync(userId, excludeCurrentContext));
|
2018-08-02 21:03:04 -04:00
|
|
|
|
return Task.FromResult(0);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 09:29:38 -04:00
|
|
|
|
public Task PushSyncSendCreateAsync(Send send)
|
2022-08-29 14:53:16 -04:00
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
PushToServices((s) => s.PushSyncSendCreateAsync(send));
|
2018-08-02 17:23:37 -04:00
|
|
|
|
return Task.FromResult(0);
|
2022-08-29 14:53:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-19 00:39:03 -04:00
|
|
|
|
public Task PushSyncSendUpdateAsync(Send send)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2019-03-19 00:39:03 -04:00
|
|
|
|
PushToServices((s) => s.PushSyncSendUpdateAsync(send));
|
2018-08-02 21:03:04 -04:00
|
|
|
|
return Task.FromResult(0);
|
2022-09-26 13:21:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushAuthRequestAsync(AuthRequest authRequest)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushAuthRequestAsync(authRequest));
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushAuthRequestResponseAsync(AuthRequest authRequest)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushAuthRequestResponseAsync(authRequest));
|
|
|
|
|
|
return Task.FromResult(0);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncSendDeleteAsync(Send send)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-02 17:23:37 -04:00
|
|
|
|
PushToServices((s) => s.PushSyncSendDeleteAsync(send));
|
|
|
|
|
|
return Task.FromResult(0);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-19 00:39:03 -04:00
|
|
|
|
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier,
|
2024-11-21 21:39:28 +00:00
|
|
|
|
string? deviceId = null, ClientType? clientType = null)
|
2022-08-29 14:53:16 -04:00
|
|
|
|
{
|
2024-10-21 14:58:57 +01:00
|
|
|
|
PushToServices((s) => s.SendPayloadToUserAsync(userId, type, payload, identifier, deviceId, clientType));
|
2018-08-02 17:23:37 -04:00
|
|
|
|
return Task.FromResult(0);
|
2022-08-29 14:53:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-19 00:39:03 -04:00
|
|
|
|
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
|
2024-11-21 21:39:28 +00:00
|
|
|
|
string? deviceId = null, ClientType? clientType = null)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2024-10-21 14:58:57 +01:00
|
|
|
|
PushToServices((s) => s.SendPayloadToOrganizationAsync(orgId, type, payload, identifier, deviceId, clientType));
|
2018-08-02 17:23:37 -04:00
|
|
|
|
return Task.FromResult(0);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-21 22:43:14 +00:00
|
|
|
|
public Task PushSyncNotificationCreateAsync(Notification notification)
|
2024-10-21 14:58:57 +01:00
|
|
|
|
{
|
2024-11-21 22:43:14 +00:00
|
|
|
|
PushToServices((s) => s.PushSyncNotificationCreateAsync(notification));
|
2024-11-21 21:39:28 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task PushSyncNotificationUpdateAsync(Notification notification, NotificationStatus? notificationStatus)
|
|
|
|
|
|
{
|
|
|
|
|
|
PushToServices((s) => s.PushSyncNotificationUpdateAsync(notification, notificationStatus));
|
2024-10-21 14:58:57 +01:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
private void PushToServices(Func<IPushNotificationService, Task> pushFunc)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2024-11-21 21:39:28 +00:00
|
|
|
|
if (_services == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogWarning("No services found to push notification");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var service in _services)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2024-11-21 21:39:28 +00:00
|
|
|
|
_logger.LogDebug("Pushing notification to service {}", service.GetType().Name);
|
|
|
|
|
|
pushFunc(service);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|