2020-02-03 12:03:07 +05:00
|
|
|
import Notes from "../collections/notes";
|
2020-04-13 12:46:29 +05:00
|
|
|
import Storage from "../database/storage";
|
2020-02-04 18:27:32 +05:00
|
|
|
import Notebooks from "../collections/notebooks";
|
2020-02-06 16:46:23 +05:00
|
|
|
import Trash from "../collections/trash";
|
2020-02-21 21:39:27 +05:00
|
|
|
import Tags from "../collections/tags";
|
2020-02-11 13:12:47 +05:00
|
|
|
import User from "../models/user";
|
2020-02-11 16:28:28 +05:00
|
|
|
import Sync from "./sync";
|
2020-03-07 12:29:55 +05:00
|
|
|
import Vault from "./vault";
|
2020-03-09 12:39:49 +05:00
|
|
|
import Lookup from "./lookup";
|
2020-03-19 11:30:05 +05:00
|
|
|
import Content from "../collections/content";
|
2020-04-16 03:04:44 +05:00
|
|
|
import Conflicts from "./sync/conflicts";
|
2020-04-16 03:19:50 +05:00
|
|
|
import EventManager from "../utils/event-manager";
|
2020-05-14 13:51:48 +05:00
|
|
|
import Session from "./session";
|
2020-08-24 11:14:16 +05:00
|
|
|
import { EventSourcePolyfill } from "event-source-polyfill";
|
|
|
|
|
import { HOST } from "../utils/constants";
|
2020-02-03 12:03:07 +05:00
|
|
|
|
2020-02-11 13:12:47 +05:00
|
|
|
class Database {
|
2020-02-03 12:03:07 +05:00
|
|
|
constructor(context) {
|
2020-04-13 12:46:29 +05:00
|
|
|
this.context = new Storage(context);
|
2020-05-04 17:23:16 +05:00
|
|
|
this._syncInterval = 0;
|
2020-02-03 12:03:07 +05:00
|
|
|
}
|
2020-04-15 23:25:53 +05:00
|
|
|
|
2020-05-14 13:56:39 +05:00
|
|
|
async _validate() {
|
2020-05-14 13:51:48 +05:00
|
|
|
if (!(await this.session.valid())) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
"Your system clock is not setup correctly. Please adjust your date and time and then retry."
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-05-14 13:56:39 +05:00
|
|
|
await this.session.set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async init() {
|
2020-08-24 11:14:16 +05:00
|
|
|
this.ev = new EventManager();
|
2020-08-24 11:25:09 +05:00
|
|
|
this.ev.subscribeMulti(
|
|
|
|
|
["user:loggedIn", "user:loggedOut", "user:tokenRefreshed", "user:synced"],
|
|
|
|
|
this._onUserStateChanged.bind(this)
|
|
|
|
|
);
|
|
|
|
|
|
2020-05-14 15:07:49 +05:00
|
|
|
this.session = new Session(this.context);
|
2020-05-14 13:56:39 +05:00
|
|
|
this._validate();
|
2020-05-14 13:51:48 +05:00
|
|
|
|
2020-04-16 03:27:22 +05:00
|
|
|
this.user = new User(this);
|
2020-05-23 17:20:24 +05:00
|
|
|
await this.user.sync();
|
|
|
|
|
|
2020-02-11 16:28:28 +05:00
|
|
|
this.syncer = new Sync(this);
|
2020-04-16 02:36:09 +05:00
|
|
|
this.vault = new Vault(this);
|
2020-04-04 13:29:33 +05:00
|
|
|
this.conflicts = new Conflicts(this);
|
2020-04-16 02:14:53 +05:00
|
|
|
this.lookup = new Lookup(this);
|
|
|
|
|
|
|
|
|
|
// collections
|
|
|
|
|
/** @type {Notes} */
|
2020-04-21 17:39:02 +05:00
|
|
|
this.notes = await Notes.new(this, "notes");
|
2020-04-16 02:14:53 +05:00
|
|
|
/** @type {Notebooks} */
|
2020-04-21 17:39:02 +05:00
|
|
|
this.notebooks = await Notebooks.new(this, "notebooks");
|
2020-04-16 02:14:53 +05:00
|
|
|
/** @type {Tags} */
|
|
|
|
|
this.tags = await Tags.new(this, "tags");
|
|
|
|
|
/** @type {Tags} */
|
|
|
|
|
this.colors = await Tags.new(this, "colors");
|
|
|
|
|
/** @type {Trash} */
|
2020-04-21 17:39:02 +05:00
|
|
|
this.trash = await Trash.new(this, "trash");
|
2020-04-16 02:14:53 +05:00
|
|
|
/** @type {Content} */
|
2020-04-16 02:39:35 +05:00
|
|
|
this.delta = await Content.new(this, "delta", false);
|
2020-04-16 02:14:53 +05:00
|
|
|
/** @type {Content} */
|
2020-04-16 02:39:35 +05:00
|
|
|
this.text = await Content.new(this, "text", false);
|
2020-08-24 11:14:16 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onUserStateChanged(user) {
|
|
|
|
|
if (this.evtSource) {
|
|
|
|
|
this.evtSource.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!user) return;
|
2020-08-24 11:29:39 +05:00
|
|
|
if (!user.accessToken) {
|
|
|
|
|
user = await this.user.get();
|
|
|
|
|
}
|
2020-08-24 11:14:16 +05:00
|
|
|
|
|
|
|
|
this.evtSource = new EventSourcePolyfill(`${HOST}/events`, {
|
|
|
|
|
headers: { Authorization: `Bearer ${user.accessToken}` },
|
|
|
|
|
});
|
|
|
|
|
|
2020-08-24 11:25:09 +05:00
|
|
|
this.evtSource.onopen = function () {
|
|
|
|
|
console.log("sse opened.");
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-24 11:14:16 +05:00
|
|
|
this.evtSource.onmessage = async (event) => {
|
|
|
|
|
const { type, data } = JSON.parse(event.data);
|
|
|
|
|
switch (type) {
|
|
|
|
|
case "upgrade":
|
|
|
|
|
await this.user.set({
|
|
|
|
|
notesnook: { ...user.notesnook, subscription: data },
|
|
|
|
|
});
|
2020-08-24 11:25:09 +05:00
|
|
|
this.ev.publish("user:upgraded", data);
|
2020-08-24 11:14:16 +05:00
|
|
|
break;
|
|
|
|
|
case "sync":
|
|
|
|
|
this.ev.publish("db:sync");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-02-11 16:28:28 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sync() {
|
|
|
|
|
return this.syncer.start();
|
2020-02-03 12:03:07 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 13:12:47 +05:00
|
|
|
export default Database;
|