mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2025-12-20 02:08:57 +00:00
Fix multiple choice polls to use unique voters as denominator
This commit is contained in:
@@ -2016,6 +2016,7 @@ function createMockPollWithVotes(
|
||||
}) || [];
|
||||
|
||||
const votesByOption = new Map();
|
||||
const uniqueVoterIds = new Set();
|
||||
let totalNumVotes = 0;
|
||||
|
||||
resolvedVotes.forEach(vote => {
|
||||
@@ -2024,6 +2025,7 @@ function createMockPollWithVotes(
|
||||
votesByOption.set(index, []);
|
||||
}
|
||||
votesByOption.get(index).push(vote);
|
||||
uniqueVoterIds.add(vote.from.id);
|
||||
totalNumVotes += 1;
|
||||
});
|
||||
});
|
||||
@@ -2034,6 +2036,7 @@ function createMockPollWithVotes(
|
||||
allowMultiple,
|
||||
votesByOption,
|
||||
totalNumVotes,
|
||||
uniqueVoters: uniqueVoterIds.size,
|
||||
terminatedAt,
|
||||
votes: votes?.map(v => ({
|
||||
fromConversationId: v.fromId,
|
||||
@@ -2053,6 +2056,7 @@ Poll.args = {
|
||||
allowMultiple: false,
|
||||
votesByOption: new Map(),
|
||||
totalNumVotes: 0,
|
||||
uniqueVoters: 0,
|
||||
},
|
||||
status: 'sent',
|
||||
};
|
||||
@@ -2066,6 +2070,7 @@ PollMultipleChoice.args = {
|
||||
allowMultiple: true,
|
||||
votesByOption: new Map(),
|
||||
totalNumVotes: 0,
|
||||
uniqueVoters: 0,
|
||||
},
|
||||
status: 'sent',
|
||||
};
|
||||
|
||||
@@ -100,7 +100,7 @@ export function PollMessageContents({
|
||||
const [showVotesModal, setShowVotesModal] = useState(false);
|
||||
const isIncoming = direction === 'incoming';
|
||||
|
||||
const totalVotes = poll.totalNumVotes;
|
||||
const { totalNumVotes: totalVotes, uniqueVoters } = poll;
|
||||
|
||||
let pollStatusText: string;
|
||||
if (poll.terminatedAt) {
|
||||
@@ -167,7 +167,7 @@ export function PollMessageContents({
|
||||
const pollVoteEntries = poll.votesByOption.get(index);
|
||||
const optionVotes = pollVoteEntries?.length ?? 0;
|
||||
const percentage =
|
||||
totalVotes > 0 ? (optionVotes / totalVotes) * 100 : 0;
|
||||
uniqueVoters > 0 ? (optionVotes / uniqueVoters) * 100 : 0;
|
||||
|
||||
const weVotedForThis = (pollVoteEntries ?? []).some(v => v.isMe);
|
||||
|
||||
|
||||
@@ -505,6 +505,7 @@ export type PollVoteWithUserType = {
|
||||
export type PollWithResolvedVotersType = PollMessageAttribute & {
|
||||
votesByOption: Map<number, ReadonlyArray<PollVoteWithUserType>>;
|
||||
totalNumVotes: number;
|
||||
uniqueVoters: number;
|
||||
};
|
||||
|
||||
const getPollForMessage = (
|
||||
@@ -527,6 +528,7 @@ const getPollForMessage = (
|
||||
...poll,
|
||||
votesByOption: new Map(),
|
||||
totalNumVotes: 0,
|
||||
uniqueVoters: 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -573,6 +575,7 @@ const getPollForMessage = (
|
||||
});
|
||||
|
||||
const votesByOption = new Map<number, Array<PollVoteWithUserType>>();
|
||||
const uniqueVoterIds = new Set();
|
||||
let totalNumVotes = 0;
|
||||
|
||||
for (const vote of resolvedVotes) {
|
||||
@@ -583,6 +586,7 @@ const getPollForMessage = (
|
||||
const votes = votesByOption.get(optionIndex);
|
||||
strictAssert(!!votes, 'votes should exist');
|
||||
votes.push(vote);
|
||||
uniqueVoterIds.add(vote.from.id);
|
||||
totalNumVotes += 1;
|
||||
}
|
||||
}
|
||||
@@ -591,6 +595,7 @@ const getPollForMessage = (
|
||||
...poll,
|
||||
votesByOption,
|
||||
totalNumVotes,
|
||||
uniqueVoters: uniqueVoterIds.size,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user