2018-04-01 05:18:36 -05:00
|
|
|
import renderDocument from './document';
|
|
|
|
import renderHashtag from './hashtag';
|
2018-04-01 23:15:53 -05:00
|
|
|
import config from '../../../config';
|
2018-04-01 14:15:27 -05:00
|
|
|
import DriveFile from '../../../models/drive-file';
|
2018-04-05 05:19:00 -05: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 05:18:36 -05:00
|
|
|
|
|
|
|
let inReplyTo;
|
|
|
|
|
|
|
|
if (post.replyId) {
|
|
|
|
const inReplyToPost = await Post.findOne({
|
|
|
|
_id: post.replyId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (inReplyToPost !== null) {
|
|
|
|
const inReplyToUser = await User.findOne({
|
2018-04-06 16:13:40 -05:00
|
|
|
_id: inReplyToPost.userId,
|
2018-04-01 05:18:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (inReplyToUser !== null) {
|
2018-04-06 16:13:40 -05:00
|
|
|
inReplyTo = inReplyToPost.uri || `${config.url}/@${inReplyToUser.username}/${inReplyToPost._id}`;
|
2018-04-01 05:18:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inReplyTo = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const attributedTo = `${config.url}/@${user.username}`;
|
|
|
|
|
|
|
|
return {
|
2018-04-02 11:37:11 -05:00
|
|
|
id: `${attributedTo}/${post._id}`,
|
2018-04-01 05:18:36 -05: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 05:19:00 -05:00
|
|
|
tag: (post.tags || []).map(renderHashtag)
|
2018-04-01 05:18:36 -05:00
|
|
|
};
|
|
|
|
};
|