Files
server/src/Core/Platform/PushRegistration/PushRegistrationServiceCollectionExtensions.cs
Justin Baur e5159a3ba2 [PM-19659] Clean up Notifications code (#6244)
* Move PushType to Platform Folder

- Move the PushType next to the rest of push notification code
- Specifically exclude it from needing Platform code review
- Add tests establishing rules Platform has for usage of this enum, making it safe to have no owner

* Move NotificationHub code into Platform/Push directory

* Update NotificationHub namespace imports

* Add attribute for storing push type metadata

* Rename Push Engines to have PushEngine suffix

* Move Push Registration items to their own directory

* Push code move

* Add expected usage comment

* Add Push feature registration method

- Make method able to be called multipes times with no ill effects

* Add Push Registration service entrypoint and tests

* Use new service entrypoints

* Test changes
2025-08-26 13:30:37 -04:00

55 lines
2.1 KiB
C#

using Bit.Core.Platform.Push;
using Bit.Core.Platform.Push.Internal;
using Bit.Core.Platform.PushRegistration.Internal;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Extension methods for adding the Push Registration feature.
/// </summary>
public static class PushRegistrationServiceCollectionExtensions
{
/// <summary>
/// Adds a <see cref="IPushRegistrationService"/> to the service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <returns>The <see cref="IServiceCollection"/> for chaining.</returns>
public static IServiceCollection AddPushRegistration(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
// TODO: Should add feature that brings in IInstallationDeviceRepository once that is featurized
// Register all possible variants under there concrete type.
services.TryAddSingleton<RelayPushRegistrationService>();
services.TryAddSingleton<NoopPushRegistrationService>();
services.AddHttpClient();
services.TryAddSingleton<INotificationHubPool, NotificationHubPool>();
services.TryAddSingleton<NotificationHubPushRegistrationService>();
services.TryAddSingleton<IPushRegistrationService>(static sp =>
{
var globalSettings = sp.GetRequiredService<GlobalSettings>();
if (globalSettings.SelfHosted)
{
if (CoreHelpers.SettingHasValue(globalSettings.PushRelayBaseUri) &&
CoreHelpers.SettingHasValue(globalSettings.Installation.Key))
{
return sp.GetRequiredService<RelayPushRegistrationService>();
}
return sp.GetRequiredService<NoopPushRegistrationService>();
}
return sp.GetRequiredService<NotificationHubPushRegistrationService>();
});
return services;
}
}