diff options
| author | Akihiko Odaki <nekomanma@pixiv.co.jp> | 2018-04-02 17:11:14 +0900 |
|---|---|---|
| committer | Akihiko Odaki <nekomanma@pixiv.co.jp> | 2018-04-02 17:11:14 +0900 |
| commit | ce7efc4dbb9dca05b6b99b5ada22205890ca823f (patch) | |
| tree | 7889620f2172fef7277b66a7abb00557e966fbf3 /src/post/create.ts | |
| parent | Introduce acct directory (diff) | |
| download | misskey-ce7efc4dbb9dca05b6b99b5ada22205890ca823f.tar.gz misskey-ce7efc4dbb9dca05b6b99b5ada22205890ca823f.tar.bz2 misskey-ce7efc4dbb9dca05b6b99b5ada22205890ca823f.zip | |
Distribute posts from remote
Diffstat (limited to 'src/post/create.ts')
| -rw-r--r-- | src/post/create.ts | 50 |
1 files changed, 50 insertions, 0 deletions
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); +}; |