mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-21 05:59:35 +01:00
due to these multiple objects reset password wasn't working because only the latest one would be encrypted with the new password but all previous objects would require decryption which resulted in a block while syncing
88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
import { EV, EVENTS } from "../common";
|
|
import id from "../utils/id";
|
|
import setManipulator from "../utils/set";
|
|
|
|
class Settings {
|
|
/**
|
|
*
|
|
* @param {import("./index").default} db
|
|
*/
|
|
constructor(db) {
|
|
this._db = db;
|
|
this._settings = undefined;
|
|
}
|
|
|
|
get raw() {
|
|
return this._settings;
|
|
}
|
|
|
|
async merge(item) {
|
|
// 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");
|
|
this._settings = settings;
|
|
EV.subscribe(EVENTS.userLoggedOut, () => {
|
|
this._settings = undefined;
|
|
});
|
|
EV.subscribe(EVENTS.userSignedUp, () => {
|
|
this._settings = {
|
|
type: "settings",
|
|
id: id(),
|
|
pins: [],
|
|
dateEdited: Date.now(),
|
|
dateCreated: Date.now(),
|
|
};
|
|
});
|
|
}
|
|
|
|
async pin(type, data) {
|
|
if (type !== "notebook" && type !== "topic" && type !== "tag")
|
|
throw new Error("This item cannot be pinned.");
|
|
if (this.isPinned(data.id)) return;
|
|
this._settings.pins.push({ type, data });
|
|
this._settings.dateEdited = Date.now();
|
|
await this._db.context.write("settings", this._settings);
|
|
}
|
|
|
|
async unpin(id) {
|
|
const index = this._settings.pins.findIndex((i) => i.data.id === id);
|
|
if (index <= -1) return;
|
|
this._settings.pins.splice(index, 1);
|
|
this._settings.dateEdited = Date.now();
|
|
await this._db.context.write("settings", this._settings);
|
|
}
|
|
|
|
isPinned(id) {
|
|
return this._settings.pins.findIndex((v) => v.data.id === id) > -1;
|
|
}
|
|
|
|
get pins() {
|
|
if (!this._settings) return [];
|
|
return this._settings.pins.reduce((prev, pin) => {
|
|
let item;
|
|
if (pin.type === "notebook") {
|
|
item = this._db.notebooks.notebook(pin.data.id).data;
|
|
} else if (pin.type === "topic") {
|
|
item = this._db.notebooks
|
|
.notebook(pin.data.notebookId)
|
|
.topics.topic(pin.data.id)._topic;
|
|
} else if (pin.type === "tag") {
|
|
item = this._db.tags.tag(pin.data.id);
|
|
}
|
|
if (item) prev.push(item);
|
|
else this.unpin(pin.data.id); // TODO risky.
|
|
return prev;
|
|
}, []);
|
|
}
|
|
}
|
|
export default Settings;
|