Skip to content

Commit

Permalink
Merge pull request #1295 from nextcloud/depocc/comments
Browse files Browse the repository at this point in the history
Deprecate OwncloudClient - Comments
  • Loading branch information
tobiasKaminsky authored Jun 12, 2024
2 parents 354911a + 3b672c1 commit 8471187
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CommentFileRemoteOperationIT : AbstractIT() {

assertTrue(
CommentFileRemoteOperation("test", remoteFile.localId)
.execute(client)
.execute(nextcloudClient)
.isSuccess
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,22 @@

import android.util.Log;

import com.google.gson.GsonBuilder;
import com.owncloud.android.lib.common.OwnCloudClient;
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 org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.Utf8PostMethod;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* Comment file
*/
public class CommentFileRemoteOperation extends RemoteOperation {
public class CommentFileRemoteOperation extends RemoteOperation<Void> {

private static final String TAG = CommentFileRemoteOperation.class.getSimpleName();
private static final int POST_READ_TIMEOUT = 30000;
private static final int POST_CONNECTION_TIMEOUT = 5000;

private static final String ACTOR_ID = "actorId";
private static final String ACTOR_TYPE = "actorType";
private static final String ACTOR_TYPE_VALUE = "users";
Expand All @@ -57,32 +51,27 @@ public CommentFileRemoteOperation(String message, long fileId) {
* @param client Client object to communicate with the remote ownCloud server.
*/
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
public RemoteOperationResult<Void> run(NextcloudClient client) {

Utf8PostMethod postMethod = null;
RemoteOperationResult result;
PostMethod postMethod = null;
RemoteOperationResult<Void> result;
try {
String url = client.getCommentsUri(fileId);
postMethod = new Utf8PostMethod(url);
postMethod.addRequestHeader("Content-type", "application/json");

Map<String, String> values = new HashMap<>();
values.put(ACTOR_ID, client.getUserId());
values.put(ACTOR_TYPE, ACTOR_TYPE_VALUE);
values.put(VERB, VERB_VALUE);
values.put(MESSAGE, message);

String json = new GsonBuilder().create().toJson(values, Map.class);
// request body
JSONRequestBody jsonRequestBody = new JSONRequestBody(ACTOR_ID, client.getUserId());
jsonRequestBody.put(ACTOR_TYPE, ACTOR_TYPE_VALUE);
jsonRequestBody.put(VERB, VERB_VALUE);
jsonRequestBody.put(MESSAGE, message);

postMethod.setRequestEntity(new StringRequestEntity(json));
// post method
String url = client.getCommentsUri(fileId);
postMethod = new PostMethod(url, false, jsonRequestBody.get());

int status = client.executeMethod(postMethod, POST_READ_TIMEOUT, POST_CONNECTION_TIMEOUT);
int status = client.execute(postMethod);

result = new RemoteOperationResult(isSuccess(status), postMethod);
result = new RemoteOperationResult<>(status == HttpStatus.SC_CREATED, postMethod);

client.exhaustResponse(postMethod.getResponseBodyAsStream());
} catch (IOException e) {
result = new RemoteOperationResult(e);
result = new RemoteOperationResult<>(e);
Log.e(TAG, "Post comment to file with id " + fileId + " failed: " + result.getLogMessage(), e);
} finally {
if (postMethod != null) {
Expand All @@ -92,8 +81,4 @@ protected RemoteOperationResult run(OwnCloudClient client) {

return result;
}

private boolean isSuccess(int status) {
return status == HttpStatus.SC_CREATED;
}
}

0 comments on commit 8471187

Please sign in to comment.