Changeset - efd60bd374ad
[Not reviewed]
0 4 0
Jan Kaluza - 9 years ago 2016-01-28 19:47:25
jkaluza@redhat.com
Slack: Add generic SlackAPI::createChannel to create slack channel and invite transport bot to it.
4 files changed with 46 insertions and 64 deletions:
0 comments (0 inline, 0 general)
spectrum/src/frontends/slack/SlackAPI.cpp
Show inline comments
 
@@ -353,6 +353,35 @@ std::string SlackAPI::SlackObjectToPlainText(const std::string &object, bool isC
 
	return ret;
 
}
 

	
 
void SlackAPI::handleSlackChannelInvite(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback) {
 
	callback(channel);
 
}
 

	
 
void SlackAPI::handleSlackChannelCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback) {
 
	std::string channelId = getChannelId(req, ok, resp, data);
 
	if (channelId.empty()) {
 
		LOG4CXX_INFO(logger, "Error creating channel " << channel << ".");
 
		return;
 
	}
 

	
 
	channelsInvite(channelId, user, boost::bind(&SlackAPI::handleSlackChannelInvite, this, _1, _2, _3, _4, channelId, user, callback));
 
}
 

	
 
void SlackAPI::handleSlackChannelList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback) {
 
	std::map<std::string, SlackChannelInfo> channels;
 
	SlackAPI::getSlackChannelInfo(req, ok, resp, data, channels);
 

	
 
	if (channels.find(channel) != channels.end()) {
 
		channelsInvite(channel, user, boost::bind(&SlackAPI::handleSlackChannelInvite, this, _1, _2, _3, _4, channels[channel].id, user, callback));
 
	}
 
	else {
 
		channelsCreate(channel, boost::bind(&SlackAPI::handleSlackChannelCreate, this, _1, _2, _3, _4, channel, user, callback));
 
	}
 
}
 

	
 
void SlackAPI::createChannel(const std::string &channel, const std::string &user, CreateChannelCallback callback) {
 
	channelsList(boost::bind(&SlackAPI::handleSlackChannelList, this, _1, _2, _3, _4, channel, user, callback));
 
}
 

	
 

	
 
}
spectrum/src/frontends/slack/SlackAPI.h
Show inline comments
 
@@ -91,9 +91,17 @@ class SlackAPI : public HTTPRequestQueue {
 
		static void getSlackUserInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map<std::string, SlackUserInfo> &users);
 
		static std::string SlackObjectToPlainText(const std::string &object, bool isChannel = false, bool returnName = false);
 

	
 
		// Creates channel if it does not exist and invites the user to the channel.
 
		typedef boost::function< void (const std::string &channelId) > CreateChannelCallback;
 
		void createChannel(const std::string &channel, const std::string &user, CreateChannelCallback callback);
 

	
 
	private:
 
		void handleSendMessage(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 

	
 
		void handleSlackChannelCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback);
 
		void handleSlackChannelList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback);
 
		void handleSlackChannelInvite(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &user, CreateChannelCallback callback);
 

	
 
	private:
 
		Component *m_component;
 
		std::string m_token;
spectrum/src/frontends/slack/SlackSession.cpp
Show inline comments
 
@@ -156,7 +156,8 @@ void SlackSession::setPurpose(const std::string &purpose, const std::string &cha
 
	m_api->setPurpose(ch, purpose);
 
}
 

	
 
void SlackSession::joinRoom(std::vector<std::string> args) {
 
void SlackSession::handleJoinRoomCreated(const std::string &channelId, std::vector<std::string> args) {
 
	args[5] = channelId;
 
	std::string &name = args[2];
 
	std::string &legacyRoom = args[3];
 
	std::string &legacyServer = args[4];
 
@@ -167,9 +168,6 @@ void SlackSession::joinRoom(std::vector<std::string> args) {
 
		m_uinfo.uin = name;
 
		m_storageBackend->setUser(m_uinfo);
 
	}
 
// 	else {
 
// 		to =  legacyRoom + "\\40" + legacyServer + "@" + m_component->getJID().toString();
 
// 	}
 

	
 
	m_jid2channel[to] = slackChannel;
 
	m_channel2jid[slackChannel] = to;
 
@@ -184,38 +182,14 @@ void SlackSession::joinRoom(std::vector<std::string> args) {
 
	m_onlineBuddiesTimer->start();
 
}
 

	
 
void SlackSession::handleJoinRoomCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::vector<std::string> args) {
 
	std::string channelId = m_api->getChannelId(req, ok, resp, data);
 
	if (channelId.empty()) {
 
		LOG4CXX_INFO(logger, args[1] << ": Error creating channel " << args[5] << ".");
 
		return;
 
	}
 

	
 
	args[5] = channelId;
 
	joinRoom(args);
 
}
 

	
 
void SlackSession::handleJoinRoomList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::vector<std::string> args) {
 
	std::map<std::string, SlackChannelInfo> channels;
 
	SlackAPI::getSlackChannelInfo(req, ok, resp, data, channels);
 

	
 
	if (channels.find(args[5]) != channels.end()) {
 
		LOG4CXX_INFO(logger, args[1] << ": Channel " << args[5] << " already exists. Joining the room.");
 
		args[5] = channels[args[5]].id;
 
		joinRoom(args);
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, args[1] << ": Channel " << args[5] << " does not exit. Creating it.");
 
		m_api->channelsCreate(args[5], boost::bind(&SlackSession::handleJoinRoomCreate, this, _1, _2, _3, _4, args));
 
	}
 
}
 

	
 
