Add forwarding, friend controls, and benefits

Add message forwarding and reply lookup, friend removal and blocking endpoints, and database-backed Govor+ privileges with customizable profile tags. Include migrations, integration tests, and updated API and operations documentation.
This commit is contained in:
Artemy
2026-10-31 18:29:40 +07:00
parent a3401ceedf
commit 632ac04b56
78 changed files with 5284 additions and 720 deletions
@@ -0,0 +1,30 @@
using Govor.Application.Friends;
using Govor.Application.Infrastructure.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers.Friends;
[ApiController]
[Authorize]
[Route("api/friends/{userId:guid}")]
public sealed class FriendManagementController(IFriendshipService friends, IFriendsBlockService blocks,
ICurrentUserService currentUser) : ControllerBase
{
[HttpDelete]
public Task<IActionResult> Remove(Guid userId) => Execute(() => friends.RemoveFriendAsync(userId, currentUser.GetCurrentUserId()));
[HttpPut("block")]
public Task<IActionResult> Block(Guid userId) => Execute(() => blocks.BlockFriendRequestAsync(userId, currentUser.GetCurrentUserId()));
[HttpDelete("block")]
public Task<IActionResult> Unblock(Guid userId) => Execute(() => blocks.UnblockFriendRequestAsync(userId, currentUser.GetCurrentUserId()));
private async Task<IActionResult> Execute(Func<Task> action)
{
try { await action(); return NoContent(); }
catch (ArgumentException ex) { return BadRequest(new { error = ex.Message }); }
catch (KeyNotFoundException) { return NotFound(); }
catch (UnauthorizedAccessException) { return Forbid(); }
}
}
@@ -0,0 +1,28 @@
using AutoMapper;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Messages;
using Govor.Contracts.Responses;
using Govor.Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Govor.API.Controllers;
/// <summary>Loads a reply target outside the current history page without exposing another conversation.</summary>
[ApiController]
[Authorize]
[Route("api/messages")]
public sealed class MessageLookupController(GovorDbContext db, ICurrentUserService currentUser, IMapper mapper) : ControllerBase
{
[HttpGet("{messageId:guid}")]
public async Task<IActionResult> Get(Guid messageId)
{
var message = await db.Messages.AsNoTracking().Include(m => m.MediaAttachments).ThenInclude(m => m.MediaFile)
.Include(m => m.Reactions).Include(m => m.MessageViews).AsSplitQuery()
.SingleOrDefaultAsync(m => m.Id == messageId);
if (message is null || !await db.HasChatAccessAsync(currentUser.GetCurrentUserId(), message.RecipientId, message.RecipientType))
return NotFound();
return Ok(mapper.Map<MessageResponse>(message));
}
}
@@ -0,0 +1,58 @@
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Profiles;
using Govor.Contracts.Requests;
using Govor.Domain.Models.Users;
using Govor.API.Hubs;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers;
[ApiController]
[Authorize]
public sealed class UserBenefitsController(UserBenefitsService benefits, ICurrentUserService currentUser,
IHubContext<ProfileHub> profiles, ILogger<UserBenefitsController> logger) : ControllerBase
{
[HttpGet("api/profile/{userId:guid}/benefits")]
public Task<IActionResult> Get(Guid userId) => Execute(() => benefits.GetAsync(userId));
[Authorize(Roles = "Admin")]
[HttpPut("api/admin/users/{userId:guid}/privileges")]
public Task<IActionResult> SetPrivileges(Guid userId, SetPrivilegesRequest request) =>
Execute(() => Change(userId, () => benefits.SetPrivilegesAsync(userId, request.Privileges)));
[HttpPut("api/profile/tag")]
public Task<IActionResult> SetTag(ProfileTagStyle tag) =>
Execute(() => Change(currentUser.GetCurrentUserId(), () => benefits.SetTagAsync(currentUser.GetCurrentUserId(), tag)));
[HttpDelete("api/profile/tag")]
public Task<IActionResult> ResetTag() => Execute(() => Change(currentUser.GetCurrentUserId(),
() => benefits.SetTagAsync(currentUser.GetCurrentUserId(), null)));
private async Task<UserBenefitsView> Change(Guid userId, Func<Task<UserBenefitsView>> save)
{
var value = await save();
try
{
await profiles.Clients.All.SendAsync("UserBenefitsUpdated", new { userId, benefits = value });
}
catch (Exception ex)
{
// The committed result stays successful. Reconnecting clients reload the profile snapshot.
logger.LogError(ex, "Failed to broadcast benefits revision {Revision} for {UserId}", value.Revision, userId);
}
return value;
}
private async Task<IActionResult> Execute(Func<Task<UserBenefitsView>> action)
{
try { return Ok(await action()); }
catch (ArgumentException ex) { return BadRequest(new { error = ex.Message }); }
catch (KeyNotFoundException) { return NotFound(); }
catch (UnauthorizedAccessException) { return Forbid(); }
catch (DbUpdateConcurrencyException) { return Conflict(new { error = "Benefits changed concurrently. Reload and retry." }); }
}
}