summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/app
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2018-03-29 14:51:06 +0900
committerGitHub <noreply@github.com>2018-03-29 14:51:06 +0900
commit0b5597c873d2d9d45be94a18e1b74f44d9925185 (patch)
tree8b4dac3a56cf703650c8207f9279028a8560a96b /src/server/api/endpoints/app
parentoops (diff)
parentResolve conflicts (diff)
downloadmisskey-0b5597c873d2d9d45be94a18e1b74f44d9925185.tar.gz
misskey-0b5597c873d2d9d45be94a18e1b74f44d9925185.tar.bz2
misskey-0b5597c873d2d9d45be94a18e1b74f44d9925185.zip
Merge pull request #1332 from syuilo/pr/1327
Pr/1327
Diffstat (limited to 'src/server/api/endpoints/app')
-rw-r--r--src/server/api/endpoints/app/create.ts108
-rw-r--r--src/server/api/endpoints/app/name_id/available.ts60
-rw-r--r--src/server/api/endpoints/app/show.ts72
3 files changed, 240 insertions, 0 deletions
diff --git a/src/server/api/endpoints/app/create.ts b/src/server/api/endpoints/app/create.ts
new file mode 100644
index 0000000000..713078463d
--- /dev/null
+++ b/src/server/api/endpoints/app/create.ts
@@ -0,0 +1,108 @@
+/**
+ * Module dependencies
+ */
+import rndstr from 'rndstr';
+import $ from 'cafy';
+import App, { isValidNameId, pack } from '../../models/app';
+
+/**
+ * @swagger
+ * /app/create:
+ * post:
+ * summary: Create an application
+ * parameters:
+ * - $ref: "#/parameters/AccessToken"
+ * -
+ * name: nameId
+ * description: Application unique name
+ * in: formData
+ * required: true
+ * type: string
+ * -
+ * name: name
+ * description: Application name
+ * in: formData
+ * required: true
+ * type: string
+ * -
+ * name: description
+ * description: Application description
+ * in: formData
+ * required: true
+ * type: string
+ * -
+ * name: permission
+ * description: Permissions that application has
+ * in: formData
+ * required: true
+ * type: array
+ * items:
+ * type: string
+ * collectionFormat: csv
+ * -
+ * name: callbackUrl
+ * description: URL called back after authentication
+ * in: formData
+ * required: false
+ * type: string
+ *
+ * responses:
+ * 200:
+ * description: Created application's information
+ * schema:
+ * $ref: "#/definitions/Application"
+ *
+ * default:
+ * description: Failed
+ * schema:
+ * $ref: "#/definitions/Error"
+ */
+
+/**
+ * Create an app
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'nameId' parameter
+ const [nameId, nameIdErr] = $(params.nameId).string().pipe(isValidNameId).$;
+ if (nameIdErr) return rej('invalid nameId param');
+
+ // Get 'name' parameter
+ const [name, nameErr] = $(params.name).string().$;
+ if (nameErr) return rej('invalid name param');
+
+ // Get 'description' parameter
+ const [description, descriptionErr] = $(params.description).string().$;
+ if (descriptionErr) return rej('invalid description param');
+
+ // Get 'permission' parameter
+ const [permission, permissionErr] = $(params.permission).array('string').unique().$;
+ if (permissionErr) return rej('invalid permission param');
+
+ // Get 'callbackUrl' parameter
+ // TODO: Check it is valid url
+ const [callbackUrl = null, callbackUrlErr] = $(params.callbackUrl).optional.nullable.string().$;
+ if (callbackUrlErr) return rej('invalid callbackUrl param');
+
+ // Generate secret
+ const secret = rndstr('a-zA-Z0-9', 32);
+
+ // Create account
+ const app = await App.insert({
+ createdAt: new Date(),
+ userId: user._id,
+ name: name,
+ nameId: nameId,
+ nameIdLower: nameId.toLowerCase(),
+ description: description,
+ permission: permission,
+ callbackUrl: callbackUrl,
+ secret: secret
+ });
+
+ // Response
+ res(await pack(app));
+});
diff --git a/src/server/api/endpoints/app/name_id/available.ts b/src/server/api/endpoints/app/name_id/available.ts
new file mode 100644
index 0000000000..6d02b26d2b
--- /dev/null
+++ b/src/server/api/endpoints/app/name_id/available.ts
@@ -0,0 +1,60 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import App from '../../../models/app';
+import { isValidNameId } from '../../../models/app';
+
+/**
+ * @swagger
+ * /app/nameId/available:
+ * post:
+ * summary: Check available nameId on creation an application
+ * parameters:
+ * -
+ * name: nameId
+ * description: Application unique name
+ * in: formData
+ * required: true
+ * type: string
+ *
+ * responses:
+ * 200:
+ * description: Success
+ * schema:
+ * type: object
+ * properties:
+ * available:
+ * description: Whether nameId is available
+ * type: boolean
+ *
+ * default:
+ * description: Failed
+ * schema:
+ * $ref: "#/definitions/Error"
+ */
+
+/**
+ * Check available nameId of app
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = async (params) => new Promise(async (res, rej) => {
+ // Get 'nameId' parameter
+ const [nameId, nameIdErr] = $(params.nameId).string().pipe(isValidNameId).$;
+ if (nameIdErr) return rej('invalid nameId param');
+
+ // Get exist
+ const exist = await App
+ .count({
+ nameIdLower: nameId.toLowerCase()
+ }, {
+ limit: 1
+ });
+
+ // Reply
+ res({
+ available: exist === 0
+ });
+});
diff --git a/src/server/api/endpoints/app/show.ts b/src/server/api/endpoints/app/show.ts
new file mode 100644
index 0000000000..34bb958eee
--- /dev/null
+++ b/src/server/api/endpoints/app/show.ts
@@ -0,0 +1,72 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import App, { pack } from '../../models/app';
+
+/**
+ * @swagger
+ * /app/show:
+ * post:
+ * summary: Show an application's information
+ * description: Require appId or nameId
+ * parameters:
+ * -
+ * name: appId
+ * description: Application ID
+ * in: formData
+ * type: string
+ * -
+ * name: nameId
+ * description: Application unique name
+ * in: formData
+ * type: string
+ *
+ * responses:
+ * 200:
+ * description: Success
+ * schema:
+ * $ref: "#/definitions/Application"
+ *
+ * default:
+ * description: Failed
+ * schema:
+ * $ref: "#/definitions/Error"
+ */
+
+/**
+ * Show an app
+ *
+ * @param {any} params
+ * @param {any} user
+ * @param {any} _
+ * @param {any} isSecure
+ * @return {Promise<any>}
+ */
+module.exports = (params, user, _, isSecure) => new Promise(async (res, rej) => {
+ // Get 'appId' parameter
+ const [appId, appIdErr] = $(params.appId).optional.id().$;
+ if (appIdErr) return rej('invalid appId param');
+
+ // Get 'nameId' parameter
+ const [nameId, nameIdErr] = $(params.nameId).optional.string().$;
+ if (nameIdErr) return rej('invalid nameId param');
+
+ if (appId === undefined && nameId === undefined) {
+ return rej('appId or nameId is required');
+ }
+
+ // Lookup app
+ const app = appId !== undefined
+ ? await App.findOne({ _id: appId })
+ : await App.findOne({ nameIdLower: nameId.toLowerCase() });
+
+ if (app === null) {
+ return rej('app not found');
+ }
+
+ // Send response
+ res(await pack(app, user, {
+ includeSecret: isSecure && app.userId.equals(user._id)
+ }));
+});