2019-04-07 12:50:36 +00:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2021-08-19 09:33:41 +00:00
|
|
|
import { App } from '../entities/app.js';
|
|
|
|
import { AccessTokens } from '../index.js';
|
|
|
|
import { SchemaType } from '@/misc/schema.js';
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
|
|
export type PackedApp = SchemaType<typeof packedAppSchema>;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
@EntityRepository(App)
|
|
|
|
export class AppRepository extends Repository<App> {
|
|
|
|
public async pack(
|
|
|
|
src: App['id'] | App,
|
2021-03-24 02:05:37 +00:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-07 12:50:36 +00:00
|
|
|
options?: {
|
|
|
|
detail?: boolean,
|
|
|
|
includeSecret?: boolean,
|
|
|
|
includeProfileImageIds?: boolean
|
|
|
|
}
|
2019-04-23 13:35:26 +00:00
|
|
|
): Promise<PackedApp> {
|
2019-04-07 12:50:36 +00:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
|
|
|
includeSecret: false,
|
|
|
|
includeProfileImageIds: false
|
|
|
|
}, options);
|
|
|
|
|
2021-02-13 06:33:38 +00:00
|
|
|
const app = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: app.id,
|
|
|
|
name: app.name,
|
2019-04-14 18:39:31 +00:00
|
|
|
callbackUrl: app.callbackUrl,
|
2019-04-18 11:46:59 +00:00
|
|
|
permission: app.permission,
|
2019-04-07 12:50:36 +00:00
|
|
|
...(opts.includeSecret ? { secret: app.secret } : {}),
|
|
|
|
...(me ? {
|
|
|
|
isAuthorized: await AccessTokens.count({
|
|
|
|
appId: app.id,
|
|
|
|
userId: me,
|
|
|
|
}).then(count => count > 0)
|
|
|
|
} : {})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
|
|
export const packedAppSchema = {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
2021-03-06 13:34:11 +00:00
|
|
|
optional: false as const, nullable: false as const
|
2019-04-23 13:35:26 +00:00
|
|
|
},
|
|
|
|
name: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
2021-03-06 13:34:11 +00:00
|
|
|
optional: false as const, nullable: false as const
|
|
|
|
},
|
|
|
|
createdAt: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const
|
2019-04-23 13:35:26 +00:00
|
|
|
},
|
2021-03-06 13:34:11 +00:00
|
|
|
lastUsedAt: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
2021-03-06 13:34:11 +00:00
|
|
|
optional: false as const, nullable: false as const
|
2019-04-23 13:35:26 +00:00
|
|
|
},
|
|
|
|
permission: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'array' as const,
|
2021-03-06 13:34:11 +00:00
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
items: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
2021-03-06 13:34:11 +00:00
|
|
|
optional: false as const, nullable: false as const
|
2019-04-23 13:35:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
secret: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
2021-03-06 13:34:11 +00:00
|
|
|
optional: true as const, nullable: false as const
|
|
|
|
},
|
|
|
|
isAuthorized: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: true as const, nullable: false as const
|
2019-04-23 13:35:26 +00:00
|
|
|
}
|
2021-03-06 13:34:11 +00:00
|
|
|
}
|
2019-04-23 13:35:26 +00:00
|
|
|
};
|