Sharkey/src/remote/activitypub/renderer/note.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

2018-04-01 13:18:36 +03:00
import renderDocument from './document';
import renderHashtag from './hashtag';
2018-04-02 07:15:53 +03:00
import config from '../../../config';
2018-04-01 22:15:27 +03:00
import DriveFile from '../../../models/drive-file';
2018-04-05 13:19:00 +03:00
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([]);
2018-04-01 13:18:36 +03:00
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}`;
return {
id: `${attributedTo}/${post._id}`,
2018-04-01 13:18:36 +03:00
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),
2018-04-05 13:19:00 +03:00
tag: (post.tags || []).map(renderHashtag)
2018-04-01 13:18:36 +03:00
};
};