From cb020faba8eb849fb3429742c7d878e1aca63b25 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Sat, 19 Mar 2016 14:55:49 -0300 Subject: [PATCH] RPC newpostcustom allows buiding post with custom defined fields --- src/bitcoinrpc.cpp | 3 ++ src/bitcoinrpc.h | 1 + src/clientversion.h | 2 +- src/twister.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index dcbbc85b..d5d8019d 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -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 }, @@ -1330,6 +1331,8 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector 5) ConvertTo(params[5]); if (strMethod == "newpostmsg" && n > 1) ConvertTo(params[1]); if (strMethod == "newpostmsg" && n > 4) ConvertTo(params[4]); + if (strMethod == "newpostcustom" && n > 1) ConvertTo(params[1]); + if (strMethod == "newpostcustom" && n > 2) ConvertTo(params[2]); if (strMethod == "newpostraw" && n > 1) ConvertTo(params[1]); if (strMethod == "newdirectmsg" && n > 1) ConvertTo(params[1]); if (strMethod == "newdirectmsg" && n > 4) ConvertTo(params[4]); diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index cebdb693..39d72950 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -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); diff --git a/src/clientversion.h b/src/clientversion.h index 41d40f4f..c444b8f1 100644 --- a/src/clientversion.h +++ b/src/clientversion.h @@ -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 diff --git a/src/twister.cpp b/src/twister.cpp index 84cfd5b4..2682c6df 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -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() ) { @@ -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 '{\"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(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 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)