Compare commits

..

No commits in common. "b463e1173ba7209d79447b1703e3e6e54f96d64c" and "52d9c6fea812a4fd92635476ae5b5be27900c233" have entirely different histories.

5 changed files with 36 additions and 142 deletions

View file

@ -9,15 +9,22 @@ const {discord, sync, db, select, from} = passthrough
const {reg} = require("../../matrix/read-registration") const {reg} = require("../../matrix/read-registration")
/** @type {import("./poll-vote")} */ /** @type {import("./poll-vote")} */
const vote = sync.require("../actions/poll-vote") const vote = sync.require("../actions/poll-vote")
/** @type {import("../../discord/interactions/poll-responses")} */ /** @type {import("../../m2d/converters/poll-components")} */
const pollResponses = sync.require("../../discord/interactions/poll-responses") const pollComponents = sync.require("../../m2d/converters/poll-components")
// This handles, in the following order:
// * verifying Matrix-side votes are accurate for a poll originating on Discord, sending missed votes to Matrix if necessary
// * sending a message to Discord if a vote in that poll has been cast on Matrix
// This does *not* handle bridging of poll closures on Discord to Matrix; that takes place in converters/message-to-event.js.
/** /**
* @file This handles, in the following order: * @param {number} percent
* * verifying Matrix-side votes are accurate for a poll originating on Discord, sending missed votes to Matrix if necessary
* * sending a message to Discord if a vote in that poll has been cast on Matrix
* This does *not* handle bridging of poll closures on Discord to Matrix; that takes place in converters/message-to-event.js.
*/ */
function barChart(percent) {
const width = 12
const bars = Math.floor(percent*width)
return "█".repeat(bars) + "▒".repeat(width-bars)
}
/** /**
* @param {string} channelID * @param {string} channelID
@ -107,14 +114,26 @@ async function endPoll(closeMessage) {
})) }))
} }
const {combinedVotes, messageString} = pollResponses.getCombinedResults(pollMessageID, true) /** @type {{matrix_option: string, option_text: string, count: number}[]} */
const pollResults = db.prepare("SELECT matrix_option, option_text, seq, count(discord_or_matrix_user_id) as count FROM poll_option LEFT JOIN poll_vote USING (message_id, matrix_option) WHERE message_id = ? GROUP BY matrix_option ORDER BY seq").all(pollMessageID)
const combinedVotes = pollResults.reduce((a, c) => a + c.count, 0)
const totalVoters = db.prepare("SELECT count(DISTINCT discord_or_matrix_user_id) as count FROM poll_vote WHERE message_id = ?").pluck().get(pollMessageID)
if (combinedVotes !== totalVotes) { // This means some votes were cast on Matrix. Now that we've corrected the vote totals, we can get the results again and post them to Discord. if (combinedVotes !== totalVotes) { // This means some votes were cast on Matrix!
// Now that we've corrected the vote totals, we can get the results again and post them to Discord!
const topAnswers = pollResults.toSorted((a, b) => b.count - a.count)
let messageString = ""
for (const option of pollResults) {
const medal = pollComponents.getMedal(topAnswers, option.count)
const countString = `${String(option.count).padStart(String(topAnswers[0].count).length)}`
const votesString = option.count === 1 ? "vote " : "votes"
const label = medal === "🥇" ? `**${option.option_text}**` : option.option_text
messageString += `\`\u200b${countString} ${votesString}\u200b\` ${barChart(option.count/totalVoters)} ${label} ${medal}\n`
}
return { return {
username: "Total results including Matrix votes", username: "Total results including Matrix votes",
avatar_url: `${reg.ooye.bridge_origin}/discord/poll-star-avatar.png`, avatar_url: `${reg.ooye.bridge_origin}/discord/poll-star-avatar.png`,
content: messageString, content: messageString
flags: DiscordTypes.MessageFlags.SuppressEmbeds
} }
} }
} }

View file

