-
Notifications
You must be signed in to change notification settings - Fork 99
Add operations for web push #1949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
p1gp1g
wants to merge
1
commit into
nextcloud:master
Choose a base branch
from
p1gp1g:feat/webpush
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
.../com/owncloud/android/lib/resources/notifications/ActivateWebPushRegistrationOperation.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2026 Simon Gougeon <git@sgougeon.fr> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| package com.owncloud.android.lib.resources.notifications | ||
|
|
||
| import com.nextcloud.common.JSONRequestBody | ||
| import com.nextcloud.common.NextcloudClient | ||
| import com.nextcloud.operations.PostMethod | ||
| import com.owncloud.android.lib.common.operations.RemoteOperation | ||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||
| import com.owncloud.android.lib.common.utils.Log_OC | ||
| import org.apache.commons.httpclient.util.HttpURLConnection | ||
|
|
||
| class ActivateWebPushRegistrationOperation( | ||
| val activationToken: String | ||
| ) : RemoteOperation<Void>() { | ||
| @Suppress("TooGenericExceptionCaught") | ||
| override fun run(client: NextcloudClient): RemoteOperationResult<Void> { | ||
| var result: RemoteOperationResult<Void> | ||
| var post: PostMethod? = null | ||
| try { | ||
| val body = JSONRequestBody(ACTIVATION_TOKEN, activationToken) | ||
| post = PostMethod("${client.baseUri}$OCS_ROUTE", true, body.get()) | ||
|
|
||
| val status = client.execute(post) | ||
| val response = post.getResponseBodyAsString() | ||
| when (status) { | ||
| HttpURLConnection.HTTP_ACCEPTED -> { | ||
| Log_OC.d(TAG, "Web push registration activated (status=202)") | ||
| result = RemoteOperationResult(true, post) | ||
| } | ||
|
|
||
| HttpURLConnection.HTTP_OK -> { | ||
| Log_OC.d(TAG, "Web push registration already activated (status=200)") | ||
| result = RemoteOperationResult(true, post) | ||
| } | ||
|
|
||
| else -> { | ||
| Log_OC.d(TAG, "Cannot activate web push registration (status=$status): $response") | ||
| result = RemoteOperationResult(false, post) | ||
| } | ||
| } | ||
| } catch (e: Exception) { | ||
| result = RemoteOperationResult(e) | ||
| Log_OC.e(TAG, "Exception while activating web push registration", e) | ||
| } finally { | ||
| post?.releaseConnection() | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| companion object { | ||
| // OCS Route | ||
| private const val OCS_ROUTE = "/ocs/v2.php/apps/notifications/api/v2/webpush/activate" | ||
| private const val ACTIVATION_TOKEN = "activationToken" | ||
|
|
||
| private val TAG = ActivateWebPushRegistrationOperation::class.java.getSimpleName() | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
library/src/main/java/com/owncloud/android/lib/resources/notifications/GetVAPIDOperation.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2026 Simon Gougeon <git@sgougeon.fr> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| package com.owncloud.android.lib.resources.notifications | ||
|
|
||
| import com.nextcloud.common.NextcloudClient | ||
| import com.nextcloud.operations.GetMethod | ||
| import com.owncloud.android.lib.common.operations.RemoteOperation | ||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||
| import com.owncloud.android.lib.common.utils.Log_OC | ||
| import com.owncloud.android.lib.resources.notifications.models.VapidResponse | ||
| import org.json.JSONObject | ||
|
|
||
| class GetVAPIDOperation : RemoteOperation<VapidResponse>() { | ||
| @Suppress("TooGenericExceptionCaught") | ||
| override fun run(client: NextcloudClient): RemoteOperationResult<VapidResponse> { | ||
| var result: RemoteOperationResult<VapidResponse> | ||
| var get: GetMethod? = null | ||
| try { | ||
| get = GetMethod("${client.baseUri}$OCS_ROUTE", true) | ||
|
|
||
| val status = client.execute(get) | ||
| val response = get.getResponseBodyAsString() | ||
|
|
||
| if (get.isSuccess()) { | ||
| result = RemoteOperationResult(true, get) | ||
| val vapid = | ||
| JSONObject(response) | ||
| .getJSONObject(OCS) | ||
| .getJSONObject(DATA) | ||
| .getString(VAPID) | ||
| result.resultData = VapidResponse(vapid) | ||
| Log_OC.d(TAG, "VAPID key found: $vapid") | ||
| } else { | ||
| Log_OC.e(TAG, "Failed getting VAPID key (status=$status): $response") | ||
| result = RemoteOperationResult(false, get) | ||
| } | ||
| } catch (e: Exception) { | ||
| result = RemoteOperationResult(e) | ||
| Log_OC.e(TAG, "Exception while getting VAPID key", e) | ||
| } finally { | ||
| get?.releaseConnection() | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| companion object { | ||
| // OCS Route | ||
| private const val OCS_ROUTE = "/ocs/v2.php/apps/notifications/api/v2/webpush/vapid$JSON_FORMAT" | ||
| private const val OCS = "ocs" | ||
| private const val DATA = "data" | ||
| private const val VAPID = "vapid" | ||
|
|
||
| private val TAG = GetVAPIDOperation::class.java.getSimpleName() | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
.../owncloud/android/lib/resources/notifications/RegisterAccountDeviceForWebPushOperation.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2026 Simon Gougeon <git@sgougeon.fr> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| package com.owncloud.android.lib.resources.notifications | ||
|
|
||
| import com.nextcloud.common.JSONRequestBody | ||
| import com.nextcloud.common.NextcloudClient | ||
| import com.nextcloud.operations.PostMethod | ||
| import com.owncloud.android.lib.common.operations.RemoteOperation | ||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||
| import com.owncloud.android.lib.common.utils.Log_OC | ||
| import org.apache.commons.httpclient.util.HttpURLConnection | ||
|
|
||
| class RegisterAccountDeviceForWebPushOperation( | ||
| val endpoint: String, | ||
| val auth: String, | ||
| val uaPublicKey: String, | ||
| val appTypes: List<String> | ||
| ) : RemoteOperation<Void>() { | ||
| @Suppress("TooGenericExceptionCaught") | ||
| override fun run(client: NextcloudClient): RemoteOperationResult<Void> { | ||
| var result: RemoteOperationResult<Void> | ||
| var post: PostMethod? = null | ||
| try { | ||
| val body = JSONRequestBody(ENDPOINT, endpoint) | ||
| body.put(AUTH, auth) | ||
| body.put(UA_PUBLIC_KEY, uaPublicKey) | ||
| body.put(APPTYPES, appTypes.joinToString(",")) | ||
| post = PostMethod("${client.baseUri}$OCS_ROUTE", true, body.get()) | ||
|
|
||
| val status = client.execute(post) | ||
| val response = post.getResponseBodyAsString() | ||
| when (status) { | ||
| HttpURLConnection.HTTP_CREATED -> { | ||
| Log_OC.d(TAG, "New web push registration created (status=201)") | ||
| result = RemoteOperationResult(true, post) | ||
| } | ||
|
|
||
| HttpURLConnection.HTTP_OK -> { | ||
| Log_OC.d(TAG, "Web push registration already activated (status=200)") | ||
| result = RemoteOperationResult(true, post) | ||
| } | ||
|
|
||
| else -> { | ||
| Log_OC.e(TAG, "Web push registration refused (status=$status): $response") | ||
| result = RemoteOperationResult(false, post) | ||
| } | ||
| } | ||
| } catch (e: Exception) { | ||
| result = RemoteOperationResult(e) | ||
| Log_OC.e(TAG, "Exception while registering web push", e) | ||
| } finally { | ||
| post?.releaseConnection() | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| companion object { | ||
| // OCS Route | ||
| private const val OCS_ROUTE = "/ocs/v2.php/apps/notifications/api/v2/webpush" | ||
| private const val ENDPOINT = "endpoint" | ||
| private const val AUTH = "auth" | ||
| private const val UA_PUBLIC_KEY = "uaPublicKey" | ||
| private const val APPTYPES = "appTypes" | ||
|
|
||
| private val TAG = RegisterAccountDeviceForWebPushOperation::class.java.getSimpleName() | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
...wncloud/android/lib/resources/notifications/UnregisterAccountDeviceForWebPushOperation.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2026 Simon Gougeon <git@sgougeon.fr> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| package com.owncloud.android.lib.resources.notifications | ||
|
|
||
| import com.nextcloud.common.NextcloudClient | ||
| import com.nextcloud.operations.DeleteMethod | ||
| import com.owncloud.android.lib.common.operations.RemoteOperation | ||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||
| import com.owncloud.android.lib.common.utils.Log_OC | ||
| import org.apache.commons.httpclient.util.HttpURLConnection | ||
|
|
||
| class UnregisterAccountDeviceForWebPushOperation : RemoteOperation<Void>() { | ||
| @Suppress("TooGenericExceptionCaught") | ||
| override fun run(client: NextcloudClient): RemoteOperationResult<Void> { | ||
| var result: RemoteOperationResult<Void> | ||
| var delete: DeleteMethod? = null | ||
| try { | ||
| delete = DeleteMethod("${client.baseUri}$OCS_ROUTE", true) | ||
|
|
||
| val status = client.execute(delete) | ||
| val response = delete.getResponseBodyAsString() | ||
| when (status) { | ||
| HttpURLConnection.HTTP_ACCEPTED -> { | ||
| Log_OC.d(TAG, "Web push registration deleted (status=202)") | ||
| result = RemoteOperationResult(true, delete) | ||
| } | ||
|
|
||
| HttpURLConnection.HTTP_OK -> { | ||
| Log_OC.d(TAG, "Web push registration already deleted (status=200)") | ||
| result = RemoteOperationResult(true, delete) | ||
| } | ||
|
|
||
| else -> { | ||
| Log_OC.e(TAG, "Web push registration refused (status=$status): $response") | ||
| result = RemoteOperationResult(false, delete) | ||
| } | ||
| } | ||
| } catch (e: Exception) { | ||
| result = RemoteOperationResult(e) | ||
| Log_OC.e(TAG, "Exception while registering web push", e) | ||
| } finally { | ||
| delete?.releaseConnection() | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| companion object { | ||
| // OCS Route | ||
| private const val OCS_ROUTE = "/ocs/v2.php/apps/notifications/api/v2/webpush" | ||
|
|
||
| private val TAG = UnregisterAccountDeviceForWebPushOperation::class.java.getSimpleName() | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
...ry/src/main/java/com/owncloud/android/lib/resources/notifications/models/VapidResponse.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2026 Simon Gougeon <git@sgougeon.fr> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| package com.owncloud.android.lib.resources.notifications.models | ||
|
|
||
| data class VapidResponse( | ||
| val vapid: String | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.