Made modifications to newpostmsg() and createSignedUserpost(); to accomidate an additional field named addy to contain a url that the userpost is referencing.
https://github.com/miguelfreitas/twister-core/blob/4509ff7e3fec6bc0b5811ca3e30c0192f7338817/src/twister.cppValue newpostmsg(const Array& params, bool fHelp)
{
// increase the prams numbers to accomidate
if (fHelp || (params.size() != 4 && params.size() != 6))
throw runtime_error(
// added <address>
"newpostmsg <username> <k> <msg> <addy> [reply_n] [reply_k]\n"
"Post a new message to swarm");
EnsureWalletIsUnlocked();
string strUsername = params[0].get_str();
int k = params[1].get_int();
string strK = boost::lexical_cast<std::string>(k);
string strMsg = params[2].get_str();
// added param field for address field
string strAddy = params[3].get_str();
string strReplyN, strReplyK;
int replyK = 0;
if( params.size() == 6 ) {
strReplyN = params[4].get_str();
replyK = params[5].get_int();
strReplyK = boost::lexical_cast<std::string>(replyK);
}
entry v;
// [MF] Warning: findLastPublicPostLocalUser requires that we follow ourselves
int lastk = findLastPublicPostLocalUser(strUsername);
if( lastk >= 0 )
v["userpost"]["lastk"] = lastk;
// added strAddy, an additional field for storing a web address
if( !createSignedUserpost(v, strUsername, k, strMsg, strAddy,
NULL, NULL, NULL,
strReplyN, replyK) )
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
ses->dht_putData(strUsername, "swarm", false,
v, strUsername, GetAdjustedTime(), 1);
}
// post to dht as well
ses->dht_putData(strUsername, string("post")+strK, false,
v, strUsername, GetAdjustedTime(), 1);
ses->dht_putData(strUsername, string("status"), false,
v, strUsername, GetAdjustedTime(), k);
// is this a reply? notify
if( strReplyN.length() ) {
ses->dht_putData(strReplyN, string("replies")+strReplyK, true,
v, strUsername, GetAdjustedTime(), 0);
}
// split and look for mentions and hashtags
vector<string> tokens;
boost::algorithm::split(tokens,strMsg,boost::algorithm::is_any_of(" \n\t.,:/?!"),
boost::algorithm::token_compress_on);
BOOST_FOREACH(string const& token, tokens) {
if( token.length() >= 2 ) {
string word = token.substr(1);
boost::algorithm::to_lower(word);
if(
token.at(0) == '#') {
ses->dht_putData(word, "hashtag", true,
v, strUsername, GetAdjustedTime(), 0);
} else if(
token.at(0) == '@') {
ses->dht_putData(word, "mention", true,
v, strUsername, GetAdjustedTime(), 0);
}
}
}
hexcapePost(v);
return entryToJson(v);
}
/*
"userpost" :
{
"n" : username,
"k" : seq number,
"t" : "post" / "dm" / "rt"
"msg" : message (post/rt)
// comment-chain mod, add addy
"addy" : address we are commenting on,
"time" : unix utc
"height" : best height at user
"dm" : encrypted message (dm) -opt
"rt" : original userpost - opt
"sig_rt" : sig of rt - opt
"reply" : - opt
{
"n" : reference username
"k" : reference k
}
}
"sig_userpost" : signature by userpost.n
*/
bool createSignedUserpost(entry &v, std::string const &username, int k,
std::string const &msg, // either msg.size() or
// comment chain mod, adding addy
std::string const &addy,
entry const *rt, entry const *sig_rt, // rt != NULL or
entry const *dm, // dm != NULL.
std::string const &reply_n, int reply_k
)
{
entry &userpost = v["userpost"];
userpost["n"] = username;
userpost["k"] = k;
// comment chain mod, adding addy
userpost["addy"] = addy;
userpost["time"] = GetAdjustedTime();
userpost["height"] = getBestHeight() - 1; // be conservative
if( msg.size() ) {
//userpost["t"] = "post";
userpost["msg"] = msg;
} else if ( rt != NULL && sig_rt != NULL ) {
//userpost["t"] = "rt";
userpost["rt"] = *rt;
userpost["sig_rt"] = *sig_rt;
} else if ( dm != NULL ) {
//userpost["t"] = "dm";
userpost["dm"] = *dm;
} else {
printf("createSignedUserpost: unknown type\n");
return false;
}
if( reply_n.size() ) {
entry &reply = userpost["reply"];
reply["n"]=reply_n;
reply["k"]=reply_k;
}
//
std::vector<char> buf;
bencode(std::back_inserter(buf), userpost);
std::string sig = createSignature(std::string(buf.data(),buf.size()), username);
if( sig.size() ) {
v["sig_userpost"] = sig;
return true;
} else {
return false;
}
}