Changeset - 6cc7c334f3c4
[Not reviewed]
0 4 2
Sarang Bharadwaj - 13 years ago 2012-06-15 11:45:41
sarang.bh@gmail.com
Retweet
6 files changed with 109 insertions and 1 deletions:
0 comments (0 inline, 0 general)
backends/twitter/Requests/RetweetRequest.cpp
Show inline comments
 
new file 100644
 
#include "RetweetRequest.h"
 
DEFINE_LOGGER(logger, "RetweetRequest")
 
void RetweetRequest::run()
 
{
 
	LOG4CXX_INFO(logger, user << " Retweeting " << data)
 
	success = twitObj->retweetById( data );
 
}
 

	
 
void RetweetRequest::finalize()
 
{
 
	replyMsg = "";
 
	if(!success) {
 
		twitObj->getLastCurlError( replyMsg );
 
		LOG4CXX_ERROR(logger, user << " " << replyMsg)
 
		callBack(user, replyMsg);
 
	} else {
 
		twitObj->getLastWebResponse( replyMsg );
 
		std::string error = getErrorMessage( replyMsg );
 
		if(error.length()) LOG4CXX_ERROR(logger, user << " " << error)
 
		else LOG4CXX_INFO(logger, user << " " << replyMsg);
 
		callBack(user, error);
 
	}
 
}
backends/twitter/Requests/RetweetRequest.h
Show inline comments
 
new file 100644
 
#ifndef RETWEET_H
 
#define RETWEET_H
 

	
 
#include "../ThreadPool.h"
 
#include "../TwitterResponseParser.h"
 
#include "../libtwitcurl/twitcurl.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 
#include <boost/function.hpp>
 
#include <string>
 
#include <iostream>
 

	
 
using namespace Transport;
 
class RetweetRequest : public Thread
 
{
 
	twitCurl *twitObj;
 
	std::string data;
 
	std::string user;
 
	std::string replyMsg;
 
	bool success;
 
	boost::function < void (std::string&, std::string &) > callBack;
 

	
 
	public:
 
	RetweetRequest(twitCurl *obj, const std::string &_user, const std::string &_data,
 
			       boost::function < void (std::string &, std::string &) > _cb) {
 
		twitObj = obj->clone();
 
		data = _data;
 
		user = _user;
 
		callBack = _cb;
 
	}
 

	
 
	~RetweetRequest() {
 
		delete twitObj;
 
	}
 

	
 
	void run();
 
	void finalize();
 
};
 

	
 
#endif
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
#include "TwitterPlugin.h"
 
#include "Requests/StatusUpdateRequest.h"
 
#include "Requests/DirectMessageRequest.h"
 
#include "Requests/TimelineRequest.h"
 
#include "Requests/FetchFriends.h"
 
#include "Requests/HelpMessageRequest.h"
 
#include "Requests/PINExchangeProcess.h"
 
#include "Requests/OAuthFlow.h"
 
#include "Requests/CreateFriendRequest.h"
 
#include "Requests/DestroyFriendRequest.h"
 
#include "Requests/RetweetRequest.h"
 

	
 
DEFINE_LOGGER(logger, "Twitter Backend");
 

	
 
TwitterPlugin *np = NULL;
 
Swift::SimpleEventLoop *loop_; // Event Loop
 

	
 
#define abs(x) ((x)<0?-(x):(x))
 
static int cmp(std::string a, std::string b)
 
{
 
	int diff = abs((int)a.size() - (int)b.size());
 
	if(a.size() < b.size()) a = std::string(diff,'0') + a;
 
	else b = std::string(diff,'0') + b;
 
@@ -153,24 +154,26 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
 
			tp->runAsThread(new DirectMessageRequest(sessions[user], user, username, data,
 
												     boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
 
		}
 
		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, "",
 
														boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4)));
 
		else if(cmd == "#friends") tp->runAsThread(new FetchFriends(sessions[user], user,
 
													   boost::bind(&TwitterPlugin::displayFriendlist, this, _1, _2, _3)));
 
		else if(cmd == "#follow") tp->runAsThread(new CreateFriendRequest(sessions[user], user, data,
 
													   boost::bind(&TwitterPlugin::createFriendResponse, this, _1, _2, _3)));
 
		else if(cmd == "#unfollow") tp->runAsThread(new DestroyFriendRequest(sessions[user], user, data,
 
													   boost::bind(&TwitterPlugin::deleteFriendResponse, this, _1, _2, _3)));
 
		else if(cmd == "#retweet") tp->runAsThread(new RetweetRequest(sessions[user], user, data,
 
													   boost::bind(&TwitterPlugin::RetweetResponse, this, _1, _2)));
 
		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, _3)));
 
	}
 
}
 

	
 
