This commit is contained in:
syuilo
2017-08-30 17:31:39 +09:00
parent 61be5c0d10
commit e7415dd42b
14 changed files with 315 additions and 60 deletions

View File

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

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

View File

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