Build / Build (push) Successful in 36s
Every answer of the Mastodon API and the client API now has its JSON shape generated when the server is built, instead of worked out by reflection while it runs; the bytes sent are unchanged. The small answers that were anonymous objects (the instance documents, notification policies, reports, lists, tags, trends, errors and the empty answers) are named types. A type without generated metadata fails at once in development and on the pasture, and in production is logged and still answered. Three lists that were sent as lazy sequences (moderation reports, the published domain blocks and the languages) are now built first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
70 lines
2.7 KiB
C#
70 lines
2.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using MongoDB.Entities;
|
|
|
|
using PrivaPub.Api.Mastodon.Entities;
|
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
|
using PrivaPub.Api.Mastodon.Mappers;
|
|
using PrivaPub.Domain.Privacy;
|
|
using PrivaPub.Domain.Social;
|
|
using PrivaPub.Models.Post;
|
|
using PrivaPub.Models.Social;
|
|
using PrivaPub.StaticServices;
|
|
|
|
using PostEntity = PrivaPub.Models.Post.Post;
|
|
|
|
namespace PrivaPub.Api.Mastodon.Controllers
|
|
{
|
|
public class ReactionsController(
|
|
DbEntities dbEntities,
|
|
MastodonMapper mapper,
|
|
IReactions reactionService) : MastodonController
|
|
{
|
|
[HttpGet(ApiPaths.Reactions.GET_M_List), Scope("read:statuses", requiresUser: false), AllowAnonymous]
|
|
public async Task<IActionResult> List(string id, CancellationToken token)
|
|
{
|
|
var post = await Visible(id, token);
|
|
if (post == default)
|
|
return NotFoundError();
|
|
var reactions = await DB.Default.Find<Reaction>().Match(r => r.PostId == post.ID).ExecuteAsync(token);
|
|
var accounts = await mapper.Accounts(reactions.Select(r => r.AccountId), token);
|
|
return Json(reactions.GroupBy(r => r.Emoji).Select(group => new PleromaReactionEntity
|
|
{
|
|
Name = group.Key,
|
|
Count = group.Count(),
|
|
Me = MyId != default && group.Any(r => r.AccountId == MyId),
|
|
Url = mapper.ProxiedUrl(group.First().EmojiURL),
|
|
Accounts = group.Select(r => accounts.GetValueOrDefault(r.AccountId)).Where(a => a != default).ToList()
|
|
}).OrderByDescending(r => r.Count).ToList());
|
|
}
|
|
|
|
[HttpPut(ApiPaths.Reactions.PUT_M_React), Scope("write:favourites")]
|
|
public async Task<IActionResult> React(string id, string emoji, CancellationToken token)
|
|
{
|
|
var post = await Visible(id, token);
|
|
if (post == default)
|
|
return NotFoundError();
|
|
if (!await reactionService.React(Me, post, Uri.UnescapeDataString(emoji), token))
|
|
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: That emoji cannot be used here");
|
|
return Json(await mapper.Status(post, MyId, token));
|
|
}
|
|
|
|
[HttpDelete(ApiPaths.Reactions.DELETE_M_Unreact), Scope("write:favourites")]
|
|
public async Task<IActionResult> Unreact(string id, string emoji, CancellationToken token)
|
|
{
|
|
var post = await Visible(id, token);
|
|
if (post == default)
|
|
return NotFoundError();
|
|
await reactionService.Unreact(Me, post, Uri.UnescapeDataString(emoji), token);
|
|
return Json(await mapper.Status(post, MyId, token));
|
|
}
|
|
|
|
async Task<PostEntity> Visible(string id, CancellationToken token)
|
|
{
|
|
var post = await dbEntities.Posts.Match(p => p.ID == id && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
|
|
return post != default && post.Visibility != PostVisibility.LocalGeo && await VisibilityPolicy.CanSee(post, MyId, token) ? post : default;
|
|
}
|
|
}
|
|
}
|