Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove a notification if its event has been redacted. #3191

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ElementX/Sources/Other/Extensions/UNNotificationContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ extension UNNotificationContent {
@objc var roomID: String? {
userInfo[NotificationConstants.UserInfoKey.roomIdentifier] as? String
}

@objc var eventID: String? {
userInfo[NotificationConstants.UserInfoKey.eventIdentifier] as? String
}
}

extension UNMutableNotificationContent {
Expand All @@ -64,6 +68,15 @@ extension UNMutableNotificationContent {
userInfo[NotificationConstants.UserInfoKey.roomIdentifier] = newValue
}
}

override var eventID: String? {
get {
userInfo[NotificationConstants.UserInfoKey.eventIdentifier] as? String
}
set {
userInfo[NotificationConstants.UserInfoKey.eventIdentifier] = newValue
}
}

func addMediaAttachment(using mediaProvider: MediaProviderProtocol?,
mediaSource: MediaSourceProxy) async -> UNMutableNotificationContent {
Expand Down
4 changes: 4 additions & 0 deletions NSE/Sources/NotificationContentBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ struct NotificationContentBuilder {
let notification = UNMutableNotificationContent()
notification.receiverID = notificationItem.receiverID
notification.roomID = notificationItem.roomID
notification.eventID = switch notificationItem.event {
case .timeline(let event): event.eventId()
case .invite, .none: nil
}
notification.sound = notificationItem.isNoisy ? UNNotificationSound(named: UNNotificationSoundName(rawValue: "message.caf")) : nil
// So that the UI groups notification that are received for the same room but also for the same user
// Removing the @ fixes an iOS bug where the notification crashes if the mute button is tapped
Expand Down
43 changes: 35 additions & 8 deletions NSE/Sources/NotificationServiceExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
guard !DataProtectionManager.isDeviceLockedAfterReboot(containerURL: URL.appGroupContainerDirectory),
let roomId = request.roomId,
let eventId = request.eventId,
let roomID = request.roomID,
let eventID = request.eventID,
let clientID = request.pusherNotificationClientIdentifier,
let credentials = keychainController.restorationTokens().first(where: { $0.restorationToken.pusherNotificationClientIdentifier == clientID }) else {
// We cannot process this notification, it might be due to one of these:
Expand Down Expand Up @@ -103,8 +103,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension {

Task {
await run(with: credentials,
roomId: roomId,
eventId: eventId,
roomID: roomID,
eventID: eventID,
unreadCount: request.unreadCount)
}
}
Expand All @@ -117,18 +117,18 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
}

private func run(with credentials: KeychainCredentials,
roomId: String,
eventId: String,
roomID: String,
eventID: String,
unreadCount: Int?) async {
MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)")
MXLog.info("\(tag) run with roomId: \(roomID), eventId: \(eventID)")

guard let userSession = Self.userSession else {
MXLog.error("Invalid NSE User Session, discarding.")
return discard(unreadCount: unreadCount)
}

do {
guard let itemProxy = await userSession.notificationItemProxy(roomID: roomId, eventID: eventId) else {
guard let itemProxy = await userSession.notificationItemProxy(roomID: roomID, eventID: eventID) else {
MXLog.info("\(tag) no notification for the event, discard")
return discard(unreadCount: unreadCount)
}
Expand All @@ -137,6 +137,10 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
return discard(unreadCount: unreadCount)
}

if await handleRedactionNotification(itemProxy) {
return discard(unreadCount: unreadCount)
}

// After the first processing, update the modified content
modifiedContent = try await notificationContentBuilder.content(for: itemProxy, mediaProvider: nil)

Expand Down Expand Up @@ -246,6 +250,29 @@ class NotificationServiceExtension: UNNotificationServiceExtension {

return false
}

/// Handles a notification for an `m.room.redaction` event.
/// - Returns: A boolean indicating whether the notification was handled.
private func handleRedactionNotification(_ itemProxy: NotificationItemProxyProtocol) async -> Bool {
guard case let .timeline(event) = itemProxy.event,
case let .messageLike(content) = try? event.eventType(),
case let .roomRedaction(redactedEventID, _) = content else {
return false
}

guard let redactedEventID else {
MXLog.error("Unable to redact notification due to missing event ID.")
return true // Return true as there's no point showing this notification.
}

let deliveredNotifications = await UNUserNotificationCenter.current().deliveredNotifications()

if let targetNotification = deliveredNotifications.first(where: { $0.request.content.eventID == redactedEventID }) {
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [targetNotification.request.identifier])
}

return true
}
}

// https://stackoverflow.com/a/77300959/730924
Expand Down
4 changes: 2 additions & 2 deletions NSE/Sources/Other/UNNotificationRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import Foundation
import UserNotifications

extension UNNotificationRequest {
var roomId: String? {
var roomID: String? {
content.userInfo[NotificationConstants.UserInfoKey.roomIdentifier] as? String
}

var eventId: String? {
var eventID: String? {
content.userInfo[NotificationConstants.UserInfoKey.eventIdentifier] as? String
}

Expand Down
Loading