2022-09-17 20:27:08 +02:00
|
|
|
import { In, IsNull } from 'typeorm';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { UsersRepository } from '@/models/index.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2022-12-04 02:16:03 +01:00
|
|
|
import { RemoteUserResolveService } from '@/core/RemoteUserResolveService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-01 09:45:49 +01:00
|
|
|
import PerUserPvChart from '@/core/chart/charts/per-user-pv.js';
|
2023-01-12 13:02:26 +01:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { ApiError } from '../../error.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { ApiLoggerService } from '../../ApiLoggerService.js';
|
|
|
|
import type { FindOptionsWhere } from 'typeorm';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2018-03-27 09:51:12 +02:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Show the properties of a user.',
|
|
|
|
|
2019-02-23 03:20:58 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
optional: false, nullable: false,
|
|
|
|
oneOf: [
|
|
|
|
{
|
|
|
|
type: 'object',
|
|
|
|
ref: 'UserDetailed',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'array',
|
|
|
|
items: {
|
|
|
|
type: 'object',
|
|
|
|
ref: 'UserDetailed',
|
2022-04-05 17:04:25 +02:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
},
|
2022-04-05 17:04:25 +02:00
|
|
|
],
|
2019-02-23 03:20:58 +01:00
|
|
|
},
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
|
|
|
failedToResolveRemoteUser: {
|
|
|
|
message: 'Failed to resolve remote user.',
|
|
|
|
code: 'FAILED_TO_RESOLVE_REMOTE_USER',
|
|
|
|
id: 'ef7b9be4-9cba-4e6f-ab41-90ed171c7d3c',
|
2022-01-18 14:27:10 +01:00
|
|
|
kind: 'server',
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '4362f8dc-731f-4ad8-a694-be5a88922a24',
|
2023-03-20 06:26:11 +01:00
|
|
|
httpStatusCode: 404,
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-11-01 19:32:24 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
2023-02-26 03:28:05 +01:00
|
|
|
properties: {
|
|
|
|
userId: { type: 'string', format: 'misskey:id' },
|
|
|
|
userIds: { type: 'array', uniqueItems: true, items: {
|
|
|
|
type: 'string', format: 'misskey:id',
|
|
|
|
} },
|
|
|
|
username: { type: 'string' },
|
|
|
|
host: {
|
|
|
|
type: 'string',
|
|
|
|
nullable: true,
|
|
|
|
description: 'The local host is represented with `null`.',
|
2022-04-03 06:57:26 +02:00
|
|
|
},
|
2023-02-26 03:28:05 +01:00
|
|
|
},
|
|
|
|
anyOf: [
|
|
|
|
{ required: ['userId'] },
|
|
|
|
{ required: ['userIds'] },
|
|
|
|
{ required: ['username'] },
|
2022-04-03 06:57:26 +02:00
|
|
|
],
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
2022-12-04 02:16:03 +01:00
|
|
|
private remoteUserResolveService: RemoteUserResolveService,
|
2023-01-12 13:02:26 +01:00
|
|
|
private roleService: RoleService,
|
2023-01-01 09:45:49 +01:00
|
|
|
private perUserPvChart: PerUserPvChart,
|
2022-09-17 20:27:08 +02:00
|
|
|
private apiLoggerService: ApiLoggerService,
|
|
|
|
) {
|
2023-01-01 09:45:49 +01:00
|
|
|
super(meta, paramDef, async (ps, me, _1, _2, _3, ip) => {
|
2022-09-17 20:27:08 +02:00
|
|
|
let user;
|
|
|
|
|
2023-01-12 13:02:26 +01:00
|
|
|
const isModerator = await this.roleService.isModerator(me);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (ps.userIds) {
|
|
|
|
if (ps.userIds.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:02:26 +01:00
|
|
|
const users = await this.usersRepository.findBy(isModerator ? {
|
2022-09-17 20:27:08 +02:00
|
|
|
id: In(ps.userIds),
|
|
|
|
} : {
|
|
|
|
id: In(ps.userIds),
|
|
|
|
isSuspended: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// リクエストされた通りに並べ替え
|
|
|
|
const _users: User[] = [];
|
|
|
|
for (const id of ps.userIds) {
|
|
|
|
_users.push(users.find(x => x.id === id)!);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await Promise.all(_users.map(u => this.userEntityService.pack(u, me, {
|
|
|
|
detail: true,
|
|
|
|
})));
|
|
|
|
} else {
|
|
|
|
// Lookup user
|
|
|
|
if (typeof ps.host === 'string' && typeof ps.username === 'string') {
|
2022-12-04 02:16:03 +01:00
|
|
|
user = await this.remoteUserResolveService.resolveUser(ps.username, ps.host).catch(err => {
|
2022-09-17 20:27:08 +02:00
|
|
|
this.apiLoggerService.logger.warn(`failed to resolve remote user: ${err}`);
|
|
|
|
throw new ApiError(meta.errors.failedToResolveRemoteUser);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const q: FindOptionsWhere<User> = ps.userId != null
|
|
|
|
? { id: ps.userId }
|
|
|
|
: { usernameLower: ps.username!.toLowerCase(), host: IsNull() };
|
|
|
|
|
|
|
|
user = await this.usersRepository.findOneBy(q);
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:02:26 +01:00
|
|
|
if (user == null || (!isModerator && user.isSuspended)) {
|
2022-09-17 20:27:08 +02:00
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
|
|
|
}
|
|
|
|
|
2023-01-03 00:29:25 +01:00
|
|
|
if (user.host == null) {
|
|
|
|
if (me == null && ip != null) {
|
|
|
|
this.perUserPvChart.commitByVisitor(user, ip);
|
|
|
|
} else if (me && me.id !== user.id) {
|
|
|
|
this.perUserPvChart.commitByUser(user, me.id);
|
|
|
|
}
|
2023-01-01 09:45:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
return await this.userEntityService.pack(user, me, {
|
|
|
|
detail: true,
|
|
|
|
});
|
|
|
|
}
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|
2018-04-25 15:37:08 +02:00
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|