From 4675a99d49e470824f1b4bdb8c20b167e19410d8 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 13 Apr 2018 14:39:08 +0900 Subject: [PATCH] Refactor --- src/remote/activitypub/renderer/context.ts | 5 ----- src/remote/activitypub/renderer/index.ts | 7 +++++++ src/server/activitypub.ts | 21 +++++---------------- src/services/following/create.ts | 10 +++------- src/services/following/delete.ts | 6 ++---- src/services/note/create.ts | 5 ++--- src/services/note/reaction/create.ts | 6 ++---- 7 files changed, 21 insertions(+), 39 deletions(-) delete mode 100644 src/remote/activitypub/renderer/context.ts create mode 100644 src/remote/activitypub/renderer/index.ts diff --git a/src/remote/activitypub/renderer/context.ts b/src/remote/activitypub/renderer/context.ts deleted file mode 100644 index b56f727ae..000000000 --- a/src/remote/activitypub/renderer/context.ts +++ /dev/null @@ -1,5 +0,0 @@ -export default [ - 'https://www.w3.org/ns/activitystreams', - 'https://w3id.org/security/v1', - { Hashtag: 'as:Hashtag' } -]; diff --git a/src/remote/activitypub/renderer/index.ts b/src/remote/activitypub/renderer/index.ts new file mode 100644 index 000000000..ee7f49616 --- /dev/null +++ b/src/remote/activitypub/renderer/index.ts @@ -0,0 +1,7 @@ +export default (x: any) => Object.assign({ + '@context': [ + 'https://www.w3.org/ns/activitystreams', + 'https://w3id.org/security/v1', + { Hashtag: 'as:Hashtag' } + ] +}, x); diff --git a/src/server/activitypub.ts b/src/server/activitypub.ts index acd10b788..2a99bccfc 100644 --- a/src/server/activitypub.ts +++ b/src/server/activitypub.ts @@ -2,8 +2,7 @@ import * as Router from 'koa-router'; import { parseRequest } from 'http-signature'; import { createHttp } from '../queue'; -import context from '../remote/activitypub/renderer/context'; -import render from '../remote/activitypub/renderer/note'; +import pack from '../remote/activitypub/renderer'; import Note from '../models/note'; import User, { isLocalUser } from '../models/user'; import renderNote from '../remote/activitypub/renderer/note'; @@ -57,10 +56,7 @@ router.get('/notes/:note', async (ctx, next) => { return; } - const rendered = await render(note); - rendered['@context'] = context; - - ctx.body = rendered; + ctx.body = pack(await renderNote(note)); }); // outbot @@ -81,9 +77,8 @@ router.get('/users/:user/outbox', async ctx => { const renderedNotes = await Promise.all(notes.map(note => renderNote(note))); const rendered = renderOrderedCollection(`${config.url}/users/${userId}/inbox`, user.notesCount, renderedNotes); - rendered['@context'] = context; - ctx.body = rendered; + ctx.body = pack(rendered); }); // publickey @@ -98,10 +93,7 @@ router.get('/users/:user/publickey', async ctx => { } if (isLocalUser(user)) { - const rendered = renderKey(user); - rendered['@context'] = context; - - ctx.body = rendered; + ctx.body = pack(renderKey(user)); } else { ctx.status = 400; } @@ -118,10 +110,7 @@ router.get('/users/:user', async ctx => { return; } - const rendered = renderPerson(user); - rendered['@context'] = context; - - ctx.body = rendered; + ctx.body = pack(renderPerson(user)); }); // follow form diff --git a/src/services/following/create.ts b/src/services/following/create.ts index 31e3be19e..3289e3129 100644 --- a/src/services/following/create.ts +++ b/src/services/following/create.ts @@ -4,7 +4,7 @@ import FollowingLog from '../../models/following-log'; import FollowedLog from '../../models/followed-log'; import event from '../../publishers/stream'; import notify from '../../publishers/notify'; -import context from '../../remote/activitypub/renderer/context'; +import pack from '../../remote/activitypub/renderer'; import renderFollow from '../../remote/activitypub/renderer/follow'; import renderAccept from '../../remote/activitypub/renderer/accept'; import { deliver } from '../../queue'; @@ -57,16 +57,12 @@ export default async function(follower: IUser, followee: IUser, activity?) { } if (isLocalUser(follower) && isRemoteUser(followee)) { - const content = renderFollow(follower, followee); - content['@context'] = context; - + const content = pack(renderFollow(follower, followee)); deliver(follower, content, followee.inbox).save(); } if (isRemoteUser(follower) && isLocalUser(followee)) { - const content = renderAccept(activity); - content['@context'] = context; - + const content = pack(renderAccept(activity)); deliver(followee, content, follower.inbox).save(); } } diff --git a/src/services/following/delete.ts b/src/services/following/delete.ts index d79bf64f5..8b6c56816 100644 --- a/src/services/following/delete.ts +++ b/src/services/following/delete.ts @@ -3,7 +3,7 @@ import Following from '../../models/following'; import FollowingLog from '../../models/following-log'; import FollowedLog from '../../models/followed-log'; import event from '../../publishers/stream'; -import context from '../../remote/activitypub/renderer/context'; +import pack from '../../remote/activitypub/renderer'; import renderFollow from '../../remote/activitypub/renderer/follow'; import renderUndo from '../../remote/activitypub/renderer/undo'; import { deliver } from '../../queue'; @@ -56,9 +56,7 @@ export default async function(follower: IUser, followee: IUser, activity?) { } if (isLocalUser(follower) && isRemoteUser(followee)) { - const content = renderUndo(renderFollow(follower, followee)); - content['@context'] = context; - + const content = pack(renderUndo(renderFollow(follower, followee))); deliver(follower, content, followee.inbox).save(); } } diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 8f0b84bcc..b238cd560 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -6,7 +6,7 @@ import { deliver } from '../../queue'; import renderNote from '../../remote/activitypub/renderer/note'; import renderCreate from '../../remote/activitypub/renderer/create'; import renderAnnounce from '../../remote/activitypub/renderer/announce'; -import context from '../../remote/activitypub/renderer/context'; +import packAp from '../../remote/activitypub/renderer'; import { IDriveFile } from '../../models/drive-file'; import notify from '../../publishers/notify'; import NoteWatching from '../../models/note-watching'; @@ -132,8 +132,7 @@ export default async (user: IUser, data: { const content = data.renote && data.text == null ? renderAnnounce(data.renote.uri ? data.renote.uri : await renderNote(data.renote)) : renderCreate(await renderNote(note)); - content['@context'] = context; - return content; + return packAp(content); }; // 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送 diff --git a/src/services/note/reaction/create.ts b/src/services/note/reaction/create.ts index 88158034f..69a14248d 100644 --- a/src/services/note/reaction/create.ts +++ b/src/services/note/reaction/create.ts @@ -8,7 +8,7 @@ import NoteWatching from '../../../models/note-watching'; import watch from '../watch'; import renderLike from '../../../remote/activitypub/renderer/like'; import { deliver } from '../../../queue'; -import context from '../../../remote/activitypub/renderer/context'; +import pack from '../../../remote/activitypub/renderer'; export default async (user: IUser, note: INote, reaction: string) => new Promise(async (res, rej) => { // Myself @@ -85,9 +85,7 @@ export default async (user: IUser, note: INote, reaction: string) => new Promise //#region 配信 // リアクターがローカルユーザーかつリアクション対象がリモートユーザーの投稿なら配送 if (isLocalUser(user) && isRemoteUser(note._user)) { - const content = renderLike(user, note); - content['@context'] = context; - + const content = pack(renderLike(user, note)); deliver(user, content, note._user.inbox).save(); } //#endregion