From 50bc92aac06a0cd1261be0921ee0b5fd048f4f1c Mon Sep 17 00:00:00 2001 From: Daniel Dietzler <36593685+danieldietzler@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:37:51 +0200 Subject: [PATCH] refactor(server): make access core singleton (#4609) --- server/src/domain/access/access.core.ts | 16 +++++++++++++++- server/src/domain/album/album.service.ts | 2 +- server/src/domain/asset/asset.service.ts | 2 +- server/src/domain/audit/audit.service.ts | 2 +- server/src/domain/library/library.service.ts | 2 +- server/src/domain/person/person.service.ts | 2 +- .../domain/shared-link/shared-link.service.ts | 2 +- server/src/immich/api-v1/asset/asset.service.ts | 2 +- .../test/repositories/access.repository.mock.ts | 8 ++++++-- 9 files changed, 28 insertions(+), 10 deletions(-) diff --git a/server/src/domain/access/access.core.ts b/server/src/domain/access/access.core.ts index 7f75a76830..7c5a664308 100644 --- a/server/src/domain/access/access.core.ts +++ b/server/src/domain/access/access.core.ts @@ -37,8 +37,22 @@ export enum Permission { PERSON_MERGE = 'person.merge', } +let instance: AccessCore | null; + export class AccessCore { - constructor(private repository: IAccessRepository) {} + private constructor(private repository: IAccessRepository) {} + + static create(repository: IAccessRepository) { + if (!instance) { + instance = new AccessCore(repository); + } + + return instance; + } + + static reset() { + instance = null; + } requireUploadAccess(authUser: AuthUserDto | null): AuthUserDto { if (!authUser || (authUser.isPublicUser && !authUser.isAllowUpload)) { diff --git a/server/src/domain/album/album.service.ts b/server/src/domain/album/album.service.ts index 04b885040a..e6f1edfa77 100644 --- a/server/src/domain/album/album.service.ts +++ b/server/src/domain/album/album.service.ts @@ -31,7 +31,7 @@ export class AlbumService { @Inject(IJobRepository) private jobRepository: IJobRepository, @Inject(IUserRepository) private userRepository: IUserRepository, ) { - this.access = new AccessCore(accessRepository); + this.access = AccessCore.create(accessRepository); } async getCount(authUser: AuthUserDto): Promise { diff --git a/server/src/domain/asset/asset.service.ts b/server/src/domain/asset/asset.service.ts index 57623fa1b2..6823c00305 100644 --- a/server/src/domain/asset/asset.service.ts +++ b/server/src/domain/asset/asset.service.ts @@ -89,7 +89,7 @@ export class AssetService { @Inject(IStorageRepository) private storageRepository: IStorageRepository, @Inject(ICommunicationRepository) private communicationRepository: ICommunicationRepository, ) { - this.access = new AccessCore(accessRepository); + this.access = AccessCore.create(accessRepository); this.configCore = SystemConfigCore.create(configRepository); this.storageCore = new StorageCore(storageRepository, assetRepository, moveRepository, personRepository); } diff --git a/server/src/domain/audit/audit.service.ts b/server/src/domain/audit/audit.service.ts index dc3d65b653..1eaa720815 100644 --- a/server/src/domain/audit/audit.service.ts +++ b/server/src/domain/audit/audit.service.ts @@ -40,7 +40,7 @@ export class AuditService { @Inject(IStorageRepository) private storageRepository: IStorageRepository, @Inject(IUserRepository) private userRepository: IUserRepository, ) { - this.access = new AccessCore(accessRepository); + this.access = AccessCore.create(accessRepository); } async handleCleanup(): Promise { diff --git a/server/src/domain/library/library.service.ts b/server/src/domain/library/library.service.ts index a59663bf77..d226b22c7c 100644 --- a/server/src/domain/library/library.service.ts +++ b/server/src/domain/library/library.service.ts @@ -43,7 +43,7 @@ export class LibraryService { @Inject(IStorageRepository) private storageRepository: IStorageRepository, @Inject(IUserRepository) private userRepository: IUserRepository, ) { - this.access = new AccessCore(accessRepository); + this.access = AccessCore.create(accessRepository); } async getStatistics(authUser: AuthUserDto, id: string): Promise { diff --git a/server/src/domain/person/person.service.ts b/server/src/domain/person/person.service.ts index 162ab8fdb4..eb0bdc5c6c 100644 --- a/server/src/domain/person/person.service.ts +++ b/server/src/domain/person/person.service.ts @@ -56,7 +56,7 @@ export class PersonService { @Inject(IStorageRepository) private storageRepository: IStorageRepository, @Inject(IJobRepository) private jobRepository: IJobRepository, ) { - this.access = new AccessCore(accessRepository); + this.access = AccessCore.create(accessRepository); this.configCore = SystemConfigCore.create(configRepository); this.storageCore = new StorageCore(storageRepository, assetRepository, moveRepository, repository); } diff --git a/server/src/domain/shared-link/shared-link.service.ts b/server/src/domain/shared-link/shared-link.service.ts index 9e2a0fc8a0..2cb87c8ebc 100644 --- a/server/src/domain/shared-link/shared-link.service.ts +++ b/server/src/domain/shared-link/shared-link.service.ts @@ -16,7 +16,7 @@ export class SharedLinkService { @Inject(ICryptoRepository) private cryptoRepository: ICryptoRepository, @Inject(ISharedLinkRepository) private repository: ISharedLinkRepository, ) { - this.access = new AccessCore(accessRepository); + this.access = AccessCore.create(accessRepository); } getAll(authUser: AuthUserDto): Promise { diff --git a/server/src/immich/api-v1/asset/asset.service.ts b/server/src/immich/api-v1/asset/asset.service.ts index 415fb380de..1de4edeea6 100644 --- a/server/src/immich/api-v1/asset/asset.service.ts +++ b/server/src/immich/api-v1/asset/asset.service.ts @@ -68,7 +68,7 @@ export class AssetService { @Inject(IStorageRepository) private storageRepository: IStorageRepository, ) { this.assetCore = new AssetCore(_assetRepository, jobRepository); - this.access = new AccessCore(accessRepository); + this.access = AccessCore.create(accessRepository); } public async uploadFile( diff --git a/server/test/repositories/access.repository.mock.ts b/server/test/repositories/access.repository.mock.ts index c9568283eb..416d5043a4 100644 --- a/server/test/repositories/access.repository.mock.ts +++ b/server/test/repositories/access.repository.mock.ts @@ -1,4 +1,4 @@ -import { IAccessRepository } from '@app/domain'; +import { AccessCore, IAccessRepository } from '@app/domain'; export interface IAccessRepositoryMock { asset: jest.Mocked; @@ -8,7 +8,11 @@ export interface IAccessRepositoryMock { person: jest.Mocked; } -export const newAccessRepositoryMock = (): IAccessRepositoryMock => { +export const newAccessRepositoryMock = (reset = true): IAccessRepositoryMock => { + if (reset) { + AccessCore.reset(); + } + return { asset: { hasOwnerAccess: jest.fn(),