void SlackSession::handleJoinMessage(const std::string &message, std::vector<std::string> &args, bool quiet) {
 
	LOG4CXX_INFO(logger, args[1] << ": Going to join the room, checking the ID of channel " << args[5]);
 
	m_api->channelsList(boost::bind(&SlackSession::handleJoinRoomList, this, _1, _2, _3, _4, args));
 
	m_api->createChannel(args[5], m_rtm->getSelfId(), boost::bind(&SlackSession::handleJoinRoomCreated, this, _1, args));
 
}
 

	
 
void SlackSession::handleSlackChannelInvite(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
void SlackSession::handleSlackChannelCreated(const std::string &channelId) {
 
	m_slackChannel = channelId;
 

	
 
	Swift::Presence::ref presence = Swift::Presence::create();
 
	presence->setFrom(Swift::JID("", m_uinfo.jid, "default"));
 
	presence->setTo(m_component->getJID());
 
@@ -224,30 +198,6 @@ void SlackSession::handleSlackChannelInvite(HTTPRequest *req, bool ok, rapidjson
 
	m_component->getFrontend()->onPresenceReceived(presence);
 
}
 

	
 
void SlackSession::handleSlackChannelCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	std::string channelId = m_api->getChannelId(req, ok, resp, data);
 
	if (channelId.empty()) {
 
		LOG4CXX_INFO(logger,"Error creating channel " << m_slackChannel << ".");
 
		return;
 
	}
 

	
 
	m_slackChannel = channelId;
 
	m_api->channelsInvite(m_slackChannel, m_rtm->getSelfId(), boost::bind(&SlackSession::handleSlackChannelInvite, this, _1, _2, _3, _4));
 
}
 

	
 
void SlackSession::handleSlackChannelList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	std::map<std::string, SlackChannelInfo> channels;
 
	SlackAPI::getSlackChannelInfo(req, ok, resp, data, channels);
 

	
 
	if (channels.find(m_slackChannel) != channels.end()) {
 
		m_slackChannel = channels[m_slackChannel].id;
 
		m_api->channelsInvite(m_slackChannel, m_rtm->getSelfId(), boost::bind(&SlackSession::handleSlackChannelInvite, this, _1, _2, _3, _4));
 
	}
 
	else {
 
		m_api->channelsCreate(m_slackChannel, boost::bind(&SlackSession::handleSlackChannelCreate, this, _1, _2, _3, _4));
 
	}
 
}
 

	
 
void SlackSession::handleLeaveMessage(const std::string &message, std::vector<std::string> &args, bool quiet) {
 
	// .spectrum2 leave.room channel
 
	std::string slackChannel = SlackAPI::SlackObjectToPlainText(args[2], true);
 
@@ -485,7 +435,7 @@ void SlackSession::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document &
 
		else {
 
			m_storageBackend->getUserSetting(m_uinfo.id, "slack_channel", type, m_slackChannel);
 
			if (!m_slackChannel.empty()) {
 
				m_api->channelsList(boost::bind(&SlackSession::handleSlackChannelList, this, _1, _2, _3, _4));
 
				m_api->createChannel(m_slackChannel, m_rtm->getSelfId(), boost::bind(&SlackSession::handleSlackChannelCreated, this, _1));
 
			}
 
			else {
 
				std::string msg;
spectrum/src/frontends/slack/SlackSession.h
Show inline comments
 
@@ -69,13 +69,8 @@ class SlackSession {
 
		void handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message, const std::string &ts, bool quiet);
 
		void handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 

	
 
		void joinRoom(std::vector<std::string> args);
 
		void handleJoinRoomCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::vector<std::string> args);
 
		void handleJoinRoomList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::vector<std::string> args);
 

	
 
		void handleSlackChannelCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 
		void handleSlackChannelList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 
		void handleSlackChannelInvite(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
 
		void handleJoinRoomCreated(const std::string &channelId, std::vector<std::string> args);
 
		void handleSlackChannelCreated(const std::string &channelId);
 

	
 
		void sendOnlineBuddies();
 

	
0 comments (0 inline, 0 general)