feat: implement notes/nb syncing (untested)

This commit is contained in:
thecodrr
2020-02-12 00:52:17 +05:00
parent e4aaf97c5d
commit 4cc24e687c
2 changed files with 29 additions and 7 deletions

View File

@@ -53,20 +53,23 @@ export default class Sync {
if (!user) return false; if (!user) return false;
let lastSyncedTimestamp = user.lastSyncedTimestamp || 0; let lastSyncedTimestamp = user.lastSyncedTimestamp || 0;
let serverResponse = await this._fetch(lastSyncedTimestamp); let serverResponse = await this._fetch(lastSyncedTimestamp);
let data = this._merge(serverResponse, lastSyncedTimestamp); let data = this._merge({ serverResponse, lastSyncedTimestamp, user });
await this.db.user.set({ lastSyncedTimestamp: data.lastSynced });
await this._send(data); await this._send(data);
return true; return true;
} }
_merge(serverResponse, lastSyncedTimestamp) { _merge({ serverResponse, lastSyncedTimestamp, user }) {
const { notes, notebooks /* tags, colors, trash */ } = serverResponse; const { notes, notebooks /* tags, colors, trash */ } = serverResponse;
notes.forEach(async note => { notes.forEach(async note => {
note = JSON.parse(note.data);
let localNote = this.db.notes.note(note.id); let localNote = this.db.notes.note(note.id);
if (!localNote || note.dateEdited > localNote.data.dateEdited) { if (!localNote || note.dateEdited > localNote.data.dateEdited) {
await this.db.notes.add({ ...note, remote: true }); await this.db.notes.add({ ...note, remote: true });
} }
}); });
notebooks.forEach(async nb => { notebooks.forEach(async nb => {
nb = JSON.parse(nb.data);
let localNb = this.db.notebooks.notebook(nb.id); let localNb = this.db.notebooks.notebook(nb.id);
if (!localNb || nb.dateEdited > localNb.data.dateEdited) { if (!localNb || nb.dateEdited > localNb.data.dateEdited) {
await this.db.notebooks.add({ ...nb, remote: true }); await this.db.notebooks.add({ ...nb, remote: true });
@@ -74,13 +77,26 @@ export default class Sync {
}); });
// TODO trash, colors, tags // TODO trash, colors, tags
return { return {
notes: this.db.notes.filter(v => v.dateEdited > lastSyncedTimestamp), notes: this.db.notes
notebooks: this.db.notebooks.filter( .filter(v => v.dateEdited > lastSyncedTimestamp)
v => v.dateEdited > lastSyncedTimestamp .map(v => ({
), dateEdited: v.dateEdited,
dateCreated: v.dateCreated,
data: JSON.stringify(v),
userId: user.Id
})),
notebooks: this.db.notebooks
.filter(v => v.dateEdited > lastSyncedTimestamp)
.map(v => ({
dateEdited: v.dateEdited,
dateCreated: v.dateCreated,
data: JSON.stringify(v),
userId: user.Id
})),
tags: [], tags: [],
colors: [], colors: [],
tags: [] tags: [],
lastSynced: Date.now()
}; };
} }

View File

@@ -15,6 +15,12 @@ export default class User {
return this.context.read("user"); return this.context.read("user");
} }
async set(user) {
if (!user) return;
user = { ...(await this.get()), ...user };
await this.context.write("user", user);
}
async login(username, password) { async login(username, password) {
let response = await authRequest("oauth/token", { let response = await authRequest("oauth/token", {
username, username,