This commit is contained in:
otsmr 2025-06-27 09:31:03 +02:00
parent 0195daf709
commit 2e81c17eb7

View file

@ -55,7 +55,7 @@ class NotificationService: UNNotificationServiceExtension {
func getPushNotificationData(pushData: String) -> ( func getPushNotificationData(pushData: String) -> (
title: String, body: String, notificationId: Int64 title: String, body: String, notificationId: Int64
)? { )? {
guard let data = Data(base64Encoded: pushData) else { guard let data = Data(base64Encoded: pushData) else {
NSLog("Failed to decode base64 string") NSLog("Failed to decode base64 string")
return nil return nil
@ -64,7 +64,6 @@ func getPushNotificationData(pushData: String) -> (
do { do {
let pushData = try EncryptedPushNotification(serializedBytes: data) let pushData = try EncryptedPushNotification(serializedBytes: data)
var pushNotification: PushNotification? var pushNotification: PushNotification?
var pushUser: PushUser? var pushUser: PushUser?
@ -78,12 +77,13 @@ func getPushNotificationData(pushData: String) -> (
for tryPushUser in pushUsers! { for tryPushUser in pushUsers! {
for pushKey in tryPushUser.pushKeys { for pushKey in tryPushUser.pushKeys {
if pushKey.id == pushData.keyID { if pushKey.id == pushData.keyID {
pushNotification = tryDecryptMessage(key: pushKey.key, pushData: pushData) pushNotification = tryDecryptMessage(
key: pushKey.key, pushData: pushData)
if pushNotification != nil { if pushNotification != nil {
if (pushNotification!.messageID <= pushUser!.lastMessageID) { pushUser = tryPushUser
if pushNotification!.messageID <= pushUser!.lastMessageID {
return ("blocked", "blocked", 0) return ("blocked", "blocked", 0)
} }
pushUser = tryPushUser
break break
} }
} }
@ -94,7 +94,7 @@ func getPushNotificationData(pushData: String) -> (
NSLog("pushKeys are empty") NSLog("pushKeys are empty")
} }
} }
if pushUser?.blocked == true { if pushUser?.blocked == true {
return ("blocked", "blocked", 0) return ("blocked", "blocked", 0)
} }
@ -105,9 +105,14 @@ func getPushNotificationData(pushData: String) -> (
if pushNotification.kind == .testNotification { if pushNotification.kind == .testNotification {
return ("Test Notification", "This is a test notification.", 0) return ("Test Notification", "This is a test notification.", 0)
} else if pushUser != nil { } else if pushUser != nil {
return (pushUser!.displayName, getPushNotificationText(pushNotification: pushNotification), pushUser!.userID) return (
pushUser!.displayName,
getPushNotificationText(pushNotification: pushNotification), pushUser!.userID
)
} else { } else {
return ("", getPushNotificationTextWithoutUserId(pushKind: pushNotification.kind), 1) return (
"", getPushNotificationTextWithoutUserId(pushKind: pushNotification.kind), 1
)
} }
} else { } else {
@ -136,7 +141,7 @@ func tryDecryptMessage(key: Data, pushData: EncryptedPushNotification) -> PushNo
// Decrypt the data using the key // Decrypt the data using the key
let decryptedData = try ChaChaPoly.open(sealedBox, using: SymmetricKey(data: key)) let decryptedData = try ChaChaPoly.open(sealedBox, using: SymmetricKey(data: key))
// Here you can determine the PushKind based on the decrypted message // Here you can determine the PushKind based on the decrypted message
return try PushNotification(serializedBytes: decryptedData) return try PushNotification(serializedBytes: decryptedData)
} catch { } catch {
NSLog("Decryption failed: \(error)") NSLog("Decryption failed: \(error)")
@ -228,10 +233,10 @@ func getPushNotificationText(pushNotification: PushNotification) -> String {
.response: "has responded.", .response: "has responded.",
] ]
} }
var content = pushNotificationText[pushNotification.kind] ?? ""; var content = pushNotificationText[pushNotification.kind] ?? ""
if (pushNotification.hasReactionContent) { if pushNotification.hasReactionContent {
content.replace("{{reaction}}", with: pushNotification.reactionContent) content.replace("{{reaction}}", with: pushNotification.reactionContent)
} }