2022-02-27 04:07:39 +02:00
|
|
|
import { resolveUser } from '@/remote/resolve-user.js';
|
|
|
|
import define from '../../define.js';
|
|
|
|
import { apiLogger } from '../../logger.js';
|
|
|
|
import { ApiError } from '../../error.js';
|
|
|
|
import { Users } from '@/models/index.js';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { In } from 'typeorm';
|
2022-02-27 04:07:39 +02:00
|
|
|
import { User } from '@/models/entities/user.js';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
export const meta = {
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
requireCredential: false,
|
2018-03-27 10:51:12 +03:00
|
|
|
|
2019-02-23 04:20:58 +02:00
|
|
|
res: {
|
2022-01-18 15:27:10 +02:00
|
|
|
optional: false, nullable: false,
|
|
|
|
oneOf: [
|
|
|
|
{
|
|
|
|
type: 'object',
|
|
|
|
ref: 'UserDetailed',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'array',
|
|
|
|
items: {
|
|
|
|
type: 'object',
|
|
|
|
ref: 'UserDetailed',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
2019-02-23 04:20:58 +02:00
|
|
|
},
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
errors: {
|
|
|
|
failedToResolveRemoteUser: {
|
|
|
|
message: 'Failed to resolve remote user.',
|
|
|
|
code: 'FAILED_TO_RESOLVE_REMOTE_USER',
|
|
|
|
id: 'ef7b9be4-9cba-4e6f-ab41-90ed171c7d3c',
|
2022-01-18 15:27:10 +02:00
|
|
|
kind: 'server',
|
2019-02-22 04:46:58 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: '4362f8dc-731f-4ad8-a694-be5a88922a24',
|
2019-02-22 04:46:58 +02:00
|
|
|
},
|
2021-12-09 16:58:30 +02:00
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
} as const;
|
2018-11-01 20:32:24 +02:00
|
|
|
|
2022-02-20 06:15:40 +02:00
|
|
|
export const paramDef = {
|
2022-02-19 07:05:32 +02:00
|
|
|
type: 'object',
|
|
|
|
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 },
|
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 19:12:50 +02:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 07:05:32 +02:00
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
2018-11-01 20:32:24 +02:00
|
|
|
let user;
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2020-01-01 19:47:20 +02:00
|
|
|
const isAdminOrModerator = me && (me.isAdmin || me.isModerator);
|
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
if (ps.userIds) {
|
2019-04-15 06:54:42 +03:00
|
|
|
if (ps.userIds.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:47:20 +02:00
|
|
|
const users = await Users.find(isAdminOrModerator ? {
|
2021-12-09 16:58:30 +02:00
|
|
|
id: In(ps.userIds),
|
2020-01-01 19:47:20 +02:00
|
|
|
} : {
|
|
|
|
id: In(ps.userIds),
|
2021-12-09 16:58:30 +02:00
|
|
|
isSuspended: false,
|
2018-04-25 16:37:08 +03:00
|
|
|
});
|
2017-02-22 06:08:33 +02:00
|
|
|
|
2020-11-08 05:40:31 +02:00
|
|
|
// リクエストされた通りに並べ替え
|
2021-03-24 04:05:37 +02:00
|
|
|
const _users: User[] = [];
|
2020-11-08 05:40:31 +02:00
|
|
|
for (const id of ps.userIds) {
|
2021-03-24 04:05:37 +02:00
|
|
|
_users.push(users.find(x => x.id === id)!);
|
2020-11-08 05:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return await Promise.all(_users.map(u => Users.pack(u, me, {
|
2021-12-09 16:58:30 +02:00
|
|
|
detail: true,
|
2019-02-22 04:46:58 +02:00
|
|
|
})));
|
2018-03-27 10:51:12 +03:00
|
|
|
} else {
|
2018-04-25 16:37:08 +03:00
|
|
|
// Lookup user
|
2019-04-12 19:43:22 +03:00
|
|
|
if (typeof ps.host === 'string' && typeof ps.username === 'string') {
|
2019-04-07 17:06:07 +03:00
|
|
|
user = await resolveUser(ps.username, ps.host).catch(e => {
|
2019-02-03 11:16:57 +02:00
|
|
|
apiLogger.warn(`failed to resolve remote user: ${e}`);
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.failedToResolveRemoteUser);
|
|
|
|
});
|
2018-04-25 16:37:08 +03:00
|
|
|
} else {
|
2018-11-01 20:32:24 +02:00
|
|
|
const q: any = ps.userId != null
|
2019-04-07 15:50:36 +03:00
|
|
|
? { id: ps.userId }
|
2019-04-12 19:43:22 +03:00
|
|
|
: { usernameLower: ps.username!.toLowerCase(), host: null };
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
user = await Users.findOne(q);
|
2019-02-15 16:43:49 +02:00
|
|
|
}
|
2018-03-27 10:51:12 +03:00
|
|
|
|
2020-01-01 19:47:20 +02:00
|
|
|
if (user == null || (!isAdminOrModerator && user.isSuspended)) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2018-03-27 10:51:12 +03:00
|
|
|
}
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
return await Users.pack(user, me, {
|
2021-12-09 16:58:30 +02:00
|
|
|
detail: true,
|
2019-02-22 04:46:58 +02:00
|
|
|
});
|
2018-04-25 16:37:08 +03:00
|
|
|
}
|
2019-02-22 04:46:58 +02:00
|
|
|
});
|