fix: improve content conflict resolution

this fixes the "undefined is not a function" error originated from
SparkMD5. Basically, sometimes the remote content or local content is
an object — an invalid state. So we need to check for that.
(fixes streetwriters/notesnook#371)
This commit is contained in:
thecodrr
2022-02-25 15:27:00 +05:00
parent 3e8eb1d45f
commit 1d283c73f4

View File

@@ -179,17 +179,27 @@ class Merger {
if (!note || !note.data) return;
note = note.data;
// if hashes are equal do nothing
if (
!note.locked &&
(!remote ||
!local ||
const isRemoteObject = typeof remote.data === "object";
const isLocalObject = typeof local.data === "object";
const localHash = isLocalObject ? null : SparkMD5.hash(local.data);
const remoteHash = isRemoteObject ? null : SparkMD5.hash(remote.data);
if (!note.locked) {
// reject all invalid states
if (
isRemoteObject || // no point in accepting invalid remote object
!local.data ||
!remote.data ||
remote.data === "undefined" || //TODO not sure about this
SparkMD5.hash(local.data) === SparkMD5.hash(remote.data))
)
return;
localHash === remoteHash
)
return;
// if we have an invalid content locally but the remote one is valid,
// let's accept the remote content.
if (isLocalObject && !isRemoteObject) {
await this._db.content.add({ id: local.id, ...remote });
return;
}
}
if (remote.deleted || local.deleted || note.locked) {
// if note is locked or content is deleted we keep the most recent version.