From bda12504bc9eeb39e383899d618250d464cdcdfd Mon Sep 17 00:00:00 2001 From: ThatOneCalculator Date: Sun, 18 Jun 2023 18:36:26 -0700 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20when=20editing=20polls,?= =?UTF-8?q?=20keep=20votes=20for=20unmodified=20choices?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/server/api/endpoints/notes/edit.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/notes/edit.ts b/packages/backend/src/server/api/endpoints/notes/edit.ts index 28cfff602..06424907b 100644 --- a/packages/backend/src/server/api/endpoints/notes/edit.ts +++ b/packages/backend/src/server/api/endpoints/notes/edit.ts @@ -33,6 +33,7 @@ import { renderActivity } from "@/remote/activitypub/renderer/index.js"; import renderNote from "@/remote/activitypub/renderer/note.js"; import renderUpdate from "@/remote/activitypub/renderer/update.js"; import { deliverToRelays } from "@/services/relay.js"; +import { deliverQuestionUpdate } from "@/services/note/polls/update.js"; import { fetchMeta } from "@/misc/fetch-meta.js"; export const meta = { @@ -476,14 +477,19 @@ export default define(meta, paramDef, async (ps, user) => { if (poll.noteVisibility !== ps.visibility) { pollUpdate.noteVisibility = ps.visibility; } - // We can't do an unordered equal check because the order of choices - // is important and if it changes, we need to reset the votes. - if (JSON.stringify(poll.choices) !== JSON.stringify(pp.choices)) { - pollUpdate.choices = pp.choices; - pollUpdate.votes = new Array(pp.choices.length).fill(0); + // Keep votes for unmodified choices, reset votes if choice is modified or new + const oldVoteCounts = new Map(); + for (let i = 0; i < poll.choices.length; i++) { + oldVoteCounts.set(poll.choices[i], poll.votes[i]); } + const newVotes = pp.choices.map( + (choice) => oldVoteCounts.get(choice) || 0, + ); + pollUpdate.choices = pp.choices; + pollUpdate.votes = newVotes; if (notEmpty(pollUpdate)) { await Polls.update(note.id, pollUpdate); + await deliverQuestionUpdate(note.id); } publishing = true; }