Rename SlackInstallation to SlackSession. Allow SlackUser to move SlackSession from SlackUserManager once the SlackUser is created.

This commit is contained in:
Jan Kaluza 2015-11-25 19:20:19 +01:00
parent 6cfc7e7434
commit e0ea6762c7
6 changed files with 51 additions and 34 deletions

View file

@ -18,7 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "SlackInstallation.h"
#include "SlackSession.h"
#include "SlackFrontend.h"
#include "SlackUser.h"
#include "SlackRTM.h"
@ -40,25 +40,25 @@
namespace Transport {
DEFINE_LOGGER(logger, "SlackInstallation");
DEFINE_LOGGER(logger, "SlackSession");
SlackInstallation::SlackInstallation(Component *component, StorageBackend *storageBackend, UserInfo uinfo) : m_uinfo(uinfo) {
SlackSession::SlackSession(Component *component, StorageBackend *storageBackend, UserInfo uinfo) : m_uinfo(uinfo) {
m_component = component;
m_storageBackend = storageBackend;
m_rtm = new SlackRTM(component, storageBackend, uinfo);
m_rtm->onRTMStarted.connect(boost::bind(&SlackInstallation::handleRTMStarted, this));
m_rtm->onMessageReceived.connect(boost::bind(&SlackInstallation::handleMessageReceived, this, _1, _2, _3));
m_rtm->onRTMStarted.connect(boost::bind(&SlackSession::handleRTMStarted, this));
m_rtm->onMessageReceived.connect(boost::bind(&SlackSession::handleMessageReceived, this, _1, _2, _3));
// m_api = new SlackAPI(component, m_uinfo.encoding);
}
SlackInstallation::~SlackInstallation() {
SlackSession::~SlackSession() {
delete m_rtm;
// delete m_api;
}
void SlackInstallation::sendMessage(boost::shared_ptr<Swift::Message> message) {
void SlackSession::sendMessage(boost::shared_ptr<Swift::Message> message) {
LOG4CXX_INFO(logger, "SEND MESSAGE");
if (message->getFrom().getResource() == "myfavouritebot") {
return;
@ -66,7 +66,7 @@ void SlackInstallation::sendMessage(boost::shared_ptr<Swift::Message> message) {
m_rtm->getAPI()->sendMessage(message->getFrom().getResource(), m_jid2channel[message->getFrom().toBare().toString()], message->getBody());
}
void SlackInstallation::handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message) {
void SlackSession::handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message) {
if (m_ownerChannel != channel) {
std::string to = m_channel2jid[channel];
if (!to.empty()) {
@ -131,7 +131,7 @@ void SlackInstallation::handleMessageReceived(const std::string &channel, const
}
}
void SlackInstallation::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
void SlackSession::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
m_ownerChannel = m_rtm->getAPI()->getChannelId(req, ok, resp, data);
LOG4CXX_INFO(logger, "Opened channel with team owner: " << m_ownerChannel);
@ -143,7 +143,7 @@ void SlackInstallation::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Docum
m_rtm->sendMessage(m_ownerChannel, msg);
}
void SlackInstallation::handleRTMStarted() {
void SlackSession::handleRTMStarted() {
std::string ownerId;
std::map<std::string, SlackUserInfo> &users = m_rtm->getUsers();
for (std::map<std::string, SlackUserInfo>::iterator it = users.begin(); it != users.end(); it++) {
@ -154,7 +154,7 @@ void SlackInstallation::handleRTMStarted() {
}
}
m_rtm->getAPI()->imOpen(ownerId, boost::bind(&SlackInstallation::handleImOpen, this, _1, _2, _3, _4));
m_rtm->getAPI()->imOpen(ownerId, boost::bind(&SlackSession::handleImOpen, this, _1, _2, _3, _4));
}

View file

@ -39,11 +39,11 @@ class HTTPRequest;
class SlackRTM;
class SlackAPI;
class SlackInstallation {
class SlackSession {
public:
SlackInstallation(Component *component, StorageBackend *storageBackend, UserInfo uinfo);
SlackSession(Component *component, StorageBackend *storageBackend, UserInfo uinfo);
virtual ~SlackInstallation();
virtual ~SlackSession();
boost::signal<void (const std::string &user)> onInstallationDone;

View file

@ -20,6 +20,8 @@
#include "SlackUser.h"
#include "SlackFrontend.h"
#include "SlackSession.h"
#include "SlackUserManager.h"
#include "transport/Transport.h"
#include "transport/UserManager.h"
@ -41,9 +43,14 @@ SlackUser::SlackUser(const Swift::JID &jid, UserInfo &userInfo, Component *compo
m_component = component;
m_userManager = userManager;
m_userInfo = userInfo;
m_session = static_cast<SlackUserManager *>(userManager)->moveTempSession(m_jid.toString());
}
SlackUser::~SlackUser(){
if (m_session) {
delete m_session;
}
}
void SlackUser::disconnectUser(const std::string &error, Swift::SpectrumErrorPayload::Error e) {

View file

@ -32,6 +32,7 @@ class ConversationManager;
class UserManager;
class PresenceOracle;
struct UserInfo;
class SlackSession;
class SlackUser : public User {
public:
@ -41,11 +42,16 @@ class SlackUser : public User {
void disconnectUser(const std::string &error, Swift::SpectrumErrorPayload::Error e);
SlackSession *getSession() {
return m_session;
}
private:
Swift::JID m_jid;
Component *m_component;
UserManager *m_userManager;
UserInfo m_userInfo;
SlackSession *m_session;
};
}

View file

@ -21,7 +21,8 @@
#include "SlackUserManager.h"
#include "SlackUserRegistration.h"
#include "SlackFrontend.h"
#include "SlackInstallation.h"
#include "SlackSession.h"
#include "SlackUser.h"
#include "transport/User.h"
#include "transport/Transport.h"
@ -49,21 +50,8 @@ void SlackUserManager::reconnectUser(const std::string &user) {
return;
}
// if (!uinfo.uin.empty()) {
// LOG4CXX_INFO(logger, "Reconnecting user " << user);
// Swift::Presence::ref response = Swift::Presence::create();
// response->setTo(m_component->getJID());
// response->setFrom(user + "@" + m_component->getJID().toString());
// response->setType(Swift::Presence::Available);
// }
// else {
LOG4CXX_INFO(logger, "Cannot reconnect user " << user << ","
"because he does not have legacy network configured. "
"Continuing in Installation mode for this user until "
"he configures the legacy network.");
m_installations[user] = new SlackInstallation(m_component, m_storageBackend, uinfo);
m_installations[user]->onInstallationDone.connect(boost::bind(&SlackUserManager::reconnectUser, this, _1));
// }
LOG4CXX_INFO(logger, "Connecting user " << user << " to Slack network.");
m_tempSessions[user] = new SlackSession(m_component, m_storageBackend, uinfo);
}
void SlackUserManager::sendVCard(unsigned int id, Swift::VCard::ref vcard) {
@ -71,8 +59,22 @@ void SlackUserManager::sendVCard(unsigned int id, Swift::VCard::ref vcard) {
}
void SlackUserManager::sendMessage(boost::shared_ptr<Swift::Message> message) {
LOG4CXX_INFO(logger, message->getTo().toBare().toString());
m_installations[message->getTo().toBare().toString()]->sendMessage(message);
User *user = getUser(message->getTo().toBare().toString());
if (!user) {
LOG4CXX_ERROR(logger, "Received message for unknown user " << message->getTo().toBare().toString());
return;
}
static_cast<SlackUser *>(user)->getSession()->sendMessage(message);
}
SlackSession *SlackUserManager::moveTempSession(const std::string &user) {
if (m_tempSessions.find(user) != m_tempSessions.end()) {
SlackSession *session = m_tempSessions[user];
m_tempSessions.erase(user);
return session;
}
return NULL;
}

View file

@ -39,7 +39,7 @@ class XMPPUserRegistration;
class GatewayResponder;
class AdHocManager;
class SettingsAdHocCommandFactory;
class SlackInstallation;
class SlackSession;
class SlackUserManager : public UserManager {
public:
@ -59,11 +59,13 @@ class SlackUserManager : public UserManager {
void sendMessage(boost::shared_ptr<Swift::Message> message);
SlackSession *moveTempSession(const std::string &user);
private:
Component *m_component;
UserRegistration *m_userRegistration;
StorageBackend *m_storageBackend;
std::map<std::string, SlackInstallation *> m_installations;
std::map<std::string, SlackSession *> m_tempSessions;
};
}