using Bit.Core.Settings; using Bit.Core.Utilities; using Microsoft.Azure.NotificationHubs; using Microsoft.Extensions.Logging; namespace Bit.Core.NotificationHub; public class NotificationHubPool : INotificationHubPool { private List _connections { get; } private readonly IEnumerable _clients; private readonly ILogger _logger; public NotificationHubPool(ILogger logger, GlobalSettings globalSettings) { _logger = logger; _connections = filterInvalidHubs(globalSettings.NotificationHubPool.NotificationHubSettings); _clients = _connections.Select(c => c.HubClient); } private List filterInvalidHubs(IEnumerable hubs) { List result = new(); foreach (var hub in hubs) { var connection = NotificationHubConnection.From(hub); if (!connection.IsValid) { _logger.LogWarning("Invalid notification hub settings: {0}", hub.HubName ?? "hub name missing"); continue; } result.Add(connection); } return result; } /// /// Gets the NotificationHubClient for the given comb ID. /// /// /// /// public NotificationHubClient ClientFor(Guid comb) { var possibleConnections = _connections.Where(c => c.RegistrationEnabled(comb)).ToArray(); if (possibleConnections.Length == 0) { throw new InvalidOperationException($"No valid notification hubs are available for the given comb ({comb}).\n" + $"The comb's datetime is {CoreHelpers.DateFromComb(comb)}." + $"Hub start and end times are configured as follows:\n" + string.Join("\n", _connections.Select(c => $"Hub {c.HubName} - Start: {c.RegistrationStartDate}, End: {c.RegistrationEndDate}"))); } return possibleConnections[CoreHelpers.BinForComb(comb, possibleConnections.Length)].HubClient; } public INotificationHubProxy AllClients { get { return new NotificationHubClientProxy(_clients); } } }