2021-08-19 21:55:45 +09:00
|
|
|
import define from '../../define';
|
2021-08-19 22:04:15 +09:00
|
|
|
import { maximum } from '@/prelude/array';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { ApiError } from '../../error';
|
|
|
|
import { getUser } from '../../common/getters';
|
2019-05-20 21:44:16 +09:00
|
|
|
import { Not, In, IsNull } from 'typeorm';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { Notes, Users } from '@/models/index';
|
2017-09-08 23:29:33 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: false,
|
2018-11-02 03:32:24 +09:00
|
|
|
|
2019-02-24 19:42:26 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 19:42:26 +09:00
|
|
|
items: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
properties: {
|
|
|
|
user: {
|
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
ref: 'UserDetailed',
|
|
|
|
},
|
|
|
|
weight: {
|
|
|
|
type: 'number',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2019-02-24 19:42:26 +09:00
|
|
|
},
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: 'e6965129-7b2a-40a4-bae2-cd84cd434822',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2018-11-02 03:32:24 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
userId: { type: 'string', format: 'misskey:id' },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
},
|
|
|
|
required: ['userId'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 14:05:32 +09:00
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
2017-09-08 23:29:33 +09:00
|
|
|
// Lookup user
|
2019-02-22 14:02:56 +09: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 23:29:33 +09:00
|
|
|
});
|
|
|
|
|
2018-04-08 02:30:37 +09:00
|
|
|
// Fetch recent notes
|
2019-04-07 21:50:36 +09:00
|
|
|
const recentNotes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
2021-12-09 23:58:30 +09:00
|
|
|
replyId: Not(IsNull()),
|
2017-09-08 23:29:33 +09:00
|
|
|
},
|
2019-04-07 21:50:36 +09:00
|
|
|
order: {
|
2021-12-09 23:58:30 +09:00
|
|
|
id: -1,
|
2019-04-07 21:50:36 +09:00
|
|
|
},
|
|
|
|
take: 1000,
|
2021-12-09 23:58:30 +09:00
|
|
|
select: ['replyId'],
|
2017-09-08 23:29:33 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// 投稿が少なかったら中断
|
2018-04-08 02:30:37 +09:00
|
|
|
if (recentNotes.length === 0) {
|
2019-02-22 11:46:58 +09:00
|
|
|
return [];
|
2017-09-08 23:29:33 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
// TODO ミュートを考慮
|
|
|
|
const replyTargetNotes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
id: In(recentNotes.map(p => p.replyId)),
|
2017-09-08 23:29:33 +09:00
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
select: ['userId'],
|
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
|
2018-12-11 20:36:55 +09:00
|
|
|
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;
|
|
|
|
}
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2017-09-08 23:29:33 +09:00
|
|
|
|
|
|
|
// Calc peak
|
2018-12-11 20:36:55 +09:00
|
|
|
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
|
2022-02-19 14:05:32 +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) => ({
|
2019-04-07 21:50:36 +09:00
|
|
|
user: await Users.pack(user, me, { detail: true }),
|
2021-12-09 23:58:30 +09:00
|
|
|
weight: repliedUsers[user] / peak,
|
2017-09-08 23:29:33 +09:00
|
|
|
})));
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
return repliesObj;
|
|
|
|
});
|