From b7a6699c35f128785ddf860d4f93743cd1c5f983 Mon Sep 17 00:00:00 2001 From: mei23 Date: Sun, 20 Jan 2019 13:15:03 +0900 Subject: [PATCH] wip --- src/models/note.ts | 8 +++++--- src/remote/activitypub/models/note.ts | 6 ++++++ src/remote/activitypub/models/question.ts | 19 +++++++++++++++++++ src/remote/activitypub/renderer/note.ts | 6 +++--- src/remote/activitypub/type.ts | 1 + src/services/note/create.ts | 1 + 6 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/remote/activitypub/models/question.ts diff --git a/src/models/note.ts b/src/models/note.ts index b4ecd2de4b..8ca65bb423 100644 --- a/src/models/note.ts +++ b/src/models/note.ts @@ -38,9 +38,7 @@ export type INote = { fileIds: mongo.ObjectID[]; replyId: mongo.ObjectID; renoteId: mongo.ObjectID; - poll: { - choices: IChoice[] - }; + poll: IPoll; text: string; tags: string[]; tagsLower: string[]; @@ -100,6 +98,10 @@ export type INote = { _files?: IDriveFile[]; }; +export type IPoll = { + choices: IChoice[] +}; + export type IChoice = { id: number; text: string; diff --git a/src/remote/activitypub/models/note.ts b/src/remote/activitypub/models/note.ts index bee2d943a0..4ac6507591 100644 --- a/src/remote/activitypub/models/note.ts +++ b/src/remote/activitypub/models/note.ts @@ -14,6 +14,7 @@ import Emoji, { IEmoji } from '../../../models/emoji'; import { ITag } from './tag'; import { toUnicode } from 'punycode'; import { unique, concat, difference } from '../../../prelude/array'; +import { extractPollFromQuestion } from './question'; const log = debug('misskey:activitypub'); @@ -117,6 +118,9 @@ export async function createNote(value: any, resolver?: Resolver, silent = false const apEmojis = emojis.map(emoji => emoji.name); + const questionUri = note._misskey_question; + const poll = questionUri ? await extractPollFromQuestion(questionUri).catch(() => undefined) : undefined; + // ユーザーの情報が古かったらついでに更新しておく if (actor.lastFetchedAt == null || Date.now() - actor.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24) { updatePerson(note.attributedTo); @@ -137,6 +141,8 @@ export async function createNote(value: any, resolver?: Resolver, silent = false apMentions, apHashtags, apEmojis, + questionUri, + poll, uri: note.id }, silent); } diff --git a/src/remote/activitypub/models/question.ts b/src/remote/activitypub/models/question.ts new file mode 100644 index 0000000000..53892a409e --- /dev/null +++ b/src/remote/activitypub/models/question.ts @@ -0,0 +1,19 @@ +import { IChoice, IPoll } from '../../../models/note'; +import Resolver from '../resolver'; + +export async function extractPollFromQuestion(questionUri: string): Promise { + const resolver = new Resolver(); + const question = await resolver.resolve(questionUri) as any; + + const choices: IChoice[] = question.oneOf.map((x: any, i: number) => { + return { + id: i, + text: x.name, + votes: x._misskey_votes || 0, + } as IChoice; + }); + + return { + choices + }; +} diff --git a/src/remote/activitypub/renderer/note.ts b/src/remote/activitypub/renderer/note.ts index cc9c28b397..ac769a71d7 100644 --- a/src/remote/activitypub/renderer/note.ts +++ b/src/remote/activitypub/renderer/note.ts @@ -94,14 +94,14 @@ export default async function renderNote(note: INote, dive = true): Promise let text = note.text; - const questions = []; + let question: string; if (note.poll != null) { if (text == null) text = ''; const url = `${config.url}/notes/${note._id}`; // TODO: i18n text += `\n\n[投票を見る](${url})`; - questions.push(await renderQuestion(user as ILocalUser, note)); + question = `${config.url}/questions/${note._id}`; } let apText = text; @@ -124,7 +124,6 @@ export default async function renderNote(note: INote, dive = true): Promise ...hashtagTags, ...mentionTags, ...apemojis, - ...questions, ]; return { @@ -135,6 +134,7 @@ export default async function renderNote(note: INote, dive = true): Promise content, _misskey_content: text, _misskey_quote: quote, + _misskey_question: question, published: note.createdAt.toISOString(), to, cc, diff --git a/src/remote/activitypub/type.ts b/src/remote/activitypub/type.ts index 9ffe73a670..b902abea23 100644 --- a/src/remote/activitypub/type.ts +++ b/src/remote/activitypub/type.ts @@ -42,6 +42,7 @@ export interface INote extends IObject { type: 'Note'; _misskey_content: string; _misskey_quote: string; + _misskey_question: string; } export interface IPerson extends IObject { diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 18073f3aa6..cf7e109d29 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -102,6 +102,7 @@ type Option = { apMentions?: IUser[]; apHashtags?: string[]; apEmojis?: string[]; + questionUri?: string; uri?: string; app?: IApp; };