From 5c4620d117b1fc068e99879ad2eeab25252ec230 Mon Sep 17 00:00:00 2001 From: erqan Date: Tue, 11 Aug 2015 19:06:11 +0300 Subject: [PATCH] dispatching hashtags and mentions in comments it also reduces the `createSignedUserpost`'s arguments --- src/twister.cpp | 164 ++++++++++++++++++++++++++---------------------- 1 file changed, 90 insertions(+), 74 deletions(-) diff --git a/src/twister.cpp b/src/twister.cpp index 4ac0c352..15bfb19a 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -1243,6 +1243,44 @@ void notifyNewGroupMember(string &groupAlias, string &newmember, string &invited storeGroupDM(groupAlias,stoDM); } +// dispatch new msg for mentions and hashtags +void dispatchHM(string const &strMsg, string const &strUsername, entry const &v) +{ + if (strMsg.size() == 0) + return; + // split and look for mentions and hashtags + vector tokens; + boost::algorithm::split(tokens,strMsg,boost::algorithm::is_any_of(msgTokensDelimiter), + boost::algorithm::token_compress_on); + BOOST_FOREACH(string const& token, tokens) { + if( token.length() >= 2 ) { + char delim = token.at(0); + if( delim != '#' && delim != '@' ) continue; + string target = (delim == '#') ? "hashtag" : "mention"; + string word = token.substr(1); +#ifdef HAVE_BOOST_LOCALE + word = boost::locale::to_lower(word); +#else + boost::algorithm::to_lower(word); +#endif + if( word.find(delim) == string::npos ) { + dhtPutData(word, target, true, + v, strUsername, GetAdjustedTime(), 0); + } else { + vector subtokens; + boost::algorithm::split(subtokens,word,std::bind1st(std::equal_to(),delim), + boost::algorithm::token_compress_on); + BOOST_FOREACH(string const& word, subtokens) { + if( word.length() ) { + dhtPutData(word, target, true, + v, strUsername, GetAdjustedTime(), 0); + } + } + } + } + } +} + // try decrypting new DM received by any torrent we follow bool processReceivedDM(lazy_entry const* post) { @@ -1561,13 +1599,9 @@ bool usernameExists(std::string const &username) */ bool createSignedUserpost(entry &v, std::string const &username, int k, - std::string const &msg, // either msg.size() or - entry const *rt, entry const *sig_rt, // rt != NULL or - entry const *dm, // dm != NULL. - entry const *fav, entry const *sig_fav, - entry const *pfav, - std::string const &reply_n, int reply_k - ) + int flag, std::string const &msg, + entry const *ent, entry const *sig, + std::string const &reply_n = "", int reply_k = 0) { entry &userpost = v["userpost"]; @@ -1581,21 +1615,29 @@ bool createSignedUserpost(entry &v, std::string const &username, int k, //userpost["t"] = "post"; userpost["msg"] = msg; } - if ( rt != NULL && sig_rt != NULL ) { + switch(flag) + { + case USERPOST_FLAG_RT: //userpost["t"] = "rt"; - userpost["rt"] = *rt; - userpost["sig_rt"] = *sig_rt; - } else if ( fav != NULL && sig_fav != NULL ) { - userpost["fav"] = *fav; - userpost["sig_fav"] = *sig_fav; - } else if ( dm != NULL ) { + userpost["rt"] = *ent; + userpost["sig_rt"] = *sig; + break; + case USERPOST_FLAG_FAV: + userpost["fav"] = *ent; + userpost["sig_fav"] = *sig; + break; + case USERPOST_FLAG_DM: //userpost["t"] = "dm"; - userpost["dm"] = *dm; - } else if ( pfav != NULL ) { - userpost["pfav"] = *pfav; - } else if ( !msg.size() ) { - printf("createSignedUserpost: unknown type\n"); - return false; + userpost["dm"] = *ent; + break; + case USERPOST_FLAG_P_FAV: + userpost["pfav"] = *ent; + break; + default: + if ( !msg.size() ) { + printf("createSignedUserpost: unknown type\n"); + return false; + } } if( reply_n.size() ) { @@ -2114,10 +2156,9 @@ Value newpostmsg(const Array& params, bool fHelp) if( lastk >= 0 ) v["userpost"]["lastk"] = lastk; - if( !createSignedUserpost(v, strUsername, k, strMsg, - NULL, NULL, NULL, - NULL, NULL, NULL, - strReplyN, replyK) ) + if( !createSignedUserpost(v, strUsername, k, 0, + strMsg, NULL, NULL, + strReplyN, replyK) ) throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); vector buf; @@ -2149,37 +2190,8 @@ Value newpostmsg(const Array& params, bool fHelp) v, strUsername, GetAdjustedTime(), 0); } - // split and look for mentions and hashtags - vector tokens; - boost::algorithm::split(tokens,strMsg,boost::algorithm::is_any_of(msgTokensDelimiter), - boost::algorithm::token_compress_on); - BOOST_FOREACH(string const& token, tokens) { - if( token.length() >= 2 ) { - char delim = token.at(0); - if( delim != '#' && delim != '@' ) continue; - string target = (delim == '#') ? "hashtag" : "mention"; - string word = token.substr(1); -#ifdef HAVE_BOOST_LOCALE - word = boost::locale::to_lower(word); -#else - boost::algorithm::to_lower(word); -#endif - if( word.find(delim) == string::npos ) { - dhtPutData(word, target, true, - v, strUsername, GetAdjustedTime(), 0); - } else { - vector subtokens; - boost::algorithm::split(subtokens,word,std::bind1st(std::equal_to(),delim), - boost::algorithm::token_compress_on); - BOOST_FOREACH(string const& word, subtokens) { - if( word.length() ) { - dhtPutData(word, target, true, - v, strUsername, GetAdjustedTime(), 0); - } - } - } - } - } + //look for mentions and hashtags in msg + dispatchHM(strMsg, strUsername, v); hexcapePost(v); return entryToJson(v); @@ -2265,9 +2277,9 @@ Value newdirectmsg(const Array& params, bool fHelp) BOOST_FOREACH(entry *dm, dmsToSend) { entry v; - if( !createSignedUserpost(v, strFrom, k, "", - NULL, NULL, dm, - NULL, NULL, NULL, + if( !createSignedUserpost(v, strFrom, k, + USERPOST_FLAG_DM, + "", dm, NULL, std::string(""), 0) ) throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); @@ -2304,7 +2316,7 @@ Value newrtmsg(const Array& params, bool fHelp) { if (fHelp || params.size() < 3 || params.size() > 4) throw runtime_error( - "newrtmsg [msg]\n" + "newrtmsg [comment]\n" "Post a new RT to swarm"); EnsureWalletIsUnlocked(); @@ -2316,7 +2328,7 @@ Value newrtmsg(const Array& params, bool fHelp) unHexcapePost(vrt); entry const *rt = vrt.find_key("userpost"); entry const *sig_rt= vrt.find_key("sig_userpost"); - string msg = params.size() > 3 ? params[3].get_str() : ""; + string strComment = params.size() > 3 ? params[3].get_str() : ""; entry v; // [MF] Warning: findLastPublicPostLocalUser requires that we follow ourselves @@ -2324,9 +2336,9 @@ Value newrtmsg(const Array& params, bool fHelp) if( lastk >= 0 ) v["userpost"]["lastk"] = lastk; - if( !createSignedUserpost(v, strUsername, k, msg, - rt, sig_rt, NULL, - NULL, NULL, NULL, + if( !createSignedUserpost(v, strUsername, k, + USERPOST_FLAG_RT, + strComment, rt, sig_rt, std::string(""), 0) ) throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); @@ -2360,6 +2372,8 @@ Value newrtmsg(const Array& params, bool fHelp) dhtPutData(rt_user, string("rts")+rt_k, true, v, strUsername, GetAdjustedTime(), 0); } + //look for hashtags and mentions in comment + dispatchHM(strComment, strUsername, v); hexcapePost(v); return entryToJson(v); @@ -2370,7 +2384,7 @@ Value newfavmsg(const Array& params, bool fHelp) { if (fHelp || params.size() < 3 || params.size() > 5) throw runtime_error( - "newfavmsg [private=false] [msg=''] \n" + "newfavmsg [private=false] [comment=''] \n" "Add a post to swarm as a favorite"); EnsureWalletIsUnlocked(); @@ -2378,7 +2392,7 @@ Value newfavmsg(const Array& params, bool fHelp) string strUsername = params[0].get_str(); int k = params[1].get_int(); string strK = boost::lexical_cast(k); - string msg = (params.size() > 4) ? params[4].get_str() : ""; + string strComment = (params.size() > 4) ? params[4].get_str() : ""; entry vfav = jsonToEntry(params[2].get_obj()); bool isPriv = (params.size() > 3) ? params[3].get_bool() : false; unHexcapePost(vfav); @@ -2398,15 +2412,15 @@ Value newfavmsg(const Array& params, bool fHelp) throw JSONRPCError(RPC_INTERNAL_ERROR, "error encrypting to pubkey of destination user"); - if( !createSignedUserpost(v, strUsername, k, msg, - NULL, NULL, NULL, - NULL, NULL, &pfav, + if( !createSignedUserpost(v, strUsername, k, + USERPOST_FLAG_P_FAV, + strComment, &pfav, NULL, std::string(""), 0) ) throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); } - else if( !createSignedUserpost(v, strUsername, k, msg, - NULL, NULL, NULL, - fav, sig_fav, NULL, + else if( !createSignedUserpost(v, strUsername, k, + USERPOST_FLAG_FAV, + strComment, fav, sig_fav, std::string(""), 0) ) throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); @@ -2422,6 +2436,8 @@ Value newfavmsg(const Array& params, bool fHelp) // if member of torrent post it directly h.add_piece(k,buf.data(),buf.size()); } + //look for mentions and hashtags in comment + dispatchHM(strComment, strUsername, v); hexcapePost(v); return entryToJson(v); @@ -3663,9 +3679,9 @@ Value getgroupinfo(const Array& params, bool fHelp) static void signAndAddDM(const std::string &strFrom, int k, const entry *dm) { entry v; - if( !createSignedUserpost(v, strFrom, k, "", - NULL, NULL, dm, - NULL, NULL, NULL, + if( !createSignedUserpost(v, strFrom, k, + USERPOST_FLAG_DM, + "", dm, NULL, std::string(""), 0) ) throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user");