Files
SocialPub/PrivaPub/Controllers/ClientToServer/GroupController.cs
T
thepraandClaude Opus 5.5 c2f439d1e9
Build / Build (push) Successful in 34s
Deploy / privapub.thepra.dev (push) Successful in 57s
Deploy / Release notes on the Releases tab (push) Successful in 3s
Every failure the server answers with names its own error code
Each refusal and failure the client API sends now starts with a code no other failure uses, such as "[ERR0030006] That
code is not valid.", so a screenshot or a report points at the exact place it came from. Nothing another program reads
changes: ActivityPub, the Mastodon API's errors, sign-in tokens and the error kinds decePubClient branches on stay as they
were. The house-rules checks move to 1.1.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
2026-11-10 08:11:24 +01:00

82 lines
4.5 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using PrivaPub.ClientModels;
using PrivaPub.ClientModels.Group;
using PrivaPub.Extensions;
using PrivaPub.Resources;
using PrivaPub.Services;
namespace PrivaPub.Controllers.ClientToServer
{
[ApiController,
Route(ApiPaths.Group.Prefix),
Authorize(Policy = Policies.IsUser)]
public class GroupController(
IGroupUsersService groupUsersService,
IStringLocalizer<GenericRes> localizer) : ControllerBase
{
[HttpGet, Route(ApiPaths.Group.GET_List)]
public async Task<IActionResult> List([FromQuery] string avatarId, CancellationToken token) =>
Answer(await groupUsersService.GetGroups(User.GetUserId(), avatarId, token));
[HttpPost, Route(ApiPaths.Group.POST_Insert)]
public async Task<IActionResult> Insert(InsertGroupForm form, CancellationToken token) =>
!ModelState.IsValid ? Invalid() : Answer(await groupUsersService.InsertGroup(User.GetUserId(), form, token));
[HttpPost, Route(ApiPaths.Group.POST_Update)]
public async Task<IActionResult> Update(UpdateGroupForm form, CancellationToken token) =>
!ModelState.IsValid ? Invalid() : Answer(await groupUsersService.UpdateGroup(User.GetUserId(), form, token));
[HttpPost, Route(ApiPaths.Group.POST_Join)]
public async Task<IActionResult> Join(JoinGroupForm form, CancellationToken token) =>
!ModelState.IsValid ? Invalid() : Answer(await groupUsersService.JoinGroup(User.GetUserId(), form, token));
[HttpPost, Route(ApiPaths.Group.POST_Leave)]
public async Task<IActionResult> Leave(GroupMembershipForm form, CancellationToken token) =>
!ModelState.IsValid ? Invalid() : Answer(await groupUsersService.LeaveGroup(User.GetUserId(), form, token));
[HttpPost, Route(ApiPaths.Group.POST_Approve)]
public async Task<IActionResult> Approve(GroupMembershipForm form, CancellationToken token) =>
!ModelState.IsValid ? Invalid() : Answer(await groupUsersService.ApproveMember(User.GetUserId(), form, token));
[HttpGet, Route(ApiPaths.Group.GET_Members)]
public async Task<IActionResult> Members([FromQuery] string avatarId, [FromQuery] string groupId, CancellationToken token) =>
Answer(await groupUsersService.GetMembers(User.GetUserId(), avatarId, groupId, token));
[HttpPost, Route(ApiPaths.Group.POST_Reject)]
public async Task<IActionResult> Reject(GroupMembershipForm form, CancellationToken token) =>
!ModelState.IsValid ? Invalid() : Answer(await groupUsersService.RejectMember(User.GetUserId(), form, token));
[HttpPost, Route(ApiPaths.Group.POST_Remove)]
public async Task<IActionResult> Remove(GroupMembershipForm form, CancellationToken token) =>
!ModelState.IsValid ? Invalid() : Answer(await groupUsersService.RemoveMember(User.GetUserId(), form, token));
// the group's owner deletes it, with the login's password (R6): its members and followers are told, its name stays
// taken (GroupRemoval)
[HttpPost, Route(ApiPaths.Group.POST_Delete), Microsoft.AspNetCore.RateLimiting.EnableRateLimiting(Infrastructure.RateLimiting.Accounts)]
public async Task<IActionResult> Delete(DeleteGroupForm form, [FromServices] IRootUsersService users, [FromServices] IGroupRemoval groups,
[FromServices] StaticServices.DbEntities dbEntities, CancellationToken token)
{
if (!ModelState.IsValid)
return Invalid();
var rootId = User.GetUserId();
if (!await users.PasswordMatches(rootId, form.Password, token))
return StatusCode(StatusCodes.Status403Forbidden, new WebResult().Invalidate(15001.ToCodedError(localizer[RootUsersService.NoMatch]), StatusCodes.Status403Forbidden));
var owns = await dbEntities.RootToAvatars.Match(ra => ra.RootId == rootId && ra.AvatarId == form.AvatarId).ExecuteAnyAsync(token);
var group = owns ? await dbEntities.Groups.MatchID(form.GroupId).Match(g => !g.DeletionAt.HasValue).ExecuteFirstAsync(token) : default;
if (group == default)
return NotFound(new WebResult().Invalidate(15002.ToCodedError(localizer["Group not found."]), StatusCodes.Status404NotFound));
if (group.OwnerAvatarId != form.AvatarId)
return StatusCode(StatusCodes.Status403Forbidden, new WebResult().Invalidate(15003.ToCodedError(localizer["Only the group's owner can delete it."]), StatusCodes.Status403Forbidden));
await groups.Remove(group.ID, CancellationToken.None);
return Ok();
}
IActionResult Invalid() => BadRequest(new WebResult().Invalidate(15004.ToCodedError(localizer["Invalid model."])));
IActionResult Answer(WebResult result) => result.IsValid ? Ok(result.Data) : StatusCode(result.StatusCode, result);
}
}