なんかもうめっちゃ変えた
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import { Not, In, IsNull } from 'typeorm';
|
||||
import { maximum } from '@/prelude/array.js';
|
||||
import { Notes, Users } from '@/models/index.js';
|
||||
import define from '../../define.js';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { maximum } from '@/misc/prelude/array.js';
|
||||
import { NotesRepository, UsersRepository } from '@/models/index.js';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { ApiError } from '../../error.js';
|
||||
import { getUser } from '../../common/getters.js';
|
||||
import { GetterService } from '../../common/GetterService.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['users'],
|
||||
@@ -51,64 +54,78 @@ export const paramDef = {
|
||||
} 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;
|
||||
});
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
constructor(
|
||||
@Inject(DI.usersRepository)
|
||||
private usersRepository: UsersRepository,
|
||||
|
||||
// Fetch recent notes
|
||||
const recentNotes = await Notes.find({
|
||||
where: {
|
||||
userId: user.id,
|
||||
replyId: Not(IsNull()),
|
||||
},
|
||||
order: {
|
||||
id: -1,
|
||||
},
|
||||
take: 1000,
|
||||
select: ['replyId'],
|
||||
});
|
||||
@Inject(DI.notesRepository)
|
||||
private notesRepository: NotesRepository,
|
||||
|
||||
// 投稿が少なかったら中断
|
||||
if (recentNotes.length === 0) {
|
||||
return [];
|
||||
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'],
|
||||
});
|
||||
|
||||
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 this.userEntityService.pack(user, me, { detail: true }),
|
||||
weight: repliedUsers[user] / peak,
|
||||
})));
|
||||
|
||||
return repliesObj;
|
||||
});
|
||||
}
|
||||
|
||||
// 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;
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user