2026-10-01 18:39:18 +02:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
using MongoDB.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
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
public class ReactionsController(
|
|
|
|
|
DbEntities dbEntities,
|
|
|
|
|
MastodonMapper mapper,
|
|
|
|
|
IReactions reactionService) : MastodonController
|
2026-10-01 18:39:18 +02:00
|
|
|
{
|
2026-11-10 01:59:20 +01:00
|
|
|
[HttpGet(ApiPaths.Reactions.GET_M_List), Scope("read:statuses", requiresUser: false), AllowAnonymous]
|
2026-10-01 18:39:18 +02:00
|
|
|
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);
|
2026-11-10 01:24:43 +01:00
|
|
|
var accounts = await mapper.Accounts(reactions.Select(r => r.AccountId), token);
|
2026-10-01 18:39:18 +02:00
|
|
|
return Json(reactions.GroupBy(r => r.Emoji).Select(group => new
|
|
|
|
|
{
|
|
|
|
|
name = group.Key,
|
|
|
|
|
count = group.Count(),
|
|
|
|
|
me = MyId != default && group.Any(r => r.AccountId == MyId),
|
2026-11-10 01:24:43 +01:00
|
|
|
url = mapper.ProxiedUrl(group.First().EmojiURL),
|
2026-10-01 18:39:18 +02:00
|
|
|
accounts = group.Select(r => accounts.GetValueOrDefault(r.AccountId)).Where(a => a != default).ToList()
|
|
|
|
|
}).OrderByDescending(r => r.count).ToList());
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 01:59:20 +01:00
|
|
|
[HttpPut(ApiPaths.Reactions.PUT_M_React), Scope("write:favourites")]
|
2026-10-01 18:39:18 +02:00
|
|
|
public async Task<IActionResult> React(string id, string emoji, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var post = await Visible(id, token);
|
|
|
|
|
if (post == default)
|
|
|
|
|
return NotFoundError();
|
2026-11-10 01:24:43 +01:00
|
|
|
if (!await reactionService.React(Me, post, Uri.UnescapeDataString(emoji), token))
|
2026-10-01 18:39:18 +02:00
|
|
|
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: That emoji cannot be used here");
|
2026-11-10 01:24:43 +01:00
|
|
|
return Json(await mapper.Status(post, MyId, token));
|
2026-10-01 18:39:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-10 01:59:20 +01:00
|
|
|
[HttpDelete(ApiPaths.Reactions.DELETE_M_Unreact), Scope("write:favourites")]
|
2026-10-01 18:39:18 +02:00
|
|
|
public async Task<IActionResult> Unreact(string id, string emoji, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var post = await Visible(id, token);
|
|
|
|
|
if (post == default)
|
|
|
|
|
return NotFoundError();
|
2026-11-10 01:24:43 +01:00
|
|
|
await reactionService.Unreact(Me, post, Uri.UnescapeDataString(emoji), token);
|
|
|
|
|
return Json(await mapper.Status(post, MyId, token));
|
2026-10-01 18:39:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task<PostEntity> Visible(string id, CancellationToken token)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
var post = await dbEntities.Posts.Match(p => p.ID == id && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
|
2026-10-01 18:39:18 +02:00
|
|
|
return post != default && post.Visibility != PostVisibility.LocalGeo && await VisibilityPolicy.CanSee(post, MyId, token) ? post : default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|