2022-06-14 09:01:23 +00:00
|
|
|
import { Not, In, IsNull } from 'typeorm';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { maximum } from '@/misc/prelude/array.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { NotesRepository, UsersRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { ApiError } from '../../error.js';
|
2022-09-23 22:15:16 +00:00
|
|
|
import { GetterService } from '@/server/api/GetterService.js';
|
2017-09-08 14:29:33 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: false,
|
2018-11-01 18:32:24 +00:00
|
|
|
|
2022-06-10 05:25:20 +00:00
|
|
|
description: 'Get a list of other users that the specified user frequently replies to.',
|
|
|
|
|
2019-02-24 10:42:26 +00:00
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 10:42:26 +00:00
|
|
|
items: {
|
2022-01-18 13:27:10 +00: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 14:58:30 +00:00
|
|
|
},
|
2019-02-24 10:42:26 +00:00
|
|
|
},
|
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'e6965129-7b2a-40a4-bae2-cd84cd434822',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-11-01 18:32:24 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00: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-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private getterService: GetterService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
// Lookup user
|
|
|
|
const user = await this.getterService.getUser(ps.userId).catch(err => {
|
|
|
|
if (err.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fetch recent notes
|
|
|
|
const recentNotes = await this.notesRepository.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
replyId: Not(IsNull()),
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
id: -1,
|
|
|
|
},
|
|
|
|
take: 1000,
|
|
|
|
select: ['replyId'],
|
|
|
|
});
|
|
|
|
|
|
|
|
// 投稿が少なかったら中断
|
|
|
|
if (recentNotes.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO ミュートを考慮
|
|
|
|
const replyTargetNotes = await this.notesRepository.find({
|
|
|
|
where: {
|
|
|
|
id: In(recentNotes.map(p => p.replyId)),
|
|
|
|
},
|
|
|
|
select: ['userId'],
|
|
|
|
});
|
2017-09-08 14:29:33 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const repliedUsers: any = {};
|
2017-09-08 14:29:33 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Extract replies from recent notes
|
|
|
|
for (const userId of replyTargetNotes.map(x => x.userId.toString())) {
|
|
|
|
if (repliedUsers[userId]) {
|
|
|
|
repliedUsers[userId]++;
|
|
|
|
} else {
|
|
|
|
repliedUsers[userId] = 1;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 14:29:33 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Calc peak
|
|
|
|
const peak = maximum(Object.values(repliedUsers));
|
2017-09-08 14:29:33 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Sort replies by frequency
|
|
|
|
const repliedUsersSorted = Object.keys(repliedUsers).sort((a, b) => repliedUsers[b] - repliedUsers[a]);
|
2017-09-08 14:29:33 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Extract top replied users
|
|
|
|
const topRepliedUsers = repliedUsersSorted.slice(0, ps.limit);
|
2017-09-08 14:29:33 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Make replies object (includes weights)
|
|
|
|
const repliesObj = await Promise.all(topRepliedUsers.map(async (user) => ({
|
|
|
|
user: await this.userEntityService.pack(user, me, { detail: true }),
|
|
|
|
weight: repliedUsers[user] / peak,
|
|
|
|
})));
|
2017-09-08 14:29:33 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return repliesObj;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|