2026-10-01 18:35:20 +02:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
|
|
|
|
using PrivaPub.Api.Mastodon.Mappers;
|
|
|
|
|
using PrivaPub.Domain.Privacy;
|
|
|
|
|
using PrivaPub.Domain.Statuses;
|
|
|
|
|
using PrivaPub.Models.Post;
|
|
|
|
|
using PrivaPub.StaticServices;
|
|
|
|
|
|
|
|
|
|
using PostEntity = PrivaPub.Models.Post.Post;
|
|
|
|
|
|
|
|
|
|
namespace PrivaPub.Api.Mastodon.Controllers
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
public class PollsController(
|
|
|
|
|
DbEntities dbEntities,
|
|
|
|
|
MastodonMapper mapper,
|
|
|
|
|
IPollService pollService) : MastodonController
|
2026-10-01 18:35:20 +02:00
|
|
|
{
|
|
|
|
|
[HttpGet("/api/v1/polls/{id}"), Scope("read:statuses", requiresUser: false), AllowAnonymous]
|
|
|
|
|
public async Task<IActionResult> Get(string id, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var post = await Visible(id, token);
|
2026-11-10 01:24:43 +01:00
|
|
|
return post?.Poll == default ? NotFoundError() : Json(mapper.Poll(post, MyId, await pollService.OwnVotes(post.ID, MyId, token)));
|
2026-10-01 18:35:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/api/v1/polls/{id}/votes"), Scope("write:statuses")]
|
|
|
|
|
public async Task<IActionResult> Vote(string id, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var post = await Visible(id, token);
|
|
|
|
|
if (post?.Poll == default)
|
|
|
|
|
return NotFoundError();
|
|
|
|
|
var choices = Params.List("choices").Select(c => int.TryParse(c, out var index) ? index : -1).ToList();
|
2026-11-10 01:24:43 +01:00
|
|
|
var outcome = await pollService.Vote(Me, post, choices, token);
|
2026-10-01 18:35:20 +02:00
|
|
|
if (!outcome.Ok)
|
|
|
|
|
return Error(outcome.Status, outcome.Error);
|
2026-11-10 01:24:43 +01:00
|
|
|
return Json(mapper.Poll(outcome.Post, MyId, await pollService.OwnVotes(post.ID, MyId, token)));
|
2026-10-01 18:35:20 +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).ExecuteFirstAsync(token);
|
2026-10-01 18:35:20 +02:00
|
|
|
return post != default && post.Visibility != PostVisibility.LocalGeo && await VisibilityPolicy.CanSee(post, MyId, token) ? post : default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|