misskey/src/server/api/endpoints/app/name_id/available.ts

61 lines
1.3 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
/**
* Module dependencies
*/
2017-03-09 03:50:09 +09:00
import $ from 'cafy';
2018-03-29 20:32:18 +09:00
import App from '../../../../../models/app';
import { isValidNameId } from '../../../../../models/app';
2016-12-29 07:49:51 +09:00
2017-01-06 17:45:04 +09:00
/**
* @swagger
2018-03-29 14:48:47 +09:00
* /app/nameId/available:
2018-04-08 02:30:37 +09:00
* note:
2018-03-29 14:48:47 +09:00
* summary: Check available nameId on creation an application
2017-01-06 17:45:04 +09:00
* parameters:
* -
2018-03-29 14:48:47 +09:00
* name: nameId
2017-01-06 17:45:04 +09:00
* description: Application unique name
* in: formData
* required: true
* type: string
2017-03-01 17:37:01 +09:00
*
2017-01-06 17:45:04 +09:00
* responses:
* 200:
* description: Success
* schema:
* type: object
* properties:
* available:
2018-03-29 14:48:47 +09:00
* description: Whether nameId is available
2017-01-06 17:45:04 +09:00
* type: boolean
2017-03-01 17:37:01 +09:00
*
2017-01-06 17:45:04 +09:00
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
2016-12-29 07:49:51 +09:00
/**
2018-03-29 14:48:47 +09:00
* Check available nameId of app
2016-12-29 07:49:51 +09:00
*
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) => {
2018-03-29 14:48:47 +09:00
// Get 'nameId' parameter
2018-04-27 19:12:15 +09:00
const [nameId, nameIdErr] = $(params.nameId).string().pipe(isValidNameId).get();
2018-03-29 14:48:47 +09:00
if (nameIdErr) return rej('invalid nameId param');
2016-12-29 07:49:51 +09:00
// Get exist
const exist = await App
.count({
2018-03-29 14:48:47 +09:00
nameIdLower: nameId.toLowerCase()
2016-12-29 07:49:51 +09:00
}, {
limit: 1
});
// Reply
res({
available: exist === 0
});
});