2019-03-07 12:19:32 +00:00
|
|
|
import config from '../../../config';
|
2019-01-21 04:27:19 +00:00
|
|
|
import Resolver from '../resolver';
|
2019-06-28 09:54:10 +00:00
|
|
|
import { IObject, IQuestion, isQuestion, } from '../type';
|
2019-03-07 12:19:32 +00:00
|
|
|
import { apLogger } from '../logger';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { Notes, Polls } from '../../../models';
|
|
|
|
import { IPoll } from '../../../models/entities/poll';
|
2019-03-06 13:55:47 +00:00
|
|
|
|
2019-06-28 09:54:10 +00:00
|
|
|
export async function extractPollFromQuestion(source: string | IObject, resolver?: Resolver): Promise<IPoll> {
|
|
|
|
if (resolver == null) resolver = new Resolver();
|
|
|
|
|
|
|
|
const question = await resolver.resolve(source);
|
|
|
|
|
|
|
|
if (!isQuestion(question)) {
|
|
|
|
throw new Error('invalid type');
|
|
|
|
}
|
|
|
|
|
2019-03-06 13:55:47 +00:00
|
|
|
const multiple = !question.oneOf;
|
|
|
|
const expiresAt = question.endTime ? new Date(question.endTime) : null;
|
|
|
|
|
|
|
|
if (multiple && !question.anyOf) {
|
2019-04-13 19:17:24 +00:00
|
|
|
throw new Error('invalid question');
|
2019-03-06 13:55:47 +00:00
|
|
|
}
|
2019-01-21 04:27:19 +00:00
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
const choices = question[multiple ? 'anyOf' : 'oneOf']!
|
|
|
|
.map((x, i) => x.name!);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
const votes = question[multiple ? 'anyOf' : 'oneOf']!
|
2019-04-07 12:50:36 +00:00
|
|
|
.map((x, i) => x.replies && x.replies.totalItems || x._misskey_votes || 0);
|
2019-01-21 04:27:19 +00:00
|
|
|
|
|
|
|
return {
|
2019-03-06 13:55:47 +00:00
|
|
|
choices,
|
2019-04-07 12:50:36 +00:00
|
|
|
votes,
|
2019-03-06 13:55:47 +00:00
|
|
|
multiple,
|
|
|
|
expiresAt
|
2019-01-21 04:27:19 +00:00
|
|
|
};
|
|
|
|
}
|
2019-03-07 12:19:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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がこのサーバーを指しているならスキップ
|
2019-04-13 19:17:24 +00:00
|
|
|
if (uri.startsWith(config.url + '/')) throw new Error('uri points local');
|
2019-03-07 12:19:32 +00:00
|
|
|
|
|
|
|
//#region このサーバーに既に登録されているか
|
2019-04-07 12:50:36 +00:00
|
|
|
const note = await Notes.findOne({ uri });
|
2019-04-13 19:17:24 +00:00
|
|
|
if (note == null) throw new Error('Question is not registed');
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
const poll = await Polls.findOne({ noteId: note.id });
|
2019-04-13 19:17:24 +00:00
|
|
|
if (poll == null) throw new Error('Question is not registed');
|
2019-03-07 12:19:32 +00:00
|
|
|
//#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)}`);
|
|
|
|
|
2019-04-13 19:17:24 +00:00
|
|
|
if (question.type !== 'Question') throw new Error('object is not a Question');
|
2019-03-07 12:19:32 +00:00
|
|
|
|
|
|
|
const apChoices = question.oneOf || question.anyOf;
|
|
|
|
|
|
|
|
let changed = false;
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
for (const choice of poll.choices) {
|
|
|
|
const oldCount = poll.votes[poll.choices.indexOf(choice)];
|
2019-04-12 16:43:22 +00:00
|
|
|
const newCount = apChoices!.filter(ap => ap.name === choice)[0].replies!.totalItems;
|
2019-03-07 12:19:32 +00:00
|
|
|
|
|
|
|
if (oldCount != newCount) {
|
|
|
|
changed = true;
|
2019-04-07 12:50:36 +00:00
|
|
|
poll.votes[poll.choices.indexOf(choice)] = newCount;
|
2019-03-07 12:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:07:36 +00:00
|
|
|
await Polls.update({ noteId: note.id }, {
|
2019-04-07 12:50:36 +00:00
|
|
|
votes: poll.votes
|
2019-03-07 12:19:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|