summaryrefslogtreecommitdiff
path: root/src/api/endpoints/posts/reactions
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/reactions
parent[Client] :art: (diff)
downloadsharkey-b05bee58d28c3209d7f86a909f877c1e121c12ed.tar.gz
sharkey-b05bee58d28c3209d7f86a909f877c1e121c12ed.tar.bz2
sharkey-b05bee58d28c3209d7f86a909f877c1e121c12ed.zip
#298
Diffstat (limited to 'src/api/endpoints/posts/reactions')
-rw-r--r--src/api/endpoints/posts/reactions/create.ts81
-rw-r--r--src/api/endpoints/posts/reactions/delete.ts60
2 files changed, 141 insertions, 0 deletions
diff --git a/src/api/endpoints/posts/reactions/create.ts b/src/api/endpoints/posts/reactions/create.ts
new file mode 100644
index 0000000000..de4df5fbe1
--- /dev/null
+++ b/src/api/endpoints/posts/reactions/create.ts
@@ -0,0 +1,81 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Reaction from '../../../models/post-reaction';
+import Post from '../../../models/post';
+import notify from '../../../common/notify';
+
+/**
+ * React to a post
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'post_id' parameter
+ const [postId, postIdErr] = $(params.post_id).id().$;
+ if (postIdErr) return rej('invalid post_id param');
+
+ // 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
+ });
+
+ if (post === null) {
+ return rej('post not found');
+ }
+
+ // Myself
+ if (post.user_id.equals(user._id)) {
+ return rej('cannot react to my post');
+ }
+
+ // 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 reacted');
+ }
+
+ // Create reaction
+ await Reaction.insert({
+ created_at: new Date(),
+ post_id: post._id,
+ user_id: user._id,
+ reaction: reaction
+ });
+
+ // Send response
+ res();
+
+ const inc = {};
+ inc['reaction_counts.' + reaction] = 1;
+
+ // Increment reactions count
+ Post.update({ _id: post._id }, {
+ $inc: inc
+ });
+
+ // Notify
+ notify(post.user_id, user._id, 'reaction', {
+ post_id: post._id,
+ reaction: reaction
+ });
+});
diff --git a/src/api/endpoints/posts/reactions/delete.ts b/src/api/endpoints/posts/reactions/delete.ts
new file mode 100644
index 0000000000..89f6beb103
--- /dev/null
+++ b/src/api/endpoints/posts/reactions/delete.ts
@@ -0,0 +1,60 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Reaction from '../../../models/post-reaction';
+import Post from '../../../models/post';
+// import event from '../../../event';
+
+/**
+ * Unreact to a post
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'post_id' parameter
+ const [postId, postIdErr] = $(params.post_id).id().$;
+ if (postIdErr) return rej('invalid post_id param');
+
+ // Fetch unreactee
+ const post = await Post.findOne({
+ _id: postId
+ });
+
+ if (post === null) {
+ return rej('post not found');
+ }
+
+ // 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('never reacted');
+ }
+
+ // Delete reaction
+ await Reaction.update({
+ _id: exist._id
+ }, {
+ $set: {
+ deleted_at: new Date()
+ }
+ });
+
+ // Send response
+ res();
+
+ const dec = {};
+ dec['reaction_counts.' + exist.reaction] = -1;
+
+ // Decrement reactions count
+ Post.update({ _id: post._id }, {
+ $inc: dec
+ });
+});