2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-09 03:50:09 +09:00
|
|
|
import $ from 'cafy';
|
2016-12-29 07:49:51 +09:00
|
|
|
import User from '../../models/user';
|
|
|
|
import { validateUsername } from '../../models/user';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check available username
|
|
|
|
*
|
2017-03-01 17:37:01 +09:00
|
|
|
* @param {any} params
|
|
|
|
* @return {Promise<any>}
|
2016-12-29 07:49:51 +09:00
|
|
|
*/
|
2017-03-04 04:28:38 +09:00
|
|
|
module.exports = async (params) => new Promise(async (res, rej) => {
|
2016-12-29 07:49:51 +09:00
|
|
|
// Get 'username' parameter
|
2017-03-09 03:50:09 +09:00
|
|
|
const [username, usernameError] = $(params.username).string().pipe(validateUsername).$;
|
2017-03-03 08:00:10 +09:00
|
|
|
if (usernameError) return rej('invalid username param');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
// Get exist
|
|
|
|
const exist = await User
|
|
|
|
.count({
|
2018-03-27 16:51:12 +09:00
|
|
|
host: null,
|
2018-03-29 14:48:47 +09:00
|
|
|
usernameLower: username.toLowerCase()
|
2016-12-29 07:49:51 +09:00
|
|
|
}, {
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reply
|
|
|
|
res({
|
|
|
|
available: exist === 0
|
|
|
|
});
|
|
|
|
});
|