From ce7efc4dbb9dca05b6b99b5ada22205890ca823f Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Mon, 2 Apr 2018 17:11:14 +0900 Subject: Distribute posts from remote --- src/post/create.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/post/create.ts (limited to 'src/post/create.ts') diff --git a/src/post/create.ts b/src/post/create.ts new file mode 100644 index 0000000000..ecea37382d --- /dev/null +++ b/src/post/create.ts @@ -0,0 +1,50 @@ +import parseAcct from '../acct/parse'; +import Post from '../models/post'; +import User from '../models/user'; + +export default async (post, reply, repost, atMentions) => { + 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; + } + + await Promise.all(atMentions.map(async mention => { + // Fetch mentioned user + // SELECT _id + const { _id } = await User + .findOne(parseAcct(mention), { _id: true }); + + // Add mention + addMention(_id); + })); + + return Post.insert(post); +}; -- cgit v1.2.3-freya