Files
Ping/backend/Core/Ping.Application/Friends/BlockUser.cs
T
Admin 0623875dfa Add Ping backend
.NET 10 solution for Ping, a Discord style chat app: Domain, Application
and Infrastructure projects plus the Api host. SignalR for chat and
presence, LiveKit for call tokens, EF Core on Postgres, S3 compatible
storage for attachments, and OIDC auth shared with the Musify Zitadel
instance. Includes the Compose stack for local development.
2026-09-21 19:32:55 +02:00

47 lines
1.8 KiB
C#

using ErrorOr;
using Mediator;
using Microsoft.EntityFrameworkCore;
using Ping.Application.Contracts;
using Ping.Domain.Entities;
using Ping.Domain.ValueObjects;
namespace Ping.Application.Friends
{
public record BlockUserCommand(long CurrentUserId, long TargetUserId) : ICommand<ErrorOr<Success>>;
public class BlockUserCommandHandler(IDatabase database, IRealtimeNotifier notifier)
: ICommandHandler<BlockUserCommand, ErrorOr<Success>>
{
public async ValueTask<ErrorOr<Success>> Handle(BlockUserCommand request, CancellationToken cancellationToken)
{
if (request.CurrentUserId == request.TargetUserId)
return Error.Validation(description: "You cannot block yourself");
var friendship = await database.Friendships.FirstOrDefaultAsync(f =>
(f.RequesterId == request.CurrentUserId && f.AddresseeId == request.TargetUserId) ||
(f.RequesterId == request.TargetUserId && f.AddresseeId == request.CurrentUserId),
cancellationToken);
if (friendship is null)
{
friendship = new Friendship
{
Id = Guid.NewGuid(),
RequesterId = request.CurrentUserId,
AddresseeId = request.TargetUserId,
};
await database.Friendships.AddAsync(friendship, cancellationToken);
}
friendship.Status = FriendshipStatus.Blocked;
friendship.BlockedById = request.CurrentUserId;
friendship.RespondedAt = DateTime.UtcNow;
await database.SaveChangesAsync(cancellationToken);
await notifier.FriendshipUpdatedAsync(request.TargetUserId, friendship.Id, cancellationToken);
return new Success();
}
}
}