Skip to content
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

notification to v2 #453

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
8 changes: 4 additions & 4 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ services:
- su www-data -c "php /var/www/html/occ app:enable -f groupfolders"
- su www-data -c "php /var/www/html/occ groupfolders:create groupfolder"
- su www-data -c "php /var/www/html/occ groupfolders:group 1 users"
- su www-data -c "git clone --depth 1 https://github.com/nextcloud/notifications.git /var/www/html/apps/notifications/"
- su www-data -c "git clone --depth 1 -b master https://github.com/nextcloud/notifications.git /var/www/html/apps/notifications/"
- su www-data -c "php /var/www/html/occ app:enable -f notifications"
- su www-data -c "php /var/www/html/occ notification:generate test -d test"
- su www-data -c "php /var/www/html/occ notification:generate 'test' 'test message'"
- su www-data -c "git clone --depth 1 https://github.com/nextcloud/photos.git /var/www/html/apps/photos/"
- su www-data -c "cd /var/www/html/apps/photos; composer install --no-dev"
- su www-data -c "php /var/www/html/occ app:enable -f photos"
Expand Down Expand Up @@ -216,7 +216,7 @@ services:
- su www-data -c "php /var/www/html/occ groupfolders:group 1 users"
- su www-data -c "git clone --depth 1 -b $SERVER_VERSION https://github.com/nextcloud/notifications.git /var/www/html/apps/notifications/"
- su www-data -c "php /var/www/html/occ app:enable -f notifications"
- su www-data -c "php /var/www/html/occ notification:generate test test"
- su www-data -c "php /var/www/html/occ notification:generate 'test' 'test message'"
- su www-data -c "git clone --depth 1 -b $SERVER_VERSION https://github.com/nextcloud/photos.git /var/www/html/apps/photos/"
- su www-data -c "cd /var/www/html/apps/photos; composer install --no-dev"
- su www-data -c "php /var/www/html/occ app:enable -f photos"
Expand All @@ -235,6 +235,6 @@ trigger:
- pull_request
---
kind: signature
hmac: 3e71a44f6f57a4d4d853c586c0c322bf0b718d96627906b92864e12353e5a014
hmac: 889fb47b6c7f4ba0f7b0297f697e29152099b18b6f5073ad02a503ab9046fbcd

