From ddcb13dd28a22c2a1e43c31b5e6acd17557ea788 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 9 May 2022 20:11:15 -0400 Subject: [PATCH] Fix notifications sometimes getting deleted in group merging Closes #156 --- .../Utilities/NotificationGroup.swift | 3 +- .../NotificationGroupTests.swift | 35 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Pachyderm/Sources/Pachyderm/Utilities/NotificationGroup.swift b/Pachyderm/Sources/Pachyderm/Utilities/NotificationGroup.swift index 9f5b80c2..cab861b0 100644 --- a/Pachyderm/Sources/Pachyderm/Utilities/NotificationGroup.swift +++ b/Pachyderm/Sources/Pachyderm/Utilities/NotificationGroup.swift @@ -97,13 +97,14 @@ public struct NotificationGroup: Identifiable, Hashable { break } - if canMerge(notification: firstGroupFromSecond.notifications.first!, into: lastGroup) { merged[merged.count - 1].append(group: firstGroupFromSecond) } else if merged.count >= 2 { let secondToLastGroup = merged[merged.count - 2] if allowedTypes.contains(secondToLastGroup.kind), canMerge(notification: firstGroupFromSecond.notifications.first!, into: secondToLastGroup) { merged[merged.count - 2].append(group: firstGroupFromSecond) + } else { + merged.append(firstGroupFromSecond) } } else { merged.append(firstGroupFromSecond) diff --git a/Pachyderm/Tests/PachydermTests/NotificationGroupTests.swift b/Pachyderm/Tests/PachydermTests/NotificationGroupTests.swift index a2069cbd..fc361e01 100644 --- a/Pachyderm/Tests/PachydermTests/NotificationGroupTests.swift +++ b/Pachyderm/Tests/PachydermTests/NotificationGroupTests.swift @@ -148,9 +148,31 @@ class NotificationGroupTests: XCTestCase { "status": \(statusB) """.data(using: .utf8)! lazy var likeB = try! decoder.decode(Notification.self, from: likeBData) - lazy var mentionBData = """ + lazy var likeB2Data = """ { "id": "4", + "type": "favourite", + "created_at": "2019-11-23T07:29:18Z", + "account": { + "id": "2", + "username": "bar", + "acct": "bar", + "display_name": "bar", + "locked": false, + "created_at": "2019-11-02T01:01:01Z", + "followers_count": 0, + "following_count": 0, + "statuses_count": 0, + "note": "", + "url": "https://example.com/@bar", + "uri": "https://example.com/@bar", + }, + "status": \(statusB) + """.data(using: .utf8)! + lazy var likeB2 = try! decoder.decode(Notification.self, from: likeB2Data) + lazy var mentionBData = """ + { + "id": "5", "type": "mention", "created_at": "2019-11-23T07:29:18Z", "account": { @@ -218,6 +240,17 @@ class NotificationGroupTests: XCTestCase { NotificationGroup(notifications: [likeA1, likeA2])!, NotificationGroup(notifications: [likeB])!, ]) + + let group4 = NotificationGroup(notifications: [likeB2])! + let group5 = NotificationGroup(notifications: [mentionB])! + let merged3 = NotificationGroup.mergeGroups(first: [group1, group5, group2], second: [group4, group3], only: [.favourite]) + print(merged3.count) + XCTAssertEqual(merged3, [ + group1, + group5, + NotificationGroup(notifications: [likeB, likeB2]), + group3 + ]) } func testDontMergeWithUngroupableInBetween() {