From 940f549821d813f43e1b6c79b54d6f8a98cef8ee Mon Sep 17 00:00:00 2001 From: scuti Date: Thu, 28 Nov 2024 15:31:41 -0800 Subject: [PATCH] display polls --- pleroma-comments.lua | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/pleroma-comments.lua b/pleroma-comments.lua index 01a40eb..26c470f 100644 --- a/pleroma-comments.lua +++ b/pleroma-comments.lua @@ -130,6 +130,34 @@ function write_comments(pleroma_posts, instance, show_avatars) return table.concat(media_list, "\n") end + function get_poll(poll) + if type(poll) ~= "table" then + return "" + end + local bar_chart = {"
"} + local bar_template = "
$label$
" + local total_votes = math.floor(poll["votes_count"]) + for _, v in pairs(poll["options"]) do + local width = math.floor( + 0.5 + (v["votes_count"]/total_votes) * 100) + vars = { + label = width .. "% " .. v["title"], + pct = width + } + local bar = bar_template:gsub("%$(%w+)%$", vars) + print(bar) + table.insert(bar_chart, bar) + end + table.insert(bar_chart, "
") + local summary = "

$x$ people have cast $y$ votes

" + local foo = summary:gsub( + "%$(%w+)%$", + {x=math.floor(poll["voters_count"]), y=total_votes} + ) + table.insert(bar_chart, foo) + return table.concat(bar_chart,"\n") + end + if #pleroma_posts == 0 then return "" end @@ -157,9 +185,7 @@ function write_comments(pleroma_posts, instance, show_avatars) table.insert( attrs, get_media(post["media_attachments"]) ) - if type(post["poll"]) == "table" then - table.insert(attrs, "

(poll attached)

") - end + table.insert(attrs, get_poll(post["poll"])) local interpolated = template:gsub("%$(%w+)%$", { i= #replies - i + 1,