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: reduce unnecessary syncing during password change
Abdullah Attaaa3d778ad156
diff --git a/packages/core/src/api/index.js b/packages/core/src/api/index.jsindex f9f086690..02df14c31 100644--- a/packages/core/src/api/index.js+++ b/packages/core/src/api/index.js@@ -33,7 +33,6 @@ import Constants from "../utils/constants";import { EV, EVENTS } from "../common";import Settings from "./settings";import Migrations from "./migrations";-import Outbox from "./outbox";import UserManager from "./user-manager";import http from "../utils/http";import Monographs from "./monographs";@@ -90,7 +89,6 @@ class Database {this.backup = new Backup(this);this.settings = new Settings(this);this.migrations = new Migrations(this);- this.outbox = new Outbox(this);this.monographs = new Monographs(this);this.offers = new Offers();this.debug = new Debug();@@ -127,7 +125,6 @@ class Database {await this.initCollections();- await this.outbox.init();await this.migrations.init();this.isInitialized = true;if (this.migrations.required()) {@@ -275,8 +272,17 @@ class Database {return (await this.storage.read("lastSynced")) || 0;}- sync(full = true, force = false, lastSynced = null) {- return this.syncer.start(full, force, lastSynced);+ /**+ *+ * @param {{+ * type: "full" | "fetch" | "send";+ * force?: boolean;+ * serverLastSynced?: number;+ * }} options+ * @returns+ */+ sync(options) {+ return this.syncer.start(options);}/**diff --git a/packages/core/src/api/outbox.js b/packages/core/src/api/outbox.jsdeleted file mode 100644index 2cab3e578..000000000--- a/packages/core/src/api/outbox.js+++ /dev/null@@ -1,57 +0,0 @@-/*-This file is part of the Notesnook project (https://notesnook.com/)--Copyright (C) 2023 Streetwriters (Private) Limited--This program is free software: you can redistribute it and/or modify-it under the terms of the GNU General Public License as published by-the Free Software Foundation, either version 3 of the License, or-(at your option) any later version.--This program is distributed in the hope that it will be useful,-but WITHOUT ANY WARRANTY; without even the implied warranty of-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-GNU General Public License for more details.--You should have received a copy of the GNU General Public License-along with this program. If not, see <http://www.gnu.org/licenses/>.-*/--class Outbox {- /**- *- * @param {import("./index").default} db- */- constructor(db) {- this._db = db;- this.outbox = {};- }-- async init() {- this.outbox = (await this._db.storage.read("outbox")) || {};-- for (var id in this.outbox) {- const data = this.outbox[id];- switch (id) {- case "reset_password":- case "change_password":- if (await this._db.user._updatePassword(id, data))- await this.delete(id);- break;- }- }- }-- async add(id, data, action) {- this.outbox[id] = data;- await this._db.storage.write("outbox", this.outbox);- await action();- await this.delete(id);- }-- delete(id) {- delete this.outbox[id];- return this._db.storage.write("outbox", this.outbox);- }-}-export default Outbox;diff --git a/packages/core/src/api/sync/index.js b/packages/core/src/api/sync/index.jsindex d44daf34f..58fbeca50 100644--- a/packages/core/src/api/sync/index.js+++ b/packages/core/src/api/sync/index.js@@ -53,10 +53,10 @@ export default class SyncManager {this._db = db;}- async start(full, force, serverLastSynced) {+ async start(options) {try {await this.sync.autoSync.start();- await this.sync.start(full, force, serverLastSynced);+ await this.sync.start(options);return true;} catch (e) {var isHubException = e.message.includes("HubException:");@@ -176,18 +176,20 @@ class Sync {/***- * @param {boolean} full- * @param {boolean} force- * @param {number} serverLastSynced+ * @param {{+ * type: "full" | "fetch" | "send";+ * force?: boolean;+ * serverLastSynced?: number;+ * }} options*/- async start(full, force, serverLastSynced) {+ async start(options) {if (!(await checkSyncStatus(SYNC_CHECK_IDS.sync))) {await this.connection.stop();return;}if (!(await this.db.user.getUser())) return;- this.logger.info("Starting sync", { full, force, serverLastSynced });+ this.logger.info("Starting sync", options);this.connection.onclose((error) => {this.db.eventManager.publish(EVENTS.syncAborted);@@ -196,15 +198,21 @@ class Sync {throw new Error("Connection closed.");});- const { lastSynced, oldLastSynced } = await this.init(force);+ const { lastSynced, oldLastSynced } = await this.init(options.force);this.logger.info("Initialized sync", { lastSynced, oldLastSynced });const newLastSynced = Date.now();- const serverResponse = full ? await this.fetch(lastSynced) : null;+ const serverResponse =+ options.type === "fetch" || options.type === "full"+ ? await this.fetch(lastSynced)+ : null;this.logger.info("Data fetched", serverResponse);- if (await this.send(lastSynced, force, newLastSynced)) {+ if (+ (options.type === "send" || options.type === "full") &&+ (await this.send(lastSynced, options.force, newLastSynced))+ ) {this.logger.info("New data sent");await this.stop(newLastSynced);} else if (serverResponse) {@@ -212,7 +220,7 @@ class Sync {await this.stop(serverResponse.lastSynced);} else {this.logger.info("Nothing to do.");- await this.stop(serverLastSynced || oldLastSynced);+ await this.stop(options.serverLastSynced || oldLastSynced);}if (!(await checkSyncStatus(SYNC_CHECK_IDS.autoSync))) {diff --git a/packages/core/src/api/user-manager.js b/packages/core/src/api/user-manager.jsindex 378ee9d1d..18eeacf6e 100644--- a/packages/core/src/api/user-manager.js+++ b/packages/core/src/api/user-manager.js@@ -454,43 +454,44 @@ class UserManager {const attachmentsKey = await this.getAttachmentsKey();data.encryptionKey = data.encryptionKey || (await this.getEncryptionKey());- await this._db.outbox.add(type, data, async () => {- if (data.encryptionKey) await this._db.sync(true, true);- await this._storage.deriveCryptoKey(`_uk_@${email}`, {- password: new_password,- salt- });+ await this.clearSessions();- if (!(await this.resetUser(false))) return;+ if (data.encryptionKey) await this._db.sync({ type: "fetch", force: true });- if (attachmentsKey) {- const userEncryptionKey = await this.getEncryptionKey();- if (!userEncryptionKey) return;- user.attachmentsKey = await this._storage.encrypt(- userEncryptionKey,- JSON.stringify(attachmentsKey)- );- await this.updateUser(user);- }+ await this._storage.deriveCryptoKey(`_uk_@${email}`, {+ password: new_password,+ salt+ });- await this._db.sync(false, true);+ if (!(await this.resetUser(false))) return;- if (old_password)- old_password = await this._storage.hash(old_password, email);- if (new_password)- new_password = await this._storage.hash(new_password, email);+ await this._db.sync({ type: "send", force: true });- await http.patch(- `${constants.AUTH_HOST}${ENDPOINTS.patchUser}`,- {- type,- old_password,- new_password- },- token+ if (attachmentsKey) {+ const userEncryptionKey = await this.getEncryptionKey();+ if (!userEncryptionKey) return;+ user.attachmentsKey = await this._storage.encrypt(+ userEncryptionKey,+ JSON.stringify(attachmentsKey));- });+ await this.updateUser(user);+ }++ if (old_password)+ old_password = await this._storage.hash(old_password, email);+ if (new_password)+ new_password = await this._storage.hash(new_password, email);++ await http.patch(+ `${constants.AUTH_HOST}${ENDPOINTS.patchUser}`,+ {+ type,+ old_password,+ new_password+ },+ token+ );return true;}