server: refactor note/renote rendering to separate file

This commit is contained in:
Johann150 2023-02-04 15:32:25 +01:00
parent a8c0e1f827
commit 9458045c8f
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
4 changed files with 24 additions and 18 deletions

View file

@ -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<IActivity> {
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);
}
}

View file

@ -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);
});

View file

@ -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<Note> or Announce Activity
* @param note Note
*/
export async function packActivity(note: Note): Promise<any> {
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);
}
}

View file

@ -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