summaryrefslogtreecommitdiff
path: root/src/api/endpoints/posts/likes
diff options
context:
space:
mode:
authorAya Morisawa <AyaMorisawa4869@gmail.com>2017-02-27 16:50:36 +0900
committerAya Morisawa <AyaMorisawa4869@gmail.com>2017-02-27 17:39:24 +0900
commit7bf27dc4ed3fd07cabeb932842b8ae6eacf24a0b (patch)
tree4b9021c01b3eb5f79653ffef5a3587dd0a1af4f5 /src/api/endpoints/posts/likes
parentFix typo: notifcation -> notification (diff)
downloadsharkey-7bf27dc4ed3fd07cabeb932842b8ae6eacf24a0b.tar.gz
sharkey-7bf27dc4ed3fd07cabeb932842b8ae6eacf24a0b.tar.bz2
sharkey-7bf27dc4ed3fd07cabeb932842b8ae6eacf24a0b.zip
Clean up
Diffstat (limited to 'src/api/endpoints/posts/likes')
-rw-r--r--src/api/endpoints/posts/likes/create.js121
-rw-r--r--src/api/endpoints/posts/likes/delete.js109
2 files changed, 114 insertions, 116 deletions
diff --git a/src/api/endpoints/posts/likes/create.js b/src/api/endpoints/posts/likes/create.js
index 83debea960..bae8aecc0c 100644
--- a/src/api/endpoints/posts/likes/create.js
+++ b/src/api/endpoints/posts/likes/create.js
@@ -17,77 +17,76 @@ import notify from '../../../common/notify';
* @return {Promise<object>}
*/
module.exports = (params, user) =>
- new Promise(async (res, rej) =>
-{
- // Get 'post_id' parameter
- let postId = params.post_id;
- if (postId === undefined || postId === null) {
- return rej('post_id is required');
- }
+ new Promise(async (res, rej) => {
+ // Get 'post_id' parameter
+ let postId = params.post_id;
+ if (postId === undefined || postId === null) {
+ return rej('post_id is required');
+ }
- // Validate id
- if (!mongo.ObjectID.isValid(postId)) {
- return rej('incorrect post_id');
- }
+ // Validate id
+ if (!mongo.ObjectID.isValid(postId)) {
+ return rej('incorrect post_id');
+ }
- // Get likee
- const post = await Post.findOne({
- _id: new mongo.ObjectID(postId)
- });
+ // Get likee
+ const post = await Post.findOne({
+ _id: new mongo.ObjectID(postId)
+ });
- if (post === null) {
- return rej('post not found');
- }
+ if (post === null) {
+ return rej('post not found');
+ }
- // Myself
- if (post.user_id.equals(user._id)) {
- return rej('-need-translate-');
- }
+ // Myself
+ if (post.user_id.equals(user._id)) {
+ return rej('-need-translate-');
+ }
- // Check arleady liked
- const exist = await Like.findOne({
- post_id: post._id,
- user_id: user._id,
- deleted_at: { $exists: false }
- });
+ // Check arleady liked
+ const exist = await Like.findOne({
+ post_id: post._id,
+ user_id: user._id,
+ deleted_at: { $exists: false }
+ });
- if (exist !== null) {
- return rej('already liked');
- }
+ if (exist !== null) {
+ return rej('already liked');
+ }
- // Create like
- await Like.insert({
- created_at: new Date(),
- post_id: post._id,
- user_id: user._id
- });
+ // Create like
+ await Like.insert({
+ created_at: new Date(),
+ post_id: post._id,
+ user_id: user._id
+ });
- // Send response
- res();
+ // Send response
+ res();
- // Increment likes count
- Post.update({ _id: post._id }, {
- $inc: {
- likes_count: 1
- }
- });
+ // 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
- }
- });
+ // Increment user likes count
+ User.update({ _id: user._id }, {
+ $inc: {
+ likes_count: 1
+ }
+ });
- // Increment user liked count
- User.update({ _id: post.user_id }, {
- $inc: {
- liked_count: 1
- }
- });
+ // Increment user liked count
+ User.update({ _id: post.user_id }, {
+ $inc: {
+ liked_count: 1
+ }
+ });
- // Notify
- notify(post.user_id, user._id, 'like', {
- post_id: post._id
+ // Notify
+ notify(post.user_id, user._id, 'like', {
+ post_id: post._id
+ });
});
-});
diff --git a/src/api/endpoints/posts/likes/delete.js b/src/api/endpoints/posts/likes/delete.js
index e3dee23bf2..c2b8f53375 100644
--- a/src/api/endpoints/posts/likes/delete.js
+++ b/src/api/endpoints/posts/likes/delete.js
@@ -17,69 +17,68 @@ import User from '../../../models/user';
* @return {Promise<object>}
*/
module.exports = (params, user) =>
- new Promise(async (res, rej) =>
-{
- // Get 'post_id' parameter
- let postId = params.post_id;
- if (postId === undefined || postId === null) {
- return rej('post_id is required');
- }
-
- // Validate id
- if (!mongo.ObjectID.isValid(postId)) {
- return rej('incorrect post_id');
- }
+ new Promise(async (res, rej) => {
+ // Get 'post_id' parameter
+ let postId = params.post_id;
+ if (postId === undefined || postId === null) {
+ return rej('post_id is required');
+ }
- // Get likee
- const post = await Post.findOne({
- _id: new mongo.ObjectID(postId)
- });
+ // Validate id
+ if (!mongo.ObjectID.isValid(postId)) {
+ return rej('incorrect post_id');
+ }
- if (post === null) {
- return rej('post not found');
- }
+ // Get likee
+ const post = await Post.findOne({
+ _id: new mongo.ObjectID(postId)
+ });
- // Check arleady liked
- const exist = await Like.findOne({
- post_id: post._id,
- user_id: user._id,
- deleted_at: { $exists: false }
- });
+ if (post === null) {
+ return rej('post not found');
+ }
- if (exist === null) {
- return rej('already not liked');
- }
+ // Check arleady liked
+ const exist = await Like.findOne({
+ post_id: post._id,
+ user_id: user._id,
+ deleted_at: { $exists: false }
+ });
- // Delete like
- await Like.update({
- _id: exist._id
- }, {
- $set: {
- deleted_at: new Date()
+ if (exist === null) {
+ return rej('already not liked');
}
- });
- // Send response
- res();
+ // Delete like
+ await Like.update({
+ _id: exist._id
+ }, {
+ $set: {
+ deleted_at: new Date()
+ }
+ });
- // Decrement likes count
- Post.update({ _id: post._id }, {
- $inc: {
- likes_count: -1
- }
- });
+ // Send response
+ res();
- // Decrement user likes count
- User.update({ _id: user._id }, {
- $inc: {
- likes_count: -1
- }
- });
+ // Decrement likes count
+ Post.update({ _id: post._id }, {
+ $inc: {
+ likes_count: -1
+ }
+ });
- // Decrement user liked count
- User.update({ _id: post.user_id }, {
- $inc: {
- liked_count: -1
- }
+ // Decrement user likes count
+ User.update({ _id: user._id }, {
+ $inc: {
+ likes_count: -1
+ }
+ });
+
+ // Decrement user liked count
+ User.update({ _id: post.user_id }, {
+ $inc: {
+ liked_count: -1
+ }
+ });
});
-});