2017-08-30 10:31:39 +02:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import $ from 'cafy';
|
|
|
|
import User from '../../models/user';
|
|
|
|
import Post from '../../models/post';
|
2018-02-02 00:21:30 +01:00
|
|
|
import { pack } from '../../models/user';
|
2017-08-30 10:31:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pin post
|
|
|
|
*
|
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
|
|
|
*/
|
|
|
|
module.exports = async (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 pinee
|
|
|
|
const post = await Post.findOne({
|
|
|
|
_id: postId,
|
|
|
|
user_id: user._id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (post === null) {
|
|
|
|
return rej('post not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
await User.update(user._id, {
|
|
|
|
$set: {
|
|
|
|
pinned_post_id: post._id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Serialize
|
2018-02-02 00:21:30 +01:00
|
|
|
const iObj = await pack(user, user, {
|
2017-08-30 10:31:39 +02:00
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send response
|
|
|
|
res(iObj);
|
|
|
|
});
|