Refactoring

This commit is contained in:
syuilo 2017-09-08 17:24:11 +09:00
parent 3feeaccf59
commit 69ab594ac7
2 changed files with 45 additions and 35 deletions

View File

@ -3,8 +3,9 @@
*/
import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
import Post from '../models/post';
import { default as Post, IPost } from '../models/post';
import Reaction from '../models/post-reaction';
import { IUser } from '../models/user';
import Vote from '../models/poll-vote';
import serializeApp from './app';
import serializeUser from './user';
@ -14,14 +15,14 @@ import parse from '../common/text';
/**
* Serialize a post
*
* @param {any} post
* @param {any} me?
* @param {any} options?
* @return {Promise<any>}
* @param post target
* @param me? serializee
* @param options? serialize options
* @return response
*/
const self = (
post: any,
me?: any,
post: string | mongo.ObjectID | IPost,
me?: string | mongo.ObjectID | IUser,
options?: {
detail: boolean
}
@ -30,6 +31,15 @@ const self = (
detail: true,
};
// Me
const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me)
? me as mongo.ObjectID
: typeof me === 'string'
? new mongo.ObjectID(me)
: (me as IUser)._id
: null;
let _post: any;
// Populate the post if 'post' is ID
@ -59,7 +69,7 @@ const self = (
}
// Populate user
_post.user = await serializeUser(_post.user_id, me);
_post.user = await serializeUser(_post.user_id, meId);
// Populate app
if (_post.app_id) {
@ -109,23 +119,23 @@ const self = (
if (_post.reply_to_id) {
// Populate reply to post
_post.reply_to = await self(_post.reply_to_id, me, {
_post.reply_to = await self(_post.reply_to_id, meId, {
detail: false
});
}
if (_post.repost_id) {
// Populate repost
_post.repost = await self(_post.repost_id, me, {
_post.repost = await self(_post.repost_id, meId, {
detail: _post.text == null
});
}
// Poll
if (me && _post.poll) {
if (meId && _post.poll) {
const vote = await Vote
.findOne({
user_id: me._id,
user_id: meId,
post_id: id
});
@ -135,10 +145,10 @@ const self = (
}
// Fetch my reaction
if (me) {
if (meId) {
const reaction = await Reaction
.findOne({
user_id: me._id,
user_id: meId,
post_id: id,
deleted_at: { $exists: false }
});

View File

@ -3,7 +3,7 @@
*/
import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
import User from '../models/user';
import { default as User, IUser } from '../models/user';
import serializePost from './post';
import Following from '../models/following';
import getFriends from '../common/get-friends';
@ -12,14 +12,14 @@ import config from '../../conf';
/**
* Serialize a user
*
* @param {any} user
* @param {any} me?
* @param {any} options?
* @return {Promise<any>}
* @param user target
* @param me? serializee
* @param options? serialize options
* @return response
*/
export default (
user: any,
me?: any,
user: string | mongo.ObjectID | IUser,
me?: string | mongo.ObjectID | IUser,
options?: {
detail?: boolean,
includeSecrets?: boolean
@ -54,13 +54,13 @@ export default (
}
// Me
if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) {
if (typeof me === 'string') {
me = new mongo.ObjectID(me);
} else {
me = me._id;
}
}
const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me)
? me as mongo.ObjectID
: typeof me === 'string'
? new mongo.ObjectID(me)
: (me as IUser)._id
: null;
// Rename _id to id
_user.id = _user._id;
@ -92,17 +92,17 @@ export default (
? `${config.drive_url}/${_user.banner_id}`
: null;
if (!me || !me.equals(_user.id) || !opts.detail) {
if (!meId || !meId.equals(_user.id) || !opts.detail) {
delete _user.avatar_id;
delete _user.banner_id;
delete _user.drive_capacity;
}
if (me && !me.equals(_user.id)) {
if (meId && !meId.equals(_user.id)) {
// If the user is following
const follow = await Following.findOne({
follower_id: me,
follower_id: meId,
followee_id: _user.id,
deleted_at: { $exists: false }
});
@ -111,7 +111,7 @@ export default (
// If the user is followed
const follow2 = await Following.findOne({
follower_id: _user.id,
followee_id: me,
followee_id: meId,
deleted_at: { $exists: false }
});
_user.is_followed = follow2 !== null;
@ -119,13 +119,13 @@ export default (
if (opts.detail) {
if (_user.pinned_post_id) {
_user.pinned_post = await serializePost(_user.pinned_post_id, me, {
_user.pinned_post = await serializePost(_user.pinned_post_id, meId, {
detail: true
});
}
if (me && !me.equals(_user.id)) {
const myFollowingIds = await getFriends(me);
if (meId && !meId.equals(_user.id)) {
const myFollowingIds = await getFriends(meId);
// Get following you know count
const followingYouKnowCount = await Following.count({