Skip to content

Commit

Permalink
V2 with activity
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
  • Loading branch information
tobiasKaminsky committed Nov 29, 2019
1 parent c7baf2b commit e124dca
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2019 Tobias Kaminsky
* Copyright (C) 2019 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

package com.nextcloud.lib.resources.users

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.activities.GetActivitiesRemoteOperation
import com.owncloud.android.lib.resources.activities.model.Activity
import org.junit.Assert.assertTrue
import org.junit.Test
import java.util.*

class GetActivitiesRemoteOperationTest : AbstractIT() {
@Test
fun getActivities() {
val result = GetActivitiesRemoteOperation().execute(nextcloudClient)
assertTrue(result.isSuccess)

val activities = result.data[0] as ArrayList<Activity>
val lastGiven = result.data[1] as Integer;

assertTrue(activities.isNotEmpty())
assertTrue(lastGiven > 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ package com.nextcloud.lib.resources.users

import androidx.test.platform.app.InstrumentationRegistry
import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.common.OwnCloudBasicCredentials
import com.owncloud.android.lib.common.UserInfo
import com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation
import okhttp3.Credentials
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
Expand All @@ -40,27 +40,27 @@ import org.junit.Test
class GetUserInfoRemoteOperationTest : AbstractIT() {
@Test
fun testGetUserNoQuota() {
nextcloudClient.credentials = Credentials.basic("user1", "user1")
val userInfoResult = GetUserInfoRemoteOperation().execute(nextcloudClient)
client.credentials = OwnCloudBasicCredentials("user1", "user1")
val userInfoResult = GetUserInfoRemoteOperation().execute(client)
assertTrue(userInfoResult.isSuccess)

val userInfo = userInfoResult.data[0] as UserInfo
assertEquals("User One", userInfo.displayName)
assertEquals("user1", userInfo.id)

assertEquals("User One", userInfo.getDisplayName())
assertEquals("user1", userInfo.getId())
assertEquals(GetUserInfoRemoteOperation.SPACE_UNLIMITED,
userInfo.quota?.quota)
userInfo.getQuota().getQuota())
}

@Test
fun testGetUser1GbQuota() {
nextcloudClient.credentials = Credentials.basic("user2", "user2")
val userInfoResult = GetUserInfoRemoteOperation().execute(nextcloudClient)
client.credentials = OwnCloudBasicCredentials("user2", "user2")
val userInfoResult = GetUserInfoRemoteOperation().execute(client)
assertTrue(userInfoResult.isSuccess)

val userInfo = userInfoResult.data[0] as UserInfo
assertEquals("User Two", userInfo.displayName)
assertEquals("user2", userInfo.id)
assertEquals(1073741824L, userInfo.quota?.quota)

assertEquals("User Two", userInfo.getDisplayName())
assertEquals("user2", userInfo.getId())
assertEquals(1073741824, userInfo.getQuota().getQuota())
}

@After
Expand All @@ -70,6 +70,6 @@ class GetUserInfoRemoteOperationTest : AbstractIT() {
val loginName = arguments.getString("TEST_SERVER_USERNAME")
val password = arguments.getString("TEST_SERVER_PASSWORD")

nextcloudClient.credentials = Credentials.basic(loginName, password)
client.credentials = OwnCloudBasicCredentials(loginName, password)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.GetMethod;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
Expand All @@ -47,11 +49,11 @@
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
Expand Down Expand Up @@ -93,12 +95,80 @@ public GetActivitiesRemoteOperation(int lastGiven) {
}

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
protected RemoteOperationResult run(NextcloudClient client) {
RemoteOperationResult result;
int status;
GetMethod get = null;
ArrayList<Activity> activities;
String url = client.getBaseUri() + OCS_ROUTE_V12_AND_UP;

// add filter for fileId, if available
if (!fileId.isEmpty()) {
url = url + "/filter";
}

Log_OC.d(TAG, "URL: " + url);

try {
get = new GetMethod(url, true);

HashMap<String, String> parameters = new HashMap<>();
parameters.put("format", "json");
parameters.put("previews", "true");

if (lastGiven != -1) {
parameters.put("since", String.valueOf(lastGiven));
}

if (!fileId.isEmpty()) {
parameters.put("sort", "desc");
parameters.put("object_type", "files");
parameters.put("object_id", fileId);
}

get.setQueryString(parameters);

status = client.execute(get);
String response = get.getResponseBodyAsString();

String nextPageHeader = get.response.header("X-Activity-Last-Given");
if (nextPageHeader != null) {
lastGiven = Integer.parseInt(nextPageHeader);
} else {
lastGiven = -1;
}

if (isSuccess(status)) {
Log_OC.d(TAG, "Successful response: " + response);
result = new RemoteOperationResult(true, get);
// Parse the response
activities = parseResult(response);

ArrayList<Object> data = new ArrayList<>();
data.add(activities);
data.add(lastGiven);
result.setData(data);
} else {
result = new RemoteOperationResult(false, get);
Log_OC.e(TAG, "Failed response while getting user activities");
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
}
} finally {
if (get != null) {
get.releaseConnection();
}
}

return result;
}

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
int status;
org.apache.commons.httpclient.methods.GetMethod get = null;
ArrayList<Activity> activities;
String url = client.getBaseUri() + OCS_ROUTE_V12_AND_UP;

// add filter for fileId, if available
if (!fileId.isEmpty()) {
Expand All @@ -108,7 +178,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
Log_OC.d(TAG, "URL: " + url);

try {
get = new GetMethod(url);
get = new org.apache.commons.httpclient.methods.GetMethod(url);
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

ArrayList<NameValuePair> parameters = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
package com.owncloud.android.lib.resources.users;

import com.google.gson.reflect.TypeToken;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.GetMethod;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.Quota;
import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
Expand All @@ -38,9 +37,10 @@
import com.owncloud.android.lib.resources.OCSRemoteOperation;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;

import java.util.ArrayList;
import java.util.HashMap;

/**
* Gets information (id, display name, and e-mail address and many other things) about the user logged in.
Expand All @@ -54,7 +54,7 @@ public class GetUserInfoRemoteOperation extends OCSRemoteOperation {
private static final String TAG = GetUserInfoRemoteOperation.class.getSimpleName();

// OCS Route
private static final String OCS_ROUTE_SELF = "/ocs/v2.php/cloud/user";
private static final String OCS_ROUTE_SELF = "/ocs/v1.php/cloud/user";

/**
* Quota return value for a not computed space value.
Expand All @@ -77,7 +77,7 @@ public class GetUserInfoRemoteOperation extends OCSRemoteOperation {
public static final long QUOTA_LIMIT_INFO_NOT_AVAILABLE = Long.MIN_VALUE;

@Override
protected RemoteOperationResult run(NextcloudClient client) {
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
int status;
GetMethod get = null;
Expand All @@ -87,17 +87,14 @@ protected RemoteOperationResult run(NextcloudClient client) {
// get the user
try {

get = new GetMethod(url, true);
HashMap<String, String> map = new HashMap<>();
map.put("format", "json");

get.setQueryString(map);
status = client.execute(get);
get = new GetMethod(url);
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
get.setQueryString(new NameValuePair[]{new NameValuePair("format", "json")});
status = client.executeMethod(get);

if (isSuccess(status)) {
ServerResponse<UserInfo> ocsResponse = getServerResponse(get,
new TypeToken<ServerResponse<UserInfo>>() {
});
ServerResponse<UserInfo> ocsResponse = getServerResponse(get, new TypeToken<ServerResponse<UserInfo>>() {
});

UserInfo userInfo = ocsResponse.getOcs().getData();

Expand All @@ -118,11 +115,11 @@ protected RemoteOperationResult run(NextcloudClient client) {
} else {
result = new RemoteOperationResult(false, get);
String response = get.getResponseBodyAsString();
Log_OC.e(TAG, "Failed response while getting user information");
if (response.isEmpty()) {
Log_OC.e(TAG, "*** status code: " + status);
Log_OC.e(TAG, "Failed response while getting user information ");
if (response != null) {
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
} else {
Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response);
Log_OC.e(TAG, "*** status code: " + status);
}
}
} catch (Exception e) {
Expand Down

0 comments on commit e124dca

Please sign in to comment.