fix poll type
This commit is contained in:
parent
83f5f89df5
commit
6b156d792a
7 changed files with 23 additions and 19 deletions
|
@ -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?;
|
||||
|
|
|
@ -114,7 +114,7 @@ export interface ScyllaNoteEditHistory {
|
|||
export interface ScyllaPoll {
|
||||
expiresAt: Date | null;
|
||||
multiple: boolean;
|
||||
choices: Map<number, string>,
|
||||
choices: Record<number, string>,
|
||||
}
|
||||
|
||||
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"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<number, number>(
|
||||
Array.from(sNote.poll.choices.keys()).map(
|
||||
(key) => [key, 0] as [number, number],
|
||||
),
|
||||
|
||||
const counts = new Map<string, number>(
|
||||
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 });
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue