Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort-by-votes #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ export interface IPoll {
msgId: string;
uid: string; // user who created the poll
question: string;
options: Array<string>;
totalVotes: number;
votes: Array<IVoter>;
data: Array<{option: string, votes: IVoter }>;
finished?: boolean;
visibility?: pollVisibility;
singleChoice?: boolean;
Expand Down
7 changes: 3 additions & 4 deletions src/lib/createLivePollMessage.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -16,7 +16,7 @@ export async function createLivePollMessage(data: any, read: IRead, modify: IMod

const state = record.polls[pollIndex];

const options = Object.entries<any>(state.poll || {})
const options = Object.entries<string>(state.poll || {})
.filter(([key]) => key !== 'question' && key !== 'ttv')
.map(([, option]) => option)
.filter((option) => option.trim() !== '');
Expand All @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions src/lib/createPollBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>, el, i) => {
if (el === maxVoteQuantity) {
ind.push(i);
Expand All @@ -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),
Expand All @@ -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;
}
Expand Down Expand Up @@ -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: [
Expand Down
3 changes: 1 addition & 2 deletions src/lib/createPollMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 5 additions & 5 deletions src/lib/finishPollMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ export async function finishPollMessage({

if (poll.wordCloud && wordCloudAPI.value) {
let wordList = [] as Array<string>;
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 = {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/nextPollMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 10 additions & 8 deletions src/lib/storeVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions src/lib/updatePollMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/votePoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down