Skip to content

Commit acc3e5a

Browse files
committed
feat(webpush): Add webpush operations
Signed-off-by: sim <git@sgougeon.fr>
1 parent fef0e1c commit acc3e5a

5 files changed

Lines changed: 256 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2025 Your Name <your@email.com>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.owncloud.android.lib.resources.notifications
9+
10+
import com.nextcloud.common.JSONRequestBody
11+
import com.nextcloud.common.NextcloudClient
12+
import com.nextcloud.operations.PostMethod
13+
import com.owncloud.android.lib.common.operations.RemoteOperation
14+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
15+
import com.owncloud.android.lib.common.utils.Log_OC
16+
import org.apache.commons.httpclient.util.HttpURLConnection
17+
18+
class ActivateWebPushRegistrationOperation(
19+
val activationToken: String
20+
): RemoteOperation<Void>() {
21+
22+
override fun run(client: NextcloudClient): RemoteOperationResult<Void> {
23+
var result: RemoteOperationResult<Void>
24+
var post: PostMethod? = null
25+
try {
26+
val body = JSONRequestBody(ACTIVATION_TOKEN, activationToken)
27+
post = PostMethod("${client.baseUri}$OCS_ROUTE", true, body.get())
28+
29+
val status = client.execute(post)
30+
val response = post.getResponseBodyAsString()
31+
when (status) {
32+
HttpURLConnection.HTTP_ACCEPTED -> {
33+
Log_OC.d(TAG, "Web push registration activated (status=202)")
34+
result = RemoteOperationResult(true, post)
35+
}
36+
HttpURLConnection.HTTP_OK -> {
37+
Log_OC.d(TAG, "Web push registration already activated (status=200)")
38+
result = RemoteOperationResult(true, post)
39+
}
40+
else -> {
41+
Log_OC.d(TAG, "Cannot activate web push registration (status=$status): $response")
42+
result = RemoteOperationResult(false, post)
43+
}
44+
}
45+
} catch (e: Exception) {
46+
result = RemoteOperationResult(e)
47+
Log_OC.e(TAG, "Exception while activating web push registration", e)
48+
} finally {
49+
post?.releaseConnection()
50+
}
51+
return result
52+
}
53+
54+
companion object {
55+
// OCS Route
56+
private const val OCS_ROUTE = "/ocs/v2.php/apps/notifications/api/v2/webpush/activate"
57+
private const val ACTIVATION_TOKEN = "activationToken"
58+
59+
private val TAG = ActivateWebPushRegistrationOperation::class.java.getSimpleName()
60+
}
61+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2025 Your Name <your@email.com>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.owncloud.android.lib.resources.notifications
9+
10+
import com.nextcloud.common.NextcloudClient
11+
import com.nextcloud.operations.GetMethod
12+
import com.owncloud.android.lib.common.operations.RemoteOperation
13+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
14+
import com.owncloud.android.lib.common.utils.Log_OC
15+
import com.owncloud.android.lib.resources.notifications.models.VapidResponse
16+
import org.json.JSONObject
17+
18+
class GetVAPIDOperation(): RemoteOperation<VapidResponse>() {
19+
20+
override fun run(client: NextcloudClient): RemoteOperationResult<VapidResponse> {
21+
var result: RemoteOperationResult<VapidResponse>
22+
var get: GetMethod? = null
23+
try {
24+
get = GetMethod("${client.baseUri}$OCS_ROUTE", true)
25+
26+
val status = client.execute(get)
27+
val response = get.getResponseBodyAsString()
28+
29+
if (get.isSuccess()) {
30+
result = RemoteOperationResult(true, get)
31+
val vapid = JSONObject(response)
32+
.getJSONObject(OCS)
33+
.getJSONObject(DATA)
34+
.getString(VAPID)
35+
result.resultData = VapidResponse(vapid)
36+
Log_OC.d(TAG, "VAPID key found: $vapid")
37+
} else {
38+
Log_OC.e(TAG, "Failed getting VAPID key (status=$status): $response")
39+
result = RemoteOperationResult(false, get)
40+
}
41+
} catch (e: Exception) {
42+
result = RemoteOperationResult(e)
43+
Log_OC.e(TAG, "Exception while getting VAPID key", e)
44+
} finally {
45+
get?.releaseConnection()
46+
}
47+
return result
48+
}
49+
50+
companion object {
51+
// OCS Route
52+
private const val OCS_ROUTE = "/ocs/v2.php/apps/notifications/api/v2/webpush/vapid$JSON_FORMAT"
53+
private const val OCS = "ocs"
54+
private const val DATA = "data"
55+
private const val VAPID = "vapid"
56+
57+
private val TAG = GetVAPIDOperation::class.java.getSimpleName()
58+
}
59+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2025 Your Name <your@email.com>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.owncloud.android.lib.resources.notifications
9+
10+
import com.nextcloud.common.JSONRequestBody
11+
import com.nextcloud.common.NextcloudClient
12+
import com.nextcloud.operations.PostMethod
13+
import com.owncloud.android.lib.common.operations.RemoteOperation
14+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
15+
import com.owncloud.android.lib.common.utils.Log_OC
16+
import org.apache.commons.httpclient.util.HttpURLConnection
17+
18+
class RegisterAccountDeviceForWebPushOperation(
19+
val endpoint: String,
20+
val auth: String,
21+
val uaPublicKey: String,
22+
val apptypes: List<String>
23+
): RemoteOperation<Void>() {
24+
25+
override fun run(client: NextcloudClient): RemoteOperationResult<Void> {
26+
var result: RemoteOperationResult<Void>
27+
var post: PostMethod? = null
28+
try {
29+
val body = JSONRequestBody(ENDPOINT, endpoint)
30+
body.put(AUTH, auth)
31+
body.put(UA_PUBLIC_KEY, uaPublicKey)
32+
body.put(APPTYPES, apptypes.joinToString(","))
33+
post = PostMethod("${client.baseUri}$OCS_ROUTE", true, body.get())
34+
35+
val status = client.execute(post)
36+
val response = post.getResponseBodyAsString()
37+
when (status) {
38+
HttpURLConnection.HTTP_CREATED -> {
39+
Log_OC.d(TAG, "New web push registration created (status=201)")
40+
result = RemoteOperationResult(true, post)
41+
}
42+
HttpURLConnection.HTTP_OK -> {
43+
Log_OC.d(TAG, "Web push registration already activated (status=200)")
44+
result = RemoteOperationResult(true, post)
45+
}
46+
else -> {
47+
Log_OC.e(TAG, "Web push registration refused (status=$status): $response")
48+
result = RemoteOperationResult(false, post)
49+
}
50+
}
51+
} catch (e: Exception) {
52+
result = RemoteOperationResult(e)
53+
Log_OC.e(TAG, "Exception while registering web push", e)
54+
} finally {
55+
post?.releaseConnection()
56+
}
57+
return result
58+
}
59+
60+
companion object {
61+
// OCS Route
62+
private const val OCS_ROUTE = "/ocs/v2.php/apps/notifications/api/v2/webpush"
63+
private const val ENDPOINT = "endpoint"
64+
private const val AUTH = "auth"
65+
private const val UA_PUBLIC_KEY = "uaPublicKey"
66+
private const val APPTYPES = "apptypes"
67+
68+
private val TAG = RegisterAccountDeviceForWebPushOperation::class.java.getSimpleName()
69+
}
70+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2025 Your Name <your@email.com>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.owncloud.android.lib.resources.notifications
9+
10+
import com.nextcloud.common.NextcloudClient
11+
import com.nextcloud.operations.DeleteMethod
12+
import com.owncloud.android.lib.common.operations.RemoteOperation
13+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
14+
import com.owncloud.android.lib.common.utils.Log_OC
15+
import org.apache.commons.httpclient.util.HttpURLConnection
16+
17+
class UnregisterAccountDeviceForWebPushOperation(): RemoteOperation<Void>() {
18+
19+
override fun run(client: NextcloudClient): RemoteOperationResult<Void> {
20+
var result: RemoteOperationResult<Void>
21+
var delete: DeleteMethod? = null
22+
try {
23+
delete = DeleteMethod("${client.baseUri}$OCS_ROUTE", true)
24+
25+
val status = client.execute(delete)
26+
val response = delete.getResponseBodyAsString()
27+
when (status) {
28+
HttpURLConnection.HTTP_ACCEPTED -> {
29+
Log_OC.d(TAG, "Web push registration deleted (status=202)")
30+
result = RemoteOperationResult(true, delete)
31+
}
32+
HttpURLConnection.HTTP_OK -> {
33+
Log_OC.d(TAG, "Web push registration already deleted (status=200)")
34+
result = RemoteOperationResult(true, delete)
35+
}
36+
else -> {
37+
Log_OC.e(TAG, "Web push registration refused (status=$status): $response")
38+
result = RemoteOperationResult(false, delete)
39+
}
40+
}
41+
} catch (e: Exception) {
42+
result = RemoteOperationResult(e)
43+
Log_OC.e(TAG, "Exception while registering web push", e)
44+
} finally {
45+
delete?.releaseConnection()
46+
}
47+
return result
48+
}
49+
50+
companion object {
51+
// OCS Route
52+
private const val OCS_ROUTE = "/ocs/v2.php/apps/notifications/api/v2/webpush"
53+
54+
private val TAG = UnregisterAccountDeviceForWebPushOperation::class.java.getSimpleName()
55+
}
56+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2025 Your Name <your@email.com>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.owncloud.android.lib.resources.notifications.models
9+
10+
data class VapidResponse(val vapid: String)

0 commit comments

Comments
 (0)