Implement #746
This commit is contained in:
@ -167,6 +167,10 @@ const endpoints: Endpoint[] = [
|
||||
name: 'i/regenerate_token',
|
||||
withCredential: true
|
||||
},
|
||||
{
|
||||
name: 'i/pin',
|
||||
kind: 'account-write'
|
||||
},
|
||||
{
|
||||
name: 'i/appdata/get',
|
||||
withCredential: true
|
||||
|
44
src/api/endpoints/i/pin.ts
Normal file
44
src/api/endpoints/i/pin.ts
Normal file
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import User from '../../models/user';
|
||||
import Post from '../../models/post';
|
||||
import serialize from '../../serializers/user';
|
||||
|
||||
/**
|
||||
* 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
|
||||
const iObj = await serialize(user, user, {
|
||||
detail: true
|
||||
});
|
||||
|
||||
// Send response
|
||||
res(iObj);
|
||||
});
|
@ -4,6 +4,7 @@
|
||||
import * as mongo from 'mongodb';
|
||||
import deepcopy = require('deepcopy');
|
||||
import User from '../models/user';
|
||||
import serializePost from './post';
|
||||
import Following from '../models/following';
|
||||
import getFriends from '../common/get-friends';
|
||||
import config from '../../conf';
|
||||
@ -116,24 +117,32 @@ export default (
|
||||
_user.is_followed = follow2 !== null;
|
||||
}
|
||||
|
||||
if (me && !me.equals(_user.id) && opts.detail) {
|
||||
const myFollowingIds = await getFriends(me);
|
||||
if (opts.detail) {
|
||||
if (_user.pinned_post_id) {
|
||||
_user.pinned_post = await serializePost(_user.pinned_post_id, me, {
|
||||
detail: true
|
||||
});
|
||||
}
|
||||
|
||||
// Get following you know count
|
||||
const followingYouKnowCount = await Following.count({
|
||||
followee_id: { $in: myFollowingIds },
|
||||
follower_id: _user.id,
|
||||
deleted_at: { $exists: false }
|
||||
});
|
||||
_user.following_you_know_count = followingYouKnowCount;
|
||||
if (me && !me.equals(_user.id)) {
|
||||
const myFollowingIds = await getFriends(me);
|
||||
|
||||
// Get followers you know count
|
||||
const followersYouKnowCount = await Following.count({
|
||||
followee_id: _user.id,
|
||||
follower_id: { $in: myFollowingIds },
|
||||
deleted_at: { $exists: false }
|
||||
});
|
||||
_user.followers_you_know_count = followersYouKnowCount;
|
||||
// Get following you know count
|
||||
const followingYouKnowCount = await Following.count({
|
||||
followee_id: { $in: myFollowingIds },
|
||||
follower_id: _user.id,
|
||||
deleted_at: { $exists: false }
|
||||
});
|
||||
_user.following_you_know_count = followingYouKnowCount;
|
||||
|
||||
// Get followers you know count
|
||||
const followersYouKnowCount = await Following.count({
|
||||
followee_id: _user.id,
|
||||
follower_id: { $in: myFollowingIds },
|
||||
deleted_at: { $exists: false }
|
||||
});
|
||||
_user.followers_you_know_count = followersYouKnowCount;
|
||||
}
|
||||
}
|
||||
|
||||
resolve(_user);
|
||||
|
Reference in New Issue
Block a user