summaryrefslogtreecommitdiff
path: root/src/remote/activitypub/models/question.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/remote/activitypub/models/question.ts')
-rw-r--r--src/remote/activitypub/models/question.ts41
1 files changed, 21 insertions, 20 deletions
diff --git a/src/remote/activitypub/models/question.ts b/src/remote/activitypub/models/question.ts
index c073684349..a5091a6d96 100644
--- a/src/remote/activitypub/models/question.ts
+++ b/src/remote/activitypub/models/question.ts
@@ -1,8 +1,9 @@
import config from '../../../config';
-import Note, { IChoice, IPoll } from '../../../models/note';
import Resolver from '../resolver';
import { IQuestion } from '../type';
import { apLogger } from '../logger';
+import { Notes, Polls } from '../../../models';
+import { IPoll } from '../../../models/entities/poll';
export async function extractPollFromQuestion(source: string | IQuestion): Promise<IPoll> {
const question = typeof source === 'string' ? await new Resolver().resolve(source) as IQuestion : source;
@@ -14,14 +15,14 @@ export async function extractPollFromQuestion(source: string | IQuestion): Promi
}
const choices = question[multiple ? 'anyOf' : 'oneOf']
- .map((x, i) => ({
- id: i,
- text: x.name,
- votes: x.replies && x.replies.totalItems || x._misskey_votes || 0,
- } as IChoice));
+ .map((x, i) => x.name);
+
+ const votes = question[multiple ? 'anyOf' : 'oneOf']
+ .map((x, i) => x.replies && x.replies.totalItems || x._misskey_votes || 0);
return {
choices,
+ votes,
multiple,
expiresAt
};
@@ -39,9 +40,11 @@ export async function updateQuestion(value: any) {
if (uri.startsWith(config.url + '/')) throw 'uri points local';
//#region このサーバーに既に登録されているか
- const note = await Note.findOne({ uri });
-
+ const note = await Notes.findOne({ uri });
if (note == null) throw 'Question is not registed';
+
+ const poll = await Polls.findOne({ noteId: note.id });
+ if (poll == null) throw 'Question is not registed';
//#endregion
// resolve new Question object
@@ -52,27 +55,25 @@ export async function updateQuestion(value: any) {
if (question.type !== 'Question') throw 'object is not a Question';
const apChoices = question.oneOf || question.anyOf;
- const dbChoices = note.poll.choices;
let changed = false;
- for (const db of dbChoices) {
- const oldCount = db.votes;
- const newCount = apChoices.filter(ap => ap.name === db.text)[0].replies.totalItems;
+ for (const choice of poll.choices) {
+ const oldCount = poll.votes[poll.choices.indexOf(choice)];
+ const newCount = apChoices.filter(ap => ap.name === choice)[0].replies.totalItems;
if (oldCount != newCount) {
changed = true;
- db.votes = newCount;
+ poll.votes[poll.choices.indexOf(choice)] = newCount;
}
}
- await Note.update({
- _id: note._id
- }, {
- $set: {
- 'poll.choices': dbChoices,
- updatedAt: new Date(),
- }
+ await Notes.update(note.id, {
+ updatedAt: new Date(),
+ });
+
+ await Polls.update(poll.id, {
+ votes: poll.votes
});
return changed;