From 9458045c8f25f749f235d27374d9810e14669c26 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sat, 4 Feb 2023 15:32:25 +0100 Subject: [PATCH] server: refactor note/renote rendering to separate file --- .../activitypub/renderer/note-or-renote.ts | 17 +++++++++++++++++ packages/backend/src/server/activitypub.ts | 5 +++-- .../backend/src/server/activitypub/outbox.ts | 16 ++-------------- packages/backend/src/services/note/create.ts | 4 ++-- 4 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 packages/backend/src/remote/activitypub/renderer/note-or-renote.ts diff --git a/packages/backend/src/remote/activitypub/renderer/note-or-renote.ts b/packages/backend/src/remote/activitypub/renderer/note-or-renote.ts new file mode 100644 index 000000000..cb013140c --- /dev/null +++ b/packages/backend/src/remote/activitypub/renderer/note-or-renote.ts @@ -0,0 +1,17 @@ +import config from '@/config/index.js'; +import { Notes } from '@/models/index.js'; +import { Note } from '@/models/entities/note.js'; +import { isPureRenote } from '@/misc/renote.js'; +import { IActivity } from '@/remote/activitypub/types.js'; +import renderNote from '@/remote/activitypub/renderer/note.js'; +import renderCreate from '@/remote/activitypub/renderer/create.js'; +import renderAnnounce from '@/remote/activitypub/renderer/announce.js'; + +export async function renderNoteOrRenoteActivity(note: Note): Promise { + if (isPureRenote(note)) { + const renote = await Notes.findOneByOrFail({ id: note.renoteId }); + return renderAnnounce(renote.uri ?? `${config.url}/notes/${renote.id}`, note); + } else { + return renderCreate(await renderNote(note, false), note); + } +} diff --git a/packages/backend/src/server/activitypub.ts b/packages/backend/src/server/activitypub.ts index 2dc4ea1e7..6bec78f8c 100644 --- a/packages/backend/src/server/activitypub.ts +++ b/packages/backend/src/server/activitypub.ts @@ -15,7 +15,8 @@ import { ILocalUser, User } from '@/models/entities/user.js'; import { renderLike } from '@/remote/activitypub/renderer/like.js'; import { getUserKeypair } from '@/misc/keypair-store.js'; import renderFollow from '@/remote/activitypub/renderer/follow.js'; -import Outbox, { packActivity } from './activitypub/outbox.js'; +import { renderNoteOrRenoteActivity } from '@/remote/activitypub/renderer/note-or-renote.js'; +import Outbox from './activitypub/outbox.js'; import Followers from './activitypub/followers.js'; import Following from './activitypub/following.js'; import Featured from './activitypub/featured.js'; @@ -115,7 +116,7 @@ router.get('/notes/:note/activity', async ctx => { return; } - ctx.body = renderActivity(await packActivity(note)); + ctx.body = renderActivity(await renderNoteOrRenoteActivity(note)); ctx.set('Cache-Control', 'public, max-age=180'); setResponseType(ctx); }); diff --git a/packages/backend/src/server/activitypub/outbox.ts b/packages/backend/src/server/activitypub/outbox.ts index d517ffcfe..7b8fefb3b 100644 --- a/packages/backend/src/server/activitypub/outbox.ts +++ b/packages/backend/src/server/activitypub/outbox.ts @@ -7,6 +7,7 @@ import renderOrderedCollectionPage from '@/remote/activitypub/renderer/ordered-c import renderNote from '@/remote/activitypub/renderer/note.js'; import renderCreate from '@/remote/activitypub/renderer/create.js'; import renderAnnounce from '@/remote/activitypub/renderer/announce.js'; +import { renderNoteOrRenoteActivity } from '@/remote/activitypub/renderer/note-or-renote.js'; import { countIf } from '@/prelude/array.js'; import * as url from '@/prelude/url.js'; import { Users, Notes } from '@/models/index.js'; @@ -63,7 +64,7 @@ export default async (ctx: Router.RouterContext) => { if (sinceId) notes.reverse(); - const activities = await Promise.all(notes.map(note => packActivity(note))); + const activities = await Promise.all(notes.map(note => renderNoteOrRenoteActivity(note))); const rendered = renderOrderedCollectionPage( `${partOf}?${url.query({ page: 'true', @@ -94,16 +95,3 @@ export default async (ctx: Router.RouterContext) => { setResponseType(ctx); } }; - -/** - * Pack Create or Announce Activity - * @param note Note - */ -export async function packActivity(note: Note): Promise { - if (isPureRenote(note)) { - const renote = await Notes.findOneByOrFail({ id: note.renoteId }); - return renderAnnounce(renote.uri ?? `${config.url}/notes/${renote.id}`, note); - } else { - return renderCreate(await renderNote(note, false), note); - } -} diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index 04b4a22a5..8f48bc12a 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -36,7 +36,7 @@ import { Cache } from '@/misc/cache.js'; import { UserProfile } from '@/models/entities/user-profile.js'; import { getActiveWebhooks } from '@/misc/webhook-cache.js'; import { IActivity } from '@/remote/activitypub/type.js'; -import { packActivity } from '@/server/activitypub/outbox.js'; +import { renderNoteOrRenoteActivity } from '@/remote/activitypub/renderer/note-or-renote.js'; import { MINUTE } from '@/const.js'; import { updateHashtags } from '../update-hashtag.js'; import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js'; @@ -431,7 +431,7 @@ export default async (user: { id: User['id']; username: User['username']; host: //#region AP deliver if (Users.isLocalUser(user) && !data.localOnly) { (async () => { - const noteActivity = renderActivity(await packActivity(note)); + const noteActivity = renderActivity(await renderNoteOrRenoteActivity(note)); const dm = new DeliverManager(user, noteActivity); // Delivered to remote users who have been mentioned