From c2fefc36c734830df8b52a55cdb755f10c3b3483 Mon Sep 17 00:00:00 2001 From: Sarang Bharadwaj Date: Sun, 10 Jun 2012 22:23:22 +0530 Subject: [PATCH] Polling for user's DMs --- .../twitter/Requests/DirectMessageRequest.cpp | 23 ++++----- .../twitter/Requests/DirectMessageRequest.h | 10 ++-- backends/twitter/TwitterPlugin.cpp | 50 +++++++++++++++++-- backends/twitter/TwitterPlugin.h | 10 +++- 4 files changed, 70 insertions(+), 23 deletions(-) diff --git a/backends/twitter/Requests/DirectMessageRequest.cpp b/backends/twitter/Requests/DirectMessageRequest.cpp index 7eb2c82b..5cee8969 100644 --- a/backends/twitter/Requests/DirectMessageRequest.cpp +++ b/backends/twitter/Requests/DirectMessageRequest.cpp @@ -6,23 +6,20 @@ DEFINE_LOGGER(logger, "DirectMessageRequest") void DirectMessageRequest::run() { replyMsg = ""; - if(twitObj->directMessageSend(username, data, false) == false) { - LOG4CXX_ERROR(logger, user << ": Error while sending directed message to " << username ); - return; - } - twitObj->getLastWebResponse( replyMsg ); + success = twitObj->directMessageSend(username, data, false); + if(success) twitObj->getLastWebResponse( replyMsg ); } void DirectMessageRequest::finalize() { - if(replyMsg.length()) { + if(!success) { + LOG4CXX_ERROR(logger, user << ": Error while sending directed message to " << username ); + twitObj->getLastCurlError( replyMsg ); + callBack(user, replyMsg); + } else { std::string error = getErrorMessage(replyMsg); - if(error.length()) { - np->handleMessage(user, "twitter-account", error); - LOG4CXX_INFO(logger, user << ": " << error); - } else { - LOG4CXX_INFO(logger, user << ": Sent " << data << " to " << username) - LOG4CXX_INFO(logger, user << ": Twitter reponse - " << replyMsg) - } + if(error.length()) LOG4CXX_ERROR(logger, user << " - " << error) + else LOG4CXX_INFO(logger, user << " - " << replyMsg) + callBack(user, error); } } diff --git a/backends/twitter/Requests/DirectMessageRequest.h b/backends/twitter/Requests/DirectMessageRequest.h index 2dd5ae7a..c1e00c53 100644 --- a/backends/twitter/Requests/DirectMessageRequest.h +++ b/backends/twitter/Requests/DirectMessageRequest.h @@ -3,9 +3,9 @@ #include "../ThreadPool.h" #include "../libtwitcurl/twitcurl.h" -#include "transport/networkplugin.h" #include "transport/logging.h" #include +#include #include using namespace Transport; @@ -17,15 +17,17 @@ class DirectMessageRequest : public Thread std::string user; std::string username; std::string replyMsg; - NetworkPlugin *np; + boost::function< void (std::string&, std::string&) > callBack; + bool success; public: - DirectMessageRequest(NetworkPlugin *_np, twitCurl *obj, const std::string &_user, const std::string & _username, const std::string &_data) { + DirectMessageRequest(twitCurl *obj, const std::string &_user, const std::string & _username, const std::string &_data, + boost::function< void (std::string&, std::string&) > cb) { twitObj = obj->clone(); data = _data; user = _user; username = _username; - np = _np; + callBack = cb; } ~DirectMessageRequest() { diff --git a/backends/twitter/TwitterPlugin.cpp b/backends/twitter/TwitterPlugin.cpp index 20ba95cc..ecdadaba 100644 --- a/backends/twitter/TwitterPlugin.cpp +++ b/backends/twitter/TwitterPlugin.cpp @@ -42,6 +42,7 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora m_timer = m_factories->getTimerFactory()->createTimer(60000); m_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this)); + m_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this)); m_timer->start(); LOG4CXX_INFO(logger, "Starting the plugin."); @@ -129,7 +130,8 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std: else if(cmd == "#help") tp->runAsThread(new HelpMessageRequest(np, user)); else if(cmd[0] == '@') { std::string username = cmd.substr(1); - tp->runAsThread(new DirectMessageRequest(np, sessions[user], user, username, data)); + tp->runAsThread(new DirectMessageRequest(sessions[user], user, username, data, + boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2))); } else if(cmd == "#status") tp->runAsThread(new StatusUpdateRequest(np, sessions[user], user, data)); else if(cmd == "#timeline") tp->runAsThread(new TimelineRequest(sessions[user], user, data, "", @@ -137,6 +139,11 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std: else if(cmd == "#friends") tp->runAsThread(new FetchFriends(sessions[user], user, boost::bind(&TwitterPlugin::displayFriendlist, this, _1, _2, _3))); else handleMessage(user, "twitter-account", "Unknown command! Type #help for a list of available commands."); + } + + else { + tp->runAsThread(new DirectMessageRequest(sessions[user], user, legacyName, message, + boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2))); } } @@ -165,6 +172,19 @@ void TwitterPlugin::pollForTweets() m_timer->start(); } +void TwitterPlugin::pollForDirectMessages() +{ + /*boost::mutex::scoped_lock lock(userlock); + std::set::iterator it = onlineUsers.begin(); + while(it != onlineUsers.end()) { + std::string user = *it; + tp->runAsThread(new DirectMessage(sessions[user], user, "", mostRecentDirectMessageID[user], + boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3))); + it++; + } + m_timer->start();*/ +} + bool TwitterPlugin::getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret) { @@ -258,9 +278,10 @@ void TwitterPlugin::pinExchangeComplete(const std::string user, const std::strin onlineUsers.insert(user); mostRecentTweetID[user] = ""; + mostRecentDirectMessageID[user] = ""; } -void TwitterPlugin::updateUsersLastTweetID(const std::string user, const std::string ID) +void TwitterPlugin::updateLastTweetID(const std::string user, const std::string ID) { boost::mutex::scoped_lock lock(userlock); mostRecentTweetID[user] = ID; @@ -274,6 +295,20 @@ std::string TwitterPlugin::getMostRecentTweetID(const std::string user) return ID; } +void TwitterPlugin::updateLastDMID(const std::string user, const std::string ID) +{ + boost::mutex::scoped_lock lock(userlock); + mostRecentDirectMessageID[user] = ID; +} + +std::string TwitterPlugin::getMostRecentDMID(const std::string user) +{ + boost::mutex::scoped_lock lock(userlock); + std::string ID = "-1"; + if(onlineUsers.count(user)) ID = mostRecentDirectMessageID[user]; + return ID; +} + /************************************** Twitter response functions **********************************/ void TwitterPlugin::populateRoster(std::string &user, std::vector &friends, std::string &errMsg) @@ -281,7 +316,7 @@ void TwitterPlugin::populateRoster(std::string &user, std::vector &friends if(errMsg.length() == 0) { for(int i=0 ; i(), pbnetwork::STATUS_ONLINE); + handleBuddyChanged(user, friends[i].getScreenName(), friends[i].getUserName(), std::vector(), pbnetwork::STATUS_ONLINE); } } else handleMessage(user, "twitter-account", std::string("Error populating roster - ") + errMsg); } @@ -311,7 +346,7 @@ void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested, if((userRequested == "" || userRequested == user) && tweets.size()) { std::string tweetID = getMostRecentTweetID(user); - if(tweetID != tweets[0].getID()) updateUsersLastTweetID(user, tweets[0].getID()); + if(tweetID != tweets[0].getID()) updateLastTweetID(user, tweets[0].getID()); else timeline = ""; //have already sent the tweet earlier } @@ -319,4 +354,9 @@ void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested, } else handleMessage(user, "twitter-account", errMsg); } - +void TwitterPlugin::directMessageResponse(std::string &user, std::string &errMsg) +{ + if(errMsg.length()) { + handleMessage(user, "twitter-account", std::string("Error while sending direct message! - ") + errMsg); + } +} diff --git a/backends/twitter/TwitterPlugin.h b/backends/twitter/TwitterPlugin.h index 068079d7..d300e735 100644 --- a/backends/twitter/TwitterPlugin.h +++ b/backends/twitter/TwitterPlugin.h @@ -73,6 +73,8 @@ class TwitterPlugin : public NetworkPlugin { void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector &groups); void pollForTweets(); + + void pollForDirectMessages(); bool getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret); @@ -84,14 +86,19 @@ class TwitterPlugin : public NetworkPlugin { void pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret); - void updateUsersLastTweetID(const std::string user, const std::string ID); + void updateLastTweetID(const std::string user, const std::string ID); std::string getMostRecentTweetID(const std::string user); + void updateLastDMID(const std::string user, const std::string ID); + + std::string getMostRecentDMID(const std::string user); + /****************** Twitter Response handlers **************************************/ void populateRoster(std::string &user, std::vector &friends, std::string &errMsg); void displayFriendlist(std::string &user, std::vector &friends, std::string &errMsg); void displayTweets(std::string &user, std::string &userRequested, std::vector &tweets , std::string &errMsg); + void directMessageResponse(std::string &user, std::string &errMsg); /***********************************************************************************/ private: @@ -111,6 +118,7 @@ class TwitterPlugin : public NetworkPlugin { std::map sessions; std::map connectionState; std::map mostRecentTweetID; + std::map mostRecentDirectMessageID; std::set onlineUsers; mode twitterMode; };