void TwitterPlugin::handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) 
 
{
 
	LOG4CXX_INFO(logger, user << ": Added buddy " << buddyName << ".");
 
@@ -356,25 +359,25 @@ void TwitterPlugin::displayFriendlist(std::string &user, std::vector<User> &frie
 
		userlist += "***************************************\n";
 
		handleMessage(user, "twitter-account", userlist);	
 
	} else handleMessage(user, "twitter-account", errMsg);	
 
 
 
}
 

	
 
void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , std::string &errMsg)
 
{
 
	if(errMsg.length() == 0) {
 
		std::string timeline = "";
 

	
 
		for(int i=0 ; i<tweets.size() ; i++) {
 
			timeline += " - " + tweets[i].getUserData().getScreenName() + ": " + tweets[i].getTweet() + "\n";
 
			timeline += " - " + tweets[i].getUserData().getScreenName() + ": " + tweets[i].getTweet() + " (MsgId: " + tweets[i].getID() + ")\n";
 
		}
 

	
 
		if((userRequested == "" || userRequested == user) && tweets.size()) {
 
			std::string tweetID = getMostRecentTweetID(user);
 
			if(tweetID != tweets[0].getID()) updateLastTweetID(user, tweets[0].getID());
 
			else timeline = ""; //have already sent the tweet earlier
 
		}
 

	
 
		if(timeline.length()) handleMessage(user, "twitter-account", timeline);
 
	} else handleMessage(user, "twitter-account", errMsg);	
 
}
 

	
 
@@ -435,12 +438,21 @@ void TwitterPlugin::createFriendResponse(std::string &user, std::string &frnd, s
 
}
 

	
 
void TwitterPlugin::deleteFriendResponse(std::string &user, std::string &frnd, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, "twitter-account", errMsg);
 
		return;
 
	} if(twitterMode == SINGLECONTACT) {
 
		handleMessage(user, "twitter-account", std::string("You are not following ") + frnd + "anymore");
 
	} else if(twitterMode == MULTIPLECONTACT) {
 
	}
 
}
 

	
 

	
 
void TwitterPlugin::RetweetResponse(std::string &user, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, "twitter-account", errMsg);
 
		return;
 
	}
 
}
backends/twitter/TwitterPlugin.h
Show inline comments
 
@@ -93,24 +93,25 @@ class TwitterPlugin : public NetworkPlugin {
 

	
 
		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<User> &friends, std::string &errMsg);
 
		void displayFriendlist(std::string &user, std::vector<User> &friends, std::string &errMsg);
 
		void displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , std::string &errMsg);
 
		void directMessageResponse(std::string &user, std::vector<DirectMessage> &messages, std::string &errMsg);
 
		void createFriendResponse(std::string &user, std::string &frnd, std::string &errMsg);
 
		void deleteFriendResponse(std::string &user, std::string &frnd, std::string &errMsg);
 
		void RetweetResponse(std::string &user, std::string &errMsg);
 
		/***********************************************************************************/
 

	
 
	private:
 
		enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
 
		enum mode {SINGLECONTACT, MULTIPLECONTACT, CHATROOM};
 

	
 
		Config *config;
 

	
 
		std::string consumerKey;
 
		std::string consumerSecret;
 
		std::string OAUTH_KEY;
 
		std::string OAUTH_SECRET;
backends/twitter/libtwitcurl/twitcurl.cpp
Show inline comments
 
@@ -426,24 +426,54 @@ bool twitCurl::statusDestroyById( std::string& statusId )
 
    if( statusId.length() )
 
    {
 
        /* Prepare URL */
 
        std::string buildUrl = twitterDefaults::TWITCURL_STATUDESTROY_URL + statusId +
 
                               twitCurlDefaults::TWITCURL_EXTENSIONFORMATS[m_eApiFormatType];
 
 
        /* Perform DELETE */
 
        retVal = performDelete( buildUrl );
 
    }
 
    return retVal;
 
}
 
 
/*++
 
* @method: twitCurl::retweetById
 
*
 
* @description: method to retweet a status message by its id
 
*
 
* @input: statusId - a number in std::string format
 
*
 
* @output: true if RETWEET is success, otherwise false. This does not check http
 
*          response by twitter. Use getLastWebResponse() for that.
 
*
 
*--*/
 
