2017-09-08 14:29:33 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2018-04-24 09:13:06 +00:00
|
|
|
import $ from 'cafy'; import ID from '../../../../cafy-id';
|
2018-04-07 17:30:37 +00:00
|
|
|
import Note from '../../../../models/note';
|
2018-03-29 11:32:18 +00:00
|
|
|
import User, { pack } from '../../../../models/user';
|
2017-09-08 14:29:33 +00:00
|
|
|
|
|
|
|
module.exports = (params, me) => new Promise(async (res, rej) => {
|
2018-03-29 05:48:47 +00:00
|
|
|
// Get 'userId' parameter
|
2018-05-02 09:06:16 +00:00
|
|
|
const [userId, userIdErr] = $.type(ID).get(params.userId);
|
2018-03-29 05:48:47 +00:00
|
|
|
if (userIdErr) return rej('invalid userId param');
|
2017-09-08 14:29:33 +00:00
|
|
|
|
2017-11-14 14:38:18 +00:00
|
|
|
// Get 'limit' parameter
|
2018-05-02 09:06:16 +00:00
|
|
|
const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit);
|
2017-11-14 14:38:18 +00:00
|
|
|
if (limitErr) return rej('invalid limit param');
|
|
|
|
|
2017-09-08 14:29:33 +00:00
|
|
|
// Lookup user
|
|
|
|
const user = await User.findOne({
|
|
|
|
_id: userId
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user === null) {
|
|
|
|
return rej('user not found');
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:30:37 +00:00
|
|
|
// Fetch recent notes
|
|
|
|
const recentNotes = await Note.find({
|
2018-03-29 05:48:47 +00:00
|
|
|
userId: user._id,
|
|
|
|
replyId: {
|
2017-09-08 14:29:33 +00:00
|
|
|
$exists: true,
|
|
|
|
$ne: null
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
},
|
|
|
|
limit: 1000,
|
|
|
|
fields: {
|
|
|
|
_id: false,
|
2018-03-29 05:48:47 +00:00
|
|
|
replyId: true
|
2017-09-08 14:29:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 投稿が少なかったら中断
|
2018-04-07 17:30:37 +00:00
|
|
|
if (recentNotes.length === 0) {
|
2017-09-08 14:29:33 +00:00
|
|
|
return res([]);
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:30:37 +00:00
|
|
|
const replyTargetNotes = await Note.find({
|
2017-09-08 14:29:33 +00:00
|
|
|
_id: {
|
2018-04-07 17:30:37 +00:00
|
|
|
$in: recentNotes.map(p => p.replyId)
|
2017-09-08 14:29:33 +00:00
|
|
|
},
|
2018-03-29 05:48:47 +00:00
|
|
|
userId: {
|
2017-09-08 14:29:33 +00:00
|
|
|
$ne: user._id
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: false,
|
2018-03-29 05:48:47 +00:00
|
|
|
userId: true
|
2017-09-08 14:29:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const repliedUsers = {};
|
|
|
|
|
2018-04-07 17:30:37 +00:00
|
|
|
// Extract replies from recent notes
|
|
|
|
replyTargetNotes.forEach(note => {
|
|
|
|
const userId = note.userId.toString();
|
2017-09-08 14:29:33 +00:00
|
|
|
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 14:38:18 +00:00
|
|
|
// Extract top replied users
|
|
|
|
const topRepliedUsers = repliedUsersSorted.slice(0, limit);
|
2017-09-08 14:29:33 +00:00
|
|
|
|
|
|
|
// Make replies object (includes weights)
|
|
|
|
const repliesObj = await Promise.all(topRepliedUsers.map(async (user) => ({
|
2018-02-01 23:21:30 +00:00
|
|
|
user: await pack(user, me, { detail: true }),
|
2017-09-08 14:29:33 +00:00
|
|
|
weight: repliedUsers[user] / peak
|
|
|
|
})));
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res(repliesObj);
|
|
|
|
});
|