Skip to content

Commit

Permalink
refactor(server): event names (#12084)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 authored Aug 27, 2024
1 parent aac6a4b commit 0be3c44
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 54 deletions.
8 changes: 4 additions & 4 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class ApiModule implements OnModuleInit, OnModuleDestroy {
async onModuleInit() {
const items = setupEventHandlers(this.moduleRef);

await this.eventRepository.emit('onBootstrap', 'api');
await this.eventRepository.emit('app.bootstrap', 'api');

this.logger.setContext('EventLoader');
const eventMap = _.groupBy(items, 'event');
Expand All @@ -74,7 +74,7 @@ export class ApiModule implements OnModuleInit, OnModuleDestroy {
}

async onModuleDestroy() {
await this.eventRepository.emit('onShutdown');
await this.eventRepository.emit('app.shutdown');
}
}

Expand All @@ -90,11 +90,11 @@ export class MicroservicesModule implements OnModuleInit, OnModuleDestroy {

async onModuleInit() {
setupEventHandlers(this.moduleRef);
await this.eventRepository.emit('onBootstrap', 'microservices');
await this.eventRepository.emit('app.bootstrap', 'microservices');
}

async onModuleDestroy() {
await this.eventRepository.emit('onShutdown');
await this.eventRepository.emit('app.shutdown');
}
}

Expand Down
14 changes: 7 additions & 7 deletions server/src/interfaces/event.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ export const IEventRepository = 'IEventRepository';

type EmitEventMap = {
// app events
onBootstrap: ['api' | 'microservices'];
onShutdown: [];
'app.bootstrap': ['api' | 'microservices'];
'app.shutdown': [];

// config events
onConfigUpdate: [{ newConfig: SystemConfig; oldConfig: SystemConfig }];
onConfigValidate: [{ newConfig: SystemConfig; oldConfig: SystemConfig }];
'config.update': [{ newConfig: SystemConfig; oldConfig: SystemConfig }];
'config.validate': [{ newConfig: SystemConfig; oldConfig: SystemConfig }];

// album events
onAlbumUpdate: [{ id: string; updatedBy: string }];
onAlbumInvite: [{ id: string; userId: string }];
'album.update': [{ id: string; updatedBy: string }];
'album.invite': [{ id: string; userId: string }];

// user events
onUserSignup: [{ notify: boolean; id: string; tempPassword?: string }];
'user.signup': [{ notify: boolean; id: string; tempPassword?: string }];
};

export type EmitEvent = keyof EmitEventMap;
Expand Down
8 changes: 4 additions & 4 deletions server/src/services/album.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ describe(AlbumService.name, () => {

expect(userMock.get).toHaveBeenCalledWith('user-id', {});
expect(accessMock.asset.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['123']));
expect(eventMock.emit).toHaveBeenCalledWith('onAlbumInvite', {
expect(eventMock.emit).toHaveBeenCalledWith('album.invite', {
id: albumStub.empty.id,
userId: 'user-id',
});
Expand Down Expand Up @@ -384,7 +384,7 @@ describe(AlbumService.name, () => {
userId: authStub.user2.user.id,
albumId: albumStub.sharedWithAdmin.id,
});
expect(eventMock.emit).toHaveBeenCalledWith('onAlbumInvite', {
expect(eventMock.emit).toHaveBeenCalledWith('album.invite', {
id: albumStub.sharedWithAdmin.id,
userId: userStub.user2.id,
});
Expand Down Expand Up @@ -572,7 +572,7 @@ describe(AlbumService.name, () => {
albumThumbnailAssetId: 'asset-1',
});
expect(albumMock.addAssetIds).toHaveBeenCalledWith('album-123', ['asset-1', 'asset-2', 'asset-3']);
expect(eventMock.emit).toHaveBeenCalledWith('onAlbumUpdate', {
expect(eventMock.emit).toHaveBeenCalledWith('album.update', {
id: 'album-123',
updatedBy: authStub.admin.user.id,
});
Expand Down Expand Up @@ -616,7 +616,7 @@ describe(AlbumService.name, () => {
albumThumbnailAssetId: 'asset-1',
});
expect(albumMock.addAssetIds).toHaveBeenCalledWith('album-123', ['asset-1', 'asset-2', 'asset-3']);
expect(eventMock.emit).toHaveBeenCalledWith('onAlbumUpdate', {
expect(eventMock.emit).toHaveBeenCalledWith('album.update', {
id: 'album-123',
updatedBy: authStub.user1.user.id,
});
Expand Down
6 changes: 3 additions & 3 deletions server/src/services/album.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class AlbumService {
});

for (const { userId } of albumUsers) {
await this.eventRepository.emit('onAlbumInvite', { id: album.id, userId });
await this.eventRepository.emit('album.invite', { id: album.id, userId });
}

return mapAlbumWithAssets(album);
Expand Down Expand Up @@ -192,7 +192,7 @@ export class AlbumService {
albumThumbnailAssetId: album.albumThumbnailAssetId ?? firstNewAssetId,
});

await this.eventRepository.emit('onAlbumUpdate', { id, updatedBy: auth.user.id });
await this.eventRepository.emit('album.update', { id, updatedBy: auth.user.id });
}

return results;
Expand Down Expand Up @@ -240,7 +240,7 @@ export class AlbumService {
}

await this.albumUserRepository.create({ userId: userId, albumId: id, role });
await this.eventRepository.emit('onAlbumInvite', { id, userId });
await this.eventRepository.emit('album.invite', { id, userId });
}

return this.findOrFail(id, { withAssets: true }).then(mapAlbumWithoutAssets);
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class DatabaseService {
this.logger.setContext(DatabaseService.name);
}

@OnEmit({ event: 'onBootstrap', priority: -200 })
@OnEmit({ event: 'app.bootstrap', priority: -200 })
async onBootstrap() {
const version = await this.databaseRepository.getPostgresVersion();
const current = semver.coerce(version);
Expand Down
7 changes: 4 additions & 3 deletions server/src/services/library.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class LibraryService {
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
}

@OnEmit({ event: 'onBootstrap' })
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap() {
const config = await this.configCore.getConfig({ withCache: false });

Expand Down Expand Up @@ -104,7 +104,8 @@ export class LibraryService {
});
}

onConfigValidate({ newConfig }: ArgOf<'onConfigValidate'>) {
@OnEmit({ event: 'config.validate' })
onConfigValidate({ newConfig }: ArgOf<'config.validate'>) {
const { scan } = newConfig.library;
if (!validateCronExpression(scan.cronExpression)) {
throw new Error(`Invalid cron expression ${scan.cronExpression}`);
Expand Down Expand Up @@ -189,7 +190,7 @@ export class LibraryService {
}
}

@OnEmit({ event: 'onShutdown' })
@OnEmit({ event: 'app.shutdown' })
async onShutdown() {
await this.unwatchAll();
}
Expand Down
10 changes: 5 additions & 5 deletions server/src/services/metadata.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ export class MetadataService {
);
}

@OnEmit({ event: 'onBootstrap' })
async onBootstrap(app: ArgOf<'onBootstrap'>) {
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap(app: ArgOf<'app.bootstrap'>) {
if (app !== 'microservices') {
return;
}
const config = await this.configCore.getConfig({ withCache: false });
await this.init(config);
}

@OnEmit({ event: 'onConfigUpdate' })
async onConfigUpdate({ newConfig }: ArgOf<'onConfigUpdate'>) {
@OnEmit({ event: 'config.update' })
async onConfigUpdate({ newConfig }: ArgOf<'config.update'>) {
await this.init(newConfig);
}

Expand All @@ -153,7 +153,7 @@ export class MetadataService {
}
}

@OnEmit({ event: 'onShutdown' })
@OnEmit({ event: 'app.shutdown' })
async onShutdown() {
await this.repository.teardown();
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/services/microservices.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class MicroservicesService {
private versionService: VersionService,
) {}

@OnEmit({ event: 'onBootstrap' })
async onBootstrap(app: ArgOf<'onBootstrap'>) {
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap(app: ArgOf<'app.bootstrap'>) {
if (app !== 'microservices') {
return;
}
Expand Down
16 changes: 8 additions & 8 deletions server/src/services/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export class NotificationService {
this.configCore = SystemConfigCore.create(systemMetadataRepository, logger);
}

@OnEmit({ event: 'onConfigValidate', priority: -100 })
async onConfigValidate({ oldConfig, newConfig }: ArgOf<'onConfigValidate'>) {
@OnEmit({ event: 'config.validate', priority: -100 })
async onConfigValidate({ oldConfig, newConfig }: ArgOf<'config.validate'>) {
try {
if (
newConfig.notifications.smtp.enabled &&
Expand All @@ -57,20 +57,20 @@ export class NotificationService {
}
}

@OnEmit({ event: 'onUserSignup' })
async onUserSignup({ notify, id, tempPassword }: ArgOf<'onUserSignup'>) {
@OnEmit({ event: 'user.signup' })
async onUserSignup({ notify, id, tempPassword }: ArgOf<'user.signup'>) {
if (notify) {
await this.jobRepository.queue({ name: JobName.NOTIFY_SIGNUP, data: { id, tempPassword } });
}
}

@OnEmit({ event: 'onAlbumUpdate' })
async onAlbumUpdate({ id, updatedBy }: ArgOf<'onAlbumUpdate'>) {
@OnEmit({ event: 'album.update' })
async onAlbumUpdate({ id, updatedBy }: ArgOf<'album.update'>) {
await this.jobRepository.queue({ name: JobName.NOTIFY_ALBUM_UPDATE, data: { id, senderId: updatedBy } });
}

@OnEmit({ event: 'onAlbumInvite' })
async onAlbumInvite({ id, userId }: ArgOf<'onAlbumInvite'>) {
@OnEmit({ event: 'album.invite' })
async onAlbumInvite({ id, userId }: ArgOf<'album.invite'>) {
await this.jobRepository.queue({ name: JobName.NOTIFY_ALBUM_INVITE, data: { id, recipientId: userId } });
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/services/server.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class ServerService {
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
}

@OnEmit({ event: 'onBootstrap' })
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap(): Promise<void> {
const featureFlags = await this.getFeatures();
if (featureFlags.configFile) {
Expand Down
12 changes: 6 additions & 6 deletions server/src/services/smart-info.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class SmartInfoService {
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
}

@OnEmit({ event: 'onBootstrap' })
async onBootstrap(app: ArgOf<'onBootstrap'>) {
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap(app: ArgOf<'app.bootstrap'>) {
if (app !== 'microservices') {
return;
}
Expand All @@ -49,8 +49,8 @@ export class SmartInfoService {
await this.init(config);
}

@OnEmit({ event: 'onConfigValidate' })
onConfigValidate({ newConfig }: ArgOf<'onConfigValidate'>) {
@OnEmit({ event: 'config.validate' })
onConfigValidate({ newConfig }: ArgOf<'config.validate'>) {
try {
getCLIPModelInfo(newConfig.machineLearning.clip.modelName);
} catch {
Expand All @@ -60,8 +60,8 @@ export class SmartInfoService {
}
}

@OnEmit({ event: 'onConfigUpdate' })
async onConfigUpdate({ oldConfig, newConfig }: ArgOf<'onConfigUpdate'>) {
@OnEmit({ event: 'config.update' })
async onConfigUpdate({ oldConfig, newConfig }: ArgOf<'config.update'>) {
await this.init(newConfig, oldConfig);
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/services/storage-template.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export class StorageTemplateService {
);
}

@OnEmit({ event: 'onConfigValidate' })
onConfigValidate({ newConfig }: ArgOf<'onConfigValidate'>) {
@OnEmit({ event: 'config.validate' })
onConfigValidate({ newConfig }: ArgOf<'config.validate'>) {
try {
const { compiled } = this.compile(newConfig.storageTemplate.template);
this.render(compiled, {
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class StorageService {
this.logger.setContext(StorageService.name);
}

@OnEmit({ event: 'onBootstrap' })
@OnEmit({ event: 'app.bootstrap' })
onBootstrap() {
const libraryBase = StorageCore.getBaseFolder(StorageFolder.LIBRARY);
this.storageRepository.mkdirSync(libraryBase);
Expand Down
10 changes: 5 additions & 5 deletions server/src/services/system-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class SystemConfigService {
this.core.config$.subscribe((config) => this.setLogLevel(config));
}

@OnEmit({ event: 'onBootstrap', priority: -100 })
@OnEmit({ event: 'app.bootstrap', priority: -100 })
async onBootstrap() {
const config = await this.core.getConfig({ withCache: false });
this.core.config$.next(config);
Expand All @@ -48,8 +48,8 @@ export class SystemConfigService {
return mapConfig(defaults);
}

@OnEmit({ event: 'onConfigValidate' })
onConfigValidate({ newConfig, oldConfig }: ArgOf<'onConfigValidate'>) {
@OnEmit({ event: 'config.validate' })
onConfigValidate({ newConfig, oldConfig }: ArgOf<'config.validate'>) {
if (!_.isEqual(instanceToPlain(newConfig.logging), oldConfig.logging) && this.getEnvLogLevel()) {
throw new Error('Logging cannot be changed while the environment variable IMMICH_LOG_LEVEL is set.');
}
Expand All @@ -63,7 +63,7 @@ export class SystemConfigService {
const oldConfig = await this.core.getConfig({ withCache: false });

try {
await this.eventRepository.emit('onConfigValidate', { newConfig: dto, oldConfig });
await this.eventRepository.emit('config.validate', { newConfig: dto, oldConfig });
} catch (error) {
this.logger.warn(`Unable to save system config due to a validation error: ${error}`);
throw new BadRequestException(error instanceof Error ? error.message : error);
Expand All @@ -74,7 +74,7 @@ export class SystemConfigService {
// TODO probably move web socket emits to a separate service
this.eventRepository.clientBroadcast(ClientEvent.CONFIG_UPDATE, {});
this.eventRepository.serverSend(ServerEvent.CONFIG_UPDATE, null);
await this.eventRepository.emit('onConfigUpdate', { newConfig, oldConfig });
await this.eventRepository.emit('config.update', { newConfig, oldConfig });

return mapConfig(newConfig);
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/user-admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class UserAdminService {
const { notify, ...rest } = dto;
const user = await this.userCore.createUser(rest);

await this.eventRepository.emit('onUserSignup', {
await this.eventRepository.emit('user.signup', {
notify: !!notify,
id: user.id,
tempPassword: user.shouldChangePassword ? rest.password : undefined,
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/version.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class VersionService {
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
}

@OnEmit({ event: 'onBootstrap' })
@OnEmit({ event: 'app.bootstrap' })
async onBootstrap(): Promise<void> {
await this.handleVersionCheck();
}
Expand Down

0 comments on commit 0be3c44

Please sign in to comment.