192 lines
8.2 KiB
Swift
192 lines
8.2 KiB
Swift
//
|
|
// Copyright 2022-2024 New Vector Ltd.
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
// Please see LICENSE files in the repository root for full details.
|
|
//
|
|
|
|
import CallKit
|
|
import MatrixRustSDK
|
|
import UserNotifications
|
|
|
|
class NotificationHandler {
|
|
private let settings: CommonSettingsProtocol
|
|
private let contentHandler: (UNNotificationContent) -> Void
|
|
private var notificationContent: UNMutableNotificationContent
|
|
private let tag: String
|
|
|
|
private let notificationContentBuilder: NotificationContentBuilder
|
|
|
|
init(settings: CommonSettingsProtocol,
|
|
contentHandler: @escaping (UNNotificationContent) -> Void,
|
|
notificationContent: UNMutableNotificationContent,
|
|
tag: String) {
|
|
self.settings = settings
|
|
self.contentHandler = contentHandler
|
|
self.notificationContent = notificationContent
|
|
self.tag = tag
|
|
|
|
let eventStringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(mentionBuilder: PlainMentionBuilder()),
|
|
destination: .notification)
|
|
|
|
notificationContentBuilder = NotificationContentBuilder(messageEventStringBuilder: eventStringBuilder,
|
|
settings: settings)
|
|
}
|
|
|
|
func processEvent(_ eventID: String,
|
|
roomID: String,
|
|
userSession: NSEUserSession) async {
|
|
MXLog.info("\(tag) Processing event: \(eventID) in room: \(roomID)")
|
|
|
|
guard let notificationItemProxy = await userSession.notificationItemProxy(roomID: roomID, eventID: eventID) else {
|
|
MXLog.error("\(tag) Failed retrieving notification item")
|
|
discardNotification()
|
|
return
|
|
}
|
|
|
|
switch await preprocessNotification(notificationItemProxy) {
|
|
case .processedShouldDiscard, .unsupportedShouldDiscard:
|
|
discardNotification()
|
|
case .shouldDisplay:
|
|
await notificationContentBuilder.process(notificationContent: ¬ificationContent,
|
|
notificationItem: notificationItemProxy,
|
|
mediaProvider: userSession.mediaProvider)
|
|
|
|
deliverNotification()
|
|
}
|
|
}
|
|
|
|
func handleTimeExpiration() {
|
|
// Called just before the extension will be terminated by the system.
|
|
// Use this as an opportunity to deliver your "best attempt" at modified content
|
|
MXLog.info("\(tag) Extension time will expire")
|
|
deliverNotification()
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func deliverNotification() {
|
|
MXLog.info("\(tag) Delivering notification")
|
|
contentHandler(notificationContent)
|
|
}
|
|
|
|
private func discardNotification() {
|
|
MXLog.info("\(tag) Discarding notification")
|
|
|
|
let content = UNMutableNotificationContent()
|
|
|
|
if let unreadCount = notificationContent.unreadCount {
|
|
content.badge = NSNumber(value: unreadCount)
|
|
}
|
|
|
|
contentHandler(content)
|
|
}
|
|
|
|
private func preprocessNotification(_ itemProxy: NotificationItemProxyProtocol) async -> NotificationProcessingResult {
|
|
guard case let .timeline(event) = itemProxy.event else {
|
|
return .shouldDisplay
|
|
}
|
|
|
|
switch try? event.eventType() {
|
|
case .messageLike(let content):
|
|
switch content {
|
|
case .poll,
|
|
.roomEncrypted,
|
|
.sticker:
|
|
return .shouldDisplay
|
|
case .roomMessage(let messageType, _):
|
|
switch messageType {
|
|
case .emote, .image, .audio, .video, .file, .notice, .text, .location:
|
|
return .shouldDisplay
|
|
case .other:
|
|
return .unsupportedShouldDiscard
|
|
}
|
|
case .roomRedaction(let redactedEventID, _):
|
|
guard let redactedEventID else {
|
|
MXLog.error("Unable to handle redact notification due to missing event ID")
|
|
return .processedShouldDiscard
|
|
}
|
|
|
|
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 .processedShouldDiscard
|
|
case .callNotify(let notifyType):
|
|
return await handleCallNotification(notifyType: notifyType,
|
|
timestamp: event.timestamp(),
|
|
roomID: itemProxy.roomID,
|
|
roomDisplayName: itemProxy.roomDisplayName)
|
|
case .callAnswer,
|
|
.callInvite,
|
|
.callHangup,
|
|
.callCandidates,
|
|
.keyVerificationReady,
|
|
.keyVerificationStart,
|
|
.keyVerificationCancel,
|
|
.keyVerificationAccept,
|
|
.keyVerificationKey,
|
|
.keyVerificationMac,
|
|
.keyVerificationDone,
|
|
.reactionContent:
|
|
return .unsupportedShouldDiscard
|
|
}
|
|
case .state:
|
|
return .unsupportedShouldDiscard
|
|
case .none:
|
|
return .unsupportedShouldDiscard
|
|
}
|
|
}
|
|
|
|
/// Handle incoming call notifications.
|
|
/// - Returns: A boolean indicating whether the notification was handled and should now be discarded.
|
|
private func handleCallNotification(notifyType: NotifyType,
|
|
timestamp: Timestamp,
|
|
roomID: String,
|
|
roomDisplayName: String) async -> NotificationProcessingResult {
|
|
// Handle incoming VoIP calls, show the native OS call screen
|
|
// https://developer.apple.com/documentation/callkit/sending-end-to-end-encrypted-voip-calls
|
|
//
|
|
// The way this works is the following:
|
|
// - the NSE receives the notification and decrypts it
|
|
// - checks if it's still time relevant (max 10 seconds old) and whether it should ring
|
|
// - otherwise it goes on to show it as a normal notification
|
|
// - if it should ring then it discards the notification but invokes `reportNewIncomingVoIPPushPayload`
|
|
// so that the main app can handle it
|
|
// - the main app picks this up in `PKPushRegistry.didReceiveIncomingPushWith` and
|
|
// `CXProvider.reportNewIncomingCall` to show the system UI and handle actions on it.
|
|
// N.B. this flow works properly only when background processing capabilities are enabled
|
|
guard notifyType == .ring else {
|
|
MXLog.info("Non-ringing call notification, handling as push notification")
|
|
return .shouldDisplay
|
|
}
|
|
|
|
let timestamp = Date(timeIntervalSince1970: TimeInterval(timestamp / 1000))
|
|
guard abs(timestamp.timeIntervalSinceNow) < ElementCallServiceNotificationDiscardDelta else {
|
|
MXLog.info("Call notification is too old, handling as push notification")
|
|
return .shouldDisplay
|
|
}
|
|
|
|
let payload = [ElementCallServiceNotificationKey.roomID.rawValue: roomID,
|
|
ElementCallServiceNotificationKey.roomDisplayName.rawValue: roomDisplayName]
|
|
|
|
do {
|
|
try await CXProvider.reportNewIncomingVoIPPushPayload(payload)
|
|
MXLog.info("Call notification delegated to CallKit")
|
|
} catch {
|
|
MXLog.error("Failed reporting voip call with error: \(error). Handling as push notification")
|
|
return .shouldDisplay
|
|
}
|
|
|
|
return .processedShouldDiscard
|
|
}
|
|
|
|
private enum NotificationProcessingResult {
|
|
case shouldDisplay
|
|
case processedShouldDiscard
|
|
case unsupportedShouldDiscard
|
|
}
|
|
}
|