2024-05-22 14:13:36 +02:00
|
|
|
import { UserEntity } from 'src/entities/user.entity';
|
2024-08-15 12:57:01 +02:00
|
|
|
import { UserAvatarColor, UserMetadataKey } from 'src/enum';
|
2024-06-14 17:27:12 +02:00
|
|
|
import { HumanReadableSize } from 'src/utils/bytes';
|
2024-05-22 14:13:36 +02:00
|
|
|
import { Column, DeepPartial, Entity, ManyToOne, PrimaryColumn } from 'typeorm';
|
|
|
|
|
|
|
|
@Entity('user_metadata')
|
|
|
|
export class UserMetadataEntity<T extends keyof UserMetadata = UserMetadataKey> {
|
|
|
|
@PrimaryColumn({ type: 'uuid' })
|
|
|
|
userId!: string;
|
|
|
|
|
|
|
|
@ManyToOne(() => UserEntity, (user) => user.metadata, { onUpdate: 'CASCADE', onDelete: 'CASCADE' })
|
|
|
|
user!: UserEntity;
|
|
|
|
|
|
|
|
@PrimaryColumn({ type: 'varchar' })
|
|
|
|
key!: T;
|
|
|
|
|
|
|
|
@Column({ type: 'jsonb' })
|
|
|
|
value!: UserMetadata[T];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface UserPreferences {
|
2024-08-29 21:29:04 +02:00
|
|
|
folders: {
|
2024-08-09 19:45:52 +02:00
|
|
|
enabled: boolean;
|
2024-08-29 21:29:04 +02:00
|
|
|
sidebarWeb: boolean;
|
2024-08-09 19:45:52 +02:00
|
|
|
};
|
2024-05-22 14:13:36 +02:00
|
|
|
memories: {
|
|
|
|
enabled: boolean;
|
|
|
|
};
|
2024-08-29 21:29:04 +02:00
|
|
|
people: {
|
|
|
|
enabled: boolean;
|
|
|
|
sidebarWeb: boolean;
|
|
|
|
};
|
|
|
|
ratings: {
|
|
|
|
enabled: boolean;
|
|
|
|
};
|
|
|
|
tags: {
|
|
|
|
enabled: boolean;
|
|
|
|
sidebarWeb: boolean;
|
|
|
|
};
|
2024-05-22 14:13:36 +02:00
|
|
|
avatar: {
|
|
|
|
color: UserAvatarColor;
|
|
|
|
};
|
2024-06-03 23:00:20 +02:00
|
|
|
emailNotifications: {
|
|
|
|
enabled: boolean;
|
|
|
|
albumInvite: boolean;
|
|
|
|
albumUpdate: boolean;
|
|
|
|
};
|
2024-06-14 17:27:12 +02:00
|
|
|
download: {
|
|
|
|
archiveSize: number;
|
2024-08-15 22:06:16 +02:00
|
|
|
includeEmbeddedVideos: boolean;
|
2024-06-14 17:27:12 +02:00
|
|
|
};
|
2024-07-26 17:34:35 +02:00
|
|
|
purchase: {
|
|
|
|
showSupportBadge: boolean;
|
|
|
|
hideBuyButtonUntil: string;
|
|
|
|
};
|
2024-05-22 14:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getDefaultPreferences = (user: { email: string }): UserPreferences => {
|
|
|
|
const values = Object.values(UserAvatarColor);
|
|
|
|
const randomIndex = Math.floor(
|
|
|
|
[...user.email].map((letter) => letter.codePointAt(0) ?? 0).reduce((a, b) => a + b, 0) % values.length,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
2024-08-29 21:29:04 +02:00
|
|
|
folders: {
|
2024-08-09 19:45:52 +02:00
|
|
|
enabled: false,
|
2024-08-29 21:29:04 +02:00
|
|
|
sidebarWeb: false,
|
2024-08-09 19:45:52 +02:00
|
|
|
},
|
2024-05-22 14:13:36 +02:00
|
|
|
memories: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
2024-08-29 21:29:04 +02:00
|
|
|
people: {
|
|
|
|
enabled: true,
|
|
|
|
sidebarWeb: false,
|
|
|
|
},
|
|
|
|
ratings: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
tags: {
|
|
|
|
enabled: false,
|
|
|
|
sidebarWeb: false,
|
|
|
|
},
|
2024-05-22 14:13:36 +02:00
|
|
|
avatar: {
|
|
|
|
color: values[randomIndex],
|
|
|
|
},
|
2024-06-03 23:00:20 +02:00
|
|
|
emailNotifications: {
|
|
|
|
enabled: true,
|
|
|
|
albumInvite: true,
|
|
|
|
albumUpdate: true,
|
|
|
|
},
|
2024-06-14 17:27:12 +02:00
|
|
|
download: {
|
|
|
|
archiveSize: HumanReadableSize.GiB * 4,
|
2024-08-15 22:06:16 +02:00
|
|
|
includeEmbeddedVideos: false,
|
2024-06-14 17:27:12 +02:00
|
|
|
},
|
2024-07-26 17:34:35 +02:00
|
|
|
purchase: {
|
|
|
|
showSupportBadge: true,
|
|
|
|
hideBuyButtonUntil: new Date(2022, 1, 12).toISOString(),
|
|
|
|
},
|
2024-05-22 14:13:36 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface UserMetadata extends Record<UserMetadataKey, Record<string, any>> {
|
|
|
|
[UserMetadataKey.PREFERENCES]: DeepPartial<UserPreferences>;
|
2024-07-01 19:43:16 +02:00
|
|
|
[UserMetadataKey.LICENSE]: { licenseKey: string; activationKey: string; activatedAt: Date };
|
2024-05-22 14:13:36 +02:00
|
|
|
}
|