2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { AppsRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { unique } from '@/misc/prelude/array.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { secureRndstr } from '@/misc/secure-rndstr.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { AppEntityService } from '@/core/entities/AppEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
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-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
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-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.appsRepository)
|
|
|
|
private appsRepository: AppsRepository,
|
|
|
|
|
|
|
|
private appEntityService: AppEntityService,
|
|
|
|
private idService: IdService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
// Generate secret
|
|
|
|
const secret = secureRndstr(32, true);
|
|
|
|
|
|
|
|
// for backward compatibility
|
|
|
|
const permission = unique(ps.permission.map(v => v.replace(/^(.+)(\/|-)(read|write)$/, '$3:$1')));
|
|
|
|
|
|
|
|
// Create account
|
|
|
|
const app = await this.appsRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
userId: me ? me.id : null,
|
|
|
|
name: ps.name,
|
|
|
|
description: ps.description,
|
|
|
|
permission,
|
|
|
|
callbackUrl: ps.callbackUrl,
|
|
|
|
secret: secret,
|
|
|
|
}).then(x => this.appsRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
|
|
|
return await this.appEntityService.pack(app, null, {
|
|
|
|
detail: true,
|
|
|
|
includeSecret: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|