A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
core: add support for setting user profile
Abdullah Attae58fc38a6df4
diff --git a/packages/core/src/api/user-manager.ts b/packages/core/src/api/user-manager.tsindex 1e26054ff..c59cce557 100644--- a/packages/core/src/api/user-manager.ts+++ b/packages/core/src/api/user-manager.ts@@ -17,40 +17,14 @@ You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/-import "../types";+import { User, Profile } from "../types";import http from "../utils/http";import constants from "../utils/constants";import TokenManager from "./token-manager";import { EV, EVENTS } from "../common";import { HealthCheck } from "./healthcheck";import Database from ".";-import { Cipher, SerializedKey } from "@notesnook/crypto";--export type AuthenticatorType = "app" | "sms" | "email";-export type User = {- id: string;- email: string;- isEmailConfirmed: boolean;- salt: string;- attachmentsKey?: Cipher<"base64">;- marketingConsent?: boolean;- mfa: {- isEnabled: boolean;- primaryMethod: AuthenticatorType;- secondaryMethod?: AuthenticatorType;- remainingValidCodes: number;- };- subscription: {- appId: 0;- cancelURL: string | null;- expiry: number;- productId: string;- provider: 0 | 1 | 2 | 3;- start: number;- type: 0 | 1 | 2 | 5 | 6 | 7;- updateURL: string | null;- };-};+import { SerializedKey } from "@notesnook/crypto";const ENDPOINTS = {signup: "/users",@@ -301,7 +275,7 @@ class UserManager {return true;}- async updateUser(user: User) {+ private async updateUser(user: User) {const token = await this.tokenManager.getAccessToken();await http.patch.json(`${constants.API_HOST}${ENDPOINTS.user}`,@@ -312,6 +286,28 @@ class UserManager {await this.setUser(user);}+ async setProfile(profile: Partial<Profile>) {+ const user = await this.getUser();+ const key = await this.getEncryptionKey();+ if (!user || !key) return;++ user.profile = await this.db+ .storage()+ .encrypt(key, JSON.stringify({ ...user.profile, ...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;+ }+async deleteUser(password: string) {const token = await this.tokenManager.getAccessToken();const user = await this.getUser();diff --git a/packages/core/src/database/kv.ts b/packages/core/src/database/kv.tsindex 5917adca0..5eb6c9174 100644--- a/packages/core/src/database/kv.ts+++ b/packages/core/src/database/kv.ts@@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { DatabaseAccessor, RawDatabaseSchema } from ".";import { Token } from "../api/token-manager";-import { User } from "../api/user-manager";+import { User } from "../types";interface KV {v: number;diff --git a/packages/core/src/types.ts b/packages/core/src/types.tsindex 31bd365e6..062f50bea 100644--- a/packages/core/src/types.ts+++ b/packages/core/src/types.ts@@ -506,6 +506,39 @@ export type BaseTrashItem<TItem extends BaseItem<"note" | "notebook">> =export type TrashItem = BaseTrashItem<Note> | BaseTrashItem<Notebook>;+export type AuthenticatorType = "app" | "sms" | "email";++export type User = {+ id: string;+ email: string;+ isEmailConfirmed: boolean;+ salt: string;+ attachmentsKey?: Cipher<"base64">;+ profile?: Cipher<"base64">;+ marketingConsent?: boolean;+ mfa: {+ isEnabled: boolean;+ primaryMethod: AuthenticatorType;+ secondaryMethod?: AuthenticatorType;+ remainingValidCodes: number;+ };+ subscription: {+ appId: 0;+ cancelURL: string | null;+ expiry: number;+ productId: string;+ provider: 0 | 1 | 2 | 3;+ start: number;+ type: 0 | 1 | 2 | 5 | 6 | 7;+ updateURL: string | null;+ };+};++export type Profile = {+ fullName?: string;+ profilePicture?: string;+};+export function isDeleted(item: any): item is DeletedItem {return !!item.deleted && item.type !== "trash";}