1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2024-12-28 22:51:59 +00:00

fix(server): skip smtp validation if unchanged (#12111)

* fix(server): skip smtp validation if unchanged

* update comparison + convert config to plain object
This commit is contained in:
Michel Heusschen 2024-08-29 20:10:09 +02:00 committed by GitHub
parent d08a20bd57
commit 74f18a4523
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 29 additions and 3 deletions

View file

@ -1,4 +1,6 @@
import { plainToInstance } from 'class-transformer';
import { defaults, SystemConfig } from 'src/config';
import { SystemConfigDto } from 'src/dtos/system-config.dto';
import { AlbumUserEntity } from 'src/entities/album-user.entity';
import { AssetFileEntity } from 'src/entities/asset-files.entity';
import { AssetFileType, UserMetadataKey } from 'src/enum';
@ -112,6 +114,14 @@ describe(NotificationService.name, () => {
expect(notificationMock.verifySmtp).not.toHaveBeenCalled();
});
it('skips smtp validation with DTO when there are no changes', async () => {
const oldConfig = { ...configs.smtpEnabled };
const newConfig = plainToInstance(SystemConfigDto, configs.smtpEnabled);
await expect(sut.onConfigValidate({ oldConfig, newConfig })).resolves.not.toThrow();
expect(notificationMock.verifySmtp).not.toHaveBeenCalled();
});
it('skips smtp validation when smtp is disabled', async () => {
const oldConfig = { ...configs.smtpEnabled };
const newConfig = { ...configs.smtpDisabled };

View file

@ -1,5 +1,4 @@
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { isEqual } from 'lodash';
import { DEFAULT_EXTERNAL_DOMAIN } from 'src/constants';
import { SystemConfigCore } from 'src/cores/system-config.core';
import { OnEmit } from 'src/decorators';
@ -23,6 +22,7 @@ import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interf
import { IUserRepository } from 'src/interfaces/user.interface';
import { getAssetFiles } from 'src/utils/asset.util';
import { getFilenameExtension } from 'src/utils/file';
import { isEqualObject } from 'src/utils/object';
import { getPreferences } from 'src/utils/preferences';
@Injectable()
@ -47,7 +47,7 @@ export class NotificationService {
try {
if (
newConfig.notifications.smtp.enabled &&
!isEqual(oldConfig.notifications.smtp, newConfig.notifications.smtp)
!isEqualObject(oldConfig.notifications.smtp, newConfig.notifications.smtp)
) {
await this.notificationRepository.verifySmtp(newConfig.notifications.smtp.transport);
}

View file

@ -18,6 +18,7 @@ import { SystemConfigDto, SystemConfigTemplateStorageOptionDto, mapConfig } from
import { ArgOf, ClientEvent, IEventRepository, ServerEvent } from 'src/interfaces/event.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
import { toPlainObject } from 'src/utils/object';
@Injectable()
export class SystemConfigService {
@ -63,7 +64,7 @@ export class SystemConfigService {
const oldConfig = await this.core.getConfig({ withCache: false });
try {
await this.eventRepository.emit('config.validate', { newConfig: dto, oldConfig });
await this.eventRepository.emit('config.validate', { newConfig: toPlainObject(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);

View file

@ -0,0 +1,15 @@
import { isEqual, isPlainObject } from 'lodash';
/**
* Deeply clones and converts a class instance to a plain object.
*/
export function toPlainObject<T extends object>(obj: T): T {
return isPlainObject(obj) ? obj : structuredClone(obj);
}
/**
* Performs a deep comparison between objects, converting them to plain objects first if needed.
*/
export function isEqualObject(value: object, other: object): boolean {
return isEqual(toPlainObject(value), toPlainObject(other));
}