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

116 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-02-05 04:48:08 +02:00
import $ from 'cafy';
import ID, { transform } from '../../../../misc/cafy-id';
2018-04-07 20:30:37 +03:00
import Note from '../../../../models/note';
2019-02-22 07:06:17 +02:00
import { pack } from '../../../../models/user';
2018-11-02 06:47:44 +02:00
import define from '../../define';
import { maximum } from '../../../../prelude/array';
import { getHideUserIds } from '../../common/get-hide-users';
import { ApiError } from '../../error';
2019-02-22 07:02:56 +02:00
import { getUser } from '../../common/getters';
2017-09-08 17:29:33 +03:00
2018-11-01 20:32:24 +02:00
export const meta = {
tags: ['users'],
2018-11-01 20:32:24 +02:00
requireCredential: false,
params: {
userId: {
validator: $.type(ID),
transform: transform,
2018-11-03 15:49:36 +02:00
desc: {
'ja-JP': '対象のユーザーのID',
'en-US': 'Target user ID'
}
2018-11-01 20:32:24 +02:00
},
2017-09-08 17:29:33 +03:00
2018-11-01 20:32:24 +02:00
limit: {
2019-02-13 09:33:07 +02:00
validator: $.optional.num.range(1, 100),
2018-11-01 20:32:24 +02:00
default: 10
},
},
errors: {
noSuchUser: {
message: 'No such user.',
code: 'NO_SUCH_USER',
id: 'e6965129-7b2a-40a4-bae2-cd84cd434822'
}
2018-11-01 20:32:24 +02:00
}
};
export default define(meta, async (ps, me) => {
2017-09-08 17:29:33 +03:00
// Lookup user
2019-02-22 07:02:56 +02:00
const user = await getUser(ps.userId).catch(e => {
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
throw e;
2017-09-08 17:29:33 +03:00
});
2018-04-07 20:30:37 +03:00
// Fetch recent notes
const recentNotes = await Note.find({
2018-03-29 08:48:47 +03:00
userId: user._id,
replyId: {
2017-09-08 17:29:33 +03:00
$exists: true,
$ne: null
}
}, {
sort: {
_id: -1
},
limit: 1000,
fields: {
_id: false,
2018-03-29 08:48:47 +03:00
replyId: true
2017-09-08 17:29:33 +03:00
}
});
// 投稿が少なかったら中断
2018-04-07 20:30:37 +03:00
if (recentNotes.length === 0) {
return [];
2017-09-08 17:29:33 +03:00
}
const hideUserIds = await getHideUserIds(me);
hideUserIds.push(user._id);
2018-04-07 20:30:37 +03:00
const replyTargetNotes = await Note.find({
2017-09-08 17:29:33 +03:00
_id: {
2018-04-07 20:30:37 +03:00
$in: recentNotes.map(p => p.replyId)
2017-09-08 17:29:33 +03:00
},
2018-03-29 08:48:47 +03:00
userId: {
$nin: hideUserIds
2017-09-08 17:29:33 +03:00
}
}, {
fields: {
_id: false,
2018-03-29 08:48:47 +03:00
userId: true
2017-09-08 17:29:33 +03:00
}
});
2018-06-18 03:54:53 +03:00
const repliedUsers: any = {};
2017-09-08 17:29:33 +03:00
2018-04-07 20:30:37 +03:00
// Extract replies from recent notes
for (const userId of replyTargetNotes.map(x => x.userId.toString())) {
2017-09-08 17:29:33 +03:00
if (repliedUsers[userId]) {
repliedUsers[userId]++;
} else {
repliedUsers[userId] = 1;
}
}
2017-09-08 17:29:33 +03:00
// Calc peak
const peak = maximum(Object.values(repliedUsers));
2017-09-08 17:29:33 +03:00
// Sort replies by frequency
const repliedUsersSorted = Object.keys(repliedUsers).sort((a, b) => repliedUsers[b] - repliedUsers[a]);
2017-11-14 16:38:18 +02:00
// Extract top replied users
2018-11-01 20:32:24 +02:00
const topRepliedUsers = repliedUsersSorted.slice(0, ps.limit);
2017-09-08 17:29:33 +03:00
// Make replies object (includes weights)
const repliesObj = await Promise.all(topRepliedUsers.map(async (user) => ({
2018-02-02 01:21:30 +02:00
user: await pack(user, me, { detail: true }),
2017-09-08 17:29:33 +03:00
weight: repliedUsers[user] / peak
})));
return repliesObj;
});