From d32db74ccc9452b29551e28455e8dbdf42273072 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Thu, 21 Mar 2024 13:16:21 +0500 Subject: [PATCH] core: move user profile to settings collection --- packages/core/src/api/user-manager.ts | 38 ++++++----------------- packages/core/src/collections/settings.ts | 14 +++++++++ packages/core/src/database/index.ts | 3 +- packages/core/src/types.ts | 2 +- 4 files changed, 26 insertions(+), 31 deletions(-) diff --git a/packages/core/src/api/user-manager.ts b/packages/core/src/api/user-manager.ts index 528c9a283..effeb544c 100644 --- a/packages/core/src/api/user-manager.ts +++ b/packages/core/src/api/user-manager.ts @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -import { User, Profile } from "../types"; +import { User } from "../types"; import http from "../utils/http"; import constants from "../utils/constants"; import TokenManager from "./token-manager"; @@ -275,38 +275,18 @@ class UserManager { return true; } - private async updateUser(user: User) { + private async updateUser(partial: Partial) { + const user = await this.getUser(); + if (!user) return; + const token = await this.tokenManager.getAccessToken(); await http.patch.json( `${constants.API_HOST}${ENDPOINTS.user}`, - user, + partial, token ); - await this.setUser(user); - } - - async setProfile(profile: Partial) { - const user = await this.getUser(); - const key = await this.getEncryptionKey(); - const userProfile = await this.getProfile(); - if (!user || !key) return; - - user.profile = await this.db - .storage() - .encrypt(key, JSON.stringify({ ...userProfile, ...profile })); - await this.updateUser(user); - } - - async getProfile() { - const user = await this.getUser(); - const key = await this.getEncryptionKey(); - if (!user || !key || !user.profile) return; - - const profile = JSON.parse( - await this.db.storage().decrypt(key, user.profile) - ); - return profile as Profile; + await this.setUser({ ...user, ...partial }); } async deleteUser(password: string) { @@ -410,7 +390,7 @@ class UserManager { .storage() .encrypt(userEncryptionKey, JSON.stringify(key)); - await this.updateUser(user); + await this.updateUser({ attachmentsKey: user.attachmentsKey }); return key; } @@ -527,7 +507,7 @@ class UserManager { user.attachmentsKey = await this.db .storage() .encrypt(userEncryptionKey, JSON.stringify(attachmentsKey)); - await this.updateUser(user); + await this.updateUser({ attachmentsKey: user.attachmentsKey }); } if (old_password) diff --git a/packages/core/src/collections/settings.ts b/packages/core/src/collections/settings.ts index 136e83407..9131d4a44 100644 --- a/packages/core/src/collections/settings.ts +++ b/packages/core/src/collections/settings.ts @@ -22,6 +22,7 @@ import Database from "../api"; import { GroupOptions, GroupingKey, + Profile, SettingItem, SettingItemMap, SideBarHideableSection, @@ -54,6 +55,7 @@ const defaultSettings: SettingItemMap = { titleFormat: "Note $date$ $time$", defaultNotebook: undefined, trashCleanupInterval: 7, + profile: undefined, "groupOptions:trash": DEFAULT_GROUP_OPTIONS("trash"), "groupOptions:tags": DEFAULT_GROUP_OPTIONS("tags"), @@ -195,4 +197,16 @@ export class Settings implements ICollection { setSideBarHiddenItems(section: SideBarHideableSection, ids: string[]) { return this.set(`sideBarHiddenItems:${section}`, ids); } + + getProfile() { + return this.get("profile"); + } + + setProfile(partial: Partial | undefined) { + const profile = + partial === undefined || (!partial.fullName && !partial.profilePicture) + ? undefined + : { ...this.getProfile(), ...partial }; + return this.set("profile", profile); + } } diff --git a/packages/core/src/database/index.ts b/packages/core/src/database/index.ts index 62df0ad48..13b33a461 100644 --- a/packages/core/src/database/index.ts +++ b/packages/core/src/database/index.ts @@ -235,7 +235,8 @@ const DataMappers: Partial void>> = { (row.key.startsWith("groupOptions") || row.key.startsWith("toolbarConfig") || row.key.startsWith("sideBarOrder") || - row.key.startsWith("sideBarHiddenItems")) + row.key.startsWith("sideBarHiddenItems") || + row.key.startsWith("profile")) ) row.value = JSON.parse(row.value); }, diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 9ab215cce..b2e0d4d15 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -456,6 +456,7 @@ export type SettingItemMap = { timeFormat: TimeFormat; dateFormat: string; defaultNotebook: string | undefined; + profile: Profile | undefined; } & Record<`groupOptions:${GroupingKey}`, GroupOptions> & Record<`toolbarConfig:${ToolbarConfigPlatforms}`, ToolbarConfig | undefined> & Record<`sideBarOrder:${SideBarSection}`, string[]> & @@ -515,7 +516,6 @@ export type User = { isEmailConfirmed: boolean; salt: string; attachmentsKey?: Cipher<"base64">; - profile?: Cipher<"base64">; marketingConsent?: boolean; mfa: { isEnabled: boolean;