mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
VerifyFriendshipTests
This commit is contained in:
@@ -114,14 +114,17 @@ public class MessageService : IMessageService
|
||||
|
||||
// TODO: Add a time limit for editing messages? e.g., if (message.SentAt < DateTime.UtcNow.AddMinutes(-15)) throw new Exception("Edit time limit exceeded");
|
||||
|
||||
// Keep a copy of the original message state for the result, if needed by Hub for notifications
|
||||
var originalMessageForNotification = new Message
|
||||
{
|
||||
Id = message.Id,
|
||||
SenderId = message.SenderId,
|
||||
RecipientId = message.RecipientId,
|
||||
RecipientType = message.RecipientType,
|
||||
// Populate other fields if necessary for the notification
|
||||
SentAt = message.SentAt,
|
||||
ReplyToMessageId = message.ReplyToMessageId,
|
||||
Reactions = message.Reactions,
|
||||
MediaAttachments = message.MediaAttachments,
|
||||
MessageViews = message.MessageViews,
|
||||
};
|
||||
|
||||
message.EncryptedContent = editParams.NewContent;
|
||||
@@ -160,20 +163,16 @@ public class MessageService : IMessageService
|
||||
// }
|
||||
}
|
||||
|
||||
// Keep a copy of the original message state for the result, if needed by Hub for notifications
|
||||
var originalMessageForNotification = new Message
|
||||
{
|
||||
Id = message.Id,
|
||||
SenderId = message.SenderId,
|
||||
RecipientId = message.RecipientId,
|
||||
RecipientType = message.RecipientType,
|
||||
// Populate other fields if necessary for the notification
|
||||
};
|
||||
|
||||
await _messagesRepository.RemoveAsync(deleteParams.MessageId);
|
||||
|
||||
// TODO: Delete associated media attachments from storage if they are no longer referenced.
|
||||
|
||||
_logger.LogInformation("Message {MessageId} deleted successfully by user {DeleterId}", deleteParams.MessageId, deleteParams.DeleterId);
|
||||
return new DeleteMessageResult(true, null, originalMessageForNotification);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ using Govor.Application.Exceptions.VerifyFriendship;
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Repositories.Friendships;
|
||||
using Govor.Data.Repositories.Exceptions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Govor.Application.Services;
|
||||
|
||||
|
||||
public class VerifyFriendship : IVerifyFriendship
|
||||
{
|
||||
private readonly IFriendshipsRepository _friendshipsRepository;
|
||||
@@ -21,23 +21,38 @@ public class VerifyFriendship : IVerifyFriendship
|
||||
|
||||
public async Task VerifyAsync(Guid targetUserId, Guid friendUserId)
|
||||
{
|
||||
if (targetUserId == Guid.Empty || friendUserId == Guid.Empty)
|
||||
try
|
||||
{
|
||||
_logger?.LogWarning("Invalid user IDs provided: targetUserId={TargetUserId}, friendUserId={FriendUserId}", targetUserId, friendUserId);
|
||||
throw new ArgumentException("User IDs cannot be empty.", nameof(targetUserId));
|
||||
}
|
||||
if (targetUserId == Guid.Empty || friendUserId == Guid.Empty)
|
||||
{
|
||||
_logger?.LogWarning(
|
||||
"Invalid user IDs provided: targetUserId={TargetUserId}, friendUserId={FriendUserId}", targetUserId,
|
||||
friendUserId);
|
||||
throw new ArgumentException("User IDs cannot be empty.", nameof(targetUserId));
|
||||
}
|
||||
|
||||
var friendships = await _friendshipsRepository.FindByUserIdAsync(targetUserId);
|
||||
var friendship = friendships.Where(f => f.AddresseeId == friendUserId || f.RequesterId == friendUserId)?.FirstOrDefault();
|
||||
|
||||
if (friendship == null || friendship.Status != FriendshipStatus.Accepted)
|
||||
var friendships = await _friendshipsRepository.FindByUserIdAsync(targetUserId);
|
||||
var friendship = friendships.Where(f => f.AddresseeId == friendUserId || f.RequesterId == friendUserId)
|
||||
?.FirstOrDefault();
|
||||
|
||||
if (friendship == null || friendship.Status != FriendshipStatus.Accepted)
|
||||
{
|
||||
var errorMessage = string.Format(FriendshipNotAcceptedError, targetUserId, friendUserId);
|
||||
_logger?.LogError(errorMessage);
|
||||
throw new FriendshipException(errorMessage);
|
||||
}
|
||||
|
||||
_logger?.LogInformation(
|
||||
"Friendship verified successfully for targetUserId={TargetUserId}, friendUserId={FriendUserId}",
|
||||
targetUserId, friendUserId);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
var errorMessage = string.Format(FriendshipNotAcceptedError, targetUserId, friendUserId);
|
||||
_logger?.LogError(errorMessage);
|
||||
|
||||
throw new FriendshipException(errorMessage);
|
||||
}
|
||||
|
||||
_logger?.LogInformation("Friendship verified successfully for targetUserId={TargetUserId}, friendUserId={FriendUserId}", targetUserId, friendUserId);
|
||||
}
|
||||
|
||||
public async Task<bool> TryVerifyAsync(Guid targetUserId, Guid friendUserId)
|
||||
@@ -47,7 +62,7 @@ public class VerifyFriendship : IVerifyFriendship
|
||||
await VerifyAsync(targetUserId, friendUserId);
|
||||
return true;
|
||||
}
|
||||
catch (FriendshipException ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user