Fix multiple choice polls to use unique voters as denominator

This commit is contained in:
ayumi-signal
2025-11-04 14:55:42 -08:00
committed by GitHub
parent 7bf1944ad3
commit 8b779b9d54
3 changed files with 12 additions and 2 deletions

View File

@@ -2016,6 +2016,7 @@ function createMockPollWithVotes(
}) || []; }) || [];
const votesByOption = new Map(); const votesByOption = new Map();
const uniqueVoterIds = new Set();
let totalNumVotes = 0; let totalNumVotes = 0;
resolvedVotes.forEach(vote => { resolvedVotes.forEach(vote => {
@@ -2024,6 +2025,7 @@ function createMockPollWithVotes(
votesByOption.set(index, []); votesByOption.set(index, []);
} }
votesByOption.get(index).push(vote); votesByOption.get(index).push(vote);
uniqueVoterIds.add(vote.from.id);
totalNumVotes += 1; totalNumVotes += 1;
}); });
}); });
@@ -2034,6 +2036,7 @@ function createMockPollWithVotes(
allowMultiple, allowMultiple,
votesByOption, votesByOption,
totalNumVotes, totalNumVotes,
uniqueVoters: uniqueVoterIds.size,
terminatedAt, terminatedAt,
votes: votes?.map(v => ({ votes: votes?.map(v => ({
fromConversationId: v.fromId, fromConversationId: v.fromId,
@@ -2053,6 +2056,7 @@ Poll.args = {
allowMultiple: false, allowMultiple: false,
votesByOption: new Map(), votesByOption: new Map(),
totalNumVotes: 0, totalNumVotes: 0,
uniqueVoters: 0,
}, },
status: 'sent', status: 'sent',
}; };
@@ -2066,6 +2070,7 @@ PollMultipleChoice.args = {
allowMultiple: true, allowMultiple: true,
votesByOption: new Map(), votesByOption: new Map(),
totalNumVotes: 0, totalNumVotes: 0,
uniqueVoters: 0,
}, },
status: 'sent', status: 'sent',
}; };

View File

@@ -100,7 +100,7 @@ export function PollMessageContents({
const [showVotesModal, setShowVotesModal] = useState(false); const [showVotesModal, setShowVotesModal] = useState(false);
const isIncoming = direction === 'incoming'; const isIncoming = direction === 'incoming';
const totalVotes = poll.totalNumVotes; const { totalNumVotes: totalVotes, uniqueVoters } = poll;
let pollStatusText: string; let pollStatusText: string;
if (poll.terminatedAt) { if (poll.terminatedAt) {
@@ -167,7 +167,7 @@ export function PollMessageContents({
const pollVoteEntries = poll.votesByOption.get(index); const pollVoteEntries = poll.votesByOption.get(index);
const optionVotes = pollVoteEntries?.length ?? 0; const optionVotes = pollVoteEntries?.length ?? 0;
const percentage = const percentage =
totalVotes > 0 ? (optionVotes / totalVotes) * 100 : 0; uniqueVoters > 0 ? (optionVotes / uniqueVoters) * 100 : 0;
const weVotedForThis = (pollVoteEntries ?? []).some(v => v.isMe); const weVotedForThis = (pollVoteEntries ?? []).some(v => v.isMe);

View File

@@ -505,6 +505,7 @@ export type PollVoteWithUserType = {
export type PollWithResolvedVotersType = PollMessageAttribute & { export type PollWithResolvedVotersType = PollMessageAttribute & {
votesByOption: Map<number, ReadonlyArray<PollVoteWithUserType>>; votesByOption: Map<number, ReadonlyArray<PollVoteWithUserType>>;
totalNumVotes: number; totalNumVotes: number;
uniqueVoters: number;
}; };
const getPollForMessage = ( const getPollForMessage = (
@@ -527,6 +528,7 @@ const getPollForMessage = (
...poll, ...poll,
votesByOption: new Map(), votesByOption: new Map(),
totalNumVotes: 0, totalNumVotes: 0,
uniqueVoters: 0,
}; };
} }
@@ -573,6 +575,7 @@ const getPollForMessage = (
}); });
const votesByOption = new Map<number, Array<PollVoteWithUserType>>(); const votesByOption = new Map<number, Array<PollVoteWithUserType>>();
const uniqueVoterIds = new Set();
let totalNumVotes = 0; let totalNumVotes = 0;
for (const vote of resolvedVotes) { for (const vote of resolvedVotes) {
@@ -583,6 +586,7 @@ const getPollForMessage = (
const votes = votesByOption.get(optionIndex); const votes = votesByOption.get(optionIndex);
strictAssert(!!votes, 'votes should exist'); strictAssert(!!votes, 'votes should exist');
votes.push(vote); votes.push(vote);
uniqueVoterIds.add(vote.from.id);
totalNumVotes += 1; totalNumVotes += 1;
} }
} }
@@ -591,6 +595,7 @@ const getPollForMessage = (
...poll, ...poll,
votesByOption, votesByOption,
totalNumVotes, totalNumVotes,
uniqueVoters: uniqueVoterIds.size,
}; };
}; };