2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
import { IsNull } from 'typeorm';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { UsedUsernamesRepository, UsersRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-09-20 11:33:36 +09:00
|
|
|
import { localUsernameSchema } from '@/models/User.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-04-29 17:03:14 +09:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2018-11-02 12:49:08 +09:00
|
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['users'],
|
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: false,
|
2018-11-02 12:49:08 +09:00
|
|
|
|
2021-03-06 22:34:11 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 22:34:11 +09:00
|
|
|
properties: {
|
|
|
|
|
available: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'boolean',
|
|
|
|
|
optional: false, nullable: false,
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
2022-09-18 03:27:08 +09:00
|
|
|
username: localUsernameSchema,
|
2022-02-19 14:05:32 +09:00
|
|
|
},
|
|
|
|
|
required: ['username'],
|
|
|
|
|
} as const;
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
2023-08-17 21:20:58 +09:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.usedUsernamesRepository)
|
|
|
|
|
private usedUsernamesRepository: UsedUsernamesRepository,
|
2023-04-29 17:03:14 +09:00
|
|
|
|
|
|
|
|
private metaService: MetaService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
|
const exist = await this.usersRepository.countBy({
|
|
|
|
|
host: IsNull(),
|
|
|
|
|
usernameLower: ps.username.toLowerCase(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const exist2 = await this.usedUsernamesRepository.countBy({ username: ps.username.toLowerCase() });
|
|
|
|
|
|
2023-04-29 17:03:14 +09:00
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
|
const isPreserved = meta.preservedUsernames.map(x => x.toLowerCase()).includes(ps.username.toLowerCase());
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
return {
|
2023-04-29 17:03:14 +09:00
|
|
|
available: exist === 0 && exist2 === 0 && !isPreserved,
|
2022-09-18 03:27:08 +09:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|