2019-03-07 13:19:32 +01:00
|
|
|
import config from '../../../config';
|
|
|
|
import Note, { IChoice, IPoll } from '../../../models/note';
|
2019-01-21 05:27:19 +01:00
|
|
|
import Resolver from '../resolver';
|
2019-03-07 13:19:32 +01:00
|
|
|
import { IQuestion } from '../type';
|
|
|
|
import { apLogger } from '../logger';
|
2019-03-06 14:55:47 +01:00
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
2019-01-21 05:27:19 +01:00
|
|
|
|
2019-03-06 14:55:47 +01:00
|
|
|
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));
|
2019-01-21 05:27:19 +01:00
|
|
|
|
|
|
|
return {
|
2019-03-06 14:55:47 +01:00
|
|
|
choices,
|
|
|
|
multiple,
|
|
|
|
expiresAt
|
2019-01-21 05:27:19 +01:00
|
|
|
};
|
|
|
|
}
|
2019-03-07 13:19:32 +01: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がこのサーバーを指しているならスキップ
|
|
|
|
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;
|
|
|
|
}
|