2017-03-08 20:50:09 +02:00
|
|
|
import $ from 'cafy';
|
2021-08-19 15:55:45 +03:00
|
|
|
import define from '../../define';
|
|
|
|
import { Users, UsedUsernames } from '@/models/index';
|
2018-11-02 05:49:08 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['users'],
|
|
|
|
|
2020-02-15 14:33:32 +02:00
|
|
|
requireCredential: false as const,
|
2018-11-02 05:49:08 +02:00
|
|
|
|
|
|
|
params: {
|
|
|
|
username: {
|
2019-06-14 18:07:41 +03:00
|
|
|
validator: $.use(Users.validateLocalUsername)
|
2018-11-02 05:49:08 +02:00
|
|
|
}
|
2021-03-06 15:34:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
properties: {
|
|
|
|
available: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
}
|
|
|
|
}
|
2018-11-02 05:49:08 +02:00
|
|
|
}
|
|
|
|
};
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
export default define(meta, async (ps) => {
|
2016-12-29 00:49:51 +02:00
|
|
|
// Get exist
|
2019-04-07 15:50:36 +03:00
|
|
|
const exist = await Users.count({
|
2019-02-22 04:46:58 +02:00
|
|
|
host: null,
|
|
|
|
usernameLower: ps.username.toLowerCase()
|
|
|
|
});
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2019-07-22 04:15:00 +03:00
|
|
|
const exist2 = await UsedUsernames.count({ username: ps.username.toLowerCase() });
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
return {
|
2019-07-22 04:15:00 +03:00
|
|
|
available: exist === 0 && exist2 === 0
|
2019-02-22 04:46:58 +02:00
|
|
|
};
|
|
|
|
});
|