Files
Govor/Govor.Application/Friends/FriendsBlockService.cs
Artemy 2d99c57a3b Expand server tests and enforce coverage
Consolidate server tests under tests/, upgrade them to .NET 10, and add CI coverage thresholds and reports. Expand API and application boundary coverage while fixing related issues in media handling, friend blocking, session crypto, presence, username validation, and local storage.
2026-10-24 15:30:32 +07:00

51 lines
2.4 KiB
C#

using Govor.Domain;
using Govor.Domain.Models;
using Microsoft.EntityFrameworkCore;
namespace Govor.Application.Friends;
public class FriendsBlockService(GovorDbContext context) : IFriendsBlockService
{
public async Task BlockFriendRequestAsync(Guid userId, Guid currentUserId)
{
if (userId == Guid.Empty || currentUserId == Guid.Empty || userId == currentUserId)
throw new ArgumentException("Two distinct users are required.");
if (await context.Users.CountAsync(u => u.Id == userId || u.Id == currentUserId) != 2)
throw new KeyNotFoundException("User not found.");
var friendship = await FindAsync(userId, currentUserId);
if (friendship?.Status == FriendshipStatus.Blocked)
{
if (friendship.RequesterId != currentUserId)
throw new UnauthorizedAccessException("Only the user who blocked this conversation can change the block.");
return;
}
if (friendship is null)
{
friendship = new Friendship { Id = Guid.NewGuid() };
context.Friendships.Add(friendship);
}
// A blocked row records its owner as Requester; requests cannot overwrite it.
friendship.RequesterId = currentUserId;
friendship.AddresseeId = userId;
friendship.Status = FriendshipStatus.Blocked;
await context.SaveChangesAsync();
}
public async Task UnblockFriendRequestAsync(Guid userId, Guid currentUserId)
{
if (userId == Guid.Empty || currentUserId == Guid.Empty || userId == currentUserId)
throw new ArgumentException("Two distinct users are required.");
var friendship = await FindAsync(userId, currentUserId);
if (friendship?.Status != FriendshipStatus.Blocked) return;
if (friendship.RequesterId != currentUserId)
throw new UnauthorizedAccessException("Only the user who blocked this conversation can remove the block.");
// Unblocking permits a new request; it does not restore friendship without consent.
friendship.Status = FriendshipStatus.Rejected;
await context.SaveChangesAsync();
}
private Task<Friendship?> FindAsync(Guid userId, Guid currentUserId) => context.Friendships.FirstOrDefaultAsync(f =>
(f.RequesterId == userId && f.AddresseeId == currentUserId) ||
(f.RequesterId == currentUserId && f.AddresseeId == userId));
}