efb0ffc4ec
* Fix API Schema Error * Delete SimpleSchema/SimpleObj and Move schemas to dedicated files * Userのスキーマを分割してみる * define packMany type * add , * Ensure enum schema and Make "as const" put once * test? * Revert "test?" This reverts commit97dc9bfa70
. * Revert "Fix API Schema Error" This reverts commit21b6176d97
. * ✌️ * clean up * test? * wip * wip * better schema def * ✌️ * fix * add minLength property * wip * wip * wip * anyOf/oneOf/allOfに対応? ~ relation.ts * refactor! * Define MinimumSchema * wip * wip * anyOf/oneOf/allOfが動作するようにUnionSchemaTypeを修正 * anyOf/oneOf/allOfが動作するようにUnionSchemaTypeを修正 * Update packages/backend/src/misc/schema.ts Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com> * fix * array oneOfをより正確な型に * array oneOfをより正確な型に * wip * ✌️ * なんかもういろいろ * remove * very good schema * api schema * wip * refactor: awaitAllの型定義を変えてみる * fix * specify types in awaitAll * specify types in awaitAll * ✌️ * wip * ... * ✌️ * AllowDateはやめておく * 不必要なoptional: false, nullable: falseを廃止 * Packedが展開されないように * 続packed * wip * define note type * wip * UserDetailedをMeDetailedかUserDetailedNotMeかを区別できるように * wip * wip * wip specify user type of other schemas * ok * convertSchemaToOpenApiSchemaを改修 * convertSchemaToOpenApiSchemaを改修 * Fix * fix * ✌️ * wip * 分割代入ではなくallOfで定義するように Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>
117 lines
2.5 KiB
TypeScript
117 lines
2.5 KiB
TypeScript
import $ from 'cafy';
|
|
import { ID } from '@/misc/cafy-id';
|
|
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,
|
|
|
|
params: {
|
|
userId: {
|
|
validator: $.type(ID),
|
|
},
|
|
|
|
limit: {
|
|
validator: $.optional.num.range(1, 100),
|
|
default: 10,
|
|
},
|
|
},
|
|
|
|
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;
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
|
export default define(meta, 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;
|
|
});
|