From 171b046de549f1478e928dee3177eeefab341fcf Mon Sep 17 00:00:00 2001 From: mei23 Date: Mon, 21 Jan 2019 04:38:07 +0900 Subject: [PATCH] Send Update --- src/server/api/endpoints/notes/polls/vote.ts | 3 ++ src/services/note/polls/vote.ts | 36 +++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/server/api/endpoints/notes/polls/vote.ts b/src/server/api/endpoints/notes/polls/vote.ts index 78dd1d1ff7..15894ccef4 100644 --- a/src/server/api/endpoints/notes/polls/vote.ts +++ b/src/server/api/endpoints/notes/polls/vote.ts @@ -7,6 +7,7 @@ import { publishNoteStream } from '../../../../../stream'; import notify from '../../../../../notify'; import define from '../../../define'; import createNote from '../../../../../services/note/create'; +import { publishVoteToFollowers } from '../../../../../services/note/polls/vote'; export const meta = { desc: { @@ -124,4 +125,6 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => { reply: note, }); } + + publishVoteToFollowers(user, note); })); diff --git a/src/services/note/polls/vote.ts b/src/services/note/polls/vote.ts index 8f8fb40000..786e3c4a86 100644 --- a/src/services/note/polls/vote.ts +++ b/src/services/note/polls/vote.ts @@ -5,7 +5,12 @@ import watch from '../../../services/note/watch'; import { publishNoteStream } from '../../../stream'; import notify from '../../../notify'; import createNote from '../../../services/note/create'; -import { isLocalUser, IUser } from '../../../models/user'; +import { isLocalUser, IUser, isRemoteUser } from '../../../models/user'; +import Following from '../../../models/following'; +import packAp from '../../../remote/activitypub/renderer'; +import { deliver } from '../../../queue'; +import renderUpdate from '../../../remote/activitypub/renderer/update'; +import renderQuestion from '../../../remote/activitypub/renderer/question'; export default (user: IUser, note: INote, choice: number) => new Promise(async (res, rej) => { if (!note.poll.choices.some(x => x.id == choice)) return rej('invalid choice param'); @@ -84,4 +89,33 @@ export default (user: IUser, note: INote, choice: number) => new Promise(async ( reply: note, }); } + + publishVoteToFollowers(user, note); }); + +export async function publishVoteToFollowers(user: IUser, note: INote) { + const followers = await Following.find({ + followeeId: user._id + }); + + const queue: string[] = []; + + // フォロワーがリモートユーザーかつ投稿者がローカルユーザーならUpdateを配信 + if (isLocalUser(user)) { + for (const following of followers) { + const follower = following._follower; + + if (isRemoteUser(follower)) { + const inbox = follower.sharedInbox || follower.inbox; + if (!queue.includes(inbox)) queue.push(inbox); + } + } + + if (queue.length > 0) { + const content = packAp(renderUpdate(await renderQuestion(user, note), user)); + for (const inbox of queue) { + deliver(user, content, inbox); + } + } + } +}