Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: multiple settings objects created for 1 user

thecodrrdd2a065be793
diff --git a/packages/core/api/settings.js b/packages/core/api/settings.js
index e7c0aec73..07a47f9c1 100644
--- a/packages/core/api/settings.js
+++ b/packages/core/api/settings.js
@@ -1,5 +1,6 @@
import { EV, EVENTS } from "../common";
import id from "../utils/id";
+import setManipulator from "../utils/set";
class Settings {
/**
@@ -8,13 +9,7 @@ class Settings {
*/
constructor(db) {
this._db = db;
- this._settings = {
- type: "settings",
- id: id(),
- pins: [],
- dateEdited: 0,
- dateCreated: Date.now(),
- };
+ this._settings = undefined;
}
get raw() {
@@ -22,23 +17,28 @@ class Settings {
}
async merge(item) {
- this._settings = {
- ...this._settings,
- ...item,
- };
- await this._db.context.write("settings", this._settings);
+ // TODO if (this.settings.dateEdited > (await this._db.lastSynced())) {
+ // this._settings.pins = setManipulator.union(
+ // this._settings.pins,
+ // item.pins
+ // );
+ // }
+ this._settings = item;
+ await this._db.context.write("settings", item);
}
async init() {
var settings = await this._db.context.read("settings");
- if (!settings) await this._db.context.write("settings", this._settings);
- else this._settings = settings;
+ this._settings = settings;
EV.subscribe(EVENTS.userLoggedOut, () => {
+ this._settings = undefined;
+ });
+ EV.subscribe(EVENTS.userSignedUp, () => {
this._settings = {
type: "settings",
id: id(),
pins: [],
- dateEdited: 0,
+ dateEdited: Date.now(),
dateCreated: Date.now(),
};
});
@@ -66,6 +66,7 @@ class Settings {
}
get pins() {
+ if (!this._settings) return [];
return this._settings.pins.reduce((prev, pin) => {
let item;
if (pin.type === "notebook") {
diff --git a/packages/core/api/sync/collector.js b/packages/core/api/sync/collector.js
index 8696ffb5c..cf49afffb 100644
--- a/packages/core/api/sync/collector.js
+++ b/packages/core/api/sync/collector.js
@@ -24,10 +24,10 @@ class Collector {
};
}
- async collect(lastSyncedTimestamp, force) {
+ async collect(lastSyncedTimestamp) {
this._lastSyncedTimestamp = lastSyncedTimestamp;
this.key = await this._db.user.getEncryptionKey();
- this.force = force;
+
return {
notes: await this._collect(this._db.notes.raw),
notebooks: await this._collect(this._db.notebooks.raw),
@@ -44,11 +44,9 @@ class Collector {
}
_collect(array) {
- if (this.force) {
- return Promise.all(array.map(this._map));
- }
return Promise.all(
array.reduce((prev, item) => {
+ if (!item) return prev;
if (item.dateEdited > this._lastSyncedTimestamp || item.migrated)
prev.push(this._map(item));
return prev;
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index 54c1b0d57..bd7a15100 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -63,12 +63,14 @@ export default class Sync {
}
async start(full, force) {
+ if (force) await this._db.context.write("lastSynced", 0);
+
let { lastSynced, token } = await this._performChecks();
if (full) var serverResponse = await this._fetch(lastSynced, token);
// we prepare local data before merging so we always have correct data
- const data = await this._collector.collect(lastSynced, force);
+ const data = await this._collector.collect(lastSynced);
if (full) {
// merge the server response
diff --git a/packages/core/api/user-manager.js b/packages/core/api/user-manager.js
index 754ab52bb..11a5ce5c0 100644
--- a/packages/core/api/user-manager.js
+++ b/packages/core/api/user-manager.js
@@ -39,6 +39,7 @@ class UserManager {
password: hashedPassword,
client_id: "notesnook",
});
+ EV.publish(EVENTS.userSignedUp);
return await this.login(email, password, true, hashedPassword);
}
@@ -198,7 +199,7 @@ class UserManager {
if (!token) return;
// we hash the passwords beforehand
- const { email } = await this.getUser();
+ const { email, salt } = await this.getUser();
var hashedData = {};
if (data.old_password)
hashedData.old_password = await this._db.context.hash(
@@ -223,13 +224,14 @@ class UserManager {
type,
{ newPassword: data.new_password },
async () => {
- const key = await this.getEncryptionKey();
- const { email } = await this.getUser();
+ await this._db.sync(true);
+
await this._db.context.deriveCryptoKey(`_uk_@${email}`, {
password: data.new_password,
- salt: key.salt,
+ salt,
});
- await this._db.sync(true, true);
+
+ await this._db.sync(false, true);
}
);
return true;
diff --git a/packages/core/common.js b/packages/core/common.js
index 2c3448e2f..9e51dacb0 100644
--- a/packages/core/common.js
+++ b/packages/core/common.js
@@ -25,6 +25,7 @@ export const EVENTS = {
userLoggedIn: "user:loggedIn",
userLoggedOut: "user:loggedOut",
userFetched: "user:fetched",
+ userSignedUp: "user:signedUp",
databaseSyncRequested: "db:syncRequested",
databaseMigrated: "db:migrated",
databaseUpdated: "db:updated",