2020-04-09 16:36:57 +05:00
|
|
|
/**
|
|
|
|
|
* GENERAL PROCESS:
|
2020-04-16 03:04:44 +05:00
|
|
|
* make a get request to server with current lastSynced
|
2020-04-09 16:36:57 +05:00
|
|
|
* 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:
|
2020-04-16 03:04:44 +05:00
|
|
|
* Locally, get everything that was editted/added after the lastSynced
|
2020-04-09 16:36:57 +05:00
|
|
|
* 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
|
2020-04-16 03:04:44 +05:00
|
|
|
* Update items that were editted before the lastSynced
|
|
|
|
|
* Try to merge items that were edited after the lastSynced
|
2020-04-09 16:36:57 +05:00
|
|
|
* 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.
|
|
|
|
|
*/
|
2021-01-23 10:48:21 +05:00
|
|
|
import { EV, EVENTS } from "../../common";
|
2020-09-19 11:46:36 +05:00
|
|
|
import Constants from "../../utils/constants";
|
2020-12-16 12:06:25 +05:00
|
|
|
import http from "../../utils/http";
|
|
|
|
|
import TokenManager from "../token-manager";
|
2020-04-16 03:04:44 +05:00
|
|
|
import Collector from "./collector";
|
2020-04-09 16:36:57 +05:00
|
|
|
import Merger from "./merger";
|
2021-06-16 10:08:29 +05:00
|
|
|
import { areAllEmpty } from "./utils";
|
2020-04-09 16:36:57 +05:00
|
|
|
export default class Sync {
|
|
|
|
|
/**
|
|
|
|
|
*
|
2020-04-16 03:04:44 +05:00
|
|
|
* @param {import("../index").default} db
|
2020-04-09 16:36:57 +05:00
|
|
|
*/
|
|
|
|
|
constructor(db) {
|
2020-04-16 03:04:44 +05:00
|
|
|
this._db = db;
|
|
|
|
|
this._collector = new Collector(this._db);
|
|
|
|
|
this._merger = new Merger(this._db);
|
2020-12-16 12:06:25 +05:00
|
|
|
this._tokenManager = new TokenManager(this._db);
|
2021-08-10 11:59:26 +05:00
|
|
|
this._isSyncing = false;
|
2020-04-09 16:36:57 +05:00
|
|
|
}
|
|
|
|
|
|
2020-04-16 03:04:44 +05:00
|
|
|
async _fetch(lastSynced, token) {
|
2020-12-16 12:06:25 +05:00
|
|
|
return await http.get(
|
2020-12-16 13:23:14 +05:00
|
|
|
`${Constants.API_HOST}/sync?lst=${lastSynced}`,
|
2020-12-16 12:06:25 +05:00
|
|
|
token
|
|
|
|
|
);
|
2020-04-09 16:36:57 +05:00
|
|
|
}
|
|
|
|
|
|
2020-08-24 13:07:16 +05:00
|
|
|
async _performChecks() {
|
2020-12-16 12:06:25 +05:00
|
|
|
let lastSynced = (await this._db.context.read("lastSynced")) || 0;
|
|
|
|
|
let token = await this._tokenManager.getAccessToken();
|
2020-04-09 16:36:57 +05:00
|
|
|
|
2020-04-16 03:04:44 +05:00
|
|
|
// update the conflicts status and if find any, throw
|
|
|
|
|
await this._db.conflicts.recalculate();
|
|
|
|
|
await this._db.conflicts.check();
|
2020-04-09 16:36:57 +05:00
|
|
|
|
2020-12-16 12:06:25 +05:00
|
|
|
return { lastSynced, token };
|
2020-08-24 13:07:16 +05:00
|
|
|
}
|
|
|
|
|
|
2020-12-11 20:19:28 +05:00
|
|
|
async start(full, force) {
|
2021-09-13 09:37:52 +05:00
|
|
|
if (this._isSyncing) return false;
|
|
|
|
|
|
2021-02-26 17:33:46 +05:00
|
|
|
if (force) await this._db.context.write("lastSynced", 0);
|
2020-12-05 11:26:02 +05:00
|
|
|
let { lastSynced, token } = await this._performChecks();
|
2020-08-24 13:07:16 +05:00
|
|
|
|
2021-08-10 11:59:26 +05:00
|
|
|
try {
|
2021-08-31 12:12:16 +05:00
|
|
|
const now = Date.now();
|
2021-08-10 11:59:26 +05:00
|
|
|
this._isSyncing = true;
|
2020-04-09 16:36:57 +05:00
|
|
|
|
2021-08-10 11:59:26 +05:00
|
|
|
if (full) var serverResponse = await this._fetch(lastSynced, token);
|
2020-04-09 16:36:57 +05:00
|
|
|
|
2021-08-10 11:59:26 +05:00
|
|
|
// we prepare local data before merging so we always have correct data
|
|
|
|
|
const data = await this._collector.collect(lastSynced);
|
2021-08-31 12:12:16 +05:00
|
|
|
data.lastSynced = now;
|
2020-04-16 03:04:44 +05:00
|
|
|
|
2021-08-10 11:59:26 +05:00
|
|
|
if (full) {
|
|
|
|
|
// merge the server response
|
|
|
|
|
await this._merger.merge(serverResponse, lastSynced);
|
|
|
|
|
}
|
2020-04-09 16:36:57 +05:00
|
|
|
|
2021-08-10 11:59:26 +05:00
|
|
|
// check for conflicts and throw
|
|
|
|
|
await this._db.conflicts.check();
|
2020-04-09 16:36:57 +05:00
|
|
|
|
2021-08-10 11:59:26 +05:00
|
|
|
// send the data back to server
|
|
|
|
|
lastSynced = await this._send(data, token);
|
|
|
|
|
|
|
|
|
|
// update our lastSynced time
|
|
|
|
|
if (lastSynced) {
|
|
|
|
|
await this._db.context.write("lastSynced", lastSynced);
|
|
|
|
|
}
|
2021-09-13 09:37:52 +05:00
|
|
|
|
|
|
|
|
return true;
|
2021-08-10 11:59:26 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
throw e;
|
|
|
|
|
} finally {
|
|
|
|
|
this._isSyncing = false;
|
2020-08-22 11:25:23 +05:00
|
|
|
}
|
2020-04-09 16:36:57 +05:00
|
|
|
}
|
|
|
|
|
|
2020-08-24 13:07:16 +05:00
|
|
|
async eventMerge(serverResponse) {
|
2021-07-03 12:14:11 +05:00
|
|
|
let { lastSynced, token } = await this._performChecks();
|
|
|
|
|
|
|
|
|
|
const data = await this._collector.collect(lastSynced);
|
2020-08-25 08:52:12 +05:00
|
|
|
|
2020-08-24 13:07:16 +05:00
|
|
|
// merge the server response
|
|
|
|
|
await this._merger.merge(serverResponse, lastSynced);
|
|
|
|
|
|
2021-07-03 12:14:11 +05:00
|
|
|
// check for conflicts and throw
|
|
|
|
|
await this._db.conflicts.check();
|
|
|
|
|
|
|
|
|
|
// send the data back to server
|
|
|
|
|
lastSynced = await this._send(data, token);
|
|
|
|
|
|
|
|
|
|
// update our lastSynced time
|
|
|
|
|
if (lastSynced) {
|
|
|
|
|
await this._db.context.write("lastSynced", lastSynced);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-23 10:48:21 +05:00
|
|
|
EV.publish(EVENTS.appRefreshRequested);
|
2020-12-29 15:32:56 +05:00
|
|
|
|
2020-08-24 13:07:16 +05:00
|
|
|
// check for conflicts and throw
|
2021-01-09 11:13:17 +05:00
|
|
|
// await this._db.conflicts.check();
|
2020-08-24 13:07:16 +05:00
|
|
|
|
2021-02-22 09:33:30 +05:00
|
|
|
// TODO test this.
|
|
|
|
|
// we won't be updating lastSynced time here because
|
|
|
|
|
// it can cause the lastSynced time to move ahead of any
|
|
|
|
|
// last edited (but unsynced) time resulting in edited notes
|
|
|
|
|
// not getting synced.
|
|
|
|
|
// if (serverResponse.lastSynced) {
|
|
|
|
|
// await this._db.context.write("lastSynced", serverResponse.lastSynced);
|
|
|
|
|
// }
|
2020-08-24 13:07:16 +05:00
|
|
|
}
|
|
|
|
|
|
2021-07-03 14:08:36 +05:00
|
|
|
async _send(data, token) {
|
2020-12-16 12:06:25 +05:00
|
|
|
let response = await http.post.json(
|
|
|
|
|
`${Constants.API_HOST}/sync`,
|
|
|
|
|
data,
|
|
|
|
|
token
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return response.lastSynced;
|
2020-04-09 16:36:57 +05:00
|
|
|
}
|
|
|
|
|
}
|