2019-04-07 15:50:36 +03:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
|
|
|
import { App } from '../entities/app';
|
|
|
|
import { AccessTokens } from '..';
|
2019-06-27 12:04:09 +03:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2019-04-23 16:35:26 +03:00
|
|
|
|
|
|
|
export type PackedApp = SchemaType<typeof packedAppSchema>;
|
2019-04-07 15:50:36 +03:00
|
|
|
|
|
|
|
@EntityRepository(App)
|
|
|
|
export class AppRepository extends Repository<App> {
|
|
|
|
public async pack(
|
|
|
|
src: App['id'] | App,
|
|
|
|
me?: any,
|
|
|
|
options?: {
|
|
|
|
detail?: boolean,
|
|
|
|
includeSecret?: boolean,
|
|
|
|
includeProfileImageIds?: boolean
|
|
|
|
}
|
2019-04-23 16:35:26 +03:00
|
|
|
): Promise<PackedApp> {
|
2019-04-07 15:50:36 +03:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
|
|
|
includeSecret: false,
|
|
|
|
includeProfileImageIds: false
|
|
|
|
}, options);
|
|
|
|
|
2021-02-13 08:33:38 +02:00
|
|
|
const app = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2019-04-07 15:50:36 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: app.id,
|
|
|
|
name: app.name,
|
2019-04-14 21:39:31 +03:00
|
|
|
callbackUrl: app.callbackUrl,
|
2019-04-18 14:46:59 +03:00
|
|
|
permission: app.permission,
|
2019-04-07 15:50:36 +03:00
|
|
|
...(opts.includeSecret ? { secret: app.secret } : {}),
|
|
|
|
...(me ? {
|
|
|
|
isAuthorized: await AccessTokens.count({
|
|
|
|
appId: app.id,
|
|
|
|
userId: me,
|
|
|
|
}).then(count => count > 0)
|
|
|
|
} : {})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 16:35:26 +03:00
|
|
|
|
|
|
|
export const packedAppSchema = {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this Note.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
name: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
description: 'アプリケーションの名前'
|
|
|
|
},
|
|
|
|
callbackUrl: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
description: 'コールバックするURL'
|
|
|
|
},
|
|
|
|
permission: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
items: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
secret: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
description: 'アプリケーションのシークレットキー'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|