2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { AppsRepository } from '@/models/index.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { unique } from '@/misc/prelude/array.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { secureRndstr } from '@/misc/secure-rndstr.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { AppEntityService } from '@/core/entities/AppEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['app'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
|
2019-04-15 16:26:20 +02:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'App',
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-07-16 21:36:44 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01: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-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 20:27:08 +02: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
|
2023-06-25 04:04:33 +02:00
|
|
|
const secret = secureRndstr(32);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|