mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
feat(server): user metadata (#9650)
* feat(server): user metadata * add missing method to user mock * update migration to include cascades * update sql files * test: fix e2e * chore: clean up --------- Co-authored-by: Daniel Dietzler <mail@ddietzler.dev>
This commit is contained in:
parent
a4887bfa7e
commit
06ce8247cc
24 changed files with 267 additions and 126 deletions
39
server/src/utils/preferences.ts
Normal file
39
server/src/utils/preferences.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import _ from 'lodash';
|
||||
import { UserMetadataKey, UserPreferences, getDefaultPreferences } from 'src/entities/user-metadata.entity';
|
||||
import { UserEntity } from 'src/entities/user.entity';
|
||||
import { getKeysDeep } from 'src/utils/misc';
|
||||
import { DeepPartial } from 'typeorm';
|
||||
|
||||
export const getPreferences = (user: UserEntity) => {
|
||||
const preferences = getDefaultPreferences(user);
|
||||
if (!user.metadata) {
|
||||
return preferences;
|
||||
}
|
||||
|
||||
const item = user.metadata.find(({ key }) => key === UserMetadataKey.PREFERENCES);
|
||||
const partial = item?.value || {};
|
||||
for (const property of getKeysDeep(partial)) {
|
||||
_.set(preferences, property, _.get(partial, property));
|
||||
}
|
||||
|
||||
return preferences;
|
||||
};
|
||||
|
||||
export const getPreferencesPartial = (user: { email: string }, newPreferences: UserPreferences) => {
|
||||
const defaultPreferences = getDefaultPreferences(user);
|
||||
const partial: DeepPartial<UserPreferences> = {};
|
||||
for (const property of getKeysDeep(defaultPreferences)) {
|
||||
const newValue = _.get(newPreferences, property);
|
||||
const isEmpty = newValue === undefined || newValue === null || newValue === '';
|
||||
const defaultValue = _.get(defaultPreferences, property);
|
||||
const isEqual = newValue === defaultValue || _.isEqual(newValue, defaultValue);
|
||||
|
||||
if (isEmpty || isEqual) {
|
||||
continue;
|
||||
}
|
||||
|
||||
_.set(partial, property, newValue);
|
||||
}
|
||||
|
||||
return partial;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue