Skip to content

Commit

Permalink
ConsumerAPI: Refactor IPushService (#374)
Browse files Browse the repository at this point in the history
* refactor: split IPushService into Registration and Sender services

* refactor: move IPushNotificationSender to BuildingBlocks.Application

* refactor: update IPushNotificationSender registrations

* Revert "refactor: update IPushNotificationSender registrations"

This reverts commit 4ae5c11.

* fix: renamed variables in the DeleteDeviceRegistration handler

* refactor: update IPushNotificationSender registrations
  • Loading branch information
JLSRKonk authored Nov 9, 2023
1 parent 2fae67e commit eee9155
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Backbone.DevelopmentKit.Identity.ValueObjects;

namespace Backbone.BuildingBlocks.Application.PushNotifications;

public interface IPushNotificationSender
{
Task SendNotification(IdentityAddress recipient, object notification, CancellationToken cancellationToken);
}

8 changes: 7 additions & 1 deletion ConsumerApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Backbone.Modules.Challenges.Infrastructure.Persistence.Database;
using Backbone.Modules.Devices.ConsumerApi;
using Backbone.Modules.Devices.Infrastructure.Persistence.Database;
using Backbone.Modules.Devices.Infrastructure.PushNotifications;
using Backbone.Modules.Files.ConsumerApi;
using Backbone.Modules.Files.Infrastructure.Persistence.Database;
using Backbone.Modules.Messages.ConsumerApi;
Expand All @@ -29,7 +30,6 @@
using Backbone.Modules.Tokens.ConsumerApi;
using Backbone.Modules.Tokens.Infrastructure.Persistence.Database;
using Backbone.Tooling.Extensions;
using MediatR;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Options;
Expand All @@ -38,6 +38,7 @@
using Serilog.Exceptions.Core;
using Serilog.Exceptions.EntityFrameworkCore.Destructurers;
using Serilog.Settings.Configuration;
using DevicesConfiguration = Backbone.Modules.Devices.ConsumerApi.Configuration;

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
Expand Down Expand Up @@ -168,6 +169,11 @@ static void ConfigureServices(IServiceCollection services, IConfiguration config
});

services.AddEventBus(parsedConfiguration.Infrastructure.EventBus);

var devicesConfiguration = new DevicesConfiguration();
configuration.GetSection("Modules:Devices").Bind(devicesConfiguration);

services.AddPushNotifications(devicesConfiguration.Infrastructure.PushNotifications);
}

