This commit is contained in:
ammarahm-ed
2020-02-12 02:09:08 +05:00
parent 5608d8720c
commit 04af9e1668
3 changed files with 14 additions and 5 deletions

View File

@@ -26,7 +26,7 @@
* And then it should continue. * And then it should continue.
*/ */
import Database from "./index"; import Database from "./index";
import { HOST } from "../utils/constants"; import { HOST,HEADERS } from "../utils/constants";
import fetch from "node-fetch"; import fetch from "node-fetch";
export default class Sync { export default class Sync {
@@ -54,13 +54,14 @@ export default class Sync {
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, user }); let data = this._merge({ serverResponse, lastSyncedTimestamp, user });
await this.db.user.set({ lastSyncedTimestamp: data.lastSynced }); await this.db.user.set({ lastSynced: data.lastSynced });
await this._send(data); await this._send(data);
return true; return true;
} }
_merge({ serverResponse, lastSyncedTimestamp, user }) { _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); note = JSON.parse(note.data);
let localNote = this.db.notes.note(note.id); let localNote = this.db.notes.note(note.id);
@@ -78,6 +79,7 @@ export default class Sync {
// TODO trash, colors, tags // TODO trash, colors, tags
return { return {
notes: this.db.notes notes: this.db.notes
.all
.filter(v => v.dateEdited > lastSyncedTimestamp) .filter(v => v.dateEdited > lastSyncedTimestamp)
.map(v => ({ .map(v => ({
dateEdited: v.dateEdited, dateEdited: v.dateEdited,
@@ -86,6 +88,7 @@ export default class Sync {
userId: user.Id userId: user.Id
})), })),
notebooks: this.db.notebooks notebooks: this.db.notebooks
.all
.filter(v => v.dateEdited > lastSyncedTimestamp) .filter(v => v.dateEdited > lastSyncedTimestamp)
.map(v => ({ .map(v => ({
dateEdited: v.dateEdited, dateEdited: v.dateEdited,
@@ -101,7 +104,11 @@ export default class Sync {
} }
async _send(data) { async _send(data) {
//TODO encrypt the payload //TODO encrypt the payload
let token = await this.db.user.token();
if (!token) return;
let response = await fetch(`${HOST}sync`, { let response = await fetch(`${HOST}sync`, {
method: "POST", method: "POST",
headers: { ...HEADERS, Authorization: `Bearer ${token}` }, headers: { ...HEADERS, Authorization: `Bearer ${token}` },

View File

@@ -28,9 +28,11 @@ export default class CachedCollection {
if (!item.id) throw new Error("The item must contain the id field."); if (!item.id) throw new Error("The item must contain the id field.");
let exists = this.map.has(item.id); let exists = this.map.has(item.id);
await this.updateItem(item);
if (!exists) { if (!exists) {
item.dateCreated = item.dateCreated || Date.now(); item.dateCreated = item.dateCreated || Date.now();
}
await this.updateItem(item);
if (!exists) {
await this.indexer.index(item.id); await this.indexer.index(item.id);
} }
} }

View File

@@ -32,9 +32,9 @@ export default class User {
} }
async token() { async token() {
let user = await this.user(); let user = await this.get();
if (!user) return; if (!user) return;
if (user.expiry < Date.now()) { if (user.expiry > Date.now()) {
return user.accessToken; return user.accessToken;
} }
let response = await authRequest("oauth/token", { let response = await authRequest("oauth/token", {