Follow/Unfollow users (single contact mode)

This commit is contained in:
Sarang Bharadwaj 2012-06-14 16:12:00 +05:30
parent b297329ba3
commit d7b1567e75
6 changed files with 162 additions and 0 deletions

View file

@ -0,0 +1,24 @@
#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);
}
}

View file

@ -0,0 +1,41 @@
#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

View file

@ -0,0 +1,24 @@
#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);
}
}

View file

@ -0,0 +1,40 @@
#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

View file

@ -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) {
}
}

View file

@ -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: