serializers - user: run promises in parallel as possible
This commit is contained in:
parent
11190f56ad
commit
5aa5e5cc70
|
@ -12,7 +12,7 @@ import serializeChannel from './channel';
|
|||
import serializeUser from './user';
|
||||
import serializeDriveFile from './drive-file';
|
||||
import parse from '../common/text';
|
||||
import rap from '@prezzemolo/rap'
|
||||
import rap from '@prezzemolo/rap';
|
||||
|
||||
/**
|
||||
* Serialize a post
|
||||
|
|
|
@ -8,6 +8,7 @@ import serializePost from './post';
|
|||
import Following from '../models/following';
|
||||
import getFriends from '../common/get-friends';
|
||||
import config from '../../conf';
|
||||
import rap from '@prezzemolo/rap';
|
||||
|
||||
/**
|
||||
* Serialize a user
|
||||
|
@ -104,26 +105,30 @@ export default (
|
|||
|
||||
if (meId && !meId.equals(_user.id)) {
|
||||
// If the user is following
|
||||
_user.is_following = (async () => {
|
||||
const follow = await Following.findOne({
|
||||
follower_id: meId,
|
||||
followee_id: _user.id,
|
||||
deleted_at: { $exists: false }
|
||||
});
|
||||
_user.is_following = follow !== null;
|
||||
return follow !== null;
|
||||
})()
|
||||
|
||||
// If the user is followed
|
||||
_user.is_followed = (async () => {
|
||||
const follow2 = await Following.findOne({
|
||||
follower_id: _user.id,
|
||||
followee_id: meId,
|
||||
deleted_at: { $exists: false }
|
||||
});
|
||||
_user.is_followed = follow2 !== null;
|
||||
return follow2 !== null;
|
||||
})()
|
||||
}
|
||||
|
||||
if (opts.detail) {
|
||||
if (_user.pinned_post_id) {
|
||||
// Populate pinned post
|
||||
_user.pinned_post = await serializePost(_user.pinned_post_id, meId, {
|
||||
_user.pinned_post = serializePost(_user.pinned_post_id, meId, {
|
||||
detail: true
|
||||
});
|
||||
}
|
||||
|
@ -132,23 +137,24 @@ export default (
|
|||
const myFollowingIds = await getFriends(meId);
|
||||
|
||||
// Get following you know count
|
||||
const followingYouKnowCount = await Following.count({
|
||||
_user.following_you_know_count = 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({
|
||||
_user.followers_you_know_count = Following.count({
|
||||
followee_id: _user.id,
|
||||
follower_id: { $in: myFollowingIds },
|
||||
deleted_at: { $exists: false }
|
||||
});
|
||||
_user.followers_you_know_count = followersYouKnowCount;
|
||||
}
|
||||
}
|
||||
|
||||
// resolve promises in _user object
|
||||
_user = await rap(_user)
|
||||
|
||||
resolve(_user);
|
||||
});
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue