Skip to content

Commit

Permalink
fix(SyncProcess): Refactor mergeable functions
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr committed Dec 24, 2024
1 parent da69650 commit c5560ab
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 40 deletions.
69 changes: 31 additions & 38 deletions src/lib/strategies/Default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,20 +436,19 @@ export default class SyncProcess {
this.cacheTreeRoot,
this.localTreeRoot,
// We also allow canMergeWith for folders here, because Window IDs are not stable
// If a bookmark's URL has changed we want to recreate it instead of updating it, because of Nextcloud Bookmarks' uniqueness constraints
(oldItem, newItem) => {
if (oldItem.type !== newItem.type) {
return false
}
if (oldItem.type === 'bookmark' && newItem.type === 'bookmark') {
return oldItem.url === newItem.url
if (oldItem.type === 'bookmark' && newItem.type === 'bookmark' && oldItem.url !== newItem.url) {
return false
}
if (oldItem.type === 'folder') {
if (String(oldItem.id) === String(newItem.id)) {
return true
}
if (oldItem.canMergeWith(newItem)) {
return true
}
if (Mappings.mappable(mappingsSnapshot, oldItem, newItem)) {
return true
}
if (oldItem.type === 'folder' && oldItem.canMergeWith(newItem)) {
return true
}
return false
},
Expand All @@ -461,22 +460,21 @@ export default class SyncProcess {
// We also allow canMergeWith here
// (for bookmarks, because e.g. for NextcloudFolders the id of moved bookmarks changes (because their id is "<bookmarkID>;<folderId>")
// (for folders because Window IDs are not stable)
// If a bookmark's URL has changed we want to recreate it instead of updating it, because of Nextcloud Bookmarks' uniqueness constraints
(oldItem, newItem) => {
if (oldItem.type !== newItem.type) {
return false
}
if (oldItem.type === 'bookmark' && newItem.type === 'bookmark') {
return oldItem.url === newItem.url
if (oldItem.type === 'bookmark' && newItem.type === 'bookmark' && oldItem.url !== newItem.url) {
return false
}
if (oldItem.type === 'folder') {
if (Mappings.mappable(mappingsSnapshot, oldItem, newItem)) {
newMappings.push([oldItem, newItem])
return true
}
if (oldItem.canMergeWith(newItem)) {
newMappings.push([oldItem, newItem])
return true
}
if (Mappings.mappable(mappingsSnapshot, oldItem, newItem)) {
newMappings.push([oldItem, newItem])
return true
}
if (oldItem.canMergeWith(newItem)) {
newMappings.push([oldItem, newItem])
return true
}
return false
},
Expand All @@ -491,15 +489,12 @@ export default class SyncProcess {
if (oldItem.type !== newItem.type) {
return false
}
if (oldItem.type === 'folder') {
if (String(oldItem.id) === String(newItem.id)) {
return true
}
// If a bookmark's URL has changed we want to recreate it instead of updating it, because of Nextcloud Bookmarks' uniqueness constraints
if (oldItem.type === 'bookmark' && newItem.type === 'bookmark' && oldItem.url !== newItem.url) {
return false
}
if (oldItem.type === 'bookmark' && newItem.type === 'bookmark') {
if (String(oldItem.id) === String(newItem.id) && oldItem.url === newItem.url) {
return true
}
if (Mappings.mappable(mappingsSnapshot, oldItem, newItem)) {
return true
}
return false
},
Expand All @@ -508,22 +503,20 @@ export default class SyncProcess {
serverScanner = new Scanner(
this.cacheTreeRoot,
this.serverTreeRoot,
// We also allow canMergeWith here, because e.g. for NextcloudFolders the id of moved bookmarks changes (because their id is "<bookmarkID>;<folderId>")
// We also allow canMergeWith here, because e.g. for NextcloudBookmarks the id of moved bookmarks changes (because their id is "<bookmarkID>;<folderId>")
(oldItem, newItem) => {
if (oldItem.type !== newItem.type) {
return false
}
if (oldItem.type === 'folder') {
if (Mappings.mappable(mappingsSnapshot, oldItem, newItem)) {
newMappings.push([oldItem, newItem])
return true
}
// If a bookmark's URL has changed we want to recreate it instead of updating it, because of Nextcloud Bookmarks' uniqueness constraints
if (oldItem.type === 'bookmark' && newItem.type === 'bookmark' && oldItem.url !== newItem.url) {
return false
}
if (Mappings.mappable(mappingsSnapshot, oldItem, newItem)) {
newMappings.push([oldItem, newItem])
return true
}
if (oldItem.type === 'bookmark' && newItem.type === 'bookmark') {
if (Mappings.mappable(mappingsSnapshot, oldItem, newItem) && oldItem.url === newItem.url) {
newMappings.push([oldItem, newItem])
return true
}
if (oldItem.canMergeWith(newItem)) {
newMappings.push([oldItem, newItem])
return true
Expand Down
27 changes: 25 additions & 2 deletions src/lib/strategies/Unidirectional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,20 @@ export default class UnidirectionalSyncProcess extends DefaultStrategy {
const localScanner = new Scanner(
this.serverTreeRoot,
this.localTreeRoot,
// We can't rely on a cacheTree, thus we have to accept canMergeWith results as well
(serverItem, localItem) => {
if (localItem.type === serverItem.type && (serverItem.canMergeWith(localItem) || Mappings.mappable(mappingsSnapshot, serverItem, localItem))) {
if (localItem.type !== serverItem.type) {
return false
}
// If a bookmark's URL has changed we want to recreate it instead of updating it, because of Nextcloud Bookmarks' uniqueness constraints
if (serverItem.type === 'bookmark' && localItem.type === 'bookmark' && serverItem.url !== localItem.url) {
return false
}
if (serverItem.canMergeWith(localItem)) {
newMappings.push([localItem, serverItem])
return true
}
if (Mappings.mappable(mappingsSnapshot, serverItem, localItem)) {
newMappings.push([localItem, serverItem])
return true
}
Expand All @@ -58,7 +70,18 @@ export default class UnidirectionalSyncProcess extends DefaultStrategy {
this.localTreeRoot,
this.serverTreeRoot,
(localItem, serverItem) => {
if (serverItem.type === localItem.type && (serverItem.canMergeWith(localItem) || Mappings.mappable(mappingsSnapshot, serverItem, localItem))) {
if (serverItem.type !== localItem.type) {
return false
}
// If a bookmark's URL has changed we want to recreate it instead of updating it, because of Nextcloud Bookmarks' uniqueness constraints
if (serverItem.type === 'bookmark' && localItem.type === 'bookmark' && serverItem.url !== localItem.url) {
return false
}
if (serverItem.canMergeWith(localItem)) {
newMappings.push([localItem, serverItem])
return true
}
if (Mappings.mappable(mappingsSnapshot, serverItem, localItem)) {
newMappings.push([localItem, serverItem])
return true
}
Expand Down

0 comments on commit c5560ab

Please sign in to comment.