diff options
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/endpoints.ts | 19 | ||||
| -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 | ||||
| -rw-r--r-- | src/api/models/like.ts | 3 | ||||
| -rw-r--r-- | src/api/models/post-reaction.ts | 3 | ||||
| -rw-r--r-- | src/api/serializers/notification.ts | 2 | ||||
| -rw-r--r-- | src/api/serializers/post-reaction.ts | 43 | ||||
| -rw-r--r-- | src/api/serializers/post.ts | 14 |
9 files changed, 112 insertions, 93 deletions
diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts index 17cd8ff56f..2d3716bb85 100644 --- a/src/api/endpoints.ts +++ b/src/api/endpoints.ts @@ -116,21 +116,12 @@ const endpoints: Endpoint[] = [ name: 'aggregation/users/post', }, { - name: 'aggregation/users/like' - }, - { name: 'aggregation/users/followers' }, { name: 'aggregation/users/following' }, { - name: 'aggregation/posts/like' - }, - { - name: 'aggregation/posts/likes' - }, - { name: 'aggregation/posts/repost' }, { @@ -370,26 +361,26 @@ const endpoints: Endpoint[] = [ } }, { - name: 'posts/likes', + name: 'posts/reactions', withCredential: true }, { - name: 'posts/likes/create', + name: 'posts/reactions/create', withCredential: true, limit: { duration: ms('1hour'), max: 100 }, - kind: 'like-write' + kind: 'reaction-write' }, { - name: 'posts/likes/delete', + name: 'posts/reactions/delete', withCredential: true, limit: { duration: ms('1hour'), max: 100 }, - kind: 'like-write' + kind: 'reaction-write' }, { name: 'posts/favorites/create', 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 }); }); diff --git a/src/api/models/like.ts b/src/api/models/like.ts deleted file mode 100644 index ff04d8d0f7..0000000000 --- a/src/api/models/like.ts +++ /dev/null @@ -1,3 +0,0 @@ -import db from '../../db/mongodb'; - -export default db.get('likes') as any; // fuck type definition diff --git a/src/api/models/post-reaction.ts b/src/api/models/post-reaction.ts new file mode 100644 index 0000000000..282ae5bd21 --- /dev/null +++ b/src/api/models/post-reaction.ts @@ -0,0 +1,3 @@ +import db from '../../db/mongodb'; + +export default db.get('post_reactions') as any; // fuck type definition diff --git a/src/api/serializers/notification.ts b/src/api/serializers/notification.ts index 50952e5426..ac919dc8b0 100644 --- a/src/api/serializers/notification.ts +++ b/src/api/serializers/notification.ts @@ -51,7 +51,7 @@ export default (notification: any) => new Promise<any>(async (resolve, reject) = case 'reply': case 'repost': case 'quote': - case 'like': + case 'reaction': case 'poll_vote': // Populate post _notification.post = await serializePost(_notification.post_id, me); diff --git a/src/api/serializers/post-reaction.ts b/src/api/serializers/post-reaction.ts new file mode 100644 index 0000000000..b8807a741c --- /dev/null +++ b/src/api/serializers/post-reaction.ts @@ -0,0 +1,43 @@ +/** + * Module dependencies + */ +import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); +import Reaction from '../models/post-reaction'; +import serializeUser from './user'; + +/** + * Serialize a reaction + * + * @param {any} reaction + * @param {any} me? + * @return {Promise<any>} + */ +export default ( + reaction: any, + me?: any +) => new Promise<any>(async (resolve, reject) => { + let _reaction: any; + + // Populate the reaction if 'reaction' is ID + if (mongo.ObjectID.prototype.isPrototypeOf(reaction)) { + _reaction = await Reaction.findOne({ + _id: reaction + }); + } else if (typeof reaction === 'string') { + _reaction = await Reaction.findOne({ + _id: new mongo.ObjectID(reaction) + }); + } else { + _reaction = deepcopy(reaction); + } + + // Rename _id to id + _reaction.id = _reaction._id; + delete _reaction._id; + + // Populate user + _reaction.user = await serializeUser(_reaction.user_id, me); + + resolve(_reaction); +}); diff --git a/src/api/serializers/post.ts b/src/api/serializers/post.ts index f459529697..3c96884dd1 100644 --- a/src/api/serializers/post.ts +++ b/src/api/serializers/post.ts @@ -4,7 +4,7 @@ import * as mongo from 'mongodb'; import deepcopy = require('deepcopy'); import Post from '../models/post'; -import Like from '../models/like'; +import Reaction from '../models/post-reaction'; import Vote from '../models/poll-vote'; import serializeApp from './app'; import serializeUser from './user'; @@ -100,18 +100,18 @@ const self = ( } } - // Check if it is liked + // Fetch my reaction if (me && opts.detail) { - const liked = await Like - .count({ + const reaction = await Reaction + .findOne({ user_id: me._id, post_id: id, deleted_at: { $exists: false } - }, { - limit: 1 }); - _post.is_liked = liked === 1; + if (reaction) { + _post.my_reaction = reaction.reaction; + } } resolve(_post); |