This commit is contained in:
syuilo
2017-03-20 04:24:19 +09:00
parent 516d3d600a
commit b05bee58d2
23 changed files with 371 additions and 446 deletions

View File

@ -115,21 +115,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',

View File

@ -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))));
});

View File

@ -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
const inc = {};
inc['reaction_counts.' + reaction] = 1;
// Increment reactions 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 liked count
User.update({ _id: post.user_id }, {
$inc: {
liked_count: 1
}
$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
});
});

View File

@ -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
const dec = {};
dec['reaction_counts.' + exist.reaction] = -1;
// Decrement reactions count
Post.update({ _id: post._id }, {
$inc: {
likes_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
}
$inc: dec
});
});

View File

@ -1,3 +0,0 @@
import db from '../../db/mongodb';
export default db.get('likes') as any; // fuck type definition

View File

@ -0,0 +1,3 @@
import db from '../../db/mongodb';
export default db.get('post_reactions') as any; // fuck type definition

View File

@ -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);

View File

@ -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);
});

View File

@ -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);