yumechi-no-kuni/src/remote/activitypub/renderer/note.ts
2018-04-07 17:05:14 +09:00

47 lines
1.2 KiB
TypeScript

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}/posts/${inReplyToPost._id}`;
}
}
} else {
inReplyTo = null;
}
const attributedTo = `${config.url}/@${user.username}`;
return {
id: `${config.url}/posts/${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)
};
};