Files
notesnook/packages/core/api/sync.js

150 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-02-11 16:28:28 +05:00
/**
* GENERAL PROCESS:
* make a get request to server with current lastSyncedTimestamp
* parse the response. the response should contain everything that user has on the server
* decrypt the response
* merge everything into the database and look for conflicts
* send the conflicts (if any) to the end-user for resolution
* once the conflicts have been resolved, send the updated data back to the server
*/
/**
* MERGING:
* Locally, get everything that was editted/added after the lastSyncedTimestamp
* Run forEach loop on the server response.
* Add items that do not exist in the local collections
* Remove items (without asking) that need to be removed
* Update items that were editted before the lastSyncedTimestamp
* Try to merge items that were edited after the lastSyncedTimestamp
* Items in which the content has changed, send them for conflict resolution
* Otherwise, keep the most recently updated copy.
*/
/**
* CONFLICTS:
* Syncing should pause until all the conflicts have been resolved
* And then it should continue.
*/
import Database from "./index";
2020-02-23 19:57:46 +05:00
import { HOST, HEADERS } from "../utils/constants";
2020-03-19 14:03:29 +05:00
var tfun = require("transfun/transfun.js").tfun;
if (!tfun) {
tfun = global.tfun;
}
2020-02-11 16:28:28 +05:00
export default class Sync {
/**
*
* @param {Database} db
*/
constructor(db) {
this.db = db;
}
async _fetch(lastSyncedTimestamp) {
let token = await this.db.user.token();
2020-03-19 14:30:05 +05:00
if (!token) throw new Error("You are not logged in");
2020-02-11 16:28:28 +05:00
let response = await fetch(`${HOST}sync?lst=${lastSyncedTimestamp}`, {
headers: { ...HEADERS, Authorization: `Bearer ${token}` }
});
//TODO decrypt the response.
return await response.json();
}
async start() {
let user = await this.db.user.get();
2020-03-19 14:30:05 +05:00
if (!user) throw new Error("You need to login to sync.");
2020-03-18 14:06:20 +05:00
let lastSyncedTimestamp = user.lastSynced || 0;
2020-02-11 16:28:28 +05:00
let serverResponse = await this._fetch(lastSyncedTimestamp);
let data = await this._merge({ serverResponse, lastSyncedTimestamp, user });
2020-02-11 16:28:28 +05:00
await this._send(data);
2020-03-18 14:06:20 +05:00
await this.db.user.set({ lastSynced: data.lastSynced });
2020-02-11 16:28:28 +05:00
}
async _merge({ serverResponse, lastSyncedTimestamp, user }) {
const { notes, synced, notebooks,delta, text } = serverResponse;
2020-02-23 19:57:46 +05:00
2020-03-19 14:43:42 +05:00
if (!synced) {
await syncArrayWithDatabase(
2020-03-19 14:03:29 +05:00
notes,
id => this.db.notes.note(id),
2020-03-19 14:03:29 +05:00
item => this.db.notes.add(item)
);
await syncArrayWithDatabase(
2020-03-19 14:03:29 +05:00
notebooks,
id => this.db.notebooks.notebook(id),
2020-03-19 14:03:29 +05:00
item => this.db.notebooks.add(item)
);
await syncArrayWithDatabase(
delta,
id => this.db.delta.raw(id),
item => this.db.delta.add(item)
);
await syncArrayWithDatabase(
text,
id => this.db.text.raw(id),
item => this.db.text.add(item)
2020-03-19 14:03:29 +05:00
);
}
2020-02-11 16:28:28 +05:00
// TODO trash, colors, tags
return {
2020-03-19 14:03:29 +05:00
notes: prepareForServer(this.db.notes.all, user, lastSyncedTimestamp),
notebooks: prepareForServer(
this.db.notebooks.all,
user,
lastSyncedTimestamp
),
delta: prepareForServer(
await this.db.delta.all(),
user,
lastSyncedTimestamp
),
text: prepareForServer(
await this.db.text.all(),
user,
lastSyncedTimestamp
),
2020-02-11 16:28:28 +05:00
tags: [],
colors: [],
2020-03-18 14:06:20 +05:00
trash: [],
lastSynced: Date.now()
2020-02-11 16:28:28 +05:00
};
}
async _send(data) {
//TODO encrypt the payload
2020-02-12 02:09:08 +05:00
let token = await this.db.user.token();
if (!token) return;
2020-02-11 16:28:28 +05:00
let response = await fetch(`${HOST}sync`, {
method: "POST",
headers: { ...HEADERS, Authorization: `Bearer ${token}` },
body: JSON.stringify(data)
});
return response.ok;
}
2020-03-19 14:03:29 +05:00
}
async function syncWithDatabase(remoteItem, get, add) {
let localItem = await get(remoteItem.id);
if (!localItem || remoteItem.dateEdited > localItem.dateEdited) {
await add({ ...JSON.parse(remoteItem.data), remote: true });
}
}
async function syncArrayWithDatabase(array, get, set) {
return Promise.all(array.map(async item => await syncWithDatabase(item, get, set)));
2020-03-19 14:03:29 +05:00
}
function prepareForServer(array, user, lastSyncedTimestamp) {
2020-03-19 14:03:29 +05:00
return tfun
.filter(item => item.dateEdited > lastSyncedTimestamp)(array)
.map(item => ({
id: item.id,
dateEdited: item.dateEdited,
dateCreated: item.dateCreated,
data: JSON.stringify(item),
userId: user.Id
}));
}