Fix notifications sometimes getting deleted in group merging

Closes #156
This commit is contained in:
Shadowfacts 2022-05-09 20:11:15 -04:00
parent c71bf3ba23
commit ddcb13dd28
2 changed files with 36 additions and 2 deletions

View File

@ -97,13 +97,14 @@ public struct NotificationGroup: Identifiable, Hashable {
break break
} }
if canMerge(notification: firstGroupFromSecond.notifications.first!, into: lastGroup) { if canMerge(notification: firstGroupFromSecond.notifications.first!, into: lastGroup) {
merged[merged.count - 1].append(group: firstGroupFromSecond) merged[merged.count - 1].append(group: firstGroupFromSecond)
} else if merged.count >= 2 { } else if merged.count >= 2 {
let secondToLastGroup = merged[merged.count - 2] let secondToLastGroup = merged[merged.count - 2]
if allowedTypes.contains(secondToLastGroup.kind), canMerge(notification: firstGroupFromSecond.notifications.first!, into: secondToLastGroup) { if allowedTypes.contains(secondToLastGroup.kind), canMerge(notification: firstGroupFromSecond.notifications.first!, into: secondToLastGroup) {
merged[merged.count - 2].append(group: firstGroupFromSecond) merged[merged.count - 2].append(group: firstGroupFromSecond)
} else {
merged.append(firstGroupFromSecond)
} }
} else { } else {
merged.append(firstGroupFromSecond) merged.append(firstGroupFromSecond)

View File

@ -148,9 +148,31 @@ class NotificationGroupTests: XCTestCase {
"status": \(statusB) "status": \(statusB)
""".data(using: .utf8)! """.data(using: .utf8)!
lazy var likeB = try! decoder.decode(Notification.self, from: likeBData) lazy var likeB = try! decoder.decode(Notification.self, from: likeBData)
lazy var mentionBData = """ lazy var likeB2Data = """
{ {
"id": "4", "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", "type": "mention",
"created_at": "2019-11-23T07:29:18Z", "created_at": "2019-11-23T07:29:18Z",
"account": { "account": {
@ -218,6 +240,17 @@ class NotificationGroupTests: XCTestCase {
NotificationGroup(notifications: [likeA1, likeA2])!, NotificationGroup(notifications: [likeA1, likeA2])!,
NotificationGroup(notifications: [likeB])!, 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() { func testDontMergeWithUngroupableInBetween() {