summaryrefslogtreecommitdiff
path: root/src/remote/activitypub/models/question.ts
blob: c073684349ccadde6777c3be9152240fba191c08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import config from '../../../config';
import Note, { IChoice, IPoll } from '../../../models/note';
import Resolver from '../resolver';
import { IQuestion } from '../type';
import { apLogger } from '../logger';

export async function extractPollFromQuestion(source: string | IQuestion): Promise<IPoll> {
	const question = typeof source === 'string' ? await new Resolver().resolve(source) as IQuestion : source;
	const multiple = !question.oneOf;
	const expiresAt = question.endTime ? new Date(question.endTime) : null;

	if (multiple && !question.anyOf) {
		throw 'invalid question';
	}

	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));

	return {
		choices,
		multiple,
		expiresAt
	};
}

/**
 * Update votes of Question
 * @param uri URI of AP Question object
 * @returns true if updated
 */
export async function updateQuestion(value: any) {
	const uri = typeof value == 'string' ? value : value.id;

	// URIがこのサーバーを指しているならスキップ
	if (uri.startsWith(config.url + '/')) throw 'uri points local';

	//#region このサーバーに既に登録されているか
	const note = await Note.findOne({ uri });

	if (note == null) throw 'Question is not registed';
	//#endregion

	// resolve new Question object
	const resolver = new Resolver();
	const question = await resolver.resolve(value) as IQuestion;
	apLogger.debug(`fetched question: ${JSON.stringify(question, null, 2)}`);

	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;

		if (oldCount != newCount) {
			changed = true;
			db.votes = newCount;
		}
	}

	await Note.update({
		_id: note._id
	}, {
		$set: {
			'poll.choices': dbChoices,
			updatedAt: new Date(),
		}
	});

	return changed;
}