Fixes to vote counting
This commit is contained in:
Generated
+4
-4
@@ -35,7 +35,7 @@
|
||||
"lru-cache": "^11.0.2",
|
||||
"prettier-bytes": "^1.0.4",
|
||||
"sharp": "^0.34.5",
|
||||
"snowtransfer": "^0.17.0",
|
||||
"snowtransfer": "^0.17.1",
|
||||
"stream-mime-type": "^1.0.2",
|
||||
"try-to-catch": "^3.0.1",
|
||||
"uqr": "^0.1.2",
|
||||
@@ -2727,9 +2727,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/snowtransfer": {
|
||||
"version": "0.17.0",
|
||||
"resolved": "https://registry.npmjs.org/snowtransfer/-/snowtransfer-0.17.0.tgz",
|
||||
"integrity": "sha512-H6Avpsco+HlVIkN+MbX34Q7+9g9Wci0wZQwGsvfw20VqEb7jnnk73iUcWytNMYtKZ72Ud58n6cFnQ3apTEamxw==",
|
||||
"version": "0.17.1",
|
||||
"resolved": "https://registry.npmjs.org/snowtransfer/-/snowtransfer-0.17.1.tgz",
|
||||
"integrity": "sha512-WSXj055EJhzzfD7B3oHVyRTxkqFCaxcVhwKY6B3NkBSHRyM6wHxZLq6VbFYhopUg+lMtd7S1ZO8JM+Ut+js2iA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"discord-api-types": "^0.38.37"
|
||||
|
||||
+1
-1
@@ -44,7 +44,7 @@
|
||||
"lru-cache": "^11.0.2",
|
||||
"prettier-bytes": "^1.0.4",
|
||||
"sharp": "^0.34.5",
|
||||
"snowtransfer": "^0.17.0",
|
||||
"snowtransfer": "^0.17.1",
|
||||
"stream-mime-type": "^1.0.2",
|
||||
"try-to-catch": "^3.0.1",
|
||||
"uqr": "^0.1.2",
|
||||
|
||||
+12
-22
@@ -7,18 +7,10 @@ const {isDeepStrictEqual} = require("util")
|
||||
const passthrough = require("../../passthrough")
|
||||
const {discord, sync, db, select, from} = passthrough
|
||||
const {reg} = require("../../matrix/read-registration")
|
||||
/** @type {import("../../matrix/api")} */
|
||||
const api = sync.require("../../matrix/api")
|
||||
/** @type {import("./register-user")} */
|
||||
const registerUser = sync.require("./register-user")
|
||||
/** @type {import("./create-room")} */
|
||||
const createRoom = sync.require("../actions/create-room")
|
||||
/** @type {import("./poll-vote")} */
|
||||
const vote = sync.require("../actions/poll-vote")
|
||||
/** @type {import("../../m2d/converters/poll-components")} */
|
||||
const pollComponents = sync.require("../../m2d/converters/poll-components")
|
||||
/** @type {import("../../m2d/actions/channel-webhook")} */
|
||||
const channelWebhook = sync.require("../../m2d/actions/channel-webhook")
|
||||
|
||||
// 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
|
||||
@@ -45,26 +37,22 @@ async function getAllVotesOnAnswer(channelID, messageID, answerID){
|
||||
/** @type {DiscordTypes.RESTGetAPIPollAnswerVotersResult["users"]} */
|
||||
let voteUsers = []
|
||||
let after = undefined
|
||||
while (!voteUsers.length || after) {
|
||||
while (true) {
|
||||
const curVotes = await discord.snow.channel.getPollAnswerVoters(channelID, messageID, answerID, {after: after, limit})
|
||||
if (curVotes.users.length === 0) { // Reached the end.
|
||||
break
|
||||
}
|
||||
voteUsers = voteUsers.concat(curVotes.users)
|
||||
if (curVotes.users.length >= limit) { // Loop again for the next page.
|
||||
// @ts-ignore - stupid
|
||||
after = curVotes.users.at(-1).id
|
||||
}
|
||||
voteUsers = voteUsers.concat(curVotes.users)
|
||||
}
|
||||
} else { // Reached the end.
|
||||
return voteUsers
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {typeof import("../../../test/data.js")["poll_close"]} closeMessage
|
||||
* @param {DiscordTypes.APIGuild} guild
|
||||
*/
|
||||
async function endPoll(closeMessage, guild){
|
||||
async function endPoll(closeMessage) {
|
||||
const pollCloseObject = closeMessage.embeds[0]
|
||||
|
||||
const pollMessageID = closeMessage.message_reference.message_id
|
||||
@@ -91,7 +79,7 @@ async function endPoll(closeMessage, guild){
|
||||
|
||||
for (const discordPollOption of discordPollOptions) {
|
||||
const optionUsers = await getAllVotesOnAnswer(closeMessage.channel_id, pollMessageID, discordPollOption) // Array of user IDs who voted for the option we're testing.
|
||||
optionUsers.map(user => {
|
||||
for (const user of optionUsers) {
|
||||
const userLocation = updatedAnswers.findIndex(answer => answer.user.id === user.id)
|
||||
const matrixOption = select("poll_option", "matrix_option", {message_id: pollMessageID, discord_option: discordPollOption}).pluck().get()
|
||||
assert(matrixOption)
|
||||
@@ -100,7 +88,7 @@ async function endPoll(closeMessage, guild){
|
||||
} else { // This user already voted for another option on the poll.
|
||||
updatedAnswers[userLocation].matrixOptionVotes.push(matrixOption)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Check for inconsistencies in what was cached in database vs final confirmed poll answers
|
||||
@@ -110,17 +98,19 @@ async function endPoll(closeMessage, guild){
|
||||
voteUsers = voteUsers.filter(item => item !== answer.user.id) // Remove any users we have updated answers for from voteUsers. The only remaining entries in this array will be users who voted, but then removed their votes before the poll ended.
|
||||
const cachedAnswers = select("poll_vote", "matrix_option", {discord_or_matrix_user_id: answer.user.id, message_id: pollMessageID}).pluck().all()
|
||||
if (!isDeepStrictEqual(new Set(cachedAnswers), new Set(answer.matrixOptionVotes))) {
|
||||
db.transaction(() => {
|
||||
db.prepare("DELETE FROM poll_vote WHERE discord_or_matrix_user_id = ? AND message_id = ?").run(answer.user.id, pollMessageID) // Delete existing stored votes.
|
||||
for (const matrixOption of answer.matrixOptionVotes) {
|
||||
db.prepare("INSERT INTO poll_vote (discord_or_matrix_user_id, message_id, matrix_option) VALUES (?, ?, ?)").run(answer.user.id, pollMessageID, matrixOption)
|
||||
}
|
||||
await vote.debounceSendVotes({user_id: answer.user.id, message_id: pollMessageID, channel_id: closeMessage.channel_id, answer_id: 0}, pollEventID) // Fake answer ID, not actually needed (but we're sorta faking the datatype to call this function).
|
||||
})()
|
||||
await vote.sendVotes(answer.user, closeMessage.channel_id, pollMessageID, pollEventID)
|
||||
}
|
||||
}))
|
||||
|
||||
await Promise.all(voteUsers.map(async user_id => { // Remove these votes.
|
||||
db.prepare("DELETE FROM poll_vote WHERE discord_or_matrix_user_id = ? AND message_id = ?").run(user_id, pollMessageID)
|
||||
await vote.debounceSendVotes({user_id: user_id, message_id: pollMessageID, channel_id: closeMessage.channel_id, answer_id: 0}, pollEventID)
|
||||
await vote.sendVotes(user_id, closeMessage.channel_id, pollMessageID, pollEventID)
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,6 @@ const {discord, sync, db, select, from} = passthrough
|
||||
const api = sync.require("../../matrix/api")
|
||||
/** @type {import("./register-user")} */
|
||||
const registerUser = sync.require("./register-user")
|
||||
/** @type {import("./create-room")} */
|
||||
const createRoom = sync.require("../actions/create-room")
|
||||
|
||||
const inFlightPollSema = new Semaphore()
|
||||
|
||||
@@ -59,12 +57,12 @@ async function debounceSendVotes(data, pollEventID) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {DiscordTypes.APIUser} user
|
||||
* @param {DiscordTypes.APIUser | string} userOrID
|
||||
* @param {string} channelID
|
||||
* @param {string} pollMessageID
|
||||
* @param {string} pollEventID
|
||||
*/
|
||||
async function sendVotes(user, channelID, pollMessageID, pollEventID) {
|
||||
async function sendVotes(userOrID, channelID, pollMessageID, pollEventID) {
|
||||
const latestRoomID = select("channel_room", "room_id", {channel_id: channelID}).pluck().get()
|
||||
const matchingRoomID = from("message_room").join("historical_channel_room", "historical_room_index").where({message_id: pollMessageID}).pluck("room_id").get()
|
||||
if (!latestRoomID || latestRoomID !== matchingRoomID) { // room upgrade mid-poll??
|
||||
@@ -72,9 +70,16 @@ async function sendVotes(user, channelID, pollMessageID, pollEventID) {
|
||||
return
|
||||
}
|
||||
|
||||
const senderMxid = await registerUser.ensureSimJoined(user, matchingRoomID)
|
||||
if (typeof userOrID === "string") { // just a string when double-checking a vote removal - good thing the unvoter is already here from having voted
|
||||
var userID = userOrID
|
||||
var senderMxid = from("sim").join("sim_member", "mxid").where({user_id: userOrID, room_id: matchingRoomID}).pluck("mxid").get()
|
||||
if (!senderMxid) return
|
||||
} else { // sent in full when double-checking adding a vote, so we can properly ensure joined
|
||||
var userID = userOrID.id
|
||||
var senderMxid = await registerUser.ensureSimJoined(userOrID, matchingRoomID)
|
||||
}
|
||||
|
||||
const answersArray = select("poll_vote", "matrix_option", {discord_or_matrix_user_id: user.id, message_id: pollMessageID}).pluck().all()
|
||||
const answersArray = select("poll_vote", "matrix_option", {discord_or_matrix_user_id: userID, message_id: pollMessageID}).pluck().all()
|
||||
const eventID = await api.sendEvent(matchingRoomID, "org.matrix.msc3381.poll.response", {
|
||||
"m.relates_to": {
|
||||
rel_type: "m.reference",
|
||||
|
||||
@@ -55,6 +55,16 @@ async function sendMessage(message, channel, guild, row) {
|
||||
}
|
||||
}
|
||||
|
||||
if (message.type === DiscordTypes.MessageType.PollResult) { // ensure all Discord-side votes were pushed to Matrix before a poll is closed
|
||||
const detailedResultsMessage = await pollEnd.endPoll(message)
|
||||
if (detailedResultsMessage) {
|
||||
const threadParent = select("channel_room", "thread_parent", {channel_id: message.channel_id}).pluck().get()
|
||||
const channelID = threadParent ? threadParent : message.channel_id
|
||||
const threadID = threadParent ? message.channel_id : undefined
|
||||
var sentResultsMessage = await channelWebhook.sendMessageWithWebhook(channelID, detailedResultsMessage, threadID)
|
||||
}
|
||||
}
|
||||
|
||||
const events = await messageToEvent.messageToEvent(message, guild, {}, {api, snow: discord.snow})
|
||||
const eventIDs = []
|
||||
if (events.length) {
|
||||
@@ -102,19 +112,14 @@ async function sendMessage(message, channel, guild, row) {
|
||||
})()
|
||||
}
|
||||
|
||||
if (message.type === DiscordTypes.MessageType.PollResult) { // We might need to send a message to Discord (if there were any Matrix-side votes).
|
||||
const detailedResultsMessage = await pollEnd.endPoll(message, guild)
|
||||
if (detailedResultsMessage) {
|
||||
const threadParent = select("channel_room", "thread_parent", {channel_id: message.channel_id}).pluck().get()
|
||||
const channelID = threadParent ? threadParent : message.channel_id
|
||||
const threadID = threadParent ? message.channel_id : undefined
|
||||
const sentResultsMessage = await channelWebhook.sendMessageWithWebhook(channelID, detailedResultsMessage, threadID)
|
||||
// part/reaction_part consistency for polls
|
||||
if (sentResultsMessage) {
|
||||
db.transaction(() => {
|
||||
db.prepare("INSERT OR IGNORE INTO message_room (message_id, historical_room_index) VALUES (?, ?)").run(sentResultsMessage.id, historicalRoomIndex)
|
||||
db.prepare("UPDATE event_message SET reaction_part = 1 WHERE event_id = ?").run(eventID)
|
||||
db.prepare("INSERT INTO event_message (event_id, event_type, event_subtype, message_id, part, reaction_part, source) VALUES (?, ?, ?, ?, ?, ?, 1)").run(eventID, eventType, event.msgtype || null, sentResultsMessage.id, 1, 0) // part = 1, reaction_part = 0
|
||||
})()
|
||||
}
|
||||
}
|
||||
|
||||
eventIDs.push(eventID)
|
||||
}
|
||||
|
||||
@@ -372,11 +372,17 @@ module.exports = {
|
||||
await createSpace.syncSpaceExpressions(data, false)
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {import("./discord-client")} client
|
||||
* @param {DiscordTypes.GatewayMessagePollVoteDispatchData} data
|
||||
*/
|
||||
async MESSAGE_POLL_VOTE_ADD(client, data) {
|
||||
if (retrigger.eventNotFoundThenRetrigger(data.message_id, module.exports.MESSAGE_POLL_VOTE_ADD, client, data)) return
|
||||
await vote.addVote(data)
|
||||
},
|
||||
|
||||
async MESSAGE_POLL_VOTE_REMOVE(client, data) {
|
||||
if (retrigger.eventNotFoundThenRetrigger(data.message_id, module.exports.MESSAGE_POLL_VOTE_REMOVE, client, data)) return
|
||||
await vote.removeVote(data)
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user