Sharkey/packages/backend/src/server/api/endpoints/users/get-frequently-replied-users.ts
syuilo 510de87607
refactor: use ajv instead of cafy (#8324)
* wip

* wip

* Update abuse-user-reports.ts

* Update files.ts

* Update list-remote.ts

* Update list.ts

* Update show-users.ts

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update update.ts

* Update search.ts

* Update reactions.ts

* Update search.ts

* wip

* wip

* wip

* wip

* Update update.ts

* Update relation.ts

* Update available.ts

* wip

* wip

* wip

* Update packages/backend/src/server/api/define.ts

Co-authored-by: Johann150 <johann.galle@protonmail.com>

* Update define.ts

* Update define.ts

* typo

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update update.ts

* wip

* Update signup.ts

* Update call.ts

* minimum for limit

* type

* remove needless annotation

* wip

* Update signup.ts

* wip

* wip

* fix

* Update create.ts

Co-authored-by: Johann150 <johann.galle@protonmail.com>
2022-02-19 14:05:32 +09:00

112 lines
2.5 KiB
TypeScript

import define from '../../define';
import { maximum } from '@/prelude/array';
import { ApiError } from '../../error';
import { getUser } from '../../common/getters';
import { Not, In, IsNull } from 'typeorm';
import { Notes, Users } from '@/models/index';
export const meta = {
tags: ['users'],
requireCredential: false,
res: {
type: 'array',
optional: false, nullable: false,
items: {
type: 'object',
optional: false, nullable: false,
properties: {
user: {
type: 'object',
optional: false, nullable: false,
ref: 'UserDetailed',
},
weight: {
type: 'number',
optional: false, nullable: false,
},
},
},
},
errors: {
noSuchUser: {
message: 'No such user.',
code: 'NO_SUCH_USER',
id: 'e6965129-7b2a-40a4-bae2-cd84cd434822',
},
},
} as const;
const paramDef = {
type: 'object',
properties: {
userId: { type: 'string', format: 'misskey:id' },
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
},
required: ['userId'],
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
// Lookup user
const user = await getUser(ps.userId).catch(e => {
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
throw e;
});
// Fetch recent notes
const recentNotes = await Notes.find({
where: {
userId: user.id,
replyId: Not(IsNull()),
},
order: {
id: -1,
},
take: 1000,
select: ['replyId'],
});
// 投稿が少なかったら中断
if (recentNotes.length === 0) {
return [];
}
// TODO ミュートを考慮
const replyTargetNotes = await Notes.find({
where: {
id: In(recentNotes.map(p => p.replyId)),
},
select: ['userId'],
});
const repliedUsers: any = {};
// Extract replies from recent notes
for (const userId of replyTargetNotes.map(x => x.userId.toString())) {
if (repliedUsers[userId]) {
repliedUsers[userId]++;
} else {
repliedUsers[userId] = 1;
}
}
// Calc peak
const peak = maximum(Object.values(repliedUsers));
// Sort replies by frequency
const repliedUsersSorted = Object.keys(repliedUsers).sort((a, b) => repliedUsers[b] - repliedUsers[a]);
// Extract top replied users
const topRepliedUsers = repliedUsersSorted.slice(0, ps.limit);
// Make replies object (includes weights)
const repliesObj = await Promise.all(topRepliedUsers.map(async (user) => ({
user: await Users.pack(user, me, { detail: true }),
weight: repliedUsers[user] / peak,
})));
return repliesObj;
});