Skip to content

Commit

Permalink
Merge pull request #701 from Crypter-File-Transfer/stable
Browse files Browse the repository at this point in the history
Merge 'stable' to 'main'
  • Loading branch information
Jack-Edwards authored Apr 14, 2024
2 parents 3ade3ba + e297d86 commit 9ba1736
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2024 Crypter File Transfer
*
* This file is part of the Crypter file transfer project.
*
* Crypter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Crypter source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can be released from the requirements of the aforementioned license
* by purchasing a commercial license. Buying such a license is mandatory
* as soon as you develop commercial activities involving the Crypter source
* code without disclosing the source code of your own applications.
*
* Contact the current copyright holder to discuss commercial license options.
*/

using System;
using System.Threading;
using System.Threading.Tasks;
using Crypter.Common.Enums;
using Crypter.DataAccess;
using Crypter.DataAccess.Entities;
using Crypter.DataAccess.Entities.JsonTypes.EventLogAdditionalData;
using MediatR;
using Unit = EasyMonads.Unit;

namespace Crypter.Core.Features.EventLog.Commands;

public sealed record LogSuccessfulTransferPreviewCommand(Guid ItemId, TransferItemType ItemType, Guid? UserId, DateTimeOffset Timestamp) : IRequest<Unit>;

internal sealed class LogSuccessfulTransferPreviewCommandHandler : IRequestHandler<LogSuccessfulTransferPreviewCommand, Unit>
{
private readonly DataContext _dataContext;

public LogSuccessfulTransferPreviewCommandHandler(DataContext dataContext)
{
_dataContext = dataContext;
}

public async Task<Unit> Handle(LogSuccessfulTransferPreviewCommand request, CancellationToken cancellationToken)
{
SuccessfulTransferPreviewAdditionalData additionalData = new SuccessfulTransferPreviewAdditionalData(request.ItemId, request.ItemType, request.UserId);
EventLogEntity logEntity = EventLogEntity.Create(EventLogType.TransferPreviewSuccess, additionalData, request.Timestamp);

_dataContext.EventLogs.Add(logEntity);
await _dataContext.SaveChangesAsync(CancellationToken.None);

return Unit.Default;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

namespace Crypter.Core.Features.EventLog.Commands;

public sealed record LogSuccessfulTransferUploadCommand(TransferItemType ItemType, long Size, Guid? Sender, string? Recipient, DateTimeOffset Timestamp) : IRequest<Unit>;
public sealed record LogSuccessfulTransferUploadCommand(Guid ItemId, TransferItemType ItemType, long Size, Guid? Sender, string? Recipient, DateTimeOffset Timestamp) : IRequest<Unit>;

internal sealed class LogSuccessfulTransferUploadCommandHandler : IRequestHandler<LogSuccessfulTransferUploadCommand, Unit>
{
Expand All @@ -49,7 +49,7 @@ public LogSuccessfulTransferUploadCommandHandler(DataContext dataContext)

public async Task<Unit> Handle(LogSuccessfulTransferUploadCommand request, CancellationToken cancellationToken)
{
SuccessfulTransferUploadAdditionalData additionalData = new SuccessfulTransferUploadAdditionalData(request.ItemType, request.Size, request.Sender, request.Recipient);
SuccessfulTransferUploadAdditionalData additionalData = new SuccessfulTransferUploadAdditionalData(request.ItemId, request.ItemType, request.Size, request.Sender, request.Recipient);
EventLogEntity logEntity = EventLogEntity.Create(EventLogType.TransferUploadSuccess, additionalData, request.Timestamp);

_dataContext.EventLogs.Add(logEntity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2024 Crypter File Transfer
*
* This file is part of the Crypter file transfer project.
*
* Crypter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Crypter source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can be released from the requirements of the aforementioned license
* by purchasing a commercial license. Buying such a license is mandatory
* as soon as you develop commercial activities involving the Crypter source
* code without disclosing the source code of your own applications.
*
* Contact the current copyright holder to discuss commercial license options.
*/

using System;
using System.Threading;
using System.Threading.Tasks;
using Crypter.Common.Enums;
using Crypter.Core.Services;
using Hangfire;
using MediatR;

namespace Crypter.Core.Features.Transfer.Events;

public sealed record SuccessfulTransferPreviewEvent(Guid ItemId, TransferItemType ItemType, Guid? UserId, DateTimeOffset Timestamp) : INotification;

internal sealed class SuccessfulTransferPreviewEventHandler : INotificationHandler<SuccessfulTransferPreviewEvent>
{
private readonly IBackgroundJobClient _backgroundJobClient;
private readonly IHangfireBackgroundService _hangfireBackgroundService;

public SuccessfulTransferPreviewEventHandler(
IBackgroundJobClient backgroundJobClient,
IHangfireBackgroundService hangfireBackgroundService)
{
_backgroundJobClient = backgroundJobClient;
_hangfireBackgroundService = hangfireBackgroundService;
}

public Task Handle(SuccessfulTransferPreviewEvent notification, CancellationToken cancellationToken)
{
_backgroundJobClient.Enqueue(() => _hangfireBackgroundService.LogSuccessfulTransferPreviewAsync(notification.ItemId, notification.ItemType, notification.UserId, notification.Timestamp));
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ await notification.RecipientId.IfSomeAsync(async recipientId =>
.Match((string?)null, x => x);

_backgroundJobClient.Enqueue(() =>
_hangfireBackgroundService.LogSuccessfulTransferUploadAsync(notification.ItemType, notification.Size, senderId, recipientUsername, notification.Timestamp));
_hangfireBackgroundService.LogSuccessfulTransferUploadAsync(notification.ItemId, notification.ItemType, notification.Size, senderId, recipientUsername, notification.Timestamp));
}

private async Task<Unit> QueueTransferNotificationAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
using System.Threading.Tasks;
using Crypter.Common.Contracts.Features.Transfer;
using Crypter.Common.Enums;
using Crypter.Core.Features.Transfer.Events;
using Crypter.Core.MediatorMonads;
using Crypter.Core.Repositories;
using Crypter.Core.Services;
using Crypter.DataAccess;
using EasyMonads;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Crypter.Core.Features.Transfer.Queries;
Expand All @@ -47,20 +49,22 @@ internal class AnonymousFilePreviewQueryHandler
{
private readonly DataContext _dataContext;
private readonly IHashIdService _hashIdService;
private readonly IPublisher _publisher;
private readonly ITransferRepository _transferRepository;

public AnonymousFilePreviewQueryHandler(DataContext dataContext, IHashIdService hashIdService, ITransferRepository transferRepository)
public AnonymousFilePreviewQueryHandler(DataContext dataContext, IHashIdService hashIdService, IPublisher publisher, ITransferRepository transferRepository)
{
_dataContext = dataContext;
_hashIdService = hashIdService;
_publisher = publisher;
_transferRepository = transferRepository;
}

public async Task<Either<TransferPreviewError, FileTransferPreviewResponse>> Handle(AnonymousFilePreviewQuery request, CancellationToken cancellationToken)
{
Guid id = _hashIdService.Decode(request.HashId);
Guid itemId = _hashIdService.Decode(request.HashId);
FileTransferPreviewResponse? filePreview = await _dataContext.AnonymousFileTransfers
.Where(x => x.Id == id)
.Where(x => x.Id == itemId)
.Select(x => new FileTransferPreviewResponse(
x.FileName,
x.ContentType,
Expand All @@ -80,12 +84,15 @@ public async Task<Either<TransferPreviewError, FileTransferPreviewResponse>> Han
}

bool ciphertextExists =
_transferRepository.TransferExists(id, TransferItemType.File, TransferUserType.Anonymous);
_transferRepository.TransferExists(itemId, TransferItemType.File, TransferUserType.Anonymous);

if (!ciphertextExists)
{
return TransferPreviewError.NotFound;
}

SuccessfulTransferPreviewEvent successfulTransferPreviewEvent = new SuccessfulTransferPreviewEvent(itemId, TransferItemType.File, null, DateTimeOffset.UtcNow);
await _publisher.Publish(successfulTransferPreviewEvent, CancellationToken.None);

return filePreview;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
using System.Threading.Tasks;
using Crypter.Common.Contracts.Features.Transfer;
using Crypter.Common.Enums;
using Crypter.Core.Features.Transfer.Events;
using Crypter.Core.MediatorMonads;
using Crypter.Core.Repositories;
using Crypter.Core.Services;
using Crypter.DataAccess;
using EasyMonads;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Crypter.Core.Features.Transfer.Queries;
Expand All @@ -47,20 +49,22 @@ internal class AnonymousMessagePreviewQueryHandler
{
private readonly DataContext _dataContext;
private readonly IHashIdService _hashIdService;
private readonly IPublisher _publisher;
private readonly ITransferRepository _transferRepository;

public AnonymousMessagePreviewQueryHandler(DataContext dataContext, IHashIdService hashIdService, ITransferRepository transferRepository)
public AnonymousMessagePreviewQueryHandler(DataContext dataContext, IHashIdService hashIdService, IPublisher publisher, ITransferRepository transferRepository)
{
_dataContext = dataContext;
_hashIdService = hashIdService;
_publisher = publisher;
_transferRepository = transferRepository;
}

public async Task<Either<TransferPreviewError, MessageTransferPreviewResponse>> Handle(AnonymousMessagePreviewQuery request, CancellationToken cancellationToken)
{
Guid id = _hashIdService.Decode(request.HashId);
Guid itemId = _hashIdService.Decode(request.HashId);
MessageTransferPreviewResponse? messagePreview = await _dataContext.AnonymousMessageTransfers
.Where(x => x.Id == id)
.Where(x => x.Id == itemId)
.Select(x => new MessageTransferPreviewResponse(
x.Subject,
x.Size,
Expand All @@ -79,13 +83,16 @@ public async Task<Either<TransferPreviewError, MessageTransferPreviewResponse>>
}

bool ciphertextExists = _transferRepository
.TransferExists(id, TransferItemType.Message, TransferUserType.Anonymous);
.TransferExists(itemId, TransferItemType.Message, TransferUserType.Anonymous);

if (!ciphertextExists)
{
return TransferPreviewError.NotFound;
}

SuccessfulTransferPreviewEvent successfulTransferPreviewEvent = new SuccessfulTransferPreviewEvent(itemId, TransferItemType.Message, null, DateTimeOffset.UtcNow);
await _publisher.Publish(successfulTransferPreviewEvent, CancellationToken.None);

return messagePreview;
}
}
15 changes: 11 additions & 4 deletions Crypter.Core/Features/Transfer/Queries/UserFilePreviewQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
using System.Threading.Tasks;
using Crypter.Common.Contracts.Features.Transfer;
using Crypter.Common.Enums;
using Crypter.Core.Features.Transfer.Events;
using Crypter.Core.MediatorMonads;
using Crypter.Core.Repositories;
using Crypter.Core.Services;
using Crypter.DataAccess;
using EasyMonads;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Crypter.Core.Features.Transfer.Queries;
Expand All @@ -47,12 +49,14 @@ internal class UserFilePreviewQueryHandler
{
private readonly DataContext _dataContext;
private readonly IHashIdService _hashIdService;
private readonly IPublisher _publisher;
private readonly ITransferRepository _transferRepository;

public UserFilePreviewQueryHandler(DataContext dataContext, IHashIdService hashIdService, ITransferRepository transferRepository)
public UserFilePreviewQueryHandler(DataContext dataContext, IHashIdService hashIdService, IPublisher publisher, ITransferRepository transferRepository)
{
_dataContext = dataContext;
_hashIdService = hashIdService;
_publisher = publisher;
_transferRepository = transferRepository;
}

Expand All @@ -61,10 +65,10 @@ public async Task<Either<TransferPreviewError, FileTransferPreviewResponse>> Han
Guid? nullableRequestorUserId = request.RequestorId
.Match<Guid?>(() => null, x => x);

Guid id = _hashIdService.Decode(request.HashId);
Guid itemId = _hashIdService.Decode(request.HashId);

FileTransferPreviewResponse? filePreview = await _dataContext.UserFileTransfers
.Where(x => x.Id == id)
.Where(x => x.Id == itemId)
.Where(x => x.RecipientId == null || x.RecipientId == nullableRequestorUserId)
.Select(x => new FileTransferPreviewResponse(
x.FileName,
Expand All @@ -87,13 +91,16 @@ public async Task<Either<TransferPreviewError, FileTransferPreviewResponse>> Han
}

bool ciphertextExists =
_transferRepository.TransferExists(id, TransferItemType.File, TransferUserType.User);
_transferRepository.TransferExists(itemId, TransferItemType.File, TransferUserType.User);

if (!ciphertextExists)
{
return TransferPreviewError.NotFound;
}

SuccessfulTransferPreviewEvent successfulTransferPreviewEvent = new SuccessfulTransferPreviewEvent(itemId, TransferItemType.File, nullableRequestorUserId, DateTimeOffset.UtcNow);
await _publisher.Publish(successfulTransferPreviewEvent, CancellationToken.None);

return filePreview;
}
}
15 changes: 11 additions & 4 deletions Crypter.Core/Features/Transfer/Queries/UserMessagePreviewQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
using System.Threading.Tasks;
using Crypter.Common.Contracts.Features.Transfer;
using Crypter.Common.Enums;
using Crypter.Core.Features.Transfer.Events;
using Crypter.Core.MediatorMonads;
using Crypter.Core.Repositories;
using Crypter.Core.Services;
using Crypter.DataAccess;
using EasyMonads;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Crypter.Core.Features.Transfer.Queries;
Expand All @@ -47,12 +49,14 @@ internal class UserMessagePreviewQueryHandler
{
private readonly DataContext _dataContext;
private readonly IHashIdService _hashIdService;
private readonly IPublisher _publisher;
private readonly ITransferRepository _transferRepository;

public UserMessagePreviewQueryHandler(DataContext dataContext, IHashIdService hashIdService, ITransferRepository transferRepository)
public UserMessagePreviewQueryHandler(DataContext dataContext, IHashIdService hashIdService, IPublisher publisher, ITransferRepository transferRepository)
{
_dataContext = dataContext;
_hashIdService = hashIdService;
_publisher = publisher;
_transferRepository = transferRepository;
}

Expand All @@ -61,10 +65,10 @@ public async Task<Either<TransferPreviewError, MessageTransferPreviewResponse>>
Guid? nullableRequestorUserId = request.RequestorId
.Match<Guid?>(() => null, x => x);

Guid id = _hashIdService.Decode(request.HashId);
Guid itemId = _hashIdService.Decode(request.HashId);

MessageTransferPreviewResponse? messagePreview = await _dataContext.UserMessageTransfers
.Where(x => x.Id == id)
.Where(x => x.Id == itemId)
.Where(x => x.RecipientId == null || x.RecipientId == nullableRequestorUserId)
.Select(x => new MessageTransferPreviewResponse(
x.Subject,
Expand All @@ -86,13 +90,16 @@ public async Task<Either<TransferPreviewError, MessageTransferPreviewResponse>>
}

bool ciphertextExists =
_transferRepository.TransferExists(id, TransferItemType.Message, TransferUserType.User);
_transferRepository.TransferExists(itemId, TransferItemType.Message, TransferUserType.User);

if (!ciphertextExists)
{
return TransferPreviewError.NotFound;
}

SuccessfulTransferPreviewEvent successfulTransferPreviewEvent = new SuccessfulTransferPreviewEvent(itemId, TransferItemType.Message, nullableRequestorUserId, DateTimeOffset.UtcNow);
await _publisher.Publish(successfulTransferPreviewEvent, CancellationToken.None);

return messagePreview;
}
}
Loading

0 comments on commit 9ba1736

Please sign in to comment.