mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
core: move user profile to settings collection
This commit is contained in:
@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<User>) {
|
||||
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<Profile>) {
|
||||
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)
|
||||
|
||||
@@ -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<Profile> | undefined) {
|
||||
const profile =
|
||||
partial === undefined || (!partial.fullName && !partial.profilePicture)
|
||||
? undefined
|
||||
: { ...this.getProfile(), ...partial };
|
||||
return this.set("profile", profile);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,7 +235,8 @@ const DataMappers: Partial<Record<ItemType, (row: any) => 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);
|
||||
},
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user