summaryrefslogtreecommitdiff
path: root/src/remote/activitypub/renderer/note.ts
blob: b971a53951cbe02fd0b62d735b716c49d1b7ab2c (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
import renderDocument from './document';
import renderHashtag from './hashtag';
import config from '../../../config';
import DriveFile from '../../../models/drive-file';
import Post, { IPost } from '../../../models/post';
import User, { IUser } from '../../../models/user';

export default async (user: IUser, post: IPost) => {
	const promisedFiles = post.mediaIds
		? DriveFile.find({ _id: { $in: post.mediaIds } })
		: Promise.resolve([]);

	let inReplyTo;

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

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

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

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

	return {
		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 promisedFiles).map(renderDocument),
		tag: (post.tags || []).map(renderHashtag)
	};
};