misskey/src/server/api/endpoints/users/get_frequently_replied_users.ts

106 lines
2.1 KiB
TypeScript
Raw Normal View History

2018-11-02 03:32:24 +09:00
import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id';
2018-04-08 02:30:37 +09:00
import Note from '../../../../models/note';
2018-11-02 13:47:44 +09:00
import User, { pack } from '../../../../models/user';
import define from '../../define';
import { maximum } from '../../../../prelude/array';
2017-09-08 23:29:33 +09:00
2018-11-02 03:32:24 +09:00
export const meta = {
requireCredential: false,
params: {
userId: {
validator: $.type(ID),
transform: transform,
2018-11-03 22:49:36 +09:00
desc: {
'ja-JP': '対象のユーザーのID',
'en-US': 'Target user ID'
}
2018-11-02 03:32:24 +09:00
},
2017-09-08 23:29:33 +09:00
2018-11-02 03:32:24 +09:00
limit: {
validator: $.num.optional.range(1, 100),
default: 10
},
}
};
2018-11-02 13:47:44 +09:00
export default define(meta, (ps, me) => new Promise(async (res, rej) => {
2017-09-08 23:29:33 +09:00
// Lookup user
const user = await User.findOne({
2018-11-02 03:32:24 +09:00
_id: ps.userId
2017-09-08 23:29:33 +09:00
}, {
fields: {
_id: true
}
});
if (user === null) {
return rej('user not found');
}
2018-04-08 02:30:37 +09:00
// Fetch recent notes
const recentNotes = await Note.find({
2018-03-29 14:48:47 +09:00
userId: user._id,
replyId: {
2017-09-08 23:29:33 +09:00
$exists: true,
$ne: null
}
}, {
sort: {
_id: -1
},
limit: 1000,
fields: {
_id: false,
2018-03-29 14:48:47 +09:00
replyId: true
2017-09-08 23:29:33 +09:00
}
});
// 投稿が少なかったら中断
2018-04-08 02:30:37 +09:00
if (recentNotes.length === 0) {
2017-09-08 23:29:33 +09:00
return res([]);
}
2018-04-08 02:30:37 +09:00
const replyTargetNotes = await Note.find({
2017-09-08 23:29:33 +09:00
_id: {
2018-04-08 02:30:37 +09:00
$in: recentNotes.map(p => p.replyId)
2017-09-08 23:29:33 +09:00
},
2018-03-29 14:48:47 +09:00
userId: {
2017-09-08 23:29:33 +09:00
$ne: user._id
}
}, {
fields: {
_id: false,
2018-03-29 14:48:47 +09:00
userId: true
2017-09-08 23:29:33 +09:00
}
});
2018-06-18 09:54:53 +09:00
const repliedUsers: any = {};
2017-09-08 23:29:33 +09:00
2018-04-08 02:30:37 +09:00
// Extract replies from recent notes
for (const userId of replyTargetNotes.map(x => x.userId.toString())) {
2017-09-08 23:29:33 +09:00
if (repliedUsers[userId]) {
repliedUsers[userId]++;
} else {
repliedUsers[userId] = 1;
}
}
2017-09-08 23:29:33 +09:00
// Calc peak
const peak = maximum(Object.values(repliedUsers));
2017-09-08 23:29:33 +09:00
// Sort replies by frequency
const repliedUsersSorted = Object.keys(repliedUsers).sort((a, b) => repliedUsers[b] - repliedUsers[a]);
2017-11-14 23:38:18 +09:00
// Extract top replied users
2018-11-02 03:32:24 +09:00
const topRepliedUsers = repliedUsersSorted.slice(0, ps.limit);
2017-09-08 23:29:33 +09:00
// Make replies object (includes weights)
const repliesObj = await Promise.all(topRepliedUsers.map(async (user) => ({
2018-02-02 08:21:30 +09:00
user: await pack(user, me, { detail: true }),
2017-09-08 23:29:33 +09:00
weight: repliedUsers[user] / peak
})));
res(repliesObj);
2018-11-02 13:47:44 +09:00
}));