Remote Push Notifications can now be decrypted (#854)

* refactored the NSE to use the client function

* removed unused imports in the target.yml

* some code improvements

* changelog

* code improvement

* code improvement

* more code improvements

* separated the client and the media provider in a dedicated NSEUserSession

* Update ElementX/Sources/Services/Client/ClientProxy.swift

Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>

* pr suggestions

* logging the error

---------

Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>
This commit is contained in:
Mauro
2023-05-05 17:29:46 +02:00
committed by GitHub
parent b17c987fd0
commit f679c0ca2a
10 changed files with 89 additions and 150 deletions
+28 -57
View File
@@ -22,8 +22,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
private let settings = NSESettings()
private lazy var keychainController = KeychainController(service: .sessions,
accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier)
var handler: ((UNNotificationContent) -> Void)?
var modifiedContent: UNMutableNotificationContent?
private var handler: ((UNNotificationContent) -> Void)?
private var modifiedContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
@@ -51,9 +51,9 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
MXLog.info("\(tag) Payload came: \(request.content.userInfo)")
Task {
try await run(with: credentials,
roomId: roomId,
eventId: eventId)
await run(with: credentials,
roomId: roomId,
eventId: eventId)
}
}
@@ -66,80 +66,51 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
private func run(with credentials: KeychainCredentials,
roomId: String,
eventId: String) async throws {
eventId: String) async {
MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)")
let service = NotificationServiceProxy(basePath: URL.sessionsBaseDirectory.path,
userID: credentials.userID)
do {
let userSession = try NSEUserSession(credentials: credentials)
let itemProxy = try await userSession.notificationItemProxy(roomID: roomId, eventID: eventId)
guard let itemProxy = try await service.notificationItem(roomId: roomId,
eventId: eventId) else {
MXLog.error("\(tag) got no notification item")
// After the first processing, update the modified content
modifiedContent = try await itemProxy.process(mediaProvider: nil)
// Notification should be discarded
return discard()
}
guard itemProxy.requiresMediaProvider else {
MXLog.info("\(tag) no media needed")
// First process without a media proxy.
// After this some properties of the notification should be set, like title, subtitle, sound etc.
guard let firstContent = try await itemProxy.process(mediaProvider: nil) else {
MXLog.error("\(tag) not even first content")
// We've processed the item and no media operations needed, so no need to go further
return notify()
}
// Notification should be discarded
return discard()
}
MXLog.info("\(tag) process with media")
// After the first processing, update the modified content
modifiedContent = firstContent
guard itemProxy.requiresMediaProvider else {
MXLog.info("\(tag) no media needed")
// We've processed the item and no media operations needed, so no need to go further
// There is some media to load, process it again
if let latestContent = try? await itemProxy.process(mediaProvider: userSession.mediaProvider) {
// Processing finished, hopefully with some media
modifiedContent = latestContent
}
// We still notify, but without the media attachment if it fails to load
return notify()
}
MXLog.info("\(tag) process with media")
// There is some media to load, process it again
if let latestContent = try await itemProxy.process(mediaProvider: createMediaProvider(with: credentials)) {
// Processing finished, hopefully with some media
modifiedContent = latestContent
return notify()
} else {
// This is not very likely, as it should discard the notification sooner
} catch {
MXLog.error("NSE run error: \(error)")
return discard()
}
}
private func createMediaProvider(with credentials: KeychainCredentials) throws -> MediaProviderProtocol {
let builder = ClientBuilder()
.basePath(path: URL.sessionsBaseDirectory.path)
.username(username: credentials.userID)
let client = try builder.build()
try client.restoreSession(session: credentials.restorationToken.session)
MXLog.info("\(tag) creating media provider")
return MediaProvider(mediaLoader: MediaLoader(client: client),
imageCache: .onlyOnDisk,
backgroundTaskService: nil)
}
private func notify() {
MXLog.info("\(tag) notify")
guard let modifiedContent else {
MXLog.info("\(tag) notify: no modified content")
return
return discard()
}
guard let identifier = modifiedContent.notificationID,
!settings.servedNotificationIdentifiers.contains(identifier) else {
MXLog.info("\(tag) notify: notification already served")
discard()
return
return discard()
}
settings.servedNotificationIdentifiers.insert(identifier)
+47
View File
@@ -0,0 +1,47 @@
//
// Copyright 2023 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 Foundation
import MatrixRustSDK
final class NSEUserSession {
private let client: ClientProtocol
private(set) lazy var mediaProvider: MediaProviderProtocol = MediaProvider(mediaLoader: MediaLoader(client: client),
imageCache: .onlyOnDisk,
backgroundTaskService: nil)
init(credentials: KeychainCredentials) throws {
let builder = ClientBuilder()
.basePath(path: URL.sessionsBaseDirectory.path)
.username(username: credentials.userID)
client = try builder.build()
try client.restoreSession(session: credentials.restorationToken.session)
}
func notificationItemProxy(roomID: String, eventID: String) async throws -> NotificationItemProxyProtocol {
let userID = try client.userId()
return await Task.dispatch(on: .global()) {
do {
let notification = try self.client.getNotificationItem(roomId: roomID, eventId: eventID)
return NotificationItemProxy(notificationItem: notification, receiverID: userID)
} catch {
MXLog.error("NSE: Could not get notification's content creating an empty notification instead, error: \(error)")
return EmptyNotificationItemProxy(eventID: eventID, roomID: roomID, receiverID: userID)
}
}
}
}