2019-02-05 03:48:08 +01:00
|
|
|
import $ from 'cafy';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { ID } from '../../../../misc/cafy-id';
|
2018-11-02 05:47:44 +01:00
|
|
|
import define from '../../define';
|
2018-12-11 12:36:55 +01:00
|
|
|
import { maximum } from '../../../../prelude/array';
|
2019-02-22 03:46:58 +01:00
|
|
|
import { ApiError } from '../../error';
|
2019-02-22 06:02:56 +01:00
|
|
|
import { getUser } from '../../common/getters';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Not, In } from 'typeorm';
|
|
|
|
import { Notes, Users } from '../../../../models';
|
2019-04-23 15:35:26 +02:00
|
|
|
import { types, bool } from '../../../../misc/schema';
|
2017-09-08 16:29:33 +02:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['users'],
|
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
userId: {
|
|
|
|
validator: $.type(ID),
|
2018-11-03 14:49:36 +01:00
|
|
|
desc: {
|
|
|
|
'ja-JP': '対象のユーザーのID',
|
|
|
|
'en-US': 'Target user ID'
|
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2017-09-08 16:29:33 +02:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
limit: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-01 19:32:24 +01:00
|
|
|
default: 10
|
|
|
|
},
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
2019-02-24 11:42:26 +01:00
|
|
|
res: {
|
2019-04-23 15:35:26 +02:00
|
|
|
type: types.array,
|
|
|
|
optional: bool.false, nullable: bool.false,
|
2019-02-24 11:42:26 +01:00
|
|
|
items: {
|
2019-04-23 15:35:26 +02:00
|
|
|
type: types.object,
|
|
|
|
optional: bool.false, nullable: bool.false,
|
|
|
|
ref: 'User',
|
2019-02-24 11:42:26 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
|
|
|
id: 'e6965129-7b2a-40a4-bae2-cd84cd434822'
|
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
export default define(meta, async (ps, me) => {
|
2017-09-08 16:29:33 +02:00
|
|
|
// Lookup user
|
2019-02-22 06:02:56 +01: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 16:29:33 +02:00
|
|
|
});
|
|
|
|
|
2018-04-07 19:30:37 +02:00
|
|
|
// Fetch recent notes
|
2019-04-07 14:50:36 +02:00
|
|
|
const recentNotes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
replyId: Not(null)
|
2017-09-08 16:29:33 +02:00
|
|
|
},
|
2019-04-07 14:50:36 +02:00
|
|
|
order: {
|
|
|
|
id: -1
|
|
|
|
},
|
|
|
|
take: 1000,
|
|
|
|
select: ['replyId']
|
2017-09-08 16:29:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// 投稿が少なかったら中断
|
2018-04-07 19:30:37 +02:00
|
|
|
if (recentNotes.length === 0) {
|
2019-02-22 03:46:58 +01:00
|
|
|
return [];
|
2017-09-08 16:29:33 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
// TODO ミュートを考慮
|
|
|
|
const replyTargetNotes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
id: In(recentNotes.map(p => p.replyId)),
|
2017-09-08 16:29:33 +02:00
|
|
|
},
|
2019-04-07 14:50:36 +02:00
|
|
|
select: ['userId']
|
2017-09-08 16:29:33 +02:00
|
|
|
});
|
|
|
|
|
2018-06-18 02:54:53 +02:00
|
|
|
const repliedUsers: any = {};
|
2017-09-08 16:29:33 +02:00
|
|
|
|
2018-04-07 19:30:37 +02:00
|
|
|
// Extract replies from recent notes
|
2018-12-11 12:36:55 +01:00
|
|
|
for (const userId of replyTargetNotes.map(x => x.userId.toString())) {
|
2017-09-08 16:29:33 +02:00
|
|
|
if (repliedUsers[userId]) {
|
|
|
|
repliedUsers[userId]++;
|
|
|
|
} else {
|
|
|
|
repliedUsers[userId] = 1;
|
|
|
|
}
|
2018-12-11 12:36:55 +01:00
|
|
|
}
|
2017-09-08 16:29:33 +02:00
|
|
|
|
|
|
|
// Calc peak
|
2018-12-11 12:36:55 +01:00
|
|
|
const peak = maximum(Object.values(repliedUsers));
|
2017-09-08 16:29:33 +02:00
|
|
|
|
|
|
|
// Sort replies by frequency
|
|
|
|
const repliedUsersSorted = Object.keys(repliedUsers).sort((a, b) => repliedUsers[b] - repliedUsers[a]);
|
|
|
|
|
2017-11-14 15:38:18 +01:00
|
|
|
// Extract top replied users
|
2019-04-12 18:43:22 +02:00
|
|
|
const topRepliedUsers = repliedUsersSorted.slice(0, ps.limit!);
|
2017-09-08 16:29:33 +02:00
|
|
|
|
|
|
|
// Make replies object (includes weights)
|
|
|
|
const repliesObj = await Promise.all(topRepliedUsers.map(async (user) => ({
|
2019-04-07 14:50:36 +02:00
|
|
|
user: await Users.pack(user, me, { detail: true }),
|
2017-09-08 16:29:33 +02:00
|
|
|
weight: repliedUsers[user] / peak
|
|
|
|
})));
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
return repliesObj;
|
|
|
|
});
|