Skip to content

Commit d697b11

Browse files
notification to v2
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
1 parent 158c51e commit d697b11

3 files changed

Lines changed: 62 additions & 20 deletions

File tree

.drone.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ services:
124124
- su www-data -c 'php /var/www/html/occ config:app:set external sites --value="{\"1\":{\"id\":1,\"name\":\"Nextcloud\",\"url\":\"https:\/\/www.nextcloud.com\",\"lang\":\"\",\"type\":\"link\",\"device\":\"\",\"icon\":\"external.svg\",\"groups\":[],\"redirect\":false},\"2\":{\"id\":2,\"name\":\"Forum\",\"url\":\"https:\/\/help.nextcloud.com\",\"lang\":\"\",\"type\":\"link\",\"device\":\"\",\"icon\":\"external.svg\",\"groups\":[],\"redirect\":false}}"'
125125
- su www-data -c "git clone -b enh/collaborative-lock https://github.com/nextcloud/files_lock.git /var/www/html/apps/files_lock/"
126126
- su www-data -c "php /var/www/html/occ app:enable -f files_lock"
127+
- su www-data -c "git clone -b master https://github.com/nextcloud/notifications.git /var/www/html/apps/notifications/"
128+
- su www-data -c "php /var/www/html/occ app:enable notifications"
129+
- su www-data -c "php /var/www/html/occ notification:generate 'test'"
127130
- /usr/local/bin/run.sh
128131

129132
trigger:
@@ -215,6 +218,8 @@ services:
215218
- su www-data -c "php /var/www/html/occ app:enable password_policy"
216219
- su www-data -c "php /var/www/html/occ app:enable external"
217220
- su www-data -c 'php /var/www/html/occ config:app:set external sites --value="{\"1\":{\"id\":1,\"name\":\"Nextcloud\",\"url\":\"https:\/\/www.nextcloud.com\",\"lang\":\"\",\"type\":\"link\",\"device\":\"\",\"icon\":\"external.svg\",\"groups\":[],\"redirect\":false},\"2\":{\"id\":2,\"name\":\"Forum\",\"url\":\"https:\/\/help.nextcloud.com\",\"lang\":\"\",\"type\":\"link\",\"device\":\"\",\"icon\":\"external.svg\",\"groups\":[],\"redirect\":false}}"'
221+
- su www-data -c "php /var/www/html/occ app:enable notifications"
222+
- su www-data -c "php /var/www/html/occ notification:generate 'test'"
218223
- /usr/local/bin/run.sh
219224

220225
trigger:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Tobias Kaminsky
5+
* Copyright (C) 2022 Tobias Kaminsky
6+
* Copyright (C) 2022 Nextcloud GmbH
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
package com.owncloud.android.lib.resources.notifications
23+
24+
import com.owncloud.android.AbstractIT
25+
import junit.framework.Assert.assertTrue
26+
import org.junit.Test
27+
28+
class GetNotificationsRemoteOperationIT : AbstractIT() {
29+
@Test
30+
fun testNotifications() {
31+
val result = nextcloudClient.execute(GetNotificationsRemoteOperation())
32+
assertTrue(result.isSuccess)
33+
34+
val notifications = result.resultData
35+
36+
assertTrue(notifications.isNotEmpty())
37+
}
38+
}

library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@
3434
import com.google.gson.JsonObject;
3535
import com.google.gson.JsonParser;
3636
import com.google.gson.reflect.TypeToken;
37-
import com.owncloud.android.lib.common.OwnCloudClient;
37+
import com.nextcloud.common.NextcloudClient;
38+
import com.nextcloud.operations.GetMethod;
3839
import com.owncloud.android.lib.common.operations.RemoteOperation;
3940
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
4041
import com.owncloud.android.lib.common.utils.Log_OC;
4142
import com.owncloud.android.lib.resources.notifications.models.Notification;
4243

4344
import org.apache.commons.httpclient.HttpStatus;
44-
import org.apache.commons.httpclient.methods.GetMethod;
45-
import org.json.JSONException;
4645

4746
import java.lang.reflect.Type;
47+
import java.util.HashMap;
4848
import java.util.List;
4949

5050
/**
5151
* Provides the remote notifications from the server handling the following data structure
5252
* accessible via the notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at
5353
* {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}.
5454
*/
55-
public class GetNotificationsRemoteOperation extends RemoteOperation {
55+
public class GetNotificationsRemoteOperation extends RemoteOperation<List<Notification>> {
5656

5757
// OCS Route
5858
private static final String OCS_ROUTE_LIST_V12_AND_UP =
@@ -66,39 +66,38 @@ public class GetNotificationsRemoteOperation extends RemoteOperation {
6666
private static final String NODE_DATA = "data";
6767

6868
@Override
69-
protected RemoteOperationResult run(OwnCloudClient client) {
70-
RemoteOperationResult result = null;
71-
int status = -1;
69+
public RemoteOperationResult<List<Notification>> run(NextcloudClient client) {
70+
RemoteOperationResult<List<Notification>> result;
71+
int status;
7272
GetMethod get = null;
7373
List<Notification> notifications;
7474
String url = client.getBaseUri() + OCS_ROUTE_LIST_V12_AND_UP;
7575

7676
// get the notifications
7777
try {
78-
get = new GetMethod(url);
79-
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
78+
get = new GetMethod(url, true);
79+
HashMap<String, String> map = new HashMap<>();
80+
map.put("format", "json");
81+
82+
get.setQueryString(map);
83+
status = client.execute(get);
8084

81-
status = client.executeMethod(get);
8285
String response = get.getResponseBodyAsString();
8386

8487
if (isSuccess(status)) {
85-
result = new RemoteOperationResult(true, status, get.getResponseHeaders());
88+
result = new RemoteOperationResult<>(true, get);
8689
Log_OC.d(TAG, "Successful response: " + response);
8790

8891
// Parse the response
8992
notifications = parseResult(response);
90-
result.setNotificationData(notifications);
93+
result.setResultData(notifications);
9194
} else {
92-
result = new RemoteOperationResult(false, status, get.getResponseHeaders());
95+
result = new RemoteOperationResult<>(false, get);
9396
Log_OC.e(TAG, "Failed response while getting user notifications ");
94-
if (response != null) {
95-
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
96-
} else {
97-
Log_OC.e(TAG, "*** status code: " + status);
98-
}
97+
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
9998
}
10099
} catch (Exception e) {
101-
result = new RemoteOperationResult(e);
100+
result = new RemoteOperationResult<>(e);
102101
Log_OC.e(TAG, "Exception while getting remote notifications", e);
103102
} finally {
104103
if (get != null) {
@@ -109,7 +108,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
109108
return result;
110109
}
111110

112-
private List<Notification> parseResult(String response) throws JSONException {
111+
private List<Notification> parseResult(String response) {
113112
JsonParser jsonParser = new JsonParser();
114113
JsonObject jo = (JsonObject)jsonParser.parse(response);
115114
JsonArray jsonDataArray = jo.getAsJsonObject(NODE_OCS).getAsJsonArray(NODE_DATA);

0 commit comments

Comments
 (0)