Skip to content

Commit

Permalink
Fix syncing empty favorites folders (#546)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/414235014887631/1205843304285892/f

Description:
Update bookmarks sync response handler to actually process favorites
also when an empty folder is received from the server.
  • Loading branch information
ayoy authored Nov 7, 2023
1 parent 86e4aba commit 0ac6d8e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class BookmarksResponseHandler {

let topLevelFoldersSyncables: [SyncableBookmarkAdapter]
let bookmarkSyncablesWithoutParent: [SyncableBookmarkAdapter]
let favoritesUUIDs: [String]
let favoritesUUIDs: [String]?

var entitiesByUUID: [String: BookmarkEntity] = [:]
var idsOfItemsThatRetainModifiedAt = Set<String>()
Expand All @@ -57,7 +57,7 @@ final class BookmarksResponseHandler {
var allUUIDs: Set<String> = []
var childrenToParents: [String: String] = [:]
var parentFoldersToChildren: [String: [String]] = [:]
var favoritesUUIDs: [String] = []
var favoritesUUIDs: [String]?

self.received.forEach { syncable in
guard let uuid = syncable.uuid else {
Expand Down Expand Up @@ -113,33 +113,38 @@ final class BookmarksResponseHandler {
}
try processOrphanedBookmarks()

// populate favorites
if !favoritesUUIDs.isEmpty {
guard let favoritesFolder = BookmarkUtils.fetchFavoritesFolder(context) else {
// Error - unable to process favorites
return
}
processReceivedFavorites()
}

// For non-first sync we rely fully on the server response
if !shouldDeduplicateEntities {
favoritesFolder.favoritesArray.forEach { $0.removeFromFavorites() }
} else if !favoritesFolder.favoritesArray.isEmpty {
// If we're deduplicating and there are favorires locally, we'll need to sync favorites folder back later.
// Let's keep its modifiedAt.
idsOfItemsThatRetainModifiedAt.insert(BookmarkEntity.Constants.favoritesFolderID)
}
// MARK: - Private

favoritesUUIDs.forEach { uuid in
if let bookmark = entitiesByUUID[uuid] {
bookmark.removeFromFavorites()
bookmark.addToFavorites(favoritesRoot: favoritesFolder)
}
private func processReceivedFavorites() {
guard let favoritesUUIDs else {
return
}

guard let favoritesFolder = BookmarkUtils.fetchFavoritesFolder(context) else {
// Error - unable to process favorites
return
}

// For non-first sync we rely fully on the server response
if !shouldDeduplicateEntities {
favoritesFolder.favoritesArray.forEach { $0.removeFromFavorites() }
} else if !favoritesFolder.favoritesArray.isEmpty {
// If we're deduplicating and there are favorites locally, we'll need to sync favorites folder back later.
// Let's keep its modifiedAt.
idsOfItemsThatRetainModifiedAt.insert(BookmarkEntity.Constants.favoritesFolderID)
}

favoritesUUIDs.forEach { uuid in
if let bookmark = entitiesByUUID[uuid] {
bookmark.removeFromFavorites()
bookmark.addToFavorites(favoritesRoot: favoritesFolder)
}
}
}

// MARK: - Private

private func processTopLevelFolder(_ topLevelFolderSyncable: SyncableBookmarkAdapter) throws {
guard let topLevelFolderUUID = topLevelFolderSyncable.uuid else {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,54 @@ final class BookmarksRegularSyncResponseHandlerTests: BookmarksProviderTestsBase
})
}

func testWhenPayloadDoesNotContainFavoritesFolderThenFavoritesAreNotAffected() async throws {
let context = bookmarksDatabase.makeContext(concurrencyType: .privateQueueConcurrencyType)

let bookmarkTree = BookmarkTree {
Bookmark(id: "1", isFavorite: true)
Folder(id: "2") {
Bookmark(id: "3", isFavorite: true)
}
}

let received: [Syncable] = [
.rootFolder(children: ["1", "2", "4"]),
.bookmark(id: "4")
]

let rootFolder = try await createEntitiesAndHandleSyncResponse(with: bookmarkTree, received: received, in: context)
assertEquivalent(withTimestamps: false, rootFolder, BookmarkTree {
Bookmark(id: "1", isFavorite: true)
Folder(id: "2") {
Bookmark(id: "3", isFavorite: true)
}
Bookmark(id: "4")
})
}

func testWhenPayloadContainsEmptyFavoritesFolderThenAllFavoritesAreRemoved() async throws {
let context = bookmarksDatabase.makeContext(concurrencyType: .privateQueueConcurrencyType)

let bookmarkTree = BookmarkTree {
Bookmark(id: "1", isFavorite: true)
Folder(id: "2") {
Bookmark(id: "3", isFavorite: true)
}
}

let received: [Syncable] = [
.favoritesFolder(favorites: [])
]

let rootFolder = try await createEntitiesAndHandleSyncResponse(with: bookmarkTree, received: received, in: context)
assertEquivalent(withTimestamps: false, rootFolder, BookmarkTree {
Bookmark(id: "1")
Folder(id: "2") {
Bookmark(id: "3")
}
})
}

func testThatSinglePayloadCanCreateReorderAndOrphanBookmarks() async throws {
let context = bookmarksDatabase.makeContext(concurrencyType: .privateQueueConcurrencyType)

Expand Down

0 comments on commit 0ac6d8e

Please sign in to comment.