serializers - user: run promises in parallel as possible

This commit is contained in:
otofune 2017-11-05 22:22:49 +09:00
parent 11190f56ad
commit 5aa5e5cc70
2 changed files with 24 additions and 18 deletions

View File

@ -12,7 +12,7 @@ import serializeChannel from './channel';
import serializeUser from './user'; import serializeUser from './user';
import serializeDriveFile from './drive-file'; import serializeDriveFile from './drive-file';
import parse from '../common/text'; import parse from '../common/text';
import rap from '@prezzemolo/rap' import rap from '@prezzemolo/rap';
/** /**
* Serialize a post * Serialize a post

View File

@ -8,6 +8,7 @@ import serializePost from './post';
import Following from '../models/following'; import Following from '../models/following';
import getFriends from '../common/get-friends'; import getFriends from '../common/get-friends';
import config from '../../conf'; import config from '../../conf';
import rap from '@prezzemolo/rap';
/** /**
* Serialize a user * Serialize a user
@ -104,26 +105,30 @@ export default (
if (meId && !meId.equals(_user.id)) { if (meId && !meId.equals(_user.id)) {
// If the user is following // If the user is following
_user.is_following = (async () => {
const follow = await Following.findOne({ const follow = await Following.findOne({
follower_id: meId, follower_id: meId,
followee_id: _user.id, followee_id: _user.id,
deleted_at: { $exists: false } deleted_at: { $exists: false }
}); });
_user.is_following = follow !== null; return follow !== null;
})()
// If the user is followed // If the user is followed
_user.is_followed = (async () => {
const follow2 = await Following.findOne({ const follow2 = await Following.findOne({
follower_id: _user.id, follower_id: _user.id,
followee_id: meId, followee_id: meId,
deleted_at: { $exists: false } deleted_at: { $exists: false }
}); });
_user.is_followed = follow2 !== null; return follow2 !== null;
})()
} }
if (opts.detail) { if (opts.detail) {
if (_user.pinned_post_id) { if (_user.pinned_post_id) {
// Populate pinned post // Populate pinned post
_user.pinned_post = await serializePost(_user.pinned_post_id, meId, { _user.pinned_post = serializePost(_user.pinned_post_id, meId, {
detail: true detail: true
}); });
} }
@ -132,23 +137,24 @@ export default (
const myFollowingIds = await getFriends(meId); const myFollowingIds = await getFriends(meId);
// Get following you know count // Get following you know count
const followingYouKnowCount = await Following.count({ _user.following_you_know_count = Following.count({
followee_id: { $in: myFollowingIds }, followee_id: { $in: myFollowingIds },
follower_id: _user.id, follower_id: _user.id,
deleted_at: { $exists: false } deleted_at: { $exists: false }
}); });
_user.following_you_know_count = followingYouKnowCount;
// Get followers you know count // Get followers you know count
const followersYouKnowCount = await Following.count({ _user.followers_you_know_count = Following.count({
followee_id: _user.id, followee_id: _user.id,
follower_id: { $in: myFollowingIds }, follower_id: { $in: myFollowingIds },
deleted_at: { $exists: false } deleted_at: { $exists: false }
}); });
_user.followers_you_know_count = followersYouKnowCount;
} }
} }
// resolve promises in _user object
_user = await rap(_user)
resolve(_user); resolve(_user);
}); });
/* /*