bool twitCurl::retweetById( std::string& statusId )
 
{
 
    bool retVal = false;
 
    if( statusId.length() )
 
    {
 
        /* Prepare URL */
 
        std::string buildUrl = twitterDefaults::TWITCURL_RETWEET_URL + statusId +
 
                               twitCurlDefaults::TWITCURL_EXTENSIONFORMATS[m_eApiFormatType];
 
 
        /* Send some dummy data in POST */
 
        std::string dummyData = twitCurlDefaults::TWITCURL_TEXTSTRING +
 
                                urlencode( std::string( "dummy" ) );
 
 
        /* Perform Retweet */
 
        retVal = performPost( buildUrl, dummyData );
 
    }
 
    return retVal;
 
}
 
 
/*++
 
* @method: twitCurl::timelinePublicGet
 
*
 
* @description: method to get public timeline
 
*
 
* @input: none
 
*
 
* @output: true if GET is success, otherwise false. This does not check http
 
*          response by twitter. Use getLastWebResponse() for that.
 
*
 
*--*/
 
bool twitCurl::timelinePublicGet()
backends/twitter/libtwitcurl/twitcurl.h
Show inline comments
 
@@ -52,24 +52,25 @@ namespace twitCurlDefaults
 
 
/* Default twitter URLs */
 
namespace twitterDefaults
 
{
 
 
    /* Search URLs */
 
    const std::string TWITCURL_SEARCH_URL = "http://search.twitter.com/search";
 
 
    /* Status URLs */
 
    const std::string TWITCURL_STATUSUPDATE_URL = "http://api.twitter.com/1/statuses/update";
 
    const std::string TWITCURL_STATUSSHOW_URL = "http://api.twitter.com/1/statuses/show/";
 
    const std::string TWITCURL_STATUDESTROY_URL = "http://api.twitter.com/1/statuses/destroy/";
 
	const std::string TWITCURL_RETWEET_URL = "http://api.twitter.com/1/statuses/retweet/";
 
 
    /* Timeline URLs */
 
    const std::string TWITCURL_HOME_TIMELINE_URL = "http://api.twitter.com/1/statuses/home_timeline";
 
    const std::string TWITCURL_PUBLIC_TIMELINE_URL = "http://api.twitter.com/1/statuses/public_timeline";
 
    const std::string TWITCURL_FEATURED_USERS_URL = "http://api.twitter.com/1/statuses/featured";
 
    const std::string TWITCURL_FRIENDS_TIMELINE_URL = "http://api.twitter.com/1/statuses/friends_timeline";
 
    const std::string TWITCURL_MENTIONS_URL = "http://api.twitter.com/1/statuses/mentions";
 
    const std::string TWITCURL_USERTIMELINE_URL = "http://api.twitter.com/1/statuses/user_timeline";
 
 
    /* Users URLs */
 
	const std::string TWITCURL_LOOKUPUSERS_URL = "http://api.twitter.com/1/users/lookup";
 
    const std::string TWITCURL_SHOWUSERS_URL = "http://api.twitter.com/1/users/show";
 
@@ -138,24 +139,25 @@ public:
 
    void setTwitterPassword( std::string& passWord /* in */ );
 
 
    /* Twitter API type */
 
    void setTwitterApiType( twitCurlTypes::eTwitCurlApiFormatType eType );
 
 
    /* Twitter search APIs */
 
    bool search( std::string& searchQuery /* in */ );
 
 
    /* Twitter status APIs */
 
    bool statusUpdate( std::string& newStatus /* in */ );
 
    bool statusShowById( std::string& statusId /* in */ );
 
    bool statusDestroyById( std::string& statusId /* in */ );
 
	bool retweetById( std::string& statusId /* in */);
 
 
    /* Twitter timeline APIs */
 
    bool timelineHomeGet(std::string sinceId = ""  /* in */);
 
    bool timelinePublicGet();
 
    bool timelineFriendsGet();
 
    bool timelineUserGet( bool trimUser /* in */, bool includeRetweets /* in */, unsigned int tweetCount /* in */, std::string userInfo = "" /* in */, bool isUserId = false /* in */ );
 
    bool featuredUsersGet();
 
    bool mentionsGet( std::string sinceId = "" /* in */ );
 
 
    /* Twitter user APIs */
 
	bool userLookup( std::vector<std::string> &userInfo /* in */,  bool isUserId = false /* in */);
 
    bool userGet( std::string& userInfo /* in */, bool isUserId = false /* in */ );
0 comments (0 inline, 0 general)