2020-04-09 16:36:57 +05:00
|
|
|
import Database from "../index";
|
2020-04-09 16:46:03 +05:00
|
|
|
var tfun = require("transfun/transfun.js").tfun;
|
|
|
|
|
if (!tfun) {
|
|
|
|
|
tfun = global.tfun;
|
|
|
|
|
}
|
2020-04-09 16:36:57 +05:00
|
|
|
|
|
|
|
|
class Prepare {
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {Database} db
|
|
|
|
|
*/
|
|
|
|
|
constructor(db) {
|
|
|
|
|
this._db = db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async get(lastSyncedTimestamp) {
|
|
|
|
|
this._lastSyncedTimestamp = lastSyncedTimestamp;
|
2020-04-13 12:04:49 +05:00
|
|
|
this.key = await this._db.user.key();
|
2020-04-09 16:36:57 +05:00
|
|
|
return {
|
2020-04-13 12:04:49 +05:00
|
|
|
notes: await this._prepareForServer(this._db.notes.raw),
|
|
|
|
|
notebooks: await this._prepareForServer(this._db.notebooks.raw),
|
|
|
|
|
delta: await this._prepareForServer(await this._db.delta.all()),
|
|
|
|
|
text: await this._prepareForServer(await this._db.text.all()),
|
|
|
|
|
tags: await this._prepareForServer(this._db.tags.raw),
|
|
|
|
|
colors: await this._prepareForServer(this._db.colors.raw),
|
|
|
|
|
trash: await this._prepareForServer(this._db.trash.raw),
|
2020-04-09 16:36:57 +05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 12:04:49 +05:00
|
|
|
_serialize(item) {
|
|
|
|
|
return this._db.context.encrypt(this.key, JSON.stringify(item));
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 16:36:57 +05:00
|
|
|
_prepareForServer(array) {
|
2020-04-13 12:04:49 +05:00
|
|
|
return Promise.all(
|
|
|
|
|
tfun
|
|
|
|
|
.filter((item) => item.dateEdited > this._lastSyncedTimestamp)
|
|
|
|
|
.map(async (item) => {
|
|
|
|
|
// in case of resolved delta, we do not want to send this key to the server
|
|
|
|
|
if (item.resolved) delete item.resolved;
|
2020-04-11 11:17:58 +05:00
|
|
|
|
2020-04-13 12:04:49 +05:00
|
|
|
return {
|
|
|
|
|
id: item.id,
|
|
|
|
|
...(await this._serialize(item)),
|
|
|
|
|
};
|
|
|
|
|
})(array)
|
|
|
|
|
);
|
2020-04-09 16:36:57 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default Prepare;
|