Skip to content

Commit 5b317d5

Browse files
committed
Notify recipients upon multipart transfer finalization
1 parent f092718 commit 5b317d5

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

Crypter.Core/Features/Transfer/Commands/FinalizeMultipartFileTransferCommand.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
using MediatR;
4242
using Microsoft.EntityFrameworkCore;
4343
using Microsoft.EntityFrameworkCore.Storage;
44+
using Guid = System.Guid;
4445
using Unit = EasyMonads.Unit;
4546

4647
namespace Crypter.Core.Features.Transfer.Commands;
@@ -81,6 +82,9 @@ from additionalData in ValidateRequestAsync(request)
8182
from finalizeResult in FinalizeAsync(request, additionalData).ToLeftEitherAsync(Unit.Default)
8283
let successfulMultipartFinalizeEvent = new SuccessfulMultipartFileTransferFinalizationEvent(
8384
additionalData.InitializedTransferEntity.Id,
85+
additionalData.InitializedTransferEntity.RecipientId.HasValue
86+
? Maybe<Guid>.None
87+
: additionalData.InitializedTransferEntity.RecipientId.Value,
8488
utcNow)
8589
from sideEffects in Either<FinalizeMultipartFileTransferError, Unit>.FromRightAsync(
8690
UnitPublisher.Publish(_publisher, successfulMultipartFinalizeEvent))

Crypter.Core/Features/Transfer/Common.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
using Crypter.Common.Enums;
77
using Crypter.Core.Features.Metrics.Queries;
88
using Crypter.Core.LinqExpressions;
9+
using Crypter.Core.Services;
910
using Crypter.Core.Settings;
1011
using Crypter.DataAccess;
1112
using EasyMonads;
13+
using Hangfire;
1214
using Microsoft.EntityFrameworkCore;
1315

1416
namespace Crypter.Core.Features.Transfer;
@@ -89,6 +91,30 @@ internal static TransferUserType DetermineUploadTransferUserType(Maybe<Guid> sen
8991
return TransferUserType.Anonymous;
9092
}
9193

94+
internal static async Task<Unit> QueueTransferNotificationAsync(
95+
DataContext dataContext,
96+
IBackgroundJobClient backgroundJobClient,
97+
IHangfireBackgroundService hangfireBackgroundService,
98+
Guid itemId,
99+
TransferItemType itemType,
100+
Guid recipientId)
101+
{
102+
bool userExpectsNotification = await dataContext.Users
103+
.Where(x => x.Id == recipientId)
104+
.Where(x => x.NotificationSetting!.EnableTransferNotifications
105+
&& x.EmailVerified
106+
&& x.NotificationSetting.EmailNotifications)
107+
.AnyAsync();
108+
109+
if (userExpectsNotification)
110+
{
111+
backgroundJobClient.Enqueue(() =>
112+
hangfireBackgroundService.SendTransferNotificationAsync(itemId, itemType));
113+
}
114+
115+
return Unit.Default;
116+
}
117+
92118
/// <summary>
93119
/// Query for a transfer recipient's user id.
94120
/// </summary>

Crypter.Core/Features/Transfer/Events/SuccessfulMultipartFileTransferFinalizationEvent.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,45 @@
2929
using System.Threading.Tasks;
3030
using Crypter.Common.Enums;
3131
using Crypter.Core.Services;
32+
using Crypter.DataAccess;
33+
using EasyMonads;
3234
using Hangfire;
3335
using MediatR;
3436

3537
namespace Crypter.Core.Features.Transfer.Events;
3638

37-
public sealed record SuccessfulMultipartFileTransferFinalizationEvent(Guid ItemId, DateTimeOffset Timestamp)
39+
public sealed record SuccessfulMultipartFileTransferFinalizationEvent(Guid ItemId, Maybe<Guid> RecipientId, DateTimeOffset Timestamp)
3840
: INotification;
3941

4042
internal sealed class SuccessfulMultipartFileTransferFinalizationEventHandler
4143
: INotificationHandler<SuccessfulMultipartFileTransferFinalizationEvent>
4244
{
4345
private readonly IBackgroundJobClient _backgroundJobClient;
46+
private readonly DataContext _dataContext;
4447
private readonly IHangfireBackgroundService _hangfireBackgroundService;
4548

4649
public SuccessfulMultipartFileTransferFinalizationEventHandler(
4750
IBackgroundJobClient backgroundJobClient,
51+
DataContext dataContext,
4852
IHangfireBackgroundService hangfireBackgroundService)
4953
{
5054
_backgroundJobClient = backgroundJobClient;
55+
_dataContext = dataContext;
5156
_hangfireBackgroundService = hangfireBackgroundService;
5257
}
5358

54-
public Task Handle(SuccessfulMultipartFileTransferFinalizationEvent notification, CancellationToken cancellationToken)
59+
public async Task Handle(SuccessfulMultipartFileTransferFinalizationEvent notification, CancellationToken cancellationToken)
5560
{
61+
await notification.RecipientId.IfSomeAsync(async recipientId =>
62+
await Common.QueueTransferNotificationAsync(
63+
_dataContext,
64+
_backgroundJobClient,
65+
_hangfireBackgroundService,
66+
notification.ItemId,
67+
TransferItemType.File,
68+
recipientId));
69+
5670
_backgroundJobClient.Enqueue(() =>
5771
_hangfireBackgroundService.LogSuccessfulMultipartTransferFinalizationAsync(notification.ItemId, TransferItemType.File, notification.Timestamp));
58-
59-
return Task.CompletedTask;
6072
}
6173
}

Crypter.Core/Features/Transfer/Events/SuccessfulTransferUploadEvent.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ public SuccessfulTransferUploadEventHandler(
6060
public async Task Handle(SuccessfulTransferUploadEvent notification, CancellationToken cancellationToken)
6161
{
6262
await notification.RecipientId.IfSomeAsync(async recipientId =>
63-
await QueueTransferNotificationAsync(notification.ItemId, notification.ItemType, recipientId));
63+
await Common.QueueTransferNotificationAsync(
64+
_dataContext,
65+
_backgroundJobClient,
66+
_hangfireBackgroundService,
67+
notification.ItemId,
68+
notification.ItemType,
69+
recipientId));
6470

6571
_backgroundJobClient.Schedule(() =>
6672
_hangfireBackgroundService.DeleteTransferAsync(notification.ItemId, notification.ItemType, notification.UserType, true),
@@ -75,25 +81,4 @@ await notification.RecipientId.IfSomeAsync(async recipientId =>
7581
_backgroundJobClient.Enqueue(() =>
7682
_hangfireBackgroundService.LogSuccessfulTransferUploadAsync(notification.ItemId, notification.ItemType, notification.Size, senderId, recipientUsername, notification.Timestamp));
7783
}
78-
79-
private async Task<Unit> QueueTransferNotificationAsync(
80-
Guid itemId,
81-
TransferItemType itemType,
82-
Guid recipientId)
83-
{
84-
bool userExpectsNotification = await _dataContext.Users
85-
.Where(x => x.Id == recipientId)
86-
.Where(x => x.NotificationSetting!.EnableTransferNotifications
87-
&& x.EmailVerified
88-
&& x.NotificationSetting.EmailNotifications)
89-
.AnyAsync();
90-
91-
if (userExpectsNotification)
92-
{
93-
_backgroundJobClient.Enqueue(() =>
94-
_hangfireBackgroundService.SendTransferNotificationAsync(itemId, itemType));
95-
}
96-
97-
return Unit.Default;
98-
}
9984
}

0 commit comments

Comments
 (0)