Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: handle attachments merge conflict

thecodrr69defdb367d4
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index 5dc5e7668..f3b2311a1 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -52,6 +52,13 @@ export default class Sync {
);
}
+ async _fetchAttachments(lastSynced, token) {
+ return await http.get(
+ `${Constants.API_HOST}/sync/attachments?lst=${lastSynced}`,
+ token
+ );
+ }
+
async _performChecks() {
let lastSynced = (await this._db.storage.read("lastSynced")) || 0;
let token = await this._tokenManager.getAccessToken();
@@ -73,6 +80,7 @@ export default class Sync {
const now = Date.now();
this._isSyncing = true;
+ await this._mergeAttachments(token, lastSynced);
await this._uploadAttachments();
// we prepare local data before merging so we always have correct data
@@ -149,6 +157,11 @@ export default class Sync {
return response.lastSynced;
}
+ async _mergeAttachments(token, lastSynced) {
+ var serverResponse = await this._fetchAttachments(lastSynced, token);
+ await this._merger.merge(serverResponse, lastSynced);
+ }
+
async _uploadAttachments() {
const attachments = this._db.attachments.pending;
try {
diff --git a/packages/core/api/sync/merger.js b/packages/core/api/sync/merger.js
index 18c3e9fb7..43db83b31 100644
--- a/packages/core/api/sync/merger.js
+++ b/packages/core/api/sync/merger.js
@@ -1,6 +1,7 @@
import { migrations } from "../../migrations";
import { areAllEmpty } from "./utils";
import SparkMD5 from "spark-md5";
+import setManipulator from "../../utils/set";
class Merger {
/**
@@ -63,28 +64,23 @@ class Merger {
}
}
- async _mergeArrayWithConflicts(array, get, set, resolve) {
+ async _mergeArrayWithConflicts(array, mergeItem) {
if (!array) return;
- return Promise.all(
- array.map(
- async (item) =>
- await this._mergeItemWithConflicts(item, get, set, resolve)
- )
- );
+ return Promise.all(array.map(mergeItem));
}
async merge(serverResponse, lastSynced) {
if (!serverResponse) return false;
this._lastSynced = lastSynced;
const {
- notes,
+ notes = [],
synced,
- notebooks,
- content,
- trash,
- vaultKey,
- settings,
- attachments,
+ notebooks = [],
+ content = [],
+ trash = [],
+ vaultKey = [],
+ settings = [],
+ attachments = [],
} = serverResponse;
if (synced || areAllEmpty(serverResponse)) return false;
@@ -94,11 +90,31 @@ class Merger {
await this._db.vault._setKey(await this._deserialize(vaultKey, false));
}
- await this._mergeArray(
- attachments,
- (id) => this._db.attachments.attachment(id),
- (item) => this._db.attachments.add(item)
- );
+ await this._mergeArrayWithConflicts(attachments, async (item) => {
+ const remoteAttachment = await this._deserialize(item);
+ const localAttachment = this._db.attachments.attachment(
+ remoteAttachment.metadata.hash
+ );
+ if (
+ localAttachment &&
+ localAttachment.dateUploaded !== remoteAttachment.dateUploaded
+ ) {
+ const noteIds = localAttachment.noteIds.slice();
+ const isRemoved = await this._db.attachments.remove(
+ localAttachment.metadata.hash,
+ true
+ );
+ if (!isRemoved)
+ throw new Error(
+ "Conflict could not be resolved in one of the attachments."
+ );
+ remoteAttachment.noteIds = setManipulator.union(
+ remoteAttachment.noteIds,
+ noteIds
+ );
+ }
+ await this._db.attachments.add(remoteAttachment);
+ });
await this._mergeArray(
settings,
@@ -118,38 +134,40 @@ class Merger {
(item) => this._db.notebooks.merge(item)
);
- await this._mergeArrayWithConflicts(
- content,
- (id) => this._db.content.raw(id, false),
- (item) => this._db.content.add(item),
- async (local, remote) => {
- let note = this._db.notes.note(local.noteId);
- if (!note || !note.data) return;
- note = note.data;
-
- // if hashes are equal do nothing
- if (
- !note.locked &&
- (!remote ||
- !local ||
- !local.data ||
- !remote.data ||
- remote.data === "undefined" || //TODO not sure about this
- SparkMD5.hash(local.data) === SparkMD5.hash(remote.data))
- )
- return;
-
- if (remote.deleted || local.deleted || note.locked) {
- // if note is locked or content is deleted we keep the most recent version.
- if (remote.dateEdited > local.dateEdited)
- await this._db.content.add({ id: local.id, ...remote });
- } else {
- // otherwise we trigger the conflicts
- await this._db.content.add({ ...local, conflicted: remote });
- await this._db.notes.add({ id: local.noteId, conflicted: true });
- await this._db.storage.write("hasConflicts", true);
+ await this._mergeArrayWithConflicts(content, (item) =>
+ this._mergeItemWithConflicts(
+ item,
+ (id) => this._db.content.raw(id, false),
+ (item) => this._db.content.add(item),
+ async (local, remote) => {
+ let note = this._db.notes.note(local.noteId);
+ if (!note || !note.data) return;
+ note = note.data;
+
+ // if hashes are equal do nothing
+ if (
+ !note.locked &&
+ (!remote ||
+ !local ||
+ !local.data ||
+ !remote.data ||
+ remote.data === "undefined" || //TODO not sure about this
+ SparkMD5.hash(local.data) === SparkMD5.hash(remote.data))
+ )
+ return;
+
+ if (remote.deleted || local.deleted || note.locked) {
+ // if note is locked or content is deleted we keep the most recent version.
+ if (remote.dateEdited > local.dateEdited)
+ await this._db.content.add({ id: local.id, ...remote });
+ } else {
+ // otherwise we trigger the conflicts
+ await this._db.content.add({ ...local, conflicted: remote });
+ await this._db.notes.add({ id: local.noteId, conflicted: true });
+ await this._db.storage.write("hasConflicts", true);
+ }
}
- }
+ )
);
await this._mergeArray(
diff --git a/packages/core/collections/attachments.js b/packages/core/collections/attachments.js
index 907440df9..bf2b1edeb 100644
--- a/packages/core/collections/attachments.js
+++ b/packages/core/collections/attachments.js
@@ -101,6 +101,16 @@ export default class Attachments extends Collection {
return await this._collection.updateItem(attachment);
}
+ async remove(hash, localOnly) {
+ const attachment = this.all.find((a) => a.metadata.hash === hash);
+ if (!attachment) return false;
+ if (await this._db.fs.deleteFile(hash, localOnly)) {
+ await this._collection.deleteItem(attachment.id);
+ return true;
+ }
+ return false;
+ }
+
/**
* Get specified type of attachments of a note
* @param {string} noteId
diff --git a/packages/core/database/fs.js b/packages/core/database/fs.js
index 8c4dfe9ca..745729282 100644
--- a/packages/core/database/fs.js
+++ b/packages/core/database/fs.js
@@ -60,7 +60,9 @@ export default class FileStorage {
});
}
- async deleteFile(filename) {
+ async deleteFile(filename, localOnly) {
+ if (localOnly) return await this.fs.deleteFile(filename);
+
const token = await this.tokenManager.getToken();
const url = `${hosts.API_HOST}/s3?name=${hash}`;
return await this.fs.deleteFile(filename, {