summaryrefslogtreecommitdiff
path: root/src/server/activitypub/post.ts
blob: 355c6035630cca07c8ed3ca704fdfe34be382a86 (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
42
43
import * as express from 'express';
import context from '../../remote/activitypub/renderer/context';
import render from '../../remote/activitypub/renderer/note';
import parseAcct from '../../acct/parse';
import Post from '../../models/post';
import User from '../../models/user';

const app = express.Router();

app.get('/@:user/:post', async (req, res, next) => {
	const accepted = req.accepts(['html', 'application/activity+json', 'application/ld+json']);
	if (!(['application/activity+json', 'application/ld+json'] as any[]).includes(accepted)) {
		return next();
	}

	const { username, host } = parseAcct(req.params.user);
	if (host !== null) {
		return res.sendStatus(422);
	}

	const user = await User.findOne({
		usernameLower: username.toLowerCase(),
		host: null
	});
	if (user === null) {
		return res.sendStatus(404);
	}

	const post = await Post.findOne({
		_id: req.params.post,
		userId: user._id
	});
	if (post === null) {
		return res.sendStatus(404);
	}

	const rendered = await render(user, post);
	rendered['@context'] = context;

	res.json(rendered);
});

export default app;