Post with polls

This commit is contained in:
2025-08-05 19:53:19 +08:00
parent d7a39ab574
commit 709dc44d57
6 changed files with 195 additions and 39 deletions

View File

@@ -3,7 +3,9 @@ using DysonNetwork.Shared.Auth;
using DysonNetwork.Shared.Content;
using DysonNetwork.Shared.Data;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Sphere.Poll;
using DysonNetwork.Sphere.Publisher;
using DysonNetwork.Sphere.WebReader;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@@ -18,7 +20,8 @@ public class PostController(
PostService ps,
PublisherService pub,
AccountService.AccountServiceClient accounts,
ActionLogService.ActionLogServiceClient als
ActionLogService.ActionLogServiceClient als,
PollService polls
)
: ControllerBase
{
@@ -275,6 +278,8 @@ public class PostController(
public Instant? PublishedAt { get; set; }
public Guid? RepliedPostId { get; set; }
public Guid? ForwardedPostId { get; set; }
public Guid? PollId { get; set; }
}
[HttpPost]
@@ -336,6 +341,25 @@ public class PostController(
post.ForwardedPostId = forwardedPost.Id;
}
if (request.PollId is not null)
{
try
{
var pollEmbed = await polls.MakePollEmbed(request.PollId.Value);
post.Meta ??= new Dictionary<string, object>();
if (!post.Meta.TryGetValue("embeds", out var existingEmbeds) ||
existingEmbeds is not List<EmbeddableBase>)
post.Meta["embeds"] = new List<Dictionary<string, object>>();
var embeds = (List<Dictionary<string, object>>)post.Meta["embeds"];
embeds.Add(pollEmbed.ToDictionary());
post.Meta["embeds"] = embeds;
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
try
{
post = await ps.PostAsync(
@@ -469,6 +493,27 @@ public class PostController(
if (request.Type is not null) post.Type = request.Type.Value;
if (request.Meta is not null) post.Meta = request.Meta;
if (request.PollId is not null)
{
try
{
var pollEmbed = await polls.MakePollEmbed(request.PollId.Value);
post.Meta ??= new Dictionary<string, object>();
if (!post.Meta.TryGetValue("embeds", out var existingEmbeds) ||
existingEmbeds is not List<EmbeddableBase>)
post.Meta["embeds"] = new List<Dictionary<string, object>>();
var embeds = (List<Dictionary<string, object>>)post.Meta["embeds"];
// Remove all old poll embeds
embeds.RemoveAll(e => e.TryGetValue("type", out var type) && type.ToString() == "poll");
embeds.Add(pollEmbed.ToDictionary());
post.Meta["embeds"] = embeds;
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
try
{
post = await ps.UpdatePostAsync(