mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 10:33:45 +00:00
Remove delivered notifications for rooms that have meantime become fully read and have messages newer than what's delivered
- this is a best effort solution to cleanup outdated notifications when using multiple clients and reading them elsewhere
This commit is contained in:
committed by
Stefan Ceriu
parent
e14672e1f6
commit
899c33a5ad
@@ -128,6 +128,28 @@ final class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
.map(\.request.identifier)
|
||||
notificationCenter.removeDeliveredNotifications(withIdentifiers: notificationsIdentifiers)
|
||||
}
|
||||
|
||||
func removeDeliveredNotificationsForFullyReadRooms(_ rooms: [RoomSummary]) async {
|
||||
let roomsToLastMessageDates = rooms
|
||||
.filter { $0.hasUnreadMessages == false }
|
||||
.reduce(into: [:]) { partialResult, roomSummary in
|
||||
partialResult[roomSummary.id] = roomSummary.lastMessageDate
|
||||
}
|
||||
|
||||
let notificationsIdentifiers = await notificationCenter
|
||||
.deliveredNotifications()
|
||||
.filter { notification in
|
||||
guard let roomID = notification.request.content.roomID,
|
||||
let lastMessageDate = roomsToLastMessageDates[roomID] else {
|
||||
return false
|
||||
}
|
||||
|
||||
return notification.date <= lastMessageDate
|
||||
}
|
||||
.map(\.request.identifier)
|
||||
|
||||
notificationCenter.removeDeliveredNotifications(withIdentifiers: notificationsIdentifiers)
|
||||
}
|
||||
|
||||
private func setPusher(with deviceToken: Data, clientProxy: ClientProxyProtocol) async -> Bool {
|
||||
do {
|
||||
|
||||
@@ -33,4 +33,6 @@ protocol NotificationManagerProtocol: AnyObject {
|
||||
func requestAuthorization()
|
||||
|
||||
func removeDeliveredMessageNotifications(for roomID: String) async
|
||||
|
||||
func removeDeliveredNotificationsForFullyReadRooms(_ rooms: [RoomSummary]) async
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ struct RoomSummary {
|
||||
let avatarURL: URL?
|
||||
let heroes: [UserProfileProxy]
|
||||
let lastMessage: AttributedString?
|
||||
let lastMessageDate: Date?
|
||||
let lastMessageFormattedTimestamp: String?
|
||||
let unreadMessagesCount: UInt
|
||||
let unreadMentionsCount: UInt
|
||||
@@ -95,6 +96,7 @@ extension RoomSummary {
|
||||
avatarURL = nil
|
||||
heroes = []
|
||||
lastMessage = AttributedString(string)
|
||||
lastMessageDate = .now
|
||||
lastMessageFormattedTimestamp = "Now"
|
||||
unreadMessagesCount = hasUnreadMessages ? 1 : 0
|
||||
unreadMentionsCount = hasUnreadMentions ? 1 : 0
|
||||
|
||||
@@ -242,10 +242,12 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
|
||||
}
|
||||
|
||||
var attributedLastMessage: AttributedString?
|
||||
var lastMessageDate: Date?
|
||||
var lastMessageFormattedTimestamp: String?
|
||||
|
||||
if let latestRoomMessage = roomDetails.latestEvent {
|
||||
let lastMessage = EventTimelineItemProxy(item: latestRoomMessage, uniqueID: .init("0"))
|
||||
lastMessageDate = lastMessage.timestamp
|
||||
lastMessageFormattedTimestamp = lastMessage.timestamp.formattedMinimal()
|
||||
attributedLastMessage = eventStringBuilder.buildAttributedString(for: lastMessage)
|
||||
}
|
||||
@@ -271,6 +273,7 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
|
||||
avatarURL: roomInfo.avatarUrl.flatMap(URL.init(string:)),
|
||||
heroes: roomInfo.heroes.map(UserProfileProxy.init),
|
||||
lastMessage: attributedLastMessage,
|
||||
lastMessageDate: lastMessageDate,
|
||||
lastMessageFormattedTimestamp: lastMessageFormattedTimestamp,
|
||||
unreadMessagesCount: UInt(roomInfo.numUnreadMessages),
|
||||
unreadMentionsCount: UInt(roomInfo.numUnreadMentions),
|
||||
|
||||
Reference in New Issue
Block a user