summaryrefslogtreecommitdiff
path: root/src/server/activitypub/featured.ts
blob: 1598cc680f6b8638cdad23427765ad94ea999d3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as Router from '@koa/router';
import config from '@/config/index';
import { renderActivity } from '@/remote/activitypub/renderer/index';
import renderOrderedCollection from '@/remote/activitypub/renderer/ordered-collection';
import { setResponseType } from '../activitypub';
import renderNote from '@/remote/activitypub/renderer/note';
import { Users, Notes, UserNotePinings } from '@/models/index';

export default async (ctx: Router.RouterContext) => {
	const userId = ctx.params.user;

	// Verify user
	const user = await Users.findOne({
		id: userId,
		host: null
	});

	if (user == null) {
		ctx.status = 404;
		return;
	}

	const pinings = await UserNotePinings.find({
		where: { userId: user.id },
		order: { id: 'DESC' }
	});

	const pinnedNotes = await Promise.all(pinings.map(pining =>
		Notes.findOneOrFail(pining.noteId)));

	const renderedNotes = await Promise.all(pinnedNotes.map(note => renderNote(note)));

	const rendered = renderOrderedCollection(
		`${config.url}/users/${userId}/collections/featured`,
		renderedNotes.length, undefined, undefined, renderedNotes
	);

	ctx.body = renderActivity(rendered);
	ctx.set('Cache-Control', 'public, max-age=180');
	setResponseType(ctx);
};