Changeset - d7b1567e7588
[Not reviewed]
0 2 4
Sarang Bharadwaj - 13 years ago 2012-06-14 12:42:00
sarang.bh@gmail.com
Follow/Unfollow users (single contact mode)
6 files changed with 162 insertions and 0 deletions:
0 comments (0 inline, 0 general)
backends/twitter/Requests/CreateFriendRequest.cpp
Show inline comments
 
new file 100644
 
#include "CreateFriendRequest.h"
 
DEFINE_LOGGER(logger, "CreateFriendRequest")
 

	
 
void CreateFriendRequest::run()
 
{
 
	replyMsg = "";
 
	success = twitObj->friendshipCreate(frnd, false);
 
	if(success) twitObj->getLastWebResponse(replyMsg);
 
}
 

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

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

	
 
using namespace Transport;
 

	
 
class CreateFriendRequest : public Thread
 
{
 
	twitCurl *twitObj;
 
	std::string user;
 
	std::string frnd;
 
	std::string replyMsg;
 
	boost::function< void (std::string&, std::string&, std::string&) > callBack;
 
	
 
	bool success;
 

	
 
	public:
 
	CreateFriendRequest(twitCurl *obj, const std::string &_user, const std::string & _frnd,
 
			     		 boost::function< void (std::string&, std::string&, std::string&) >  cb) {
 
		twitObj = obj->clone();
 
		user = _user;
 
		frnd = _frnd;
 
		callBack = cb;
 
	}
 

	
 
	~CreateFriendRequest() {
 
		delete twitObj;
 
	}
 

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

	
 
#endif
backends/twitter/Requests/DestroyFriendRequest.cpp
Show inline comments
 
new file 100644
 
#include "DestroyFriendRequest.h"
 
DEFINE_LOGGER(logger, "DestroyFriendRequest")
 

	
 
void DestroyFriendRequest::run()
 
{
 
	replyMsg = "";
 
	success = twitObj->friendshipDestroy(frnd, false);
 
	if(success) twitObj->getLastWebResponse(replyMsg);
 
}
 

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

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

	
 
using namespace Transport;
 

	
 
class DestroyFriendRequest : public Thread
 
{
 
	twitCurl *twitObj;
 
	std::string user;
 
	std::string frnd;
 
	std::string replyMsg;
 
	boost::function< void (std::string&, std::string&, std::string&) > callBack;
 
	bool success;
 

	
 
	public:
 
	DestroyFriendRequest(twitCurl *obj, const std::string &_user, const std::string & _frnd,
 
			     		 boost::function< void (std::string&, std::string&, std::string&) >  cb) {
 
		twitObj = obj->clone();
 
		user = _user;
 
		frnd = _frnd;
 
		callBack = cb;
 
	}
 

	
 
	~DestroyFriendRequest() {
 
		delete twitObj;
 
	}
 

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

	
 
#endif
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
@@ -6,6 +6,8 @@
 
#include "Requests/HelpMessageRequest.h"
 
#include "Requests/PINExchangeProcess.h"
 
#include "Requests/OAuthFlow.h"
 
#include "Requests/CreateFriendRequest.h"
 
#include "Requests/DestroyFriendRequest.h"
 

	
 
DEFINE_LOGGER(logger, "Twitter Backend");
 

	
 
@@ -156,6 +158,10 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
 
														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 handleMessage(user, "twitter-account", "Unknown command! Type #help for a list of available commands.");
 
	} 
 

	
 
@@ -413,3 +419,28 @@ void TwitterPlugin::directMessageResponse(std::string &user, std::vector<DirectM
 
		updateLastDMID(user, maxID);
 
	}
 
}
 

	
 
void TwitterPlugin::createFriendResponse(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 now following ") + frnd);
 
	} else if(twitterMode == MULTIPLECONTACT) {
 
		handleBuddyChanged(user, frnd, frnd, std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
	}
 
}
 

	
 
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) {
 
	}
 
}
backends/twitter/TwitterPlugin.h
Show inline comments
 
@@ -100,6 +100,8 @@ class TwitterPlugin : public NetworkPlugin {
 
		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);
 
		/***********************************************************************************/
 

	
 
	private:
0 comments (0 inline, 0 general)