diff --git a/damus/Models/PushNotificationClient.swift b/damus/Models/PushNotificationClient.swift index ed3fad9a..13e75758 100644 --- a/damus/Models/PushNotificationClient.swift +++ b/damus/Models/PushNotificationClient.swift @@ -57,7 +57,43 @@ struct PushNotificationClient { return } - + func revoke_token() async throws { + guard let device_token else { return } + // Send the device token and pubkey to the server + let token = device_token.map { String(format: "%02.2hhx", $0) }.joined() + + Log.info("Revoking device token from server: %s", for: .push_notifications, token) + + let pubkey = self.keypair.pubkey + + // Send those as JSON to the server + let json: [String: Any] = ["deviceToken": token, "pubkey": pubkey.hex()] + + // create post request + let url = self.settings.send_device_token_to_localhost ? Constants.DEVICE_TOKEN_REVOKER_TEST_URL : Constants.DEVICE_TOKEN_REVOKER_PRODUCTION_URL + let json_data = try JSONSerialization.data(withJSONObject: json) + + + let (data, response) = try await make_nip98_authenticated_request( + method: .post, + url: url, + payload: json_data, + payload_type: .json, + auth_keypair: self.keypair + ) + + if let httpResponse = response as? HTTPURLResponse { + switch httpResponse.statusCode { + case 200: + Log.info("Sent device token removal request to Damus push notification server successfully", for: .push_notifications) + default: + Log.error("Error in sending device_token removal to Damus push notification server. HTTP status code: %d; Response: %s", for: .push_notifications, httpResponse.statusCode, String(data: data, encoding: .utf8) ?? "Unknown") + throw ClientError.http_response_error(status_code: httpResponse.statusCode, response: data) + } + } + + return + } } // MARK: Helper structures diff --git a/damus/Util/Constants.swift b/damus/Util/Constants.swift index 7e2207d4..a72ed0c3 100644 --- a/damus/Util/Constants.swift +++ b/damus/Util/Constants.swift @@ -12,6 +12,8 @@ class Constants { static let DAMUS_APP_GROUP_IDENTIFIER: String = "group.com.damus" static let DEVICE_TOKEN_RECEIVER_PRODUCTION_URL: URL = URL(string: "https://notify.damus.io:8000/user-info")! static let DEVICE_TOKEN_RECEIVER_TEST_URL: URL = URL(string: "http://localhost:8000/user-info")! + static let DEVICE_TOKEN_REVOKER_PRODUCTION_URL: URL = URL(string: "https://notify.damus.io:8000/user-info/remove")! + static let DEVICE_TOKEN_REVOKER_TEST_URL: URL = URL(string: "http://localhost:8000/user-info/remove")! static let MAIN_APP_BUNDLE_IDENTIFIER: String = "com.jb55.damus2" static let NOTIFICATION_EXTENSION_BUNDLE_IDENTIFIER: String = "com.jb55.damus2.DamusNotificationService" diff --git a/damus/Views/Settings/NotificationSettingsView.swift b/damus/Views/Settings/NotificationSettingsView.swift index 0767fbe9..7af78d9a 100644 --- a/damus/Views/Settings/NotificationSettingsView.swift +++ b/damus/Views/Settings/NotificationSettingsView.swift @@ -36,12 +36,16 @@ struct NotificationSettingsView: View { if newValue == .push { Task { try await damus_state.push_notification_client.send_token() } } + else { + Task { try await damus_state.push_notification_client.revoke_token() } + } } ) ) { ForEach(UserSettingsStore.NotificationsMode.allCases, id: \.self) { notification_mode in Text(notification_mode.text_description()) .tag(notification_mode.rawValue) + } } }