summaryrefslogtreecommitdiff
path: root/src/api/endpoints/posts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-03-20 04:24:19 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-03-20 04:24:19 +0900
commitb05bee58d28c3209d7f86a909f877c1e121c12ed (patch)
tree96efc12c89479b63dbc339ab18bcf9dde9dd6bcc /src/api/endpoints/posts
parent[Client] :art: (diff)
downloadsharkey-b05bee58d28c3209d7f86a909f877c1e121c12ed.tar.gz
sharkey-b05bee58d28c3209d7f86a909f877c1e121c12ed.tar.bz2
sharkey-b05bee58d28c3209d7f86a909f877c1e121c12ed.zip
#298
Diffstat (limited to 'src/api/endpoints/posts')
-rw-r--r--src/api/endpoints/posts/reactions.ts (renamed from src/api/endpoints/posts/likes.ts)12
-rw-r--r--src/api/endpoints/posts/reactions/create.ts (renamed from src/api/endpoints/posts/likes/create.ts)61
-rw-r--r--src/api/endpoints/posts/reactions/delete.ts (renamed from src/api/endpoints/posts/likes/delete.ts)48
3 files changed, 53 insertions, 68 deletions
diff --git a/src/api/endpoints/posts/likes.ts b/src/api/endpoints/posts/reactions.ts
index 29aff1de38..eab5d9b258 100644
--- a/src/api/endpoints/posts/likes.ts
+++ b/src/api/endpoints/posts/reactions.ts
@@ -3,11 +3,11 @@
*/
import $ from 'cafy';
import Post from '../../models/post';
-import Like from '../../models/like';
-import serialize from '../../serializers/user';
+import Reaction from '../../models/post-reaction';
+import serialize from '../../serializers/post-reaction';
/**
- * Show a likes of a post
+ * Show reactions of a post
*
* @param {any} params
* @param {any} user
@@ -40,7 +40,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
}
// Issue query
- const likes = await Like
+ const reactions = await Reaction
.find({
post_id: post._id,
deleted_at: { $exists: false }
@@ -53,6 +53,6 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
});
// Serialize
- res(await Promise.all(likes.map(async like =>
- await serialize(like.user_id, user))));
+ res(await Promise.all(reactions.map(async reaction =>
+ await serialize(reaction, user))));
});
diff --git a/src/api/endpoints/posts/likes/create.ts b/src/api/endpoints/posts/reactions/create.ts
index 3a7650dead..de4df5fbe1 100644
--- a/src/api/endpoints/posts/likes/create.ts
+++ b/src/api/endpoints/posts/reactions/create.ts
@@ -2,13 +2,12 @@
* Module dependencies
*/
import $ from 'cafy';
-import Like from '../../../models/like';
+import Reaction from '../../../models/post-reaction';
import Post from '../../../models/post';
-import User from '../../../models/user';
import notify from '../../../common/notify';
/**
- * Like a post
+ * React to a post
*
* @param {any} params
* @param {any} user
@@ -19,7 +18,18 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
- // Get likee
+ // Get 'reaction' parameter
+ const [reaction, reactionErr] = $(params.reaction).string().or([
+ 'like',
+ 'love',
+ 'laugh',
+ 'hmm',
+ 'surprise',
+ 'congrats'
+ ]).$;
+ if (reactionErr) return rej('invalid reaction param');
+
+ // Fetch reactee
const post = await Post.findOne({
_id: postId
});
@@ -30,53 +40,42 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Myself
if (post.user_id.equals(user._id)) {
- return rej('-need-translate-');
+ return rej('cannot react to my post');
}
- // if already liked
- const exist = await Like.findOne({
+ // if already reacted
+ const exist = await Reaction.findOne({
post_id: post._id,
user_id: user._id,
deleted_at: { $exists: false }
});
if (exist !== null) {
- return rej('already liked');
+ return rej('already reacted');
}
- // Create like
- await Like.insert({
+ // Create reaction
+ await Reaction.insert({
created_at: new Date(),
post_id: post._id,
- user_id: user._id
+ user_id: user._id,
+ reaction: reaction
});
// Send response
res();
- // Increment likes count
- Post.update({ _id: post._id }, {
- $inc: {
- likes_count: 1
- }
- });
-
- // Increment user likes count
- User.update({ _id: user._id }, {
- $inc: {
- likes_count: 1
- }
- });
+ const inc = {};
+ inc['reaction_counts.' + reaction] = 1;
- // Increment user liked count
- User.update({ _id: post.user_id }, {
- $inc: {
- liked_count: 1
- }
+ // Increment reactions count
+ Post.update({ _id: post._id }, {
+ $inc: inc
});
// Notify
- notify(post.user_id, user._id, 'like', {
- post_id: post._id
+ notify(post.user_id, user._id, 'reaction', {
+ post_id: post._id,
+ reaction: reaction
});
});
diff --git a/src/api/endpoints/posts/likes/delete.ts b/src/api/endpoints/posts/reactions/delete.ts
index d90f2937e0..89f6beb103 100644
--- a/src/api/endpoints/posts/likes/delete.ts
+++ b/src/api/endpoints/posts/reactions/delete.ts
@@ -2,13 +2,12 @@
* Module dependencies
*/
import $ from 'cafy';
-import Like from '../../../models/like';
+import Reaction from '../../../models/post-reaction';
import Post from '../../../models/post';
-import User from '../../../models/user';
// import event from '../../../event';
/**
- * Unlike a post
+ * Unreact to a post
*
* @param {any} params
* @param {any} user
@@ -19,7 +18,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
- // Get likee
+ // Fetch unreactee
const post = await Post.findOne({
_id: postId
});
@@ -28,47 +27,34 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
return rej('post not found');
}
- // if already liked
- const exist = await Like.findOne({
+ // if already unreacted
+ const exist = await Reaction.findOne({
post_id: post._id,
user_id: user._id,
deleted_at: { $exists: false }
});
if (exist === null) {
- return rej('already not liked');
+ return rej('never reacted');
}
- // Delete like
- await Like.update({
+ // Delete reaction
+ await Reaction.update({
_id: exist._id
}, {
- $set: {
- deleted_at: new Date()
- }
- });
+ $set: {
+ deleted_at: new Date()
+ }
+ });
// Send response
res();
- // Decrement likes count
- Post.update({ _id: post._id }, {
- $inc: {
- likes_count: -1
- }
- });
-
- // Decrement user likes count
- User.update({ _id: user._id }, {
- $inc: {
- likes_count: -1
- }
- });
+ const dec = {};
+ dec['reaction_counts.' + exist.reaction] = -1;
- // Decrement user liked count
- User.update({ _id: post.user_id }, {
- $inc: {
- liked_count: -1
- }
+ // Decrement reactions count
+ Post.update({ _id: post._id }, {
+ $inc: dec
});
});