-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into NMSHDB-145-Backbone-Support-Show-sent-and-re…
…ceived-messages-of-an-identity
- Loading branch information
Showing
14 changed files
with
305 additions
and
21 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...nEvents/Incoming/RelationshipStatusChanged/RelationshipStatusChangedDomainEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Text; | ||
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus; | ||
using Backbone.DevelopmentKit.Identity.ValueObjects; | ||
using Backbone.Modules.Messages.Application.Infrastructure.Persistence.Repository; | ||
using Backbone.Modules.Messages.Domain.DomainEvents.Incoming; | ||
using Backbone.Modules.Messages.Domain.Entities; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Backbone.Modules.Messages.Application.DomainEvents.Incoming.RelationshipStatusChanged; | ||
|
||
public class RelationshipStatusChangedDomainEventHandler : IDomainEventHandler<RelationshipStatusChangedDomainEvent> | ||
{ | ||
private const string DELETED_IDENTITY_STRING = "deleted identity"; | ||
private readonly IMessagesRepository _messagesRepository; | ||
private readonly ILogger<RelationshipStatusChangedDomainEventHandler> _logger; | ||
private readonly ApplicationOptions _applicationOptions; | ||
|
||
public RelationshipStatusChangedDomainEventHandler(IMessagesRepository messagesRepository, IOptions<ApplicationOptions> applicationOptions, | ||
ILogger<RelationshipStatusChangedDomainEventHandler> logger) | ||
{ | ||
_messagesRepository = messagesRepository; | ||
_logger = logger; | ||
_applicationOptions = applicationOptions.Value; | ||
} | ||
|
||
public async Task Handle(RelationshipStatusChangedDomainEvent @event) | ||
{ | ||
if (@event.NewStatus != RelationshipStatus.ReadyForDeletion.ToString()) | ||
{ | ||
_logger.LogTrace("Relationship status changed to {newStatus}. No Message anonymization required.", @event.NewStatus); | ||
return; | ||
} | ||
|
||
var anonymizedIdentityAddress = IdentityAddress.Create(Encoding.Unicode.GetBytes(DELETED_IDENTITY_STRING), _applicationOptions.DidDomainName); | ||
var messagesExchangedBetweenRelationshipParticipants = (await _messagesRepository.Find(Message.WasExchangedBetween(@event.Initiator, @event.Peer), CancellationToken.None)).ToList(); | ||
foreach (var message in messagesExchangedBetweenRelationshipParticipants) | ||
{ | ||
message.SanitizeAfterRelationshipDeleted(@event.Initiator, @event.Peer, anonymizedIdentityAddress); | ||
} | ||
|
||
await _messagesRepository.Update(messagesExchangedBetweenRelationshipParticipants); | ||
} | ||
} | ||
|
||
internal static partial class RelationshipStatusChangedLogs | ||
{ | ||
[LoggerMessage( | ||
EventName = "Messages.RelationshipStatusChangedDomainEventHandler.RelationshipStatusChanged", | ||
Level = LogLevel.Debug, | ||
Message = "Relationship status changed to {newStatus}. No Message anonymization required.")] | ||
public static partial void RelationshipStatusChanged(this ILogger logger, string newStatus); | ||
} |
14 changes: 14 additions & 0 deletions
14
Modules/Messages/src/Messages.Application/Extensions/IEventBusExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus; | ||
using Backbone.Modules.Messages.Application.DomainEvents.Incoming.RelationshipStatusChanged; | ||
using Backbone.Modules.Messages.Domain.DomainEvents.Incoming; | ||
|
||
namespace Backbone.Modules.Messages.Application.Extensions; | ||
|
||
public static class IEventBusExtensions | ||
{ | ||
public static IEventBus AddMessagesDomainEventSubscriptions(this IEventBus eventBus) | ||
{ | ||
eventBus.Subscribe<RelationshipStatusChangedDomainEvent, RelationshipStatusChangedDomainEventHandler>(); | ||
return eventBus; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...essages/src/Messages.Domain/DomainEvents/Incoming/RelationshipStatusChangedDomainEvent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Backbone.BuildingBlocks.Domain.Events; | ||
|
||
namespace Backbone.Modules.Messages.Domain.DomainEvents.Incoming; | ||
|
||
public class RelationshipStatusChangedDomainEvent : DomainEvent | ||
{ | ||
public required string RelationshipId { get; set; } | ||
public required string NewStatus { get; set; } | ||
public required string Initiator { get; set; } | ||
public required string Peer { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
Modules/Messages/test/Messages.Domain.Tests/Messages/ExpressionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Backbone.Modules.Messages.Domain.Entities; | ||
using Backbone.UnitTestTools.BaseClasses; | ||
using Backbone.UnitTestTools.Data; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Backbone.Modules.Messages.Domain.Tests.Messages; | ||
|
||
public class ExpressionTests : AbstractTestsBase | ||
{ | ||
#region WasExchangedBetween | ||
|
||
[Fact] | ||
public void WasExchangedBetween_with_true_assertion() | ||
{ | ||
// Arrange | ||
var senderAddress = TestDataGenerator.CreateRandomIdentityAddress(); | ||
var recipientAddress = TestDataGenerator.CreateRandomIdentityAddress(); | ||
var recipient = new RecipientInformation(recipientAddress, []); | ||
var message = new Message(senderAddress, TestDataGenerator.CreateRandomDeviceId(), [], [], [recipient]); | ||
|
||
// Act | ||
var resultOne = message.EvaluateWasExchangedBetweenExpression(senderAddress, recipientAddress); | ||
var resultTwo = message.EvaluateWasExchangedBetweenExpression(recipientAddress, senderAddress); | ||
|
||
// Assert | ||
resultOne.Should().BeTrue(); | ||
resultTwo.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void WasExchangedBetween_with_false_assertion() | ||
{ | ||
// Arrange | ||
var senderAddress = TestDataGenerator.CreateRandomIdentityAddress(); | ||
var recipientAddress = TestDataGenerator.CreateRandomIdentityAddress(); | ||
var recipient = new RecipientInformation(recipientAddress, []); | ||
var message = new Message(senderAddress, TestDataGenerator.CreateRandomDeviceId(), [], [], [recipient]); | ||
|
||
// Act | ||
var result = message.EvaluateWasExchangedBetweenExpression(senderAddress, TestDataGenerator.CreateRandomIdentityAddress()); | ||
|
||
// Assert | ||
result.Should().BeFalse(); | ||
} | ||
|
||
#endregion | ||
} | ||
|
||
file static class MessageExtensions | ||
{ | ||
public static bool EvaluateWasExchangedBetweenExpression(this Message message, string identityAddressOne, string identityAddressTwo) | ||
{ | ||
var expression = Message.WasExchangedBetween(identityAddressOne, identityAddressTwo); | ||
return expression.Compile()(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
Modules/Messages/test/Messages.Domain.Tests/TestHelpers/TestData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Backbone.DevelopmentKit.Identity.ValueObjects; | ||
using Backbone.Modules.Messages.Domain.Entities; | ||
using Backbone.UnitTestTools.Data; | ||
|
||
namespace Backbone.Modules.Messages.Domain.Tests.TestHelpers; | ||
|
||
public static class TestData | ||
{ | ||
public static Message CreateMessageWithOneRecipient(IdentityAddress? senderAddress = null, IdentityAddress? recipientAddress = null) | ||
{ | ||
senderAddress ??= TestDataGenerator.CreateRandomIdentityAddress(); | ||
recipientAddress ??= TestDataGenerator.CreateRandomIdentityAddress(); | ||
|
||
var recipient = new RecipientInformation(recipientAddress, []); | ||
return new Message(senderAddress, TestDataGenerator.CreateRandomDeviceId(), [], [], [recipient]); | ||
} | ||
|
||
public static Message CreateMessageWithTwoRecipients(IdentityAddress? senderAddress = null, IdentityAddress? recipient1Address = null, IdentityAddress? recipient2Address = null) | ||
{ | ||
senderAddress ??= TestDataGenerator.CreateRandomIdentityAddress(); | ||
recipient1Address ??= TestDataGenerator.CreateRandomIdentityAddress(); | ||
recipient2Address ??= TestDataGenerator.CreateRandomIdentityAddress(); | ||
|
||
return new Message(senderAddress, TestDataGenerator.CreateRandomDeviceId(), [], [], [new RecipientInformation(recipient1Address, []), new RecipientInformation(recipient2Address, [])]); | ||
} | ||
} |
Oops, something went wrong.