static void Configure(WebApplication app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;

public interface IPushService
public interface IPushNotificationRegistrationService
{
Task UpdateRegistration(IdentityAddress address, DeviceId deviceId, PnsHandle handle, string appId, Environment environment, CancellationToken cancellationToken);
Task DeleteRegistration(DeviceId deviceId, CancellationToken cancellationToken);
Task SendNotification(IdentityAddress recipient, object notification, CancellationToken cancellationToken);
}

Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus;
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;
using Backbone.BuildingBlocks.Application.PushNotifications;
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications.Datawallet;

namespace Backbone.Modules.Devices.Application.IntegrationEvents.Incoming.DatawalletModificationCreated;

public class DatawalletModifiedIntegrationEventHandler : IIntegrationEventHandler<DatawalletModifiedIntegrationEvent>
{
private readonly IPushService _pushService;
private readonly IPushNotificationSender _pushSenderService;

public DatawalletModifiedIntegrationEventHandler(IPushService pushService)
public DatawalletModifiedIntegrationEventHandler(IPushNotificationSender pushSenderService)
{
_pushService = pushService;
_pushSenderService = pushSenderService;
}

public async Task Handle(DatawalletModifiedIntegrationEvent integrationEvent)
{
await _pushService.SendNotification(integrationEvent.Identity, new DatawalletModificationsCreatedPushNotification(integrationEvent.ModifiedByDevice), CancellationToken.None);
await _pushSenderService.SendNotification(integrationEvent.Identity, new DatawalletModificationsCreatedPushNotification(integrationEvent.ModifiedByDevice), CancellationToken.None);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus;
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;
using Backbone.BuildingBlocks.Application.PushNotifications;
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications.ExternalEvents;

namespace Backbone.Modules.Devices.Application.IntegrationEvents.Incoming.ExternalEventCreated;

public class ExternalEventCreatedIntegrationEventHandler : IIntegrationEventHandler<ExternalEventCreatedIntegrationEvent>
{
private readonly IPushService _pushService;
private readonly IPushNotificationSender _pushSenderService;

public ExternalEventCreatedIntegrationEventHandler(IPushService pushService)
public ExternalEventCreatedIntegrationEventHandler(IPushNotificationSender pushSenderService)
{
_pushService = pushService;
_pushSenderService = pushSenderService;
}

public async Task Handle(ExternalEventCreatedIntegrationEvent @event)
{
await _pushService.SendNotification(@event.Owner, new ExternalEventCreatedPushNotification(), CancellationToken.None);
await _pushSenderService.SendNotification(@event.Owner, new ExternalEventCreatedPushNotification(), CancellationToken.None);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
namespace Backbone.Modules.Devices.Application.PushNotifications.Commands.DeleteDeviceRegistration;
internal class Handler : IRequestHandler<DeleteDeviceRegistrationCommand>
{
private readonly IPushService _pushService;
private readonly IPushNotificationRegistrationService _pushRegistrationService;
private readonly DeviceId _activeDevice;

public Handler(IPushService pushService, IUserContext userContext)
public Handler(IPushNotificationRegistrationService pushRegistrationService, IUserContext userContext)
{
_pushService = pushService;
_pushRegistrationService = pushRegistrationService;
_activeDevice = userContext.GetDeviceId();
}


public async Task Handle(DeleteDeviceRegistrationCommand request, CancellationToken cancellationToken)
{
await _pushService.DeleteRegistration(_activeDevice, cancellationToken);
await _pushRegistrationService.DeleteRegistration(_activeDevice, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.UserContext;
using Backbone.BuildingBlocks.Application.PushNotifications;
using Backbone.DevelopmentKit.Identity.ValueObjects;
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;
using MediatR;

namespace Backbone.Modules.Devices.Application.PushNotifications.Commands.SendTestNotification;

public class Handler : IRequestHandler<SendTestNotificationCommand, Unit>
{
private readonly IdentityAddress _activeIdentity;
private readonly IPushService _pushService;
private readonly IPushNotificationSender _pushSenderService;

public Handler(IUserContext userContext, IPushService pushService)
public Handler(IUserContext userContext, IPushNotificationSender pushSenderService)
{
_pushService = pushService;
_pushSenderService = pushSenderService;
_activeIdentity = userContext.GetAddress();
}

public async Task<Unit> Handle(SendTestNotificationCommand request, CancellationToken cancellationToken)
{
await _pushService.SendNotification(_activeIdentity, request.Data, cancellationToken);
await _pushSenderService.SendNotification(_activeIdentity, request.Data, cancellationToken);
return Unit.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace Backbone.Modules.Devices.Application.PushNotifications.Commands.Update
public class Handler : IRequestHandler<UpdateDeviceRegistrationCommand, Unit>
{
private readonly IdentityAddress _activeIdentity;
private readonly IPushService _pushService;
private readonly IPushNotificationRegistrationService _pushRegistrationService;
private readonly DeviceId _activeDevice;
private const string PRODUCTION_ENVIRONMENT = "Production";
private const string DEVELOPMENT_ENVIRONMENT = "Development";

public Handler(IPushService pushService, IUserContext userContext)
public Handler(IPushNotificationRegistrationService pushRegistrationService, IUserContext userContext)
{
_pushService = pushService;
_pushRegistrationService = pushRegistrationService;
_activeIdentity = userContext.GetAddress();
_activeDevice = userContext.GetDeviceId();
}
Expand All @@ -30,7 +30,7 @@ public async Task<Unit> Handle(UpdateDeviceRegistrationCommand request, Cancella
var parseHandleResult = PnsHandle.Parse(request.Handle, DeserializePlatform(request.Platform));
if (parseHandleResult.IsSuccess)
{
await _pushService.UpdateRegistration(_activeIdentity, _activeDevice, parseHandleResult.Value, request.AppId, DeserializeEnvironment(request.Environment ?? PRODUCTION_ENVIRONMENT), cancellationToken);
await _pushRegistrationService.UpdateRegistration(_activeIdentity, _activeDevice, parseHandleResult.Value, request.AppId, DeserializeEnvironment(request.Environment ?? PRODUCTION_ENVIRONMENT), cancellationToken);
}
else
{
Expand Down
2 changes: 0 additions & 2 deletions Modules/Devices/src/Devices.ConsumerApi/DevicesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public override void ConfigureServices(IServiceCollection services, IConfigurati
options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider;
});

services.AddPushNotifications(parsedConfiguration.Infrastructure.PushNotifications);

services.AddSingleton<ISignatureHelper, SignatureHelper>(_ => SignatureHelper.CreateEd25519WithRawKeyFormat());

services.AddSqlDatabaseHealthCheck(Name, parsedConfiguration.Infrastructure.SqlDatabase.Provider, parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Backbone.BuildingBlocks.Infrastructure.Exceptions;
using Backbone.BuildingBlocks.Application.PushNotifications;
using Backbone.BuildingBlocks.Infrastructure.Exceptions;
using Backbone.DevelopmentKit.Identity.ValueObjects;
using Backbone.Modules.Devices.Application.Infrastructure.Persistence.Repository;
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;
Expand All @@ -10,7 +11,7 @@

namespace Backbone.Modules.Devices.Infrastructure.PushNotifications.DirectPush;

public class DirectPushService : IPushService
public class DirectPushService : IPushNotificationRegistrationService, IPushNotificationSender
{
private readonly IPnsRegistrationRepository _pnsRegistrationRepository;
private readonly ILogger<DirectPushService> _logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;
using Backbone.BuildingBlocks.Application.PushNotifications;
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;
using Backbone.Modules.Devices.Infrastructure.PushNotifications.DirectPush.ApplePushNotificationService;
using Backbone.Modules.Devices.Infrastructure.PushNotifications.DirectPush.FirebaseCloudMessaging;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -28,7 +29,8 @@ private static void AddApns(this IServiceCollection services)
{
services.AddHttpClient();
services.AddTransient<ApplePushNotificationServiceConnector>();
services.AddTransient<IPushService, DirectPushService>();
services.AddTransient<IPushNotificationRegistrationService, DirectPushService>();
services.AddTransient<IPushNotificationSender, DirectPushService>();
services.AddTransient<IJwtGenerator, JwtGenerator>();
services.AddSingleton<ApnsJwtCache>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Backbone.DevelopmentKit.Identity.ValueObjects;
using Backbone.BuildingBlocks.Application.PushNotifications;
using Backbone.DevelopmentKit.Identity.ValueObjects;
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;
using Backbone.Modules.Devices.Domain.Aggregates.PushNotifications.Handles;
using Microsoft.Extensions.Logging;
using Environment = Backbone.Modules.Devices.Domain.Aggregates.PushNotifications.Environment;

namespace Backbone.Modules.Devices.Infrastructure.PushNotifications.Dummy;

public class DummyPushService : IPushService
public class DummyPushService : IPushNotificationRegistrationService, IPushNotificationSender
{
private readonly ILogger<DummyPushService> _logger;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;
using Backbone.BuildingBlocks.Application.PushNotifications;
using Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;
using Microsoft.Extensions.DependencyInjection;

namespace Backbone.Modules.Devices.Infrastructure.PushNotifications.Dummy;
Expand All @@ -7,6 +8,7 @@ public static class IServiceCollectionExtensions
{
public static void AddDummyPushNotifications(this IServiceCollection services)
{
services.AddTransient<IPushService, DummyPushService>();
services.AddTransient<IPushNotificationRegistrationService, DummyPushService>();
services.AddTransient<IPushNotificationSender, DummyPushService>();
}
}

0 comments on commit eee9155

Please sign in to comment.