mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-18 20:49:36 +01:00
core: reduce unnecessary syncing during password change
This commit is contained in:
committed by
Abdullah Atta
parent
3465a816c6
commit
aa3d778ad1
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
@@ -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))) {
|
||||
|
||||
@@ -454,44 +454,45 @@ 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._db.sync(false, true);
|
||||
|
||||
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
|
||||
);
|
||||
await this._storage.deriveCryptoKey(`_uk_@${email}`, {
|
||||
password: new_password,
|
||||
salt
|
||||
});
|
||||
|
||||
if (!(await this.resetUser(false))) return;
|
||||
|
||||
await this._db.sync({ type: "send", 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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user