From 6b156d792a57edcb3abafd94cfa40da566e6ec8c Mon Sep 17 00:00:00 2001 From: Namekuji Date: Fri, 18 Aug 2023 03:58:55 -0400 Subject: [PATCH] fix poll type --- .../scylla-migration/src/setup.rs | 1 + packages/backend/src/db/scylla.ts | 4 ++-- .../backend/src/models/repositories/note.ts | 23 ++++++++++--------- .../src/remote/activitypub/models/note.ts | 4 ++-- .../src/remote/activitypub/renderer/vote.ts | 2 +- packages/backend/src/services/note/create.ts | 4 +++- .../backend/src/services/note/polls/vote.ts | 4 ++-- 7 files changed, 23 insertions(+), 19 deletions(-) diff --git a/packages/backend/native-utils/scylla-migration/src/setup.rs b/packages/backend/native-utils/scylla-migration/src/setup.rs index d1b98c908c..edd31e90ac 100644 --- a/packages/backend/native-utils/scylla-migration/src/setup.rs +++ b/packages/backend/native-utils/scylla-migration/src/setup.rs @@ -49,6 +49,7 @@ impl Initializer { ("promo_note", "FK_e263909ca4fe5d57f8d4230dd5c"), ("promo_read", "FK_a46a1a603ecee695d7db26da5f4"), ("user_note_pining", "FK_68881008f7c3588ad7ecae471cf"), + ("notification", "FK_769cb6b73a1efe22ddf733ac453"), ]; let mut conn = PgConnection::connect(&self.postgres_url).await?; diff --git a/packages/backend/src/db/scylla.ts b/packages/backend/src/db/scylla.ts index 8125c6df54..99e5266e93 100644 --- a/packages/backend/src/db/scylla.ts +++ b/packages/backend/src/db/scylla.ts @@ -114,7 +114,7 @@ export interface ScyllaNoteEditHistory { export interface ScyllaPoll { expiresAt: Date | null; multiple: boolean; - choices: Map, + choices: Record, } export interface ScyllaPollVote { @@ -128,7 +128,7 @@ export function parseScyllaPollVote(row: types.Row): ScyllaPollVote { return { noteId: row.get("noteId"), userId: row.get("userId"), - choice: row.get("choice"), + choice: new Set(row.get("choice") ?? []), createdAt: row.get("createdAt"), } } diff --git a/packages/backend/src/models/repositories/note.ts b/packages/backend/src/models/repositories/note.ts index d1ef7ffcb0..f821227aee 100644 --- a/packages/backend/src/models/repositories/note.ts +++ b/packages/backend/src/models/repositories/note.ts @@ -50,30 +50,29 @@ export async function populatePoll( const votes = await scyllaClient .execute(prepared.poll.select, [note.id], { prepare: true }) .then((result) => result.rows.map(parseScyllaPollVote)); - const options = new Map( - Array.from(sNote.poll.choices.keys()).map( - (key) => [key, 0] as [number, number], - ), + + const counts = new Map( + Object.keys(sNote.poll.choices).map((i) => [i, 0] as [string, number]), ); for (const vote of votes) { for (const choice of vote.choice) { - const count = options.get(choice); - if (count) { - options.set(choice, count + 1); + const count = counts.get(choice.toString()); + if (count !== undefined) { + counts.set(choice.toString(), count + 1); } } } const choices: { text: string; votes: number; isVoted: boolean }[] = []; - for (const [index, text] of sNote.poll.choices.entries()) { - const count = options.get(index); - if (count) { + for (const [index, text] of Object.entries(sNote.poll.choices)) { + const count = counts.get(index); + if (count !== undefined) { choices.push({ text, votes: count, isVoted: votes.some( - (v) => v.noteId === meId && v.choice.has(index), + (v) => v.userId === meId && v.choice.has(parseInt(index)), ), }); } @@ -85,6 +84,8 @@ export async function populatePoll( choices, }; } + + throw new Error("poll not found"); } const poll = await Polls.findOneByOrFail({ noteId: note.id }); diff --git a/packages/backend/src/remote/activitypub/models/note.ts b/packages/backend/src/remote/activitypub/models/note.ts index 951f52db8e..baad3117a5 100644 --- a/packages/backend/src/remote/activitypub/models/note.ts +++ b/packages/backend/src/remote/activitypub/models/note.ts @@ -339,11 +339,11 @@ export async function createNote( return null; } - const entry = Array.from(scyllaNote.poll.choices.entries()).find( + const entry = Object.entries(scyllaNote.poll.choices).find( ([_, v]) => v === note.name, ); if (entry) { - await vote(actor, scyllaNote, entry[0]); + await vote(actor, scyllaNote, parseInt(entry[0])); } return null; diff --git a/packages/backend/src/remote/activitypub/renderer/vote.ts b/packages/backend/src/remote/activitypub/renderer/vote.ts index 258034c9fc..d9d58b19ec 100644 --- a/packages/backend/src/remote/activitypub/renderer/vote.ts +++ b/packages/backend/src/remote/activitypub/renderer/vote.ts @@ -49,7 +49,7 @@ export async function renderScyllaPollVote( attributedTo: `${config.url}/users/${user.id}`, to: [pollOwner.uri], inReplyTo: note.uri, - name: note.poll.choices.get(choice), + name: note.poll.choices[choice], }, }; } diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index f528dffc30..024ebdabe2 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -846,7 +846,9 @@ async function insertNote( poll = { expiresAt, - choices: new Map(data.poll.choices.map((v, i) => [i, v] as [number, string])), + choices: Object.fromEntries( + data.poll.choices.map((v, i) => [i, v] as [number, string]), + ), multiple: data.poll.multiple, }; } diff --git a/packages/backend/src/services/note/polls/vote.ts b/packages/backend/src/services/note/polls/vote.ts index 7415d1e715..9599ef450a 100644 --- a/packages/backend/src/services/note/polls/vote.ts +++ b/packages/backend/src/services/note/polls/vote.ts @@ -25,7 +25,7 @@ export default async function ( throw new Error("poll not found"); } - if (!Array.from(scyllaNote.poll.choices.keys()).includes(choice)) { + if (!Object.keys(scyllaNote.poll.choices).includes(choice.toString())) { throw new Error("invalid choice param"); } @@ -57,7 +57,7 @@ export default async function ( await scyllaClient.execute( prepared.poll.insert, - [scyllaNote.id, user.id, newChoice, new Date()], + [scyllaNote.id, user.id, Array.from(newChoice), new Date()], { prepare: true }, ); } else {