summaryrefslogtreecommitdiff
path: root/src/server/api
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-04-07 17:05:14 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-04-07 17:05:14 +0900
commitc5f23bce7864bc1cffb82b54f62e019c4c04137d (patch)
tree8ec7313f4ec333bcb4ca3e19a5106c602740870c /src/server/api
parentRefactor (diff)
downloadsharkey-c5f23bce7864bc1cffb82b54f62e019c4c04137d.tar.gz
sharkey-c5f23bce7864bc1cffb82b54f62e019c4c04137d.tar.bz2
sharkey-c5f23bce7864bc1cffb82b54f62e019c4c04137d.zip
Implement like
Diffstat (limited to 'src/server/api')
-rw-r--r--src/server/api/endpoints/posts/reactions/create.ts88
1 files changed, 6 insertions, 82 deletions
diff --git a/src/server/api/endpoints/posts/reactions/create.ts b/src/server/api/endpoints/posts/reactions/create.ts
index f1b0c7dd29..71fa6a2955 100644
--- a/src/server/api/endpoints/posts/reactions/create.ts
+++ b/src/server/api/endpoints/posts/reactions/create.ts
@@ -3,20 +3,11 @@
*/
import $ from 'cafy';
import Reaction from '../../../../../models/post-reaction';
-import Post, { pack as packPost } from '../../../../../models/post';
-import { pack as packUser } from '../../../../../models/user';
-import Watching from '../../../../../models/post-watching';
-import watch from '../../../../../post/watch';
-import { publishPostStream } from '../../../../../publishers/stream';
-import notify from '../../../../../publishers/notify';
-import pushSw from '../../../../../publishers/push-sw';
+import Post from '../../../../../models/post';
+import create from '../../../../../services/post/reaction/create';
/**
* React to a post
- *
- * @param {any} params
- * @param {any} user
- * @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'postId' parameter
@@ -46,78 +37,11 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
return rej('post not found');
}
- // Myself
- if (post.userId.equals(user._id)) {
- return rej('cannot react to my post');
+ try {
+ await create(user, post, reaction);
+ } catch (e) {
+ rej(e);
}
- // if already reacted
- const exist = await Reaction.findOne({
- postId: post._id,
- userId: user._id,
- deletedAt: { $exists: false }
- });
-
- if (exist !== null) {
- return rej('already reacted');
- }
-
- // Create reaction
- await Reaction.insert({
- createdAt: new Date(),
- postId: post._id,
- userId: user._id,
- reaction: reaction
- });
-
- // Send response
res();
-
- const inc = {};
- inc[`reactionCounts.${reaction}`] = 1;
-
- // Increment reactions count
- await Post.update({ _id: post._id }, {
- $inc: inc
- });
-
- publishPostStream(post._id, 'reacted');
-
- // Notify
- notify(post.userId, user._id, 'reaction', {
- postId: post._id,
- reaction: reaction
- });
-
- pushSw(post.userId, 'reaction', {
- user: await packUser(user, post.userId),
- post: await packPost(post, post.userId),
- reaction: reaction
- });
-
- // Fetch watchers
- Watching
- .find({
- postId: post._id,
- userId: { $ne: user._id },
- // 削除されたドキュメントは除く
- deletedAt: { $exists: false }
- }, {
- fields: {
- userId: true
- }
- })
- .then(watchers => {
- watchers.forEach(watcher => {
- notify(watcher.userId, user._id, 'reaction', {
- postId: post._id,
- reaction: reaction
- });
- });
- });
-
- // この投稿をWatchする
- if (user.account.settings.autoWatch !== false) {
- watch(user._id, post);
- }
});