@ -16,12 +16,8 @@ function eventCanBeEdited(ev) {
if (ev.old.event_type === "m.room.message" && ev.old.event_subtype !== "m.text" && ev.old.event_subtype !== "m.emote" && ev.old.event_subtype !== "m.notice") { if (ev.old.event_type === "m.room.message" && ev.old.event_subtype !== "m.text" && ev.old.event_subtype !== "m.emote" && ev.old.event_subtype !== "m.notice") {
return false return false
} }
// Discord does not allow stickers to be edited. // Discord does not allow stickers to be edited. Poll closures are sent as "edits", but not in a way we care about.
if (ev.old.event_type === "m.sticker") { if (ev.old.event_type === "m.sticker" || ev.old.event_type === "org.matrix.msc3381.poll.start") {
return false
}
// Discord does not allow the data of polls to be edited, they may only be responded to.
if (ev.old.event_type === "org.matrix.msc3381.poll.start" || ev.old.event_type === "org.matrix.msc3381.poll.end") {
return false return false
} }
// Anything else is fair game. // Anything else is fair game.

View file

@ -20,8 +20,6 @@ const mxUtils = sync.require("../../matrix/utils")
const dUtils = sync.require("../../discord/utils") const dUtils = sync.require("../../discord/utils")
/** @type {import("./find-mentions")} */ /** @type {import("./find-mentions")} */
const findMentions = sync.require("./find-mentions") const findMentions = sync.require("./find-mentions")
/** @type {import("../../discord/interactions/poll-responses")} */
const pollResponses = sync.require("../../discord/interactions/poll-responses")
const {reg} = require("../../matrix/read-registration") const {reg} = require("../../matrix/read-registration")
/** /**
@ -271,21 +269,7 @@ async function messageToEvent(message, guild, options = {}, di) {
} }
if (message.type === DiscordTypes.MessageType.PollResult) { if (message.type === DiscordTypes.MessageType.PollResult) {
const pollMessageID = message.message_reference?.message_id const event_id = select("event_message", "event_id", {message_id: message.message_reference?.message_id}).pluck().get()
if (!pollMessageID) return []
const event_id = select("event_message", "event_id", {message_id: pollMessageID}).pluck().get()
const roomID = select("channel_room", "room_id", {channel_id: message.channel_id}).pluck().get()
const pollQuestionText = select("poll", "question_text", {message_id: pollMessageID}).pluck().get()
if (!event_id || !roomID || !pollQuestionText) return [] // drop it if the corresponding poll start was not bridged
const rep = new mxUtils.MatrixStringBuilder()
rep.addLine(`The poll ${pollQuestionText} has closed.`, tag`The poll <a href="https://matrix.to/#/${roomID}/${event_id}">${pollQuestionText}</a> has closed.`)
const {messageString} = pollResponses.getCombinedResults(pollMessageID, true) // poll results have already been double-checked before this point, so these totals will be accurate
rep.addLine(markdown.toHTML(messageString, {discordOnly: true, escapeHTML: false}), markdown.toHTML(messageString, {}))
const {body, formatted_body} = rep.get()
return [{ return [{
$type: "org.matrix.msc3381.poll.end", $type: "org.matrix.msc3381.poll.end",
"m.relates_to": { "m.relates_to": {
@ -293,11 +277,7 @@ async function messageToEvent(message, guild, options = {}, di) {
event_id event_id
}, },
"org.matrix.msc3381.poll.end": {}, "org.matrix.msc3381.poll.end": {},
"org.matrix.msc1767.text": body, body: "This poll has ended.",
"org.matrix.msc1767.html": formatted_body,
body: body,
format: "org.matrix.custom.html",
formatted_body: formatted_body,
msgtype: "m.text" msgtype: "m.text"
}] }]
} }

View file

@ -1,94 +0,0 @@
// @ts-check
const DiscordTypes = require("discord-api-types/v10")
const {discord, sync, db, select, from} = require("../../passthrough")
const {id: botID} = require("../../../addbot")
const {InteractionMethods} = require("snowtransfer")
/** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api")
/** @type {import("../../m2d/converters/poll-components")} */
const pollComponents = sync.require("../../m2d/converters/poll-components")
const {reg} = require("../../matrix/read-registration")
/**
* @param {number} percentc
*/
function barChart(percent) {
const width = 12
const bars = Math.floor(percent*width)
return "█".repeat(bars) + "▒".repeat(width-bars)
}
/**
* @param {string} pollMessageID
* @param {boolean} isClosed
*/
function getCombinedResults(pollMessageID, isClosed) {
/** @type {{matrix_option: string, option_text: string, count: number}[]} */
const pollResults = db.prepare("SELECT matrix_option, option_text, seq, count(discord_or_matrix_user_id) as count FROM poll_option LEFT JOIN poll_vote USING (message_id, matrix_option) WHERE message_id = ? GROUP BY matrix_option ORDER BY seq").all(pollMessageID)
const combinedVotes = pollResults.reduce((a, c) => a + c.count, 0)
const totalVoters = db.prepare("SELECT count(DISTINCT discord_or_matrix_user_id) as count FROM poll_vote WHERE message_id = ?").pluck().get(pollMessageID)
const topAnswers = pollResults.toSorted((a, b) => b.count - a.count)
let messageString = ""
for (const option of pollResults) {
const medal = isClosed ? pollComponents.getMedal(topAnswers, option.count) : ""
const countString = `${String(option.count).padStart(String(topAnswers[0].count).length)}`
const votesString = option.count === 1 ? "vote " : "votes"
const label = medal === "🥇" ? `**${option.option_text}**` : option.option_text
messageString += `\`\u200b${countString} ${votesString}\u200b\` ${barChart(option.count/totalVoters)} ${label} ${medal}\n`
}
return {messageString, combinedVotes, totalVoters}
}
/**
* @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction
* @param {{api: typeof api}} di
* @returns {AsyncGenerator<{[k in keyof InteractionMethods]?: Parameters<InteractionMethods[k]>[2]}>}
*/
async function* _interact({data}, {api}) {
const row = select("poll", "is_closed", {message_id: data.target_id}).get()
if (!row) {
return yield {createInteractionResponse: {
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
data: {
content: "This poll hasn't been bridged to Matrix.",
flags: DiscordTypes.MessageFlags.Ephemeral
}
}}
}
const {messageString} = getCombinedResults(data.target_id, !!row.is_closed)
return yield {createInteractionResponse: {
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
data: {
embeds: [{
author: {
name: "Current results including Matrix votes",
icon_url: `${reg.ooye.bridge_origin}/discord/poll-star-avatar.png`
},
description: messageString
}],
flags: DiscordTypes.MessageFlags.Ephemeral
}
}}
}
/* c8 ignore start */
/** @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction */
async function interact(interaction) {
for await (const response of _interact(interaction, {api})) {
if (response.createInteractionResponse) {
await discord.snow.interaction.createInteractionResponse(interaction.id, interaction.token, response.createInteractionResponse)
}
}
}
module.exports.interact = interact
module.exports._interact = _interact
module.exports.getCombinedResults = getCombinedResults

View file

@ -10,7 +10,6 @@ const permissions = sync.require("./interactions/permissions.js")
const reactions = sync.require("./interactions/reactions.js") const reactions = sync.require("./interactions/reactions.js")
const privacy = sync.require("./interactions/privacy.js") const privacy = sync.require("./interactions/privacy.js")
const poll = sync.require("./interactions/poll.js") const poll = sync.require("./interactions/poll.js")
const pollResponses = sync.require("./interactions/poll-responses.js")
const ping = sync.require("./interactions/ping.js") const ping = sync.require("./interactions/ping.js")
// User must have EVERY permission in default_member_permissions to be able to use the command // User must have EVERY permission in default_member_permissions to be able to use the command
@ -25,7 +24,7 @@ discord.snow.interaction.bulkOverwriteApplicationCommands(id, [{
type: DiscordTypes.ApplicationCommandType.Message, type: DiscordTypes.ApplicationCommandType.Message,
default_member_permissions: String(DiscordTypes.PermissionFlagsBits.KickMembers | DiscordTypes.PermissionFlagsBits.ManageRoles) default_member_permissions: String(DiscordTypes.PermissionFlagsBits.KickMembers | DiscordTypes.PermissionFlagsBits.ManageRoles)
}, { }, {
name: "Responses", name: "Reactions",
contexts: [DiscordTypes.InteractionContextType.Guild], contexts: [DiscordTypes.InteractionContextType.Guild],
type: DiscordTypes.ApplicationCommandType.Message type: DiscordTypes.ApplicationCommandType.Message
}, { }, {
@ -108,14 +107,8 @@ async function dispatchInteraction(interaction) {
await permissions.interact(interaction) await permissions.interact(interaction)
} else if (interactionId === "permissions_edit") { } else if (interactionId === "permissions_edit") {
await permissions.interactEdit(interaction) await permissions.interactEdit(interaction)
} else if (interactionId === "Responses") { } else if (interactionId === "Reactions") {
/** @type {DiscordTypes.APIMessageApplicationCommandGuildInteraction} */ // @ts-ignore await reactions.interact(interaction)
const messageInteraction = interaction
if (messageInteraction.data.resolved.messages[messageInteraction.data.target_id]?.poll) {
await pollResponses.interact(messageInteraction)
} else {
await reactions.interact(messageInteraction)
}
} else if (interactionId === "ping") { } else if (interactionId === "ping") {
await ping.interact(interaction) await ping.interact(interaction)
} else if (interactionId === "privacy") { } else if (interactionId === "privacy") {