From 803ab38fad68d62493dfd8395f5160ac8176a1b5 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Fri, 12 Aug 2022 01:46:42 +0200 Subject: Move UserSettings to own entity --- src/util/entities/User.ts | 175 +++++++++++----------------------------------- 1 file changed, 40 insertions(+), 135 deletions(-) (limited to 'src/util/entities/User.ts') diff --git a/src/util/entities/User.ts b/src/util/entities/User.ts index ea726368..79ea6c3c 100644 --- a/src/util/entities/User.ts +++ b/src/util/entities/User.ts @@ -6,11 +6,15 @@ import { FindOneOptions, JoinColumn, OneToMany, + OneToOne, } from "typeorm"; import { BaseClass } from "./BaseClass"; import { BitField } from "../util/BitField"; import { Relationship } from "./Relationship"; import { ConnectedAccount } from "./ConnectedAccount"; +import { Member } from "./Member"; +import { UserSettings } from "./UserSettings"; +import { Session } from "./Session"; import { Config, FieldErrors, @@ -18,7 +22,6 @@ import { trimSpecial, adjustEmail, } from ".."; -import { Member, Session } from "."; export enum PublicUserEnum { username, @@ -46,7 +49,7 @@ export enum PrivateUserEnum { purchased_flags, premium_usage_flags, disabled, - settings, + // settings, // now a relation // locale } export type PrivateUserKeys = keyof typeof PrivateUserEnum | PublicUserKeys; @@ -89,67 +92,67 @@ export class User extends BaseClass { phone?: string; // phone number of the user @Column({ select: false }) - desktop: boolean; // if the user has desktop app installed + desktop: boolean = false; // if the user has desktop app installed @Column({ select: false }) - mobile: boolean; // if the user has mobile app installed + mobile: boolean = false; // if the user has mobile app installed @Column() - premium: boolean; // if user bought individual premium + premium: boolean = false; // if user bought individual premium @Column() - premium_type: number; // individual premium level + premium_type: number = 0; // individual premium level @Column() - bot: boolean; // if user is bot + bot: boolean = false; // if user is bot @Column() - bio: string; // short description of the user (max 190 chars -> should be configurable) + bio: string = ""; // short description of the user (max 190 chars -> should be configurable) @Column() - system: boolean; // shouldn't be used, the api sends this field type true, if the generated message comes from a system generated author + system: boolean = false; // shouldn't be used, the api sends this field type true, if the generated message comes from a system generated author @Column({ select: false }) - nsfw_allowed: boolean; // if the user can do age-restricted actions (NSFW channels/guilds/commands) + nsfw_allowed: boolean = true; // if the user can do age-restricted actions (NSFW channels/guilds/commands) // TODO: depending on age @Column({ select: false }) - mfa_enabled: boolean; // if multi factor authentication is enabled + mfa_enabled: boolean = false; // if multi factor authentication is enabled @Column({ select: false, nullable: true }) - totp_secret?: string; + totp_secret?: string = ""; @Column({ nullable: true, select: false }) - totp_last_ticket?: string; + totp_last_ticket?: string = ""; @Column() - created_at: Date; // registration date + created_at: Date = new Date(); // registration date @Column({ nullable: true }) - premium_since: Date; // premium date + premium_since: Date = new Date(); // premium date @Column({ select: false }) - verified: boolean; // if the user is offically verified + verified: boolean = true; // email is verified @Column() - disabled: boolean; // if the account is disabled + disabled: boolean = false; // if the account is disabled @Column() - deleted: boolean; // if the user was deleted + deleted: boolean = false; // if the user was deleted @Column({ nullable: true, select: false }) email?: string; // email of the user @Column() - flags: string; // UserFlags + flags: string = "0"; // UserFlags // TODO: generate @Column() - public_flags: number; + public_flags: number = 0; @Column() - purchased_flags: number; + purchased_flags: number = 0; @Column() - premium_usage_flags: number; + premium_usage_flags: number = 0; @Column({ type: "bigint" }) rights: string; // Rights @@ -186,14 +189,19 @@ export class User extends BaseClass { }; @Column({ type: "simple-array", select: false }) - fingerprints: string[]; // array of fingerprints -> used to prevent multiple accounts - - @Column({ type: "simple-json", select: false }) + fingerprints: string[] = []; // array of fingerprints -> used to prevent multiple accounts + + @OneToOne(() => UserSettings, { + cascade: true, + orphanedRowAction: "delete", + eager: false + }) + @JoinColumn() settings: UserSettings; // workaround to prevent fossord-unaware clients from deleting settings not used by them @Column({ type: "simple-json", select: false }) - extended_settings: string; + extended_settings: string = "{}"; // TODO: I don't like this method? validate() { @@ -327,40 +335,25 @@ export class User extends BaseClass { req.language === "en" ? "en-US" : req.language || "en-US"; const user = User.create({ - created_at: new Date(), username: username, discriminator, id: Snowflake.generate(), - bot: false, - system: false, - premium_since: new Date(), - desktop: false, - mobile: false, - premium: true, - premium_type: 2, - bio: "", - mfa_enabled: false, - verified: true, - disabled: false, - deleted: false, email: email, rights: Config.get().register.defaultRights, - nsfw_allowed: true, // TODO: depending on age - public_flags: 0, - flags: "0", // TODO: generate data: { hash: password, valid_tokens_since: new Date(), }, - settings: { ...defaultSettings, locale: language }, - purchased_flags: 5, // TODO: idk what the values for this are - premium_usage_flags: 2, // TODO: idk what the values for this are - extended_settings: "", // TODO: was {} - fingerprints: [], + extended_settings: "{}", + premium_type: Config.get().defaults.user.premium_type, + premium: Config.get().defaults.user.premium, + verified: Config.get().defaults.user.verified, + settings: { ...new UserSettings(), locale: language } }); user.validate(); await user.save(); + await user.settings.save(); setImmediate(async () => { if (Config.get().guild.autoJoin.enabled) { @@ -374,94 +367,6 @@ export class User extends BaseClass { } } -export const defaultSettings: UserSettings = { - afk_timeout: 3600, - allow_accessibility_detection: true, - animate_emoji: true, - animate_stickers: 0, - contact_sync_enabled: false, - convert_emoticons: false, - custom_status: null, - default_guilds_restricted: false, - detect_platform_accounts: false, - developer_mode: true, - disable_games_tab: true, - enable_tts_command: false, - explicit_content_filter: 0, - friend_source_flags: { all: true }, - gateway_connected: false, - gif_auto_play: true, - guild_folders: [], - guild_positions: [], - inline_attachment_media: true, - inline_embed_media: true, - locale: "en-US", - message_display_compact: false, - native_phone_integration_enabled: true, - render_embeds: true, - render_reactions: true, - restricted_guilds: [], - show_current_game: true, - status: "online", - stream_notifications_enabled: false, - theme: "dark", - timezone_offset: 0, // TODO: timezone from request - - banner_color: null, - friend_discovery_flags: 0, - view_nsfw_guilds: true, - passwordless: false, -}; - -export interface UserSettings { - afk_timeout: number; - allow_accessibility_detection: boolean; - animate_emoji: boolean; - animate_stickers: number; - contact_sync_enabled: boolean; - convert_emoticons: boolean; - custom_status: { - emoji_id?: string; - emoji_name?: string; - expires_at?: number; - text?: string; - } | null; - default_guilds_restricted: boolean; - detect_platform_accounts: boolean; - developer_mode: boolean; - disable_games_tab: boolean; - enable_tts_command: boolean; - explicit_content_filter: number; - friend_source_flags: { all: boolean; }; - gateway_connected: boolean; - gif_auto_play: boolean; - // every top guild is displayed as a "folder" - guild_folders: { - color?: number; - guild_ids: string[]; - id?: number; - name?: string; - }[]; - guild_positions: string[]; // guild ids ordered by position - inline_attachment_media: boolean; - inline_embed_media: boolean; - locale: string; // en_US - message_display_compact: boolean; - native_phone_integration_enabled: boolean; - render_embeds: boolean; - render_reactions: boolean; - restricted_guilds: string[]; - show_current_game: boolean; - status: "online" | "offline" | "dnd" | "idle" | "invisible"; - stream_notifications_enabled: boolean; - theme: "dark" | "light"; // dark - timezone_offset: number; // e.g -60 - banner_color: string | null; - friend_discovery_flags: number; - view_nsfw_guilds: boolean; - passwordless: boolean; -} - export const CUSTOM_USER_FLAG_OFFSET = BigInt(1) << BigInt(32); export class UserFlags extends BitField { -- cgit 1.4.1