damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 901a6fc98fc1046b52ab0d99f088b00c0137cc93
parent a0f6bdd8d9362ba95396cb8943af4683803835b8
Author: Daniel D’Aquino <daniel@daquino.me>
Date:   Mon, 20 May 2024 14:55:17 -0700

Revoke device token when user switches to local notification mode

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Reviewed-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Models/PushNotificationClient.swift | 38+++++++++++++++++++++++++++++++++++++-
Mdamus/Util/Constants.swift | 2++
Mdamus/Views/Settings/NotificationSettingsView.swift | 4++++
3 files changed, 43 insertions(+), 1 deletion(-)

diff --git 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 @@ -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 @@ -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) + } } }