Skip to content

Commit

Permalink
RPC newpostcustom allows buiding post with custom defined fields
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelfreitas committed Mar 19, 2016
1 parent 4d59ebe commit cb020fa
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/bitcoinrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ static const CRPCCommand vRPCCommands[] =
{ "dhtputraw", &dhtputraw, false, true, true },
{ "dhtget", &dhtget, false, true, true },
{ "newpostmsg", &newpostmsg, false, true, false },
{ "newpostcustom", &newpostcustom, false, true, false },
{ "newpostraw", &newpostraw, false, true, true },
{ "newdirectmsg", &newdirectmsg, false, true, false },
{ "newrtmsg", &newrtmsg, false, true, false },
Expand Down Expand Up @@ -1330,6 +1331,8 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
if (strMethod == "dhtget" && n > 5) ConvertTo<boost::int64_t>(params[5]);
if (strMethod == "newpostmsg" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "newpostmsg" && n > 4) ConvertTo<boost::int64_t>(params[4]);
if (strMethod == "newpostcustom" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "newpostcustom" && n > 2) ConvertTo<Object>(params[2]);
if (strMethod == "newpostraw" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "newdirectmsg" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "newdirectmsg" && n > 4) ConvertTo<bool>(params[4]);
Expand Down
1 change: 1 addition & 0 deletions src/bitcoinrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ extern json_spirit::Value dhtput(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value dhtputraw(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value dhtget(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value newpostmsg(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value newpostcustom(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value newpostraw(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value newdirectmsg(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value newrtmsg(const json_spirit::Array& params, bool fHelp);
Expand Down
2 changes: 1 addition & 1 deletion src/clientversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// These need to be macros, as version.cpp's and bitcoin-qt.rc's voodoo requires it
#define CLIENT_VERSION_MAJOR 0
#define CLIENT_VERSION_MINOR 9
#define CLIENT_VERSION_REVISION 34
#define CLIENT_VERSION_REVISION 35
#define CLIENT_VERSION_BUILD 0

// Set to true for release, false for prerelease or test build
Expand Down
71 changes: 67 additions & 4 deletions src/twister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1697,10 +1697,7 @@ bool createSignedUserpost(entry &v, std::string const &username, int k,
userpost["pfav"] = *ent;
break;
default:
if ( !msg.size() ) {
printf("createSignedUserpost: unknown type\n");
return false;
}
break;
}

if( reply_n.size() ) {
Expand Down Expand Up @@ -2261,6 +2258,72 @@ Value newpostmsg(const Array& params, bool fHelp)
return entryToJson(v);
}

Value newpostcustom(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 3)
throw runtime_error(
"newpostcustom <username> <k> '{\"field1\":value,\"field2\":value,...}'\n"
"Create a post with custom fields and add it to swarm");

EnsureWalletIsUnlocked();

string strUsername = params[0].get_str();
int k = params[1].get_int();
string strK = boost::lexical_cast<std::string>(k);
Object fields = params[2].get_obj();

entry v;
entry &userpost = v["userpost"];
// [MF] Warning: findLastPublicPostLocalUser requires that we follow ourselves
int lastk = findLastPublicPostLocalUser(strUsername);
if( lastk >= 0 )
userpost["lastk"] = lastk;

for (Object::const_iterator i = fields.begin(); i != fields.end(); ++i) {
if( i->value_.type() == str_type ) {
userpost[i->name_] = i->value_.get_str();
} else if ( i->value_.type() == int_type ) {
userpost[i->name_] = i->value_.get_int();
} else {
JSONRPCError(RPC_INVALID_PARAMS,string("unknown type for parameter: ") + i->name_);
}
}

if( !createSignedUserpost(v, strUsername, k, 0,
"", NULL, NULL) )
throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user");

vector<char> buf;
bencode(std::back_inserter(buf), v);

std::string errmsg;
if( !acceptSignedPost(buf.data(),buf.size(),strUsername,k,errmsg,NULL) )
throw JSONRPCError(RPC_INVALID_PARAMS,errmsg);

torrent_handle h = startTorrentUser(strUsername, true);
if( h.is_valid() ) {
// if member of torrent post it directly
h.add_piece(k,buf.data(),buf.size());
} else {
// TODO: swarm resource forwarding not implemented
dhtPutData(strUsername, "swarm", false,
v, strUsername, GetAdjustedTime(), 1);
}

// post to dht as well
dhtPutData(strUsername, string("post")+strK, false,
v, strUsername, GetAdjustedTime(), 1);
if( userpost.find_key("msg") ) {
dhtPutData(strUsername, string("status"), false,
v, strUsername, GetAdjustedTime(), k);
//look for mentions and hashtags in msg
dispatchHM(userpost["msg"].string(), strUsername, v);
}

hexcapePost(v);
return entryToJson(v);
}

Value newpostraw(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 3)
Expand Down

0 comments on commit cb020fa

Please sign in to comment.