Fix NaN and poll event decryption problem
This commit is contained in:
parent
f7bb2027dd
commit
9fcde2e9f2
1 changed files with 81 additions and 79 deletions
|
|
@ -10,12 +10,13 @@ export default {
|
||||||
pollEndRelations: null,
|
pollEndRelations: null,
|
||||||
pollEndTs: null,
|
pollEndTs: null,
|
||||||
pollIsDisclosed: true,
|
pollIsDisclosed: true,
|
||||||
pollTentativeAnswer: null
|
pollTentativeAnswer: null,
|
||||||
}
|
};
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.$matrix.on("Room.timeline", this.pollMixinOnEvent);
|
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();
|
this.updateAnswers();
|
||||||
},
|
},
|
||||||
destroyed() {
|
destroyed() {
|
||||||
|
|
@ -23,11 +24,11 @@ export default {
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
if (this.pollResponseRelations) {
|
if (this.pollResponseRelations) {
|
||||||
this.pollResponseRelations.off('Relations.add', this.onAddRelation);
|
this.pollResponseRelations.off("Relations.add", this.onAddRelation);
|
||||||
this.pollResponseRelations = null;
|
this.pollResponseRelations = null;
|
||||||
}
|
}
|
||||||
if (this.pollEndRelations) {
|
if (this.pollEndRelations) {
|
||||||
this.pollEndRelations.off('Relations.add', this.onAddRelation);
|
this.pollEndRelations.off("Relations.add", this.onAddRelation);
|
||||||
this.pollEndRelations = null;
|
this.pollEndRelations = null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -36,21 +37,24 @@ export default {
|
||||||
let answers = (this.event && this.event.getContent()["org.matrix.msc3381.poll.start"]["answers"]) || [];
|
let answers = (this.event && this.event.getContent()["org.matrix.msc3381.poll.start"]["answers"]) || [];
|
||||||
var answerMap = {};
|
var answerMap = {};
|
||||||
var answerArray = [];
|
var answerArray = [];
|
||||||
answers.forEach(a => {
|
answers.forEach((a) => {
|
||||||
let text = a["org.matrix.msc1767.text"];
|
let text = a["org.matrix.msc1767.text"];
|
||||||
let answer = {id: a.id, text: text, numVotes: 0, percentage: 0}
|
let answer = { id: a.id, text: text, numVotes: 0, percentage: 0 };
|
||||||
answerMap[a.id] = answer;
|
answerMap[a.id] = answer;
|
||||||
answerArray.push(answer);
|
answerArray.push(answer);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Kind of poll
|
// 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
|
// Look for poll end
|
||||||
this.pollEndRelations = this.timelineSet.getRelationsForEvent(
|
this.pollEndRelations = this.timelineSet.getRelationsForEvent(
|
||||||
this.event.getId(),
|
this.event.getId(),
|
||||||
'm.reference',
|
"m.reference",
|
||||||
'org.matrix.msc3381.poll.end'
|
"org.matrix.msc3381.poll.end"
|
||||||
);
|
);
|
||||||
if (this.pollEndRelations) {
|
if (this.pollEndRelations) {
|
||||||
const endMessages = this.pollEndRelations.getRelations() || [];
|
const endMessages = this.pollEndRelations.getRelations() || [];
|
||||||
|
|
@ -62,8 +66,8 @@ export default {
|
||||||
// Process votes
|
// Process votes
|
||||||
this.pollResponseRelations = this.timelineSet.getRelationsForEvent(
|
this.pollResponseRelations = this.timelineSet.getRelationsForEvent(
|
||||||
this.event.getId(),
|
this.event.getId(),
|
||||||
'm.reference',
|
"m.reference",
|
||||||
'org.matrix.msc3381.poll.response'
|
"org.matrix.msc3381.poll.response"
|
||||||
);
|
);
|
||||||
var userVotes = {};
|
var userVotes = {};
|
||||||
if (this.pollResponseRelations) {
|
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
|
// Update percentages. For algorithm, see here: https://revs.runtime-revolution.com/getting-100-with-rounded-percentages-273ffa70252b
|
||||||
// (need to add up to 100%)
|
// (need to add up to 100%)
|
||||||
let a = answerArray.map(a => {
|
if (totalVotes > 0) {
|
||||||
let votes = (100 * a.numVotes) / totalVotes;
|
let a = answerArray.map((a) => {
|
||||||
return {
|
let votes = (100 * a.numVotes) / totalVotes;
|
||||||
answer: a,
|
return {
|
||||||
unrounded: votes,
|
answer: a,
|
||||||
floor: Math.floor(votes)
|
unrounded: votes,
|
||||||
}
|
floor: Math.floor(votes),
|
||||||
});
|
};
|
||||||
a.sort((item1,item2) => {
|
});
|
||||||
Math.abs(item2.floor) - Math.abs(item1.floor);
|
a.sort((item1, item2) => {
|
||||||
});
|
Math.abs(item2.floor) - Math.abs(item1.floor);
|
||||||
var diff = 100 - a.reduce((previousValue, currentValue) => {
|
});
|
||||||
return previousValue + currentValue.floor;
|
var diff =
|
||||||
}, 0);
|
100 -
|
||||||
const maxVotes = Math.max(...a.map(item => item.unrounded));
|
a.reduce((previousValue, currentValue) => {
|
||||||
a.map((answer, index) => {
|
return previousValue + currentValue.floor;
|
||||||
answer.answer.percentage = (index < diff) ? (answer.floor + 1) : answer.floor;
|
}, 0);
|
||||||
answer.answer.winner = (answer.unrounded == maxVotes);
|
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.pollAnswers = answerArray;
|
||||||
this.pollTotalVotes = totalVotes;
|
this.pollTotalVotes = totalVotes;
|
||||||
},
|
},
|
||||||
|
|
@ -129,27 +137,16 @@ export default {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
util
|
util
|
||||||
.sendPollAnswer(
|
.sendPollAnswer(this.$matrix.matrixClient, this.room.roomId, [this.pollTentativeAnswer], this.event)
|
||||||
this.$matrix.matrixClient,
|
.catch((err) => {
|
||||||
this.room.roomId,
|
console.log("Failed to send:", err);
|
||||||
[this.pollTentativeAnswer],
|
})
|
||||||
this.event
|
.finally(() => {
|
||||||
)
|
this.pollTentativeAnswer = null;
|
||||||
.catch((err) => {
|
});
|
||||||
console.log("Failed to send:", err);
|
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
this.pollTentativeAnswer = null;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
pollClose() {
|
pollClose() {
|
||||||
util
|
util.closePoll(this.$matrix.matrixClient, this.room.roomId, this.event).catch((err) => {
|
||||||
.closePoll(
|
|
||||||
this.$matrix.matrixClient,
|
|
||||||
this.room.roomId,
|
|
||||||
this.event
|
|
||||||
)
|
|
||||||
.catch((err) => {
|
|
||||||
console.log("Failed to send:", err);
|
console.log("Failed to send:", err);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
@ -160,10 +157,11 @@ export default {
|
||||||
if (event.getRoomId() !== this.room.roomId) {
|
if (event.getRoomId() !== this.room.roomId) {
|
||||||
return; // Not for this room
|
return; // Not for this room
|
||||||
}
|
}
|
||||||
if (
|
this.$matrix.matrixClient.decryptEventIfNeeded(event).then(() => {
|
||||||
event.getType().startsWith("org.matrix.msc3381.poll.")) {
|
if (event.getType().startsWith("org.matrix.msc3381.poll.")) {
|
||||||
this.updateAnswers();
|
this.updateAnswers();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
|
@ -171,10 +169,14 @@ export default {
|
||||||
return this.pollEndTs != null && this.pollEndTs !== undefined;
|
return this.pollEndTs != null && this.pollEndTs !== undefined;
|
||||||
},
|
},
|
||||||
userCanClosePoll() {
|
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() {
|
userHasVoted() {
|
||||||
return this.pollAnswers.some(a => a.hasMyVote);
|
return this.pollAnswers.some((a) => a.hasMyVote);
|
||||||
},
|
},
|
||||||
pollNumAnswers() {
|
pollNumAnswers() {
|
||||||
return this.pollTotalVotes;
|
return this.pollTotalVotes;
|
||||||
|
|
@ -182,35 +184,35 @@ export default {
|
||||||
pollIsAdmin() {
|
pollIsAdmin() {
|
||||||
// Admins can view results of not-yet-closed undisclosed polls.
|
// Admins can view results of not-yet-closed undisclosed polls.
|
||||||
const me = this.room && this.room.getMember(this.$matrix.currentUserId);
|
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;
|
return isAdmin;
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
pollResponseRelations: {
|
pollResponseRelations: {
|
||||||
handler(newValue, oldValue) {
|
handler(newValue, oldValue) {
|
||||||
if (oldValue) {
|
if (oldValue) {
|
||||||
oldValue.off('Relations.add', this.onAddRelation);
|
oldValue.off("Relations.add", this.onAddRelation);
|
||||||
}
|
}
|
||||||
if (newValue) {
|
if (newValue) {
|
||||||
newValue.on('Relations.add', this.onAddRelation);
|
newValue.on("Relations.add", this.onAddRelation);
|
||||||
}
|
}
|
||||||
this.updateAnswers();
|
this.updateAnswers();
|
||||||
},
|
},
|
||||||
immediate: true
|
immediate: true,
|
||||||
},
|
},
|
||||||
pollEndRelations: {
|
pollEndRelations: {
|
||||||
handler(newValue, oldValue) {
|
handler(newValue, oldValue) {
|
||||||
if (oldValue) {
|
if (oldValue) {
|
||||||
oldValue.off('Relations.add', this.onAddRelation);
|
oldValue.off("Relations.add", this.onAddRelation);
|
||||||
}
|
}
|
||||||
if (newValue) {
|
if (newValue) {
|
||||||
newValue.on('Relations.add', this.onAddRelation);
|
newValue.on("Relations.add", this.onAddRelation);
|
||||||
}
|
}
|
||||||
this.updateAnswers();
|
this.updateAnswers();
|
||||||
},
|
},
|
||||||
immediate: true
|
immediate: true,
|
||||||
}
|
},
|
||||||
|
},
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue