2023-01-08 10:22:52 +01:00
|
|
|
import CryptoKit
|
2023-01-17 11:36:01 +01:00
|
|
|
import Env
|
|
|
|
import KeychainSwift
|
2023-01-08 10:22:52 +01:00
|
|
|
import Models
|
2023-01-08 11:13:17 +01:00
|
|
|
import UIKit
|
2023-01-17 11:36:01 +01:00
|
|
|
import UserNotifications
|
2023-01-08 10:22:52 +01:00
|
|
|
|
|
|
|
@MainActor
|
|
|
|
class NotificationService: UNNotificationServiceExtension {
|
|
|
|
var contentHandler: ((UNNotificationContent) -> Void)?
|
|
|
|
var bestAttemptContent: UNMutableNotificationContent?
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-08 10:22:52 +01:00
|
|
|
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
|
|
|
|
self.contentHandler = contentHandler
|
|
|
|
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-08 10:22:52 +01:00
|
|
|
if let bestAttemptContent {
|
2023-01-08 14:16:43 +01:00
|
|
|
let privateKey = PushNotificationsService.shared.notificationsPrivateKeyAsKey
|
|
|
|
let auth = PushNotificationsService.shared.notificationsAuthKeyAsKey
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-08 10:22:52 +01:00
|
|
|
guard let encodedPayload = bestAttemptContent.userInfo["m"] as? String,
|
2023-01-17 11:36:01 +01:00
|
|
|
let payload = Data(base64Encoded: encodedPayload.URLSafeBase64ToBase64())
|
|
|
|
else {
|
2023-01-08 10:22:52 +01:00
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
return
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-08 10:22:52 +01:00
|
|
|
guard let encodedPublicKey = bestAttemptContent.userInfo["k"] as? String,
|
|
|
|
let publicKeyData = Data(base64Encoded: encodedPublicKey.URLSafeBase64ToBase64()),
|
2023-01-17 11:36:01 +01:00
|
|
|
let publicKey = try? P256.KeyAgreement.PublicKey(x963Representation: publicKeyData)
|
|
|
|
else {
|
2023-01-08 10:22:52 +01:00
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
return
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-08 10:22:52 +01:00
|
|
|
guard let encodedSalt = bestAttemptContent.userInfo["s"] as? String,
|
2023-01-17 11:36:01 +01:00
|
|
|
let salt = Data(base64Encoded: encodedSalt.URLSafeBase64ToBase64())
|
|
|
|
else {
|
2023-01-08 10:22:52 +01:00
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
return
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-08 10:22:52 +01:00
|
|
|
guard let plaintextData = NotificationService.decrypt(payload: payload,
|
|
|
|
salt: salt,
|
|
|
|
auth: auth,
|
|
|
|
privateKey: privateKey,
|
|
|
|
publicKey: publicKey),
|
2023-01-17 11:36:01 +01:00
|
|
|
let notification = try? JSONDecoder().decode(MastodonPushNotification.self, from: plaintextData)
|
|
|
|
else {
|
2023-01-08 10:22:52 +01:00
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
return
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-08 10:22:52 +01:00
|
|
|
bestAttemptContent.title = notification.title
|
|
|
|
bestAttemptContent.subtitle = ""
|
|
|
|
bestAttemptContent.body = notification.body.escape()
|
|
|
|
bestAttemptContent.userInfo["plaintext"] = plaintextData
|
2023-01-17 11:36:01 +01:00
|
|
|
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "glass.wav"))
|
|
|
|
|
2023-01-17 07:39:13 +01:00
|
|
|
let preferences = UserPreferences.shared
|
2023-01-09 18:52:53 +01:00
|
|
|
preferences.pushNotificationsCount += 1
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-09 18:52:53 +01:00
|
|
|
bestAttemptContent.badge = .init(integerLiteral: preferences.pushNotificationsCount)
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-08 11:13:17 +01:00
|
|
|
if let urlString = notification.icon,
|
2023-01-17 11:36:01 +01:00
|
|
|
let url = URL(string: urlString)
|
|
|
|
{
|
2023-01-08 11:13:17 +01:00
|
|
|
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("notification-attachments")
|
|
|
|
try? FileManager.default.createDirectory(at: temporaryDirectoryURL, withIntermediateDirectories: true, attributes: nil)
|
|
|
|
let filename = url.lastPathComponent
|
|
|
|
let fileURL = temporaryDirectoryURL.appendingPathComponent(filename)
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-08 11:13:17 +01:00
|
|
|
Task {
|
|
|
|
if let (data, _) = try? await URLSession.shared.data(for: .init(url: url)) {
|
|
|
|
if let image = UIImage(data: data) {
|
|
|
|
try? image.pngData()?.write(to: fileURL)
|
|
|
|
if let attachment = try? UNNotificationAttachment(identifier: filename, url: fileURL, options: nil) {
|
|
|
|
bestAttemptContent.attachments = [attachment]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
} else {
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
}
|
2023-01-08 10:22:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|