summaryrefslogtreecommitdiff
path: root/src/server/activitypub/post.ts
blob: 4fb3a1319bae81c5f10353cbeda7c4bd5bc8a1e2 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import * as express from 'express';
import context from '../../common/remote/activitypub/context';
import parseAcct from '../../common/user/parse-acct';
import config from '../../conf';
import DriveFile from '../../models/drive-file';
import Post from '../../models/post';
import User from '../../models/user';

const app = express();
app.disable('x-powered-by');

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 Array<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 asyncFiles = DriveFile.find({ _id: { $in: post.mediaIds } });
	let inReplyTo;

	if (post.replyId) {
		const inReplyToPost = await Post.findOne({
			_id: post.replyId,
		});

		if (inReplyToPost !== null) {
			const inReplyToUser = await User.findOne({
				_id: post.userId,
			});

			if (inReplyToUser !== null) {
				inReplyTo = `${config.url}@${inReplyToUser.username}/${inReplyToPost._id}`;
			}
		}
	} else {
		inReplyTo = null;
	}

	const attributedTo = `${config.url}/@${user.username}`;

	res.json({
		'@context': context,
		id: `${attributedTo}/${post._id}`,
		type: 'Note',
		attributedTo,
		content: post.textHtml,
		published: post.createdAt.toISOString(),
		to: 'https://www.w3.org/ns/activitystreams#Public',
		cc: `${attributedTo}/followers`,
		inReplyTo,
		attachment: (await asyncFiles).map(({ _id, contentType }) => ({
			type: 'Document',
			mediaType: contentType,
			url: `${config.drive_url}/${_id}`
		})),
		tag: post.tags.map(tag => ({
			type: 'Hashtag',
			href: `${config.url}/search?q=#${encodeURIComponent(tag)}`,
			name: '#' + tag
		}))
	});
});

export default app;