core: add support for setting user profile

This commit is contained in:
Abdullah Atta
2024-02-28 23:21:22 +05:00
committed by Abdullah Atta
parent 6440db28bd
commit fc31cc49f6
3 changed files with 59 additions and 30 deletions

View File

@@ -17,40 +17,14 @@ 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 "../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();

View File

@@ -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;

View File

@@ -505,6 +505,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";
}