fix question update
This commit is contained in:
parent
38fe9b85bd
commit
c7da13d965
1 changed files with 76 additions and 20 deletions
|
@ -4,7 +4,17 @@ import type { IObject, IQuestion } from "../type.js";
|
||||||
import { getApId, isQuestion } from "../type.js";
|
import { getApId, isQuestion } from "../type.js";
|
||||||
import { apLogger } from "../logger.js";
|
import { apLogger } from "../logger.js";
|
||||||
import { Notes, Polls } from "@/models/index.js";
|
import { Notes, Polls } from "@/models/index.js";
|
||||||
import type { IPoll } from "@/models/entities/poll.js";
|
import type { IPoll, Poll } from "@/models/entities/poll.js";
|
||||||
|
import {
|
||||||
|
parseScyllaNote,
|
||||||
|
prepared,
|
||||||
|
scyllaClient,
|
||||||
|
ScyllaPoll,
|
||||||
|
type ScyllaNote,
|
||||||
|
parseScyllaPollVote,
|
||||||
|
} from "@/db/scylla.js";
|
||||||
|
import type { Note } from "@/models/entities/note.js";
|
||||||
|
import { genId } from "@/misc/gen-id.js";
|
||||||
|
|
||||||
export async function extractPollFromQuestion(
|
export async function extractPollFromQuestion(
|
||||||
source: string | IObject,
|
source: string | IObject,
|
||||||
|
@ -60,11 +70,31 @@ export async function updateQuestion(
|
||||||
if (uri.startsWith(`${config.url}/`)) throw new Error("uri points local");
|
if (uri.startsWith(`${config.url}/`)) throw new Error("uri points local");
|
||||||
|
|
||||||
//#region Already registered with this server?
|
//#region Already registered with this server?
|
||||||
const note = await Notes.findOneBy({ uri });
|
let note: Note | ScyllaNote | null = null;
|
||||||
if (note == null) throw new Error("Question is not registed");
|
if (scyllaClient) {
|
||||||
|
const result = await scyllaClient.execute(
|
||||||
|
prepared.note.select.byUri,
|
||||||
|
[uri],
|
||||||
|
{ prepare: true },
|
||||||
|
);
|
||||||
|
if (result.rowLength > 0) {
|
||||||
|
note = parseScyllaNote(result.first());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
note = await Notes.findOneBy({ uri });
|
||||||
|
}
|
||||||
|
if (!note) throw new Error("Question is not registed");
|
||||||
|
|
||||||
const poll = await Polls.findOneBy({ noteId: note.id });
|
let poll: Poll | ScyllaPoll | null = null;
|
||||||
if (poll == null) throw new Error("Question is not registed");
|
if (scyllaClient) {
|
||||||
|
const scyllaPoll = (note as ScyllaNote).poll;
|
||||||
|
if (note.hasPoll && scyllaPoll) {
|
||||||
|
poll = scyllaPoll;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
poll = await Polls.findOneBy({ noteId: note.id });
|
||||||
|
}
|
||||||
|
if (!poll) throw new Error("Question is not registed");
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
// resolve new Question object
|
// resolve new Question object
|
||||||
|
@ -79,23 +109,49 @@ export async function updateQuestion(
|
||||||
|
|
||||||
let changed = false;
|
let changed = false;
|
||||||
|
|
||||||
for (const choice of poll.choices) {
|
if (scyllaClient) {
|
||||||
const oldCount = poll.votes[poll.choices.indexOf(choice)];
|
const votes = await scyllaClient
|
||||||
const newCount = apChoices.filter((ap) => ap.name === choice)[0].replies
|
.execute(prepared.poll.select, [note.id], { prepare: true })
|
||||||
?.totalItems;
|
.then((result) => result.rows.map(parseScyllaPollVote));
|
||||||
|
const scyllaPoll = poll as ScyllaPoll;
|
||||||
if (newCount !== undefined && oldCount !== newCount) {
|
for (const [i, name] of Object.entries(scyllaPoll.choices)) {
|
||||||
changed = true;
|
const index = parseInt(i);
|
||||||
poll.votes[poll.choices.indexOf(choice)] = newCount;
|
const oldCount = votes.filter((vote) => vote.choice.has(index)).length;
|
||||||
|
const newCount = apChoices.filter((ap) => ap.name === name)[0].replies
|
||||||
|
?.totalItems;
|
||||||
|
// Here, we assume that no implementations allow users to cancel votes.
|
||||||
|
if (newCount !== undefined && oldCount < newCount) {
|
||||||
|
changed = true;
|
||||||
|
for (let i = 0; i < newCount - oldCount; i++) {
|
||||||
|
const now = new Date();
|
||||||
|
await scyllaClient.execute(
|
||||||
|
prepared.poll.insert,
|
||||||
|
[note.id, `anonymous-${genId(now)}`, "anonymous", [index], now],
|
||||||
|
{ prepare: true },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
const dbPoll = poll as Poll;
|
||||||
|
for (const choice of dbPoll.choices) {
|
||||||
|
const oldCount = dbPoll.votes[dbPoll.choices.indexOf(choice)];
|
||||||
|
const newCount = apChoices.filter((ap) => ap.name === choice)[0].replies
|
||||||
|
?.totalItems;
|
||||||
|
|
||||||
await Polls.update(
|
if (newCount !== undefined && oldCount !== newCount) {
|
||||||
{ noteId: note.id },
|
changed = true;
|
||||||
{
|
dbPoll.votes[dbPoll.choices.indexOf(choice)] = newCount;
|
||||||
votes: poll.votes,
|
}
|
||||||
},
|
}
|
||||||
);
|
|
||||||
|
await Polls.update(
|
||||||
|
{ noteId: note.id },
|
||||||
|
{
|
||||||
|
votes: dbPoll.votes,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue