hippofish/packages/backend/src/server/api/endpoints/users/show.ts
tamaina efb0ffc4ec
refactor: APIエンドポイントファイルの定義を良い感じにする (#8154)
* 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 commit 97dc9bfa70.

* Revert "Fix API Schema Error"

This reverts commit 21b6176d97.

* ✌️

* 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>
2022-01-18 22:27:10 +09:00

117 lines
2.5 KiB
TypeScript

import $ from 'cafy';
import { resolveUser } from '@/remote/resolve-user';
import define from '../../define';
import { apiLogger } from '../../logger';
import { ApiError } from '../../error';
import { ID } from '@/misc/cafy-id';
import { Users } from '@/models/index';
import { In } from 'typeorm';
import { User } from '@/models/entities/user';
export const meta = {
tags: ['users'],
requireCredential: false,
params: {
userId: {
validator: $.optional.type(ID),
},
userIds: {
validator: $.optional.arr($.type(ID)).unique(),
},
username: {
validator: $.optional.str,
},
host: {
validator: $.optional.nullable.str,
},
},
res: {
optional: false, nullable: false,
oneOf: [
{
type: 'object',
ref: 'UserDetailed',
},
{
type: 'array',
items: {
type: 'object',
ref: 'UserDetailed',
}
},
]
},
errors: {
failedToResolveRemoteUser: {
message: 'Failed to resolve remote user.',
code: 'FAILED_TO_RESOLVE_REMOTE_USER',
id: 'ef7b9be4-9cba-4e6f-ab41-90ed171c7d3c',
kind: 'server',
},
noSuchUser: {
message: 'No such user.',
code: 'NO_SUCH_USER',
id: '4362f8dc-731f-4ad8-a694-be5a88922a24',
},
},
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, async (ps, me) => {
let user;
const isAdminOrModerator = me && (me.isAdmin || me.isModerator);
if (ps.userIds) {
if (ps.userIds.length === 0) {
return [];
}
const users = await Users.find(isAdminOrModerator ? {
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 => Users.pack(u, me, {
detail: true,
})));
} else {
// Lookup user
if (typeof ps.host === 'string' && typeof ps.username === 'string') {
user = await resolveUser(ps.username, ps.host).catch(e => {
apiLogger.warn(`failed to resolve remote user: ${e}`);
throw new ApiError(meta.errors.failedToResolveRemoteUser);
});
} else {
const q: any = ps.userId != null
? { id: ps.userId }
: { usernameLower: ps.username!.toLowerCase(), host: null };
user = await Users.findOne(q);
}
if (user == null || (!isAdminOrModerator && user.isSuspended)) {
throw new ApiError(meta.errors.noSuchUser);
}
return await Users.pack(user, me, {
detail: true,
});
}
});