diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2018-04-04 19:29:06 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2018-04-04 19:29:06 +0900 |
| commit | 2d4fe8c619177247b06e2cc878704cdc4d0c444d (patch) | |
| tree | c70a97fb2d1af11a0f29c69b00e43427980f61a7 /src | |
| parent | Merge pull request #1390 from syuilo/greenkeeper/@types/inquirer-0.0.41 (diff) | |
| download | sharkey-2d4fe8c619177247b06e2cc878704cdc4d0c444d.tar.gz sharkey-2d4fe8c619177247b06e2cc878704cdc4d0c444d.tar.bz2 sharkey-2d4fe8c619177247b06e2cc878704cdc4d0c444d.zip | |
[wip] Implement like activity
Diffstat (limited to 'src')
| -rw-r--r-- | src/remote/activitypub/act/like.ts | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/remote/activitypub/act/like.ts b/src/remote/activitypub/act/like.ts new file mode 100644 index 0000000000..ea53242017 --- /dev/null +++ b/src/remote/activitypub/act/like.ts @@ -0,0 +1,63 @@ +import { MongoError } from 'mongodb'; +import Reaction, { IPostReaction } from '../../../models/post-reaction'; +import Post from '../../../models/post'; +import queue from '../../../queue'; + +export default async (resolver, actor, activity, distribute) => { + const id = activity.object.id || activity.object; + + // Transform: + // https://misskey.ex/@syuilo/xxxx to + // xxxx + const postId = id.split('/').pop(); + + const post = await Post.findOne({ _id: postId }); + if (post === null) { + throw new Error(); + } + + if (!distribute) { + const { _id } = await Reaction.findOne({ + userId: actor._id, + postId: post._id + }); + + return { + resolver, + object: { $ref: 'postPeactions', $id: _id } + }; + } + + const promisedReaction = Reaction.insert({ + createdAt: new Date(), + userId: actor._id, + postId: post._id, + reaction: 'pudding' + }).then(reaction => new Promise<IPostReaction>((resolve, reject) => { + queue.create('http', { + type: 'reaction', + reactionId: reaction._id + }).save(error => { + if (error) { + reject(error); + } else { + resolve(reaction); + } + }); + }), async error => { + // duplicate key error + if (error instanceof MongoError && error.code === 11000) { + return Reaction.findOne({ + userId: actor._id, + postId: post._id + }); + } + + throw error; + }); + + return promisedReaction.then(({ _id }) => ({ + resolver, + object: { $ref: 'postPeactions', $id: _id } + })); +}; |