hippofish/packages/backend/src/core/entities/AppEntityService.ts

58 lines
1.5 KiB
TypeScript
Raw Normal View History

/*
* 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 { DI } from '@/di-symbols.js';
import type { AccessTokensRepository, AppsRepository } from '@/models/_.js';
2023-03-10 06:22:37 +01:00
import type { Packed } from '@/misc/json-schema.js';
import type { MiApp } from '@/models/entities/App.js';
import type { MiUser } from '@/models/entities/User.js';
import { bindThis } from '@/decorators.js';
2022-09-17 20:27:08 +02:00
@Injectable()
export class AppEntityService {
constructor(
@Inject(DI.appsRepository)
private appsRepository: AppsRepository,
@Inject(DI.accessTokensRepository)
private accessTokensRepository: AccessTokensRepository,
) {
}
@bindThis
2022-09-17 20:27:08 +02:00
public async pack(
src: MiApp['id'] | MiApp,
me?: { id: MiUser['id'] } | null | undefined,
2022-09-17 20:27:08 +02:00
options?: {
detail?: boolean,
includeSecret?: boolean,
includeProfileImageIds?: boolean
},
): Promise<Packed<'App'>> {
const opts = Object.assign({
detail: false,
includeSecret: false,
includeProfileImageIds: false,
}, options);
const app = typeof src === 'object' ? src : await this.appsRepository.findOneByOrFail({ id: src });
return {
id: app.id,
name: app.name,
callbackUrl: app.callbackUrl,
permission: app.permission,
...(opts.includeSecret ? { secret: app.secret } : {}),
...(me ? {
isAuthorized: await this.accessTokensRepository.countBy({
appId: app.id,
userId: me.id,
}).then(count => count > 0),
} : {}),
};
}
}