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

Add feature/comment options #30

Merged
merged 5 commits into from Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,17 @@
package com.hapramp.steemconnect4j;

public class Beneficiary {
private String account;
private int weight;

public Beneficiary(String account, int weight) {
this.account = account;
this.weight = weight;
}

@Override
public String toString() {
return "{\"account\":\"" + account + "\""
+ ",\"weight\":" + weight + "}";
}
}
Expand Up @@ -2,6 +2,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Map;

public class SteemConnect {
Expand Down Expand Up @@ -184,8 +185,19 @@ public void vote(String voter, String author, String permlink, String weight,
* @param jsonMetaData jsonmetadata of comment
* @param steemConnectCallback callback for response
*/
public void comment(String parentAuthor, String parentPermlink, String author, String permlink,
String title, String body, String jsonMetaData, SteemConnectCallback steemConnectCallback) {
public void comment(String parentAuthor,
String parentPermlink,
String author,
String permlink,
String title,
String body,
String jsonMetaData,
String maxAcceptedPayout,
int percentSteemDollars,
boolean allowVotes,
boolean allowCurationRewards,
ArrayList<Beneficiary> beneficiaries,
SteemConnectCallback steemConnectCallback) {
String params = StringUtils.getCommanSeparatedObjectString(
RpcJsonUtil.getKeyValuePair("parent_author", "\"" + parentAuthor + "\""),
RpcJsonUtil.getKeyValuePair("parent_permlink", "\"" + parentPermlink + "\""),
Expand All @@ -195,9 +207,22 @@ public void comment(String parentAuthor, String parentPermlink, String author, S
RpcJsonUtil.getKeyValuePair("body", "\"" + body + "\""),
RpcJsonUtil.getKeyValuePair("json_metadata", "\"" + jsonMetaData + "\"")
);
String config = StringUtils.getCommentOptionStringFormat(
author,
permlink,
maxAcceptedPayout,
allowVotes,
allowCurationRewards,
percentSteemDollars,
beneficiaries);

String operations = StringUtils.getOperationsString(
StringUtils.getCommanSeparatedArrayString(
StringUtils.getCommanSeparatedArrayString("\"comment\"", params)));
StringUtils.getCommanSeparatedArrayString("\"comment\"",
params),
StringUtils.getCommanSeparatedArrayString("\"comment_options\"",
config))
);
this.broadcast(operations, steemConnectCallback);
}

Expand Down
@@ -1,5 +1,6 @@
package com.hapramp.steemconnect4j;

import java.util.ArrayList;
import java.util.Map;

public class StringUtils {
Expand Down Expand Up @@ -84,4 +85,33 @@ public static String getQueryParamsFromMap(Map<String, String> map) {
public static String getOperationsString(String value) {
return "{\"operations\":" + value + "}";
}

/**
* Formats string for `comment_options` operation.
* @param author author
* @param permlink perlink
* @param percentSteemDollars percentSteemDollars
* @param beneficiaries List of beneficiaries
* @return Formatted string
*/
public static String getCommentOptionStringFormat(String author,
String permlink,
String maxAcceptedpayout,
boolean allowVotes,
boolean allowCurationRewards,
int percentSteemDollars,
ArrayList<Beneficiary>
beneficiaries) {
return "{" + "\"author\": \"" + author + "\","
+ "\"permlink\":" + " \"" + permlink + "\","
+ "\"max_accepted_payout\": \"" + maxAcceptedpayout + "\","
+ "\"percent_steem_dollars\": " + percentSteemDollars + ","
+ "\"allow_votes\": " + allowVotes + ","
+ "\"allow_curation_rewards\": " + allowCurationRewards + ","
+ "\"extensions\": ["
+ "[0, {"
+ "\"beneficiaries\": " + beneficiaries.toString() + "}]"
+ "]"
+ "}";
}
}
Expand Up @@ -8,6 +8,8 @@
import com.hapramp.steemconnect4j.SteemConnectCallback;
import com.hapramp.steemconnect4j.SteemConnectException;
import com.hapramp.steemconnect4j.SteemConnectOptions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -264,7 +266,11 @@ public void testComment() {
try {
steemConnect.comment("parent_author", "parent_permlink",
"title", "body", "json",
"author", "permlink", new SteemConnectCallback() {
"author", "permlink", "0 SBD",
10000,
true,
true, new ArrayList<>(),
new SteemConnectCallback() {
@Override
public void onResponse(String response) {
assertTrue(response.length() > 0);
Expand Down
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.hapramp.steemconnect4j.Beneficiary;
import com.hapramp.steemconnect4j.HttpMethod;
import com.hapramp.steemconnect4j.NetworkUtils;
import com.hapramp.steemconnect4j.Route;
Expand All @@ -12,6 +13,8 @@
import com.hapramp.steemconnect4j.SteemConnectException;
import com.hapramp.steemconnect4j.SteemConnectOptions;
import com.hapramp.steemconnect4j.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -172,6 +175,38 @@ public void testStringFormat() {
assertEquals(exp, actualString);
}

@Test
public void testCommentOptionStringFormat() {
ArrayList<Beneficiary> beneficiaries = new ArrayList<>();
beneficiaries.add(new Beneficiary("bxute", 2000));
String formattedString =
StringUtils.getCommentOptionStringFormat(
"bxute",
"permlink",
"1000000.000 SBD",
true,
true,
10000,
beneficiaries);
String exp = "{" + "\"author\": \"bxute\","
+ "\"permlink\":" + " \"permlink\","
+ "\"max_accepted_payout\": \"1000000.000 SBD\","
+ "\"percent_steem_dollars\": 10000,"
+ "\"allow_votes\": true,"
+ "\"allow_curation_rewards\": true,"
+ "\"extensions\": ["
+ "[0, {"
+ "\"beneficiaries\": [{"
+ "\"account\":\"bxute\","
+ "\"weight\":2000"
+ "}]"
+ "}]"
+ "]"
+ "}";
assertEquals(exp, formattedString);
print(exp, formattedString);
}

private void print(String exp, String actual) {
System.out.println("Expected: " + exp);
System.out.println("Actual: " + actual);
Expand Down