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
 
@@ -5,12 +5,13 @@
 
#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
 

	
 
@@ -159,12 +160,14 @@ 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 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)));
 
@@ -362,13 +365,13 @@ void TwitterPlugin::displayFriendlist(std::string &user, std::vector<User> &frie
 
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
 
@@ -441,6 +444,15 @@ void TwitterPlugin::deleteFriendResponse(std::string &user, std::string &frnd, s
 
		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
 
@@ -99,12 +99,13 @@ class TwitterPlugin : public NetworkPlugin {
 
		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};
 

	
backends/twitter/libtwitcurl/twitcurl.cpp
Show inline comments
 
@@ -432,12 +432,42 @@ bool twitCurl::statusDestroyById( std::string& statusId )
 
        /* 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
backends/twitter/libtwitcurl/twitcurl.h
Show inline comments
 
@@ -58,12 +58,13 @@ namespace twitterDefaults
 
    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";
 
@@ -144,12 +145,13 @@ public:
 
    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 */ );
0 comments (0 inline, 0 general)