Fixes to vote counting

This commit is contained in:
Cadence Ember
2026-01-26 20:51:30 +13:00
parent f3ae7ba792
commit 0c781f9b72
6 changed files with 63 additions and 57 deletions
+14 -9
View File
@@ -11,15 +11,13 @@ 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()
/**
* @param {import("discord-api-types/v10").GatewayMessagePollVoteAddDispatch["d"]} data
*/
async function addVote(data){
async function addVote(data) {
const pollEventID = from("event_message").join("poll_option", "message_id").pluck("event_id").where({message_id: data.message_id, event_type: "org.matrix.msc3381.poll.start"}).get() // Currently Discord doesn't allow sending a poll with anything else, but we bridge it after all other content so reaction_part: 0 is the part that will have the poll.
if (!pollEventID) return // Nothing can be done if the parent message was never bridged.
@@ -32,7 +30,7 @@ async function addVote(data){
/**
* @param {import("discord-api-types/v10").GatewayMessagePollVoteRemoveDispatch["d"]} data
*/
async function removeVote(data){
async function removeVote(data) {
const pollEventID = from("event_message").join("poll_option", "message_id").pluck("event_id").where({message_id: data.message_id, event_type: "org.matrix.msc3381.poll.start"}).get()
if (!pollEventID) return
@@ -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",
@@ -91,4 +96,4 @@ async function sendVotes(user, channelID, pollMessageID, pollEventID) {
module.exports.addVote = addVote
module.exports.removeVote = removeVote
module.exports.debounceSendVotes = debounceSendVotes
module.exports.sendVotes = sendVotes
module.exports.sendVotes = sendVotes