2017-09-08 09:29:33 -05:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import $ from 'cafy';
|
|
|
|
import Post from '../../models/post';
|
|
|
|
import User from '../../models/user';
|
2018-02-01 17:21:30 -06:00
|
|
|
import { pack } from '../../models/user';
|
2017-09-08 09:29:33 -05:00
|
|
|
|
|
|
|
module.exports = (params, me) => new Promise(async (res, rej) => {
|
|
|
|
// Get 'user_id' parameter
|
|
|
|
const [userId, userIdErr] = $(params.user_id).id().$;
|
|
|
|
if (userIdErr) return rej('invalid user_id param');
|
|
|
|
|
2017-11-14 08:38:18 -06:00
|
|
|
// Get 'limit' parameter
|
|
|
|
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
|
|
|
|
if (limitErr) return rej('invalid limit param');
|
|
|
|
|
2017-09-08 09:29:33 -05:00
|
|
|
// Lookup user
|
|
|
|
const user = await User.findOne({
|
|
|
|
_id: userId
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user === null) {
|
|
|
|
return rej('user not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch recent posts
|
|
|
|
const recentPosts = await Post.find({
|
|
|
|
user_id: user._id,
|
2017-10-31 20:22:40 -05:00
|
|
|
reply_id: {
|
2017-09-08 09:29:33 -05:00
|
|
|
$exists: true,
|
|
|
|
$ne: null
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
},
|
|
|
|
limit: 1000,
|
|
|
|
fields: {
|
|
|
|
_id: false,
|
2017-10-31 20:22:40 -05:00
|
|
|
reply_id: true
|
2017-09-08 09:29:33 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 投稿が少なかったら中断
|
|
|
|
if (recentPosts.length === 0) {
|
|
|
|
return res([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const replyTargetPosts = await Post.find({
|
|
|
|
_id: {
|
2017-10-31 20:22:40 -05:00
|
|
|
$in: recentPosts.map(p => p.reply_id)
|
2017-09-08 09:29:33 -05:00
|
|
|
},
|
|
|
|
user_id: {
|
|
|
|
$ne: user._id
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: false,
|
|
|
|
user_id: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const repliedUsers = {};
|
|
|
|
|
|
|
|
// Extract replies from recent posts
|
|
|
|
replyTargetPosts.forEach(post => {
|
|
|
|
const userId = post.user_id.toString();
|
|
|
|
if (repliedUsers[userId]) {
|
|
|
|
repliedUsers[userId]++;
|
|
|
|
} else {
|
|
|
|
repliedUsers[userId] = 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Calc peak
|
|
|
|
let peak = 0;
|
|
|
|
Object.keys(repliedUsers).forEach(user => {
|
|
|
|
if (repliedUsers[user] > peak) peak = repliedUsers[user];
|
|
|
|
});
|
|
|
|
|
|
|
|
// Sort replies by frequency
|
|
|
|
const repliedUsersSorted = Object.keys(repliedUsers).sort((a, b) => repliedUsers[b] - repliedUsers[a]);
|
|
|
|
|
2017-11-14 08:38:18 -06:00
|
|
|
// Extract top replied users
|
|
|
|
const topRepliedUsers = repliedUsersSorted.slice(0, limit);
|
2017-09-08 09:29:33 -05:00
|
|
|
|
|
|
|
// Make replies object (includes weights)
|
|
|
|
const repliesObj = await Promise.all(topRepliedUsers.map(async (user) => ({
|
2018-02-01 17:21:30 -06:00
|
|
|
user: await pack(user, me, { detail: true }),
|
2017-09-08 09:29:33 -05:00
|
|
|
weight: repliedUsers[user] / peak
|
|
|
|
})));
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res(repliesObj);
|
|
|
|
});
|