fix poll type

This commit is contained in:
Namekuji 2023-08-18 03:58:55 -04:00
parent 83f5f89df5
commit 6b156d792a
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
7 changed files with 23 additions and 19 deletions

View file

@ -49,6 +49,7 @@ impl Initializer {
("promo_note", "FK_e263909ca4fe5d57f8d4230dd5c"), ("promo_note", "FK_e263909ca4fe5d57f8d4230dd5c"),
("promo_read", "FK_a46a1a603ecee695d7db26da5f4"), ("promo_read", "FK_a46a1a603ecee695d7db26da5f4"),
("user_note_pining", "FK_68881008f7c3588ad7ecae471cf"), ("user_note_pining", "FK_68881008f7c3588ad7ecae471cf"),
("notification", "FK_769cb6b73a1efe22ddf733ac453"),
]; ];
let mut conn = PgConnection::connect(&self.postgres_url).await?; let mut conn = PgConnection::connect(&self.postgres_url).await?;

View file

@ -114,7 +114,7 @@ export interface ScyllaNoteEditHistory {
export interface ScyllaPoll { export interface ScyllaPoll {
expiresAt: Date | null; expiresAt: Date | null;
multiple: boolean; multiple: boolean;
choices: Map<number, string>, choices: Record<number, string>,
} }
export interface ScyllaPollVote { export interface ScyllaPollVote {
@ -128,7 +128,7 @@ export function parseScyllaPollVote(row: types.Row): ScyllaPollVote {
return { return {
noteId: row.get("noteId"), noteId: row.get("noteId"),
userId: row.get("userId"), userId: row.get("userId"),
choice: row.get("choice"), choice: new Set(row.get("choice") ?? []),
createdAt: row.get("createdAt"), createdAt: row.get("createdAt"),
} }
} }

View file

@ -50,30 +50,29 @@ export async function populatePoll(
const votes = await scyllaClient const votes = await scyllaClient
.execute(prepared.poll.select, [note.id], { prepare: true }) .execute(prepared.poll.select, [note.id], { prepare: true })
.then((result) => result.rows.map(parseScyllaPollVote)); .then((result) => result.rows.map(parseScyllaPollVote));
const options = new Map<number, number>(
Array.from(sNote.poll.choices.keys()).map( const counts = new Map<string, number>(
(key) => [key, 0] as [number, number], Object.keys(sNote.poll.choices).map((i) => [i, 0] as [string, number]),
),
); );
for (const vote of votes) { for (const vote of votes) {
for (const choice of vote.choice) { for (const choice of vote.choice) {
const count = options.get(choice); const count = counts.get(choice.toString());
if (count) { if (count !== undefined) {
options.set(choice, count + 1); counts.set(choice.toString(), count + 1);
} }
} }
} }
const choices: { text: string; votes: number; isVoted: boolean }[] = []; const choices: { text: string; votes: number; isVoted: boolean }[] = [];
for (const [index, text] of sNote.poll.choices.entries()) { for (const [index, text] of Object.entries(sNote.poll.choices)) {
const count = options.get(index); const count = counts.get(index);
if (count) { if (count !== undefined) {
choices.push({ choices.push({
text, text,
votes: count, votes: count,
isVoted: votes.some( 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, choices,
}; };
} }
throw new Error("poll not found");
} }
const poll = await Polls.findOneByOrFail({ noteId: note.id }); const poll = await Polls.findOneByOrFail({ noteId: note.id });

View file

@ -339,11 +339,11 @@ export async function createNote(
return null; return null;
} }
const entry = Array.from(scyllaNote.poll.choices.entries()).find( const entry = Object.entries(scyllaNote.poll.choices).find(
([_, v]) => v === note.name, ([_, v]) => v === note.name,
); );
if (entry) { if (entry) {
await vote(actor, scyllaNote, entry[0]); await vote(actor, scyllaNote, parseInt(entry[0]));
} }
return null; return null;

View file

@ -49,7 +49,7 @@ export async function renderScyllaPollVote(
attributedTo: `${config.url}/users/${user.id}`, attributedTo: `${config.url}/users/${user.id}`,
to: [pollOwner.uri], to: [pollOwner.uri],
inReplyTo: note.uri, inReplyTo: note.uri,
name: note.poll.choices.get(choice), name: note.poll.choices[choice],
}, },
}; };
} }

View file

@ -846,7 +846,9 @@ async function insertNote(
poll = { poll = {
expiresAt, 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, multiple: data.poll.multiple,
}; };
} }

View file

@ -25,7 +25,7 @@ export default async function (
throw new Error("poll not found"); 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"); throw new Error("invalid choice param");
} }
@ -57,7 +57,7 @@ export default async function (
await scyllaClient.execute( await scyllaClient.execute(
prepared.poll.insert, prepared.poll.insert,
[scyllaNote.id, user.id, newChoice, new Date()], [scyllaNote.id, user.id, Array.from(newChoice), new Date()],
{ prepare: true }, { prepare: true },
); );
} else { } else {