From d209d125068d0759cf536898afb0424e05ad3620 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Sat, 25 Nov 2023 12:26:36 -0700 Subject: [PATCH] fediembed: polls --- src/modules/fedimbed.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/modules/fedimbed.js b/src/modules/fedimbed.js index 2716ac4..b15179a 100644 --- a/src/modules/fedimbed.js +++ b/src/modules/fedimbed.js @@ -137,6 +137,7 @@ async function processUrl(msg, url, spoiler = false) { author, timestamp, title, + poll, emotes = []; // Fetch post @@ -360,6 +361,17 @@ async function processUrl(msg, url, spoiler = false) { } } } + + if (postData2.poll) { + poll = { + end: new Date(postData2.poll.expires_at), + total: postData2.poll.votes_count, + options: postData2.poll.options.map((o) => ({ + name: o.title, + count: o.votes_count, + })), + }; + } } } } else { @@ -453,6 +465,17 @@ async function processUrl(msg, url, spoiler = false) { avatar: authorData.icon?.url, }; } + + if (postData.endTime && postData.oneOf && postData.votersCount) { + poll = { + end: new Date(postData.endTime), + total: postData.votersCount, + options: postData.oneOf.map((o) => ({ + name: o.name, + count: o.replies.totalItems, + })), + }; + } } // We could just continue without author but it'd look ugly and be confusing. @@ -591,6 +614,24 @@ async function processUrl(msg, url, spoiler = false) { } } + if (poll) { + baseEmbed.fields.push({ + name: "Poll", + value: + poll.options + .map((o) => { + const percent = o.count / poll.total; + const bar = Math.round(percent * 30); + + return `**${o.name}** (${percent.toFixed(2)}%)\n\`[${"=".repeat( + bar + )}${" ".repeat(30 - bar)}]\``; + }) + .join("\n\n") + + `\n\nEnds: `, + }); + } + const embeds = []; if (images.length > 0) {