diff --git a/src/components/messages/pollMixin.js b/src/components/messages/pollMixin.js index 65f1bc8..e723c65 100644 --- a/src/components/messages/pollMixin.js +++ b/src/components/messages/pollMixin.js @@ -10,12 +10,13 @@ export default { pollEndRelations: null, pollEndTs: null, pollIsDisclosed: true, - pollTentativeAnswer: null - } + pollTentativeAnswer: null, + }; }, mounted() { this.$matrix.on("Room.timeline", this.pollMixinOnEvent); - this.pollQuestion = (this.event && this.event.getContent()["org.matrix.msc3381.poll.start"]["question"]["body"]) || ""; + this.pollQuestion = + (this.event && this.event.getContent()["org.matrix.msc3381.poll.start"]["question"]["body"]) || ""; this.updateAnswers(); }, destroyed() { @@ -23,11 +24,11 @@ export default { }, beforeDestroy() { if (this.pollResponseRelations) { - this.pollResponseRelations.off('Relations.add', this.onAddRelation); + this.pollResponseRelations.off("Relations.add", this.onAddRelation); this.pollResponseRelations = null; } if (this.pollEndRelations) { - this.pollEndRelations.off('Relations.add', this.onAddRelation); + this.pollEndRelations.off("Relations.add", this.onAddRelation); this.pollEndRelations = null; } }, @@ -36,21 +37,24 @@ export default { let answers = (this.event && this.event.getContent()["org.matrix.msc3381.poll.start"]["answers"]) || []; var answerMap = {}; var answerArray = []; - answers.forEach(a => { - let text = a["org.matrix.msc1767.text"]; - let answer = {id: a.id, text: text, numVotes: 0, percentage: 0} + answers.forEach((a) => { + let text = a["org.matrix.msc1767.text"]; + let answer = { id: a.id, text: text, numVotes: 0, percentage: 0 }; answerMap[a.id] = answer; answerArray.push(answer); }); // Kind of poll - this.pollIsDisclosed = (this.event && this.event.getContent()["org.matrix.msc3381.poll.start"]["kind"] != "org.matrix.msc3381.poll.undisclosed") || false; + this.pollIsDisclosed = + (this.event && + this.event.getContent()["org.matrix.msc3381.poll.start"]["kind"] != "org.matrix.msc3381.poll.undisclosed") || + false; // Look for poll end this.pollEndRelations = this.timelineSet.getRelationsForEvent( this.event.getId(), - 'm.reference', - 'org.matrix.msc3381.poll.end' + "m.reference", + "org.matrix.msc3381.poll.end" ); if (this.pollEndRelations) { const endMessages = this.pollEndRelations.getRelations() || []; @@ -62,8 +66,8 @@ export default { // Process votes this.pollResponseRelations = this.timelineSet.getRelationsForEvent( this.event.getId(), - 'm.reference', - 'org.matrix.msc3381.poll.response' + "m.reference", + "org.matrix.msc3381.poll.response" ); var userVotes = {}; if (this.pollResponseRelations) { @@ -97,25 +101,29 @@ export default { // Update percentages. For algorithm, see here: https://revs.runtime-revolution.com/getting-100-with-rounded-percentages-273ffa70252b // (need to add up to 100%) - let a = answerArray.map(a => { - let votes = (100 * a.numVotes) / totalVotes; - return { - answer: a, - unrounded: votes, - floor: Math.floor(votes) - } - }); - a.sort((item1,item2) => { - Math.abs(item2.floor) - Math.abs(item1.floor); - }); - var diff = 100 - a.reduce((previousValue, currentValue) => { - return previousValue + currentValue.floor; - }, 0); - const maxVotes = Math.max(...a.map(item => item.unrounded)); - a.map((answer, index) => { - answer.answer.percentage = (index < diff) ? (answer.floor + 1) : answer.floor; - answer.answer.winner = (answer.unrounded == maxVotes); - }); + if (totalVotes > 0) { + let a = answerArray.map((a) => { + let votes = (100 * a.numVotes) / totalVotes; + return { + answer: a, + unrounded: votes, + floor: Math.floor(votes), + }; + }); + a.sort((item1, item2) => { + Math.abs(item2.floor) - Math.abs(item1.floor); + }); + var diff = + 100 - + a.reduce((previousValue, currentValue) => { + return previousValue + currentValue.floor; + }, 0); + const maxVotes = Math.max(...a.map((item) => item.unrounded)); + a.map((answer, index) => { + answer.answer.percentage = index < diff ? answer.floor + 1 : answer.floor; + answer.answer.winner = answer.unrounded == maxVotes; + }); + } this.pollAnswers = answerArray; this.pollTotalVotes = totalVotes; }, @@ -129,27 +137,16 @@ export default { return; } util - .sendPollAnswer( - this.$matrix.matrixClient, - this.room.roomId, - [this.pollTentativeAnswer], - this.event - ) - .catch((err) => { - console.log("Failed to send:", err); - }) - .finally(() => { - this.pollTentativeAnswer = null; - }); + .sendPollAnswer(this.$matrix.matrixClient, this.room.roomId, [this.pollTentativeAnswer], this.event) + .catch((err) => { + console.log("Failed to send:", err); + }) + .finally(() => { + this.pollTentativeAnswer = null; + }); }, pollClose() { - util - .closePoll( - this.$matrix.matrixClient, - this.room.roomId, - this.event - ) - .catch((err) => { + util.closePoll(this.$matrix.matrixClient, this.room.roomId, this.event).catch((err) => { console.log("Failed to send:", err); }); }, @@ -160,10 +157,11 @@ export default { if (event.getRoomId() !== this.room.roomId) { return; // Not for this room } - if ( - event.getType().startsWith("org.matrix.msc3381.poll.")) { - this.updateAnswers(); - } + this.$matrix.matrixClient.decryptEventIfNeeded(event).then(() => { + if (event.getType().startsWith("org.matrix.msc3381.poll.")) { + this.updateAnswers(); + } + }); }, }, computed: { @@ -171,10 +169,14 @@ export default { return this.pollEndTs != null && this.pollEndTs !== undefined; }, userCanClosePoll() { - return this.room && this.room.currentState && this.room.currentState.maySendRedactionForEvent(this.event, this.$matrix.currentUserId); + return ( + this.room && + this.room.currentState && + this.room.currentState.maySendRedactionForEvent(this.event, this.$matrix.currentUserId) + ); }, userHasVoted() { - return this.pollAnswers.some(a => a.hasMyVote); + return this.pollAnswers.some((a) => a.hasMyVote); }, pollNumAnswers() { return this.pollTotalVotes; @@ -182,35 +184,35 @@ export default { pollIsAdmin() { // Admins can view results of not-yet-closed undisclosed polls. const me = this.room && this.room.getMember(this.$matrix.currentUserId); - let isAdmin = me && this.room.currentState && this.room.currentState.hasSufficientPowerLevelFor("redact", me.powerLevel); + let isAdmin = + me && this.room.currentState && this.room.currentState.hasSufficientPowerLevelFor("redact", me.powerLevel); return isAdmin; - } + }, }, watch: { pollResponseRelations: { handler(newValue, oldValue) { if (oldValue) { - oldValue.off('Relations.add', this.onAddRelation); + oldValue.off("Relations.add", this.onAddRelation); } if (newValue) { - newValue.on('Relations.add', this.onAddRelation); + newValue.on("Relations.add", this.onAddRelation); } this.updateAnswers(); - }, - immediate: true - }, - pollEndRelations: { - handler(newValue, oldValue) { - if (oldValue) { - oldValue.off('Relations.add', this.onAddRelation); - } - if (newValue) { - newValue.on('Relations.add', this.onAddRelation); - } - this.updateAnswers(); - }, - immediate: true - } - - } -} \ No newline at end of file + }, + immediate: true, + }, + pollEndRelations: { + handler(newValue, oldValue) { + if (oldValue) { + oldValue.off("Relations.add", this.onAddRelation); + } + if (newValue) { + newValue.on("Relations.add", this.onAddRelation); + } + this.updateAnswers(); + }, + immediate: true, + }, + }, +};