diff --git a/src/definition.ts b/src/definition.ts index 7ece42d..e517141 100644 --- a/src/definition.ts +++ b/src/definition.ts @@ -18,9 +18,8 @@ export interface IPoll { msgId: string; uid: string; // user who created the poll question: string; - options: Array; totalVotes: number; - votes: Array; + data: Array<{option: string, votes: IVoter }>; finished?: boolean; visibility?: pollVisibility; singleChoice?: boolean; diff --git a/src/lib/createLivePollMessage.ts b/src/lib/createLivePollMessage.ts index b470438..e3f0381 100644 --- a/src/lib/createLivePollMessage.ts +++ b/src/lib/createLivePollMessage.ts @@ -1,6 +1,6 @@ import { IModify, IPersistence, IRead } from '@rocket.chat/apps-engine/definition/accessors'; import { RocketChatAssociationModel, RocketChatAssociationRecord } from '@rocket.chat/apps-engine/definition/metadata'; -import { IPoll } from '../definition'; +import { IPoll, IVoter } from '../definition'; import { createPollBlocks } from './createPollBlocks'; export async function createLivePollMessage(data: any, read: IRead, modify: IModify, persistence: IPersistence, uid: string, pollIndex: number) { @@ -16,7 +16,7 @@ export async function createLivePollMessage(data: any, read: IRead, modify: IMod const state = record.polls[pollIndex]; - const options = Object.entries(state.poll || {}) + const options = Object.entries(state.poll || {}) .filter(([key]) => key !== 'question' && key !== 'ttv') .map(([, option]) => option) .filter((option) => option.trim() !== ''); @@ -43,9 +43,8 @@ export async function createLivePollMessage(data: any, read: IRead, modify: IMod question: state.poll.question, uid, msgId: '', - options, totalVotes: 0, - votes: options.map(() => ({ quantity: 0, voters: [] })), + data: options.map((i) => ({ option: i, votes: { quantity: 0, voters: [] } })), visibility, singleChoice: mode === 'single', liveId: id, diff --git a/src/lib/createPollBlocks.ts b/src/lib/createPollBlocks.ts index 165bc06..68f1769 100644 --- a/src/lib/createPollBlocks.ts +++ b/src/lib/createPollBlocks.ts @@ -49,10 +49,10 @@ export function createPollBlocks(block: BlockBuilder, question: string, options: block.addDividerBlock(); - const maxVoteQuantity = Math.max(...poll.votes.map((vote) => vote.quantity)); + const maxVoteQuantity = Math.max(...poll.data.map(({votes}) => votes.quantity)); // Forms array of option indices with maximum votes (more than 1 option can be max-voted) - const maxVoteIndices = poll.votes - .map((vote) => vote.quantity) + const maxVoteIndices = poll.data + .map(({votes}) => votes.quantity) .reduce((ind: Array, el, i) => { if (el === maxVoteQuantity) { ind.push(i); @@ -72,11 +72,11 @@ export function createPollBlocks(block: BlockBuilder, question: string, options: }, }); - if (!poll.votes[index]) { + if (!poll.data[index]) { return; } - const graph = buildVoteGraph(poll.votes[index], poll.totalVotes, maxVoteIndices.includes(index)); + const graph = buildVoteGraph(poll.data[index].votes, poll.totalVotes, maxVoteIndices.includes(index)); block.addContextBlock({ elements: [ block.newMarkdownTextObject(graph), @@ -86,7 +86,7 @@ export function createPollBlocks(block: BlockBuilder, question: string, options: if (poll.visibility === pollVisibility.confidential) { return; } - const voters = buildVoters(poll.votes[index], showNames, anonymousOptions.includes(poll.options[index])); + const voters = buildVoters(poll.data[index].votes, showNames, anonymousOptions.includes(poll.data[index].option)); if (!voters) { return; } @@ -129,8 +129,8 @@ export function createPollBlocks(block: BlockBuilder, question: string, options: // Word cloud when Internet access disabled if (poll.finished && poll.wordCloud && !wordCloud) { - const responseSummary = poll.votes.map((vote, index) => { - return `${poll.options[index]}(${vote.quantity})`; + const responseSummary = poll.data.map(({votes, option}) => { + return `${option}(${votes.quantity})`; }).join(' '); block.addContextBlock({ elements: [ diff --git a/src/lib/createPollMessage.ts b/src/lib/createPollMessage.ts index 7391883..4e5f115 100644 --- a/src/lib/createPollMessage.ts +++ b/src/lib/createPollMessage.ts @@ -110,9 +110,8 @@ export async function createPollMessage(data: IUIKitViewSubmitIncomingInteractio question: state.poll.question, uid, msgId: '', - options, totalVotes: 0, - votes: options.map(() => ({ quantity: 0, voters: [] })), + data: options.map((i) => ({ option: i, votes: {quantity: 0, voters: [] } })), visibility, singleChoice: mode !== 'multiple', wordCloud: wordCloud === 'enabled', diff --git a/src/lib/finishPollMessage.ts b/src/lib/finishPollMessage.ts index 164a4e2..687474b 100644 --- a/src/lib/finishPollMessage.ts +++ b/src/lib/finishPollMessage.ts @@ -85,9 +85,9 @@ export async function finishPollMessage({ if (poll.wordCloud && wordCloudAPI.value) { let wordList = [] as Array; - poll.options.map((option, index) => { + poll.data.map((i) => i).map((option, index) => { wordList = wordList.concat( - Array(poll.votes[index].quantity).fill(option), + Array(poll.data[index].votes.quantity).fill(option), ); }); const attachment = { @@ -98,7 +98,7 @@ export async function finishPollMessage({ createPollBlocks( block, poll.question, - poll.options, + poll.data.map((i) => i.option), poll, showNames.value, timeZone.value, @@ -123,7 +123,7 @@ export async function finishPollMessage({ createPollBlocks( block, poll.question, - poll.options, + poll.data.map((i) => i.option), poll, showNames.value, timeZone.value, @@ -139,7 +139,7 @@ export async function finishPollMessage({ createPollBlocks( block, poll.question, - poll.options, + poll.data.map((i) => i.option), poll, showNames.value, timeZone.value, diff --git a/src/lib/nextPollMessage.ts b/src/lib/nextPollMessage.ts index ed61bac..ffa25fa 100644 --- a/src/lib/nextPollMessage.ts +++ b/src/lib/nextPollMessage.ts @@ -59,7 +59,7 @@ export async function nextPollMessage({ data, read, persistence, modify, logger const wordCloudAPI = await read.getEnvironmentReader().getSettings().getById('wordcloud-api'); const timeZone = await read.getEnvironmentReader().getSettings().getById('timezone'); - createPollBlocks(block, poll.question, poll.options, poll, showNames.value, timeZone.value, poll.anonymousOptions, wordCloudAPI.value); + createPollBlocks(block, poll.question, poll.data.map((i) => i.option), poll, showNames.value, timeZone.value, poll.anonymousOptions, wordCloudAPI.value); message.setBlocks(block); diff --git a/src/lib/storeVote.ts b/src/lib/storeVote.ts index d48f78e..2f6b37f 100644 --- a/src/lib/storeVote.ts +++ b/src/lib/storeVote.ts @@ -12,24 +12,26 @@ export async function storeVote(poll: IPoll, voteIndex: number, { id, username, const findVoter = ({ id: voterId }) => voterId === id; const filterVoter = ({ id: voterId }) => voterId !== id; - const previousVote = poll.votes.findIndex(({ voters }) => voters.some(findVoter)); + const previousVote = poll.data.map((i) => i.votes).findIndex(({ voters }) => voters.some(findVoter)); - const hasVoted = poll.votes[voteIndex].voters.findIndex(findVoter); + const hasVoted = poll.data[voteIndex].votes.voters.findIndex(findVoter); if (hasVoted !== -1) { poll.totalVotes--; - poll.votes[voteIndex].quantity--; - poll.votes[voteIndex].voters.splice(hasVoted, 1); + poll.data[voteIndex].votes.quantity--; + poll.data[voteIndex].votes.voters.splice(hasVoted, 1); } else { poll.totalVotes++; - poll.votes[voteIndex].quantity++; - poll.votes[voteIndex].voters.push(voter); + poll.data[voteIndex].votes.quantity++; + poll.data[voteIndex].votes.voters.push(voter); } + poll.data = poll.data.sort((i, j) => j.votes.quantity - i.votes.quantity); + if (poll.singleChoice && hasVoted === -1 && previousVote !== -1) { poll.totalVotes--; - poll.votes[previousVote].quantity--; - poll.votes[previousVote].voters = poll.votes[previousVote].voters.filter(filterVoter); + poll.data[previousVote].votes.quantity--; + poll.data[previousVote].votes.voters = poll.data[previousVote].votes.voters.filter(filterVoter); } return persis.updateByAssociation(association, poll); diff --git a/src/lib/updatePollMessage.ts b/src/lib/updatePollMessage.ts index fb50db9..8bc40b3 100644 --- a/src/lib/updatePollMessage.ts +++ b/src/lib/updatePollMessage.ts @@ -47,8 +47,7 @@ export async function updatePollMessage({ try { - poll.options.push(state.addOption.option); - poll.votes.push({ quantity: 0, voters: [] }); + poll.data.push({option: state.addOption.option, votes: { quantity: 0, voters: [] } }) const message = await modify .getUpdater() @@ -68,7 +67,7 @@ export async function updatePollMessage({ createPollBlocks( block, poll.question, - poll.options, + poll.data.map((i) => i.votes), poll, showNames.value, timeZone.value, diff --git a/src/lib/votePoll.ts b/src/lib/votePoll.ts index 2464c1b..bdd13ac 100644 --- a/src/lib/votePoll.ts +++ b/src/lib/votePoll.ts @@ -39,7 +39,7 @@ export async function votePoll({ data, read, persistence, modify, pollIndex, tot const wordCloudAPI = await read.getEnvironmentReader().getSettings().getById('wordcloud-api'); const timeZone = await read.getEnvironmentReader().getSettings().getById('timezone'); - createPollBlocks(block, poll.question, poll.options, poll, showNames.value, timeZone.value, poll.anonymousOptions, wordCloudAPI.value); + createPollBlocks(block, poll.question, poll.data.map((i) => i.option), poll, showNames.value, timeZone.value, poll.anonymousOptions, wordCloudAPI.value); message.setBlocks(block);