yumechi-no-kuni/src/post/create.ts

41 lines
799 B
TypeScript
Raw Normal View History

2018-04-02 03:11:14 -05:00
import Post from '../models/post';
2018-04-04 11:24:39 -05:00
export default async (post, reply, repost, mentions) => {
2018-04-02 03:11:14 -05:00
post.mentions = [];
function addMention(mentionee) {
// Reject if already added
if (post.mentions.some(x => x.equals(mentionee))) return;
// Add mention
post.mentions.push(mentionee);
}
if (reply) {
// Add mention
addMention(reply.userId);
post.replyId = reply._id;
post._reply = { userId: reply.userId };
} else {
post.replyId = null;
post._reply = null;
}
if (repost) {
if (post.text) {
// Add mention
addMention(repost.userId);
}
post.repostId = repost._id;
post._repost = { userId: repost.userId };
} else {
post.repostId = null;
post._repost = null;
}
2018-04-04 11:24:39 -05:00
await Promise.all(mentions.map(({ _id }) => addMention(_id)));
2018-04-02 03:11:14 -05:00
return Post.insert(post);
};