2022-11-21 19:37:13 +03:00
|
|
|
//
|
|
|
|
// Copyright 2022 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Intents
|
|
|
|
import MatrixRustSDK
|
|
|
|
import UserNotifications
|
|
|
|
|
2023-11-03 21:30:58 +01:00
|
|
|
// The lifecycle of the NSE looks something like the following:
|
|
|
|
// 1) App receives notification
|
|
|
|
// 2) System creates an instance of the extension class
|
|
|
|
// and calls `didReceive` in the background
|
|
|
|
// 3) Extension processes messages / displays whatever
|
|
|
|
// notifications it needs to
|
|
|
|
// 4) Extension notifies its work is complete by calling
|
|
|
|
// the contentHandler
|
|
|
|
// 5) If the extension takes too long to perform its work
|
|
|
|
// (more than 30s), it will be notified and immediately
|
|
|
|
// terminated
|
|
|
|
//
|
|
|
|
// Note that the NSE does *not* always spawn a new process to
|
|
|
|
// handle a new notification and will also try and process notifications
|
|
|
|
// in parallel. `didReceive` could be called twice for the same process,
|
|
|
|
// but it will always be called on different threads. It may or may not be
|
|
|
|
// called on the same instance of `NotificationService` as a previous
|
|
|
|
// notification.
|
|
|
|
//
|
|
|
|
// We keep a global `environment` singleton to ensure that our app context,
|
|
|
|
// database, logging, etc. are only ever setup once per *process*
|
|
|
|
|
2023-11-09 19:12:11 +01:00
|
|
|
private let settings: NSESettingsProtocol = AppSettings()
|
|
|
|
private let notificationContentBuilder = NotificationContentBuilder(messageEventStringBuilder: RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(permalinkBaseURL: settings.permalinkBaseURL,
|
2023-11-03 21:30:58 +01:00
|
|
|
mentionBuilder: PlainMentionBuilder())))
|
|
|
|
private let keychainController = KeychainController(service: .sessions,
|
|
|
|
accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier)
|
|
|
|
private var userSessions = [String: NSEUserSession]()
|
|
|
|
|
2022-11-21 19:37:13 +03:00
|
|
|
class NotificationServiceExtension: UNNotificationServiceExtension {
|
2023-05-05 17:29:46 +02:00
|
|
|
private var handler: ((UNNotificationContent) -> Void)?
|
|
|
|
private var modifiedContent: UNMutableNotificationContent?
|
2022-11-21 19:37:13 +03:00
|
|
|
|
|
|
|
override func didReceive(_ request: UNNotificationRequest,
|
|
|
|
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
|
|
|
|
guard !DataProtectionManager.isDeviceLockedAfterReboot(containerURL: URL.appGroupContainerDirectory),
|
|
|
|
let roomId = request.roomId,
|
|
|
|
let eventId = request.eventId,
|
2023-05-03 16:28:07 +02:00
|
|
|
let clientID = request.pusherNotificationClientIdentifier,
|
|
|
|
let credentials = keychainController.restorationTokens().first(where: { $0.restorationToken.pusherNotificationClientIdentifier == clientID }) else {
|
2022-11-21 19:37:13 +03:00
|
|
|
// We cannot process this notification, it might be due to one of these:
|
|
|
|
// - Device rebooted and locked
|
|
|
|
// - Not a Matrix notification
|
|
|
|
// - User is not signed in
|
2023-04-05 20:16:30 +02:00
|
|
|
// - NotificationID could not be resolved
|
2022-11-21 19:37:13 +03:00
|
|
|
return contentHandler(request.content)
|
|
|
|
}
|
|
|
|
|
|
|
|
handler = contentHandler
|
|
|
|
modifiedContent = request.content.mutableCopy() as? UNMutableNotificationContent
|
|
|
|
|
2023-11-04 08:36:49 +00:00
|
|
|
NSELogger.configure(logLevel: settings.logLevel)
|
2022-11-21 19:37:13 +03:00
|
|
|
|
|
|
|
NSELogger.logMemory(with: tag)
|
|
|
|
|
2023-01-17 09:28:01 +00:00
|
|
|
MXLog.info("\(tag) #########################################")
|
|
|
|
MXLog.info("\(tag) Payload came: \(request.content.userInfo)")
|
2022-11-21 19:37:13 +03:00
|
|
|
|
|
|
|
Task {
|
2023-05-05 17:29:46 +02:00
|
|
|
await run(with: credentials,
|
|
|
|
roomId: roomId,
|
2023-09-05 15:35:45 +03:00
|
|
|
eventId: eventId,
|
|
|
|
unreadCount: request.unreadCount)
|
2022-11-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func serviceExtensionTimeWillExpire() {
|
|
|
|
// Called just before the extension will be terminated by the system.
|
|
|
|
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
|
2023-01-17 09:28:01 +00:00
|
|
|
MXLog.warning("\(tag) serviceExtensionTimeWillExpire")
|
2023-11-11 15:38:45 +02:00
|
|
|
notify(unreadCount: nil)
|
2022-11-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private func run(with credentials: KeychainCredentials,
|
|
|
|
roomId: String,
|
2023-09-05 15:35:45 +03:00
|
|
|
eventId: String,
|
|
|
|
unreadCount: Int?) async {
|
2023-01-17 09:28:01 +00:00
|
|
|
MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)")
|
2022-11-21 19:37:13 +03:00
|
|
|
|
2023-05-05 17:29:46 +02:00
|
|
|
do {
|
2023-11-03 21:30:58 +01:00
|
|
|
let userSession: NSEUserSession
|
|
|
|
if let existingSession = userSessions[credentials.userID] {
|
|
|
|
userSession = existingSession
|
|
|
|
} else {
|
2024-04-08 10:26:33 +03:00
|
|
|
userSession = try await NSEUserSession(credentials: credentials, clientSessionDelegate: keychainController)
|
2023-11-03 21:30:58 +01:00
|
|
|
userSessions[credentials.userID] = userSession
|
|
|
|
}
|
2023-09-15 10:01:09 +01:00
|
|
|
|
2023-07-12 12:28:41 +02:00
|
|
|
guard let itemProxy = await userSession.notificationItemProxy(roomID: roomId, eventID: eventId) else {
|
2023-06-22 19:23:33 +02:00
|
|
|
MXLog.info("\(tag) no notification for the event, discard")
|
2023-11-11 15:38:45 +02:00
|
|
|
return discard(unreadCount: unreadCount)
|
2023-06-22 13:24:37 +02:00
|
|
|
}
|
2023-09-13 12:30:41 +03:00
|
|
|
|
2023-05-05 17:29:46 +02:00
|
|
|
// After the first processing, update the modified content
|
2023-09-13 12:30:41 +03:00
|
|
|
modifiedContent = try await notificationContentBuilder.content(for: itemProxy, mediaProvider: nil)
|
|
|
|
|
2023-07-05 14:28:45 +02:00
|
|
|
guard itemProxy.hasMedia else {
|
2023-05-05 17:29:46 +02:00
|
|
|
MXLog.info("\(tag) no media needed")
|
2022-11-21 19:37:13 +03:00
|
|
|
|
2023-05-05 17:29:46 +02:00
|
|
|
// We've processed the item and no media operations needed, so no need to go further
|
2023-11-11 15:38:45 +02:00
|
|
|
return notify(unreadCount: unreadCount)
|
2023-05-05 17:29:46 +02:00
|
|
|
}
|
2022-11-21 19:37:13 +03:00
|
|
|
|
2023-05-05 17:29:46 +02:00
|
|
|
MXLog.info("\(tag) process with media")
|
2022-11-21 19:37:13 +03:00
|
|
|
|
2023-05-05 17:29:46 +02:00
|
|
|
// There is some media to load, process it again
|
2023-09-13 12:30:41 +03:00
|
|
|
if let latestContent = try? await notificationContentBuilder.content(for: itemProxy, mediaProvider: userSession.mediaProvider) {
|
2023-05-05 17:29:46 +02:00
|
|
|
// Processing finished, hopefully with some media
|
|
|
|
modifiedContent = latestContent
|
|
|
|
}
|
|
|
|
// We still notify, but without the media attachment if it fails to load
|
2023-09-05 15:35:45 +03:00
|
|
|
|
2023-11-11 15:38:45 +02:00
|
|
|
return notify(unreadCount: unreadCount)
|
2023-05-05 17:29:46 +02:00
|
|
|
} catch {
|
|
|
|
MXLog.error("NSE run error: \(error)")
|
2023-11-11 15:38:45 +02:00
|
|
|
return discard(unreadCount: unreadCount)
|
2022-11-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
}
|
2023-01-25 19:45:01 +02:00
|
|
|
|
2023-11-11 15:38:45 +02:00
|
|
|
private func notify(unreadCount: Int?) {
|
2023-01-17 09:28:01 +00:00
|
|
|
MXLog.info("\(tag) notify")
|
2022-11-21 19:37:13 +03:00
|
|
|
|
|
|
|
guard let modifiedContent else {
|
2023-01-17 09:28:01 +00:00
|
|
|
MXLog.info("\(tag) notify: no modified content")
|
2023-11-11 15:38:45 +02:00
|
|
|
return discard(unreadCount: unreadCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let unreadCount {
|
|
|
|
modifiedContent.badge = NSNumber(value: unreadCount)
|
2022-11-21 19:37:13 +03:00
|
|
|
}
|
2023-05-03 16:28:07 +02:00
|
|
|
|
2022-11-21 19:37:13 +03:00
|
|
|
handler?(modifiedContent)
|
2023-06-22 19:23:33 +02:00
|
|
|
cleanUp()
|
2022-11-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
|
2023-11-11 15:38:45 +02:00
|
|
|
private func discard(unreadCount: Int?) {
|
2023-01-17 09:28:01 +00:00
|
|
|
MXLog.info("\(tag) discard")
|
2023-11-11 15:38:45 +02:00
|
|
|
|
|
|
|
let content = UNMutableNotificationContent()
|
|
|
|
|
|
|
|
if let unreadCount {
|
|
|
|
content.badge = NSNumber(value: unreadCount)
|
|
|
|
}
|
2022-11-21 19:37:13 +03:00
|
|
|
|
2023-11-11 15:38:45 +02:00
|
|
|
handler?(content)
|
2023-06-22 19:23:33 +02:00
|
|
|
cleanUp()
|
2022-11-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private var tag: String {
|
|
|
|
"[NSE][\(Unmanaged.passUnretained(self).toOpaque())][\(Unmanaged.passUnretained(Thread.current).toOpaque())]"
|
|
|
|
}
|
|
|
|
|
2023-06-22 19:23:33 +02:00
|
|
|
private func cleanUp() {
|
|
|
|
handler = nil
|
|
|
|
modifiedContent = nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 19:37:13 +03:00
|
|
|
deinit {
|
2023-06-22 19:23:33 +02:00
|
|
|
cleanUp()
|
2022-11-21 19:37:13 +03:00
|
|
|
NSELogger.logMemory(with: tag)
|
2023-01-17 09:28:01 +00:00
|
|
|
MXLog.info("\(tag) deinit")
|
2022-11-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
}
|