Browse Source

dispatching hashtags and mentions in comments

it also reduces the `createSignedUserpost`'s arguments
miguelfreitas
erqan 9 years ago
parent
commit
5c4620d117
  1. 164
      src/twister.cpp

164
src/twister.cpp

@ -1243,6 +1243,44 @@ void notifyNewGroupMember(string &groupAlias, string &newmember, string &invited
storeGroupDM(groupAlias,stoDM); 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<string> 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<string> subtokens;
boost::algorithm::split(subtokens,word,std::bind1st(std::equal_to<char>(),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 // try decrypting new DM received by any torrent we follow
bool processReceivedDM(lazy_entry const* post) 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, bool createSignedUserpost(entry &v, std::string const &username, int k,
std::string const &msg, // either msg.size() or int flag, std::string const &msg,
entry const *rt, entry const *sig_rt, // rt != NULL or entry const *ent, entry const *sig,
entry const *dm, // dm != NULL. std::string const &reply_n = "", int reply_k = 0)
entry const *fav, entry const *sig_fav,
entry const *pfav,
std::string const &reply_n, int reply_k
)
{ {
entry &userpost = v["userpost"]; entry &userpost = v["userpost"];
@ -1581,21 +1615,29 @@ bool createSignedUserpost(entry &v, std::string const &username, int k,
//userpost["t"] = "post"; //userpost["t"] = "post";
userpost["msg"] = msg; userpost["msg"] = msg;
} }
if ( rt != NULL && sig_rt != NULL ) { switch(flag)
{
case USERPOST_FLAG_RT:
//userpost["t"] = "rt"; //userpost["t"] = "rt";
userpost["rt"] = *rt; userpost["rt"] = *ent;
userpost["sig_rt"] = *sig_rt; userpost["sig_rt"] = *sig;
} else if ( fav != NULL && sig_fav != NULL ) { break;
userpost["fav"] = *fav; case USERPOST_FLAG_FAV:
userpost["sig_fav"] = *sig_fav; userpost["fav"] = *ent;
} else if ( dm != NULL ) { userpost["sig_fav"] = *sig;
break;
case USERPOST_FLAG_DM:
//userpost["t"] = "dm"; //userpost["t"] = "dm";
userpost["dm"] = *dm; userpost["dm"] = *ent;
} else if ( pfav != NULL ) { break;
userpost["pfav"] = *pfav; case USERPOST_FLAG_P_FAV:
} else if ( !msg.size() ) { userpost["pfav"] = *ent;
printf("createSignedUserpost: unknown type\n"); break;
return false; default:
if ( !msg.size() ) {
printf("createSignedUserpost: unknown type\n");
return false;
}
} }
if( reply_n.size() ) { if( reply_n.size() ) {
@ -2114,10 +2156,9 @@ Value newpostmsg(const Array& params, bool fHelp)
if( lastk >= 0 ) if( lastk >= 0 )
v["userpost"]["lastk"] = lastk; v["userpost"]["lastk"] = lastk;
if( !createSignedUserpost(v, strUsername, k, strMsg, if( !createSignedUserpost(v, strUsername, k, 0,
NULL, NULL, NULL, strMsg, NULL, NULL,
NULL, NULL, NULL, strReplyN, replyK) )
strReplyN, replyK) )
throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user");
vector<char> buf; vector<char> buf;
@ -2149,37 +2190,8 @@ Value newpostmsg(const Array& params, bool fHelp)
v, strUsername, GetAdjustedTime(), 0); v, strUsername, GetAdjustedTime(), 0);
} }
// split and look for mentions and hashtags //look for mentions and hashtags in msg
vector<string> tokens; dispatchHM(strMsg, strUsername, v);
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<string> subtokens;
boost::algorithm::split(subtokens,word,std::bind1st(std::equal_to<char>(),delim),
boost::algorithm::token_compress_on);
BOOST_FOREACH(string const& word, subtokens) {
if( word.length() ) {
dhtPutData(word, target, true,
v, strUsername, GetAdjustedTime(), 0);
}
}
}
}
}
hexcapePost(v); hexcapePost(v);
return entryToJson(v); return entryToJson(v);
@ -2265,9 +2277,9 @@ Value newdirectmsg(const Array& params, bool fHelp)
BOOST_FOREACH(entry *dm, dmsToSend) { BOOST_FOREACH(entry *dm, dmsToSend) {
entry v; entry v;
if( !createSignedUserpost(v, strFrom, k, "", if( !createSignedUserpost(v, strFrom, k,
NULL, NULL, dm, USERPOST_FLAG_DM,
NULL, NULL, NULL, "", dm, NULL,
std::string(""), 0) ) std::string(""), 0) )
throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); 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) if (fHelp || params.size() < 3 || params.size() > 4)
throw runtime_error( throw runtime_error(
"newrtmsg <username> <k> <rt_v_object> [msg]\n" "newrtmsg <username> <k> <rt_v_object> [comment]\n"
"Post a new RT to swarm"); "Post a new RT to swarm");
EnsureWalletIsUnlocked(); EnsureWalletIsUnlocked();
@ -2316,7 +2328,7 @@ Value newrtmsg(const Array& params, bool fHelp)
unHexcapePost(vrt); unHexcapePost(vrt);
entry const *rt = vrt.find_key("userpost"); entry const *rt = vrt.find_key("userpost");
entry const *sig_rt= vrt.find_key("sig_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; entry v;
// [MF] Warning: findLastPublicPostLocalUser requires that we follow ourselves // [MF] Warning: findLastPublicPostLocalUser requires that we follow ourselves
@ -2324,9 +2336,9 @@ Value newrtmsg(const Array& params, bool fHelp)
if( lastk >= 0 ) if( lastk >= 0 )
v["userpost"]["lastk"] = lastk; v["userpost"]["lastk"] = lastk;
if( !createSignedUserpost(v, strUsername, k, msg, if( !createSignedUserpost(v, strUsername, k,
rt, sig_rt, NULL, USERPOST_FLAG_RT,
NULL, NULL, NULL, strComment, rt, sig_rt,
std::string(""), 0) ) std::string(""), 0) )
throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); 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, dhtPutData(rt_user, string("rts")+rt_k, true,
v, strUsername, GetAdjustedTime(), 0); v, strUsername, GetAdjustedTime(), 0);
} }
//look for hashtags and mentions in comment
dispatchHM(strComment, strUsername, v);
hexcapePost(v); hexcapePost(v);
return entryToJson(v); return entryToJson(v);
@ -2370,7 +2384,7 @@ Value newfavmsg(const Array& params, bool fHelp)
{ {
if (fHelp || params.size() < 3 || params.size() > 5) if (fHelp || params.size() < 3 || params.size() > 5)
throw runtime_error( throw runtime_error(
"newfavmsg <username> <k> <fav_v_object> [private=false] [msg=''] \n" "newfavmsg <username> <k> <fav_v_object> [private=false] [comment=''] \n"
"Add a post to swarm as a favorite"); "Add a post to swarm as a favorite");
EnsureWalletIsUnlocked(); EnsureWalletIsUnlocked();
@ -2378,7 +2392,7 @@ Value newfavmsg(const Array& params, bool fHelp)
string strUsername = params[0].get_str(); string strUsername = params[0].get_str();
int k = params[1].get_int(); int k = params[1].get_int();
string strK = boost::lexical_cast<std::string>(k); string strK = boost::lexical_cast<std::string>(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()); entry vfav = jsonToEntry(params[2].get_obj());
bool isPriv = (params.size() > 3) ? params[3].get_bool() : false; bool isPriv = (params.size() > 3) ? params[3].get_bool() : false;
unHexcapePost(vfav); unHexcapePost(vfav);
@ -2398,15 +2412,15 @@ Value newfavmsg(const Array& params, bool fHelp)
throw JSONRPCError(RPC_INTERNAL_ERROR, throw JSONRPCError(RPC_INTERNAL_ERROR,
"error encrypting to pubkey of destination user"); "error encrypting to pubkey of destination user");
if( !createSignedUserpost(v, strUsername, k, msg, if( !createSignedUserpost(v, strUsername, k,
NULL, NULL, NULL, USERPOST_FLAG_P_FAV,
NULL, NULL, &pfav, strComment, &pfav, NULL,
std::string(""), 0) ) std::string(""), 0) )
throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user");
} }
else if( !createSignedUserpost(v, strUsername, k, msg, else if( !createSignedUserpost(v, strUsername, k,
NULL, NULL, NULL, USERPOST_FLAG_FAV,
fav, sig_fav, NULL, strComment, fav, sig_fav,
std::string(""), 0) ) std::string(""), 0) )
throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); 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 // if member of torrent post it directly
h.add_piece(k,buf.data(),buf.size()); h.add_piece(k,buf.data(),buf.size());
} }
//look for mentions and hashtags in comment
dispatchHM(strComment, strUsername, v);
hexcapePost(v); hexcapePost(v);
return entryToJson(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) static void signAndAddDM(const std::string &strFrom, int k, const entry *dm)
{ {
entry v; entry v;
if( !createSignedUserpost(v, strFrom, k, "", if( !createSignedUserpost(v, strFrom, k,
NULL, NULL, dm, USERPOST_FLAG_DM,
NULL, NULL, NULL, "", dm, NULL,
std::string(""), 0) ) std::string(""), 0) )
throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user");

Loading…
Cancel
Save