2021-08-19 21:55:45 +09:00
|
|
|
import define from '../../define';
|
|
|
|
import { Apps } from '@/models/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
2021-08-19 22:04:15 +09:00
|
|
|
import { unique } from '@/prelude/array';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { secureRndstr } from '@/misc/secure-rndstr';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-07-17 04:36:44 +09:00
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['app'],
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: false,
|
2019-04-23 22:35:26 +09:00
|
|
|
|
2019-04-15 23:26:20 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'App',
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2018-07-17 04:36:44 +09:00
|
|
|
|
2022-02-19 14:05:32 +09:00
|
|
|
const paramDef = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
name: { type: 'string' },
|
|
|
|
description: { type: 'string' },
|
|
|
|
permission: { type: 'array', uniqueItems: true, items: {
|
|
|
|
type: 'string',
|
|
|
|
} },
|
|
|
|
callbackUrl: { type: 'string', nullable: true },
|
|
|
|
},
|
|
|
|
required: ['name', 'description', 'permission'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 14:05:32 +09:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2016-12-29 07:49:51 +09:00
|
|
|
// Generate secret
|
2020-03-29 23:16:36 +09:00
|
|
|
const secret = secureRndstr(32, true);
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-15 03:48:54 +09:00
|
|
|
// for backward compatibility
|
|
|
|
const permission = unique(ps.permission.map(v => v.replace(/^(.+)(\/|-)(read|write)$/, '$3:$1')));
|
|
|
|
|
2016-12-29 07:49:51 +09:00
|
|
|
// Create account
|
2022-01-26 00:51:26 +09:00
|
|
|
const app = await Apps.insert({
|
2019-04-07 21:50:36 +09:00
|
|
|
id: genId(),
|
2018-03-29 14:48:47 +09:00
|
|
|
createdAt: new Date(),
|
2019-04-13 19:36:57 +09:00
|
|
|
userId: user ? user.id : null,
|
2018-11-06 01:51:42 +09:00
|
|
|
name: ps.name,
|
2018-11-02 12:49:08 +09:00
|
|
|
description: ps.description,
|
2019-04-15 03:48:54 +09:00
|
|
|
permission,
|
2018-11-02 12:49:08 +09:00
|
|
|
callbackUrl: ps.callbackUrl,
|
2021-12-09 23:58:30 +09:00
|
|
|
secret: secret,
|
2022-01-26 00:51:26 +09:00
|
|
|
}).then(x => Apps.findOneOrFail(x.identifiers[0]));
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
return await Apps.pack(app, null, {
|
2018-11-01 09:19:22 +09:00
|
|
|
detail: true,
|
2021-12-09 23:58:30 +09:00
|
|
|
includeSecret: true,
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|
|
|
|
});
|