...
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public abstract class AbstractIT {
public static OwnCloudClient client;
public static OwnCloudClient client2;
protected static NextcloudClient nextcloudClient;
protected static NextcloudClient nextcloudClientAdmin;
protected static Context context;
protected static Uri url;

Expand Down Expand Up @@ -109,6 +110,12 @@ public static void beforeAll() throws InterruptedException,
String credentials = Credentials.basic(loginName, password);
nextcloudClient = new NextcloudClient(url, userId, credentials, context);

nextcloudClientAdmin = new NextcloudClient(
url,
"admin",
Credentials.basic("admin", "admin"),
context);

waitForServer(client, url);
testConnection();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2022 Tobias Kaminsky <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.notifications

import com.owncloud.android.AbstractIT
import org.junit.Assert.assertTrue
import org.junit.Test

class DeleteAllNotificationsRemoteOperationIT : AbstractIT() {
@Test
fun testDeleteAllNotification() {
// create one notification
assertTrue(
nextcloudClientAdmin.execute(
CreateNotificationRemoteOperation(
nextcloudClient.userId,
"test"
)
).isSuccess
)

var result = nextcloudClient.execute(GetNotificationsRemoteOperation())
assertTrue(result.isSuccess)
assertTrue(result.resultData.isNotEmpty())

assertTrue(nextcloudClient.execute(DeleteAllNotificationsRemoteOperation()).isSuccess)

result = nextcloudClient.execute(GetNotificationsRemoteOperation())
assertTrue(result.isSuccess)
assertTrue(result.resultData.isEmpty())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2022 Tobias Kaminsky <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.notifications

import com.owncloud.android.AbstractIT
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class DeleteNotificationRemoteOperationIT : AbstractIT() {
@Test
fun deleteOneNotification() {
// create one notification
assertTrue(
nextcloudClientAdmin.execute(
CreateNotificationRemoteOperation(
nextcloudClient.userId,
"test"
)
).isSuccess
)

val result = nextcloudClient.execute(GetNotificationsRemoteOperation())
assertTrue(result.isSuccess)
assertTrue(result.resultData.isNotEmpty())

val firstNotificationId = result.resultData.first().notificationId

assertTrue(DeleteNotificationRemoteOperation(firstNotificationId).execute(nextcloudClient).isSuccess)

val getResult = GetNotificationRemoteOperation(firstNotificationId).execute(nextcloudClient)
assertFalse(getResult.isSuccess)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2022 Tobias Kaminsky <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.notifications

import com.owncloud.android.AbstractIT
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test

class GetNotificationRemoteOperationIT : AbstractIT() {
@Test
fun testSingleNotification() {
// create one notification
assertTrue(
nextcloudClientAdmin.execute(
CreateNotificationRemoteOperation(
nextcloudClient.userId,
"test"
)
).isSuccess
)

// get all notifications
val resultAll = nextcloudClient.execute(GetNotificationsRemoteOperation())
assertTrue(resultAll.isSuccess)

val allNotifications = resultAll.resultData
val firstNotificationId = allNotifications.first().notificationId

// check one specific
val result = nextcloudClient.execute(GetNotificationRemoteOperation(firstNotificationId))
assertTrue(result.isSuccess)

val notification = result.resultData
assertNotNull(notification)
assertEquals(firstNotificationId, notification.notificationId)
}

@Test
fun testNonExisting() {
assertFalse(GetNotificationRemoteOperation(-1).execute(nextcloudClient).isSuccess)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2022 Tobias Kaminsky <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.notifications

import com.owncloud.android.AbstractIT
import junit.framework.Assert.assertTrue
import org.junit.Assert
import org.junit.Test

class GetNotificationsRemoteOperationIT : AbstractIT() {
@Test
fun testNotifications() {
// create one notification
Assert.assertTrue(
nextcloudClientAdmin.execute(
CreateNotificationRemoteOperation(
nextcloudClient.userId,
"test"
)
).isSuccess
)

val result = nextcloudClient.execute(GetNotificationsRemoteOperation())
assertTrue(result.isSuccess)
assertTrue(result.resultData.isNotEmpty())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class NotificationIT : AbstractIT() {
val count = all.resultData.size

// get one
assertTrue(count > 0)
val firstNotification = all.resultData[0]
val first = GetNotificationRemoteOperation(firstNotification.notificationId).execute(nextcloudClient)
assertTrue(first.isSuccess)
Expand Down
7 changes: 4 additions & 3 deletions library/src/main/java/com/nextcloud/operations/PostMethod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* SPDX-FileCopyrightText: 2023 Tobias Kaminsky <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.nextcloud.operations

import com.nextcloud.common.OkHttpMethodBase
Expand All @@ -15,12 +16,12 @@ import okhttp3.RequestBody
* HTTP POST method that uses OkHttp with new NextcloudClient
* UTF8 by default
*/
class PostMethod(
open class PostMethod(
uri: String,
useOcsApiRequestHeader: Boolean,
val body: RequestBody
val body: RequestBody?
) : OkHttpMethodBase(uri, useOcsApiRequestHeader) {
override fun applyType(temp: Request.Builder) {
temp.post(body)
body?.let { temp.post(it) }
}
}
25 changes: 25 additions & 0 deletions library/src/main/java/com/nextcloud/operations/Utf8PostMethod.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2022 Tobias Kaminsky <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.nextcloud.operations

import okhttp3.Request
import okhttp3.RequestBody

/**
* HTTP POST method that uses OkHttp with new NextcloudClient
*/
class Utf8PostMethod(
uri: String,
useOcsApiRequestHeader: Boolean,
body: RequestBody?
) : PostMethod(uri, useOcsApiRequestHeader, body) {
override fun applyType(temp: Request.Builder) {
temp.addHeader("Content-Type", "charset=utf-8")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2022 Tobias Kaminsky <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.notifications

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 com.owncloud.android.lib.resources.notifications.models.Notification
import okhttp3.FormBody
import org.apache.commons.httpclient.HttpStatus
import java.io.IOException

/**
* Provides the remote notifications from the server handling the following data structure accessible via the
* notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at {@link
* "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}.
*/
class CreateNotificationRemoteOperation(private val userId: String, private val message: String) :
RemoteOperation<Notification>() {
override fun run(client: NextcloudClient): RemoteOperationResult<Notification> {
var result: RemoteOperationResult<Notification>
val status: Int
var post: PostMethod? = null
val url =
client.baseUri.toString() + "/ocs/v2.php/apps/notifications/api/v2/admin_notifications/"

try {
val bodyRequest =
FormBody
.Builder()
.add("shortMessage", message)
.build()

post = PostMethod(url + userId, true, bodyRequest)
status = client.execute(post)
val response = post.getResponseBodyAsString()
if (isSuccess(status)) {
result = RemoteOperationResult(true, post)
Log_OC.d(TAG, "Successful response: $response")
} else {
result = RemoteOperationResult(false, post)
Log_OC.e(TAG, "Failed response while getting user notifications ")
Log_OC.e(TAG, "*** status code: $status ; response message: $response")
}
} catch (e: IOException) {
result = RemoteOperationResult(e)
Log_OC.e(TAG, "Exception while getting remote notifications", e)
} finally {
post?.releaseConnection()
}
return result
}

private fun isSuccess(status: Int): Boolean {
return status == HttpStatus.SC_OK
}

companion object {
private val TAG = CreateNotificationRemoteOperation::class.java.simpleName
}
}
Loading
Loading