2019-02-04 20:48:08 -06:00
|
|
|
import $ from 'cafy';
|
|
|
|
import ID, { transform } from '../../../../misc/cafy-id';
|
2018-04-07 12:30:37 -05:00
|
|
|
import Note from '../../../../models/note';
|
2018-11-01 23:47:44 -05:00
|
|
|
import User, { pack } from '../../../../models/user';
|
|
|
|
import define from '../../define';
|
2018-12-11 05:36:55 -06:00
|
|
|
import { maximum } from '../../../../prelude/array';
|
2019-02-12 02:19:15 -06:00
|
|
|
import { getHideUserIds } from '../../common/get-hide-users';
|
2019-02-21 20:46:58 -06:00
|
|
|
import { ApiError } from '../../error';
|
2017-09-08 09:29:33 -05:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
export const meta = {
|
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
userId: {
|
|
|
|
validator: $.type(ID),
|
|
|
|
transform: transform,
|
2018-11-03 08:49:36 -05:00
|
|
|
desc: {
|
|
|
|
'ja-JP': '対象のユーザーのID',
|
|
|
|
'en-US': 'Target user ID'
|
|
|
|
}
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2017-09-08 09:29:33 -05:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
limit: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-01 13:32:24 -05:00
|
|
|
default: 10
|
|
|
|
},
|
2019-02-21 20:46:58 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
|
|
|
id: 'e6965129-7b2a-40a4-bae2-cd84cd434822'
|
|
|
|
}
|
2018-11-01 13:32:24 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-21 20:46:58 -06:00
|
|
|
export default define(meta, async (ps, me) => {
|
2017-09-08 09:29:33 -05:00
|
|
|
// Lookup user
|
|
|
|
const user = await User.findOne({
|
2018-11-01 13:32:24 -05:00
|
|
|
_id: ps.userId
|
2017-09-08 09:29:33 -05:00
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user === null) {
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2017-09-08 09:29:33 -05:00
|
|
|
}
|
|
|
|
|
2018-04-07 12:30:37 -05:00
|
|
|
// Fetch recent notes
|
|
|
|
const recentNotes = await Note.find({
|
2018-03-29 00:48:47 -05:00
|
|
|
userId: user._id,
|
|
|
|
replyId: {
|
2017-09-08 09:29:33 -05:00
|
|
|
$exists: true,
|
|
|
|
$ne: null
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
},
|
|
|
|
limit: 1000,
|
|
|
|
fields: {
|
|
|
|
_id: false,
|
2018-03-29 00:48:47 -05:00
|
|
|
replyId: true
|
2017-09-08 09:29:33 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 投稿が少なかったら中断
|
2018-04-07 12:30:37 -05:00
|
|
|
if (recentNotes.length === 0) {
|
2019-02-21 20:46:58 -06:00
|
|
|
return [];
|
2017-09-08 09:29:33 -05:00
|
|
|
}
|
|
|
|
|
2019-02-12 02:19:15 -06:00
|
|
|
const hideUserIds = await getHideUserIds(me);
|
|
|
|
hideUserIds.push(user._id);
|
|
|
|
|
2018-04-07 12:30:37 -05:00
|
|
|
const replyTargetNotes = await Note.find({
|
2017-09-08 09:29:33 -05:00
|
|
|
_id: {
|
2018-04-07 12:30:37 -05:00
|
|
|
$in: recentNotes.map(p => p.replyId)
|
2017-09-08 09:29:33 -05:00
|
|
|
},
|
2018-03-29 00:48:47 -05:00
|
|
|
userId: {
|
2019-02-12 02:19:15 -06:00
|
|
|
$nin: hideUserIds
|
2017-09-08 09:29:33 -05:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: false,
|
2018-03-29 00:48:47 -05:00
|
|
|
userId: true
|
2017-09-08 09:29:33 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-17 19:54:53 -05:00
|
|
|
const repliedUsers: any = {};
|
2017-09-08 09:29:33 -05:00
|
|
|
|
2018-04-07 12:30:37 -05:00
|
|
|
// Extract replies from recent notes
|
2018-12-11 05:36:55 -06:00
|
|
|
for (const userId of replyTargetNotes.map(x => x.userId.toString())) {
|
2017-09-08 09:29:33 -05:00
|
|
|
if (repliedUsers[userId]) {
|
|
|
|
repliedUsers[userId]++;
|
|
|
|
} else {
|
|
|
|
repliedUsers[userId] = 1;
|
|
|
|
}
|
2018-12-11 05:36:55 -06:00
|
|
|
}
|
2017-09-08 09:29:33 -05:00
|
|
|
|
|
|
|
// Calc peak
|
2018-12-11 05:36:55 -06:00
|
|
|
const peak = maximum(Object.values(repliedUsers));
|
2017-09-08 09:29:33 -05:00
|
|
|
|
|
|
|
// 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
|
2018-11-01 13:32:24 -05:00
|
|
|
const topRepliedUsers = repliedUsersSorted.slice(0, ps.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
|
|
|
|
})));
|
|
|
|
|
2019-02-21 20:46:58 -06:00
|
|
|
return repliesObj;
|
|
|
|
});
|