From b55393bf9fb6329530975fc6fbcdf05c34d0787f Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Fri, 29 Jan 2016 13:45:28 +0100 Subject: [PATCH] Slack: Store channel/im/user name to ID mapping in SlackIdManager --- spectrum/src/frontends/slack/SlackAPI.cpp | 33 ++++-- spectrum/src/frontends/slack/SlackAPI.h | 4 +- .../src/frontends/slack/SlackIdManager.cpp | 77 +++++++++++++ spectrum/src/frontends/slack/SlackIdManager.h | 101 ++++++++++++++++++ spectrum/src/frontends/slack/SlackRTM.cpp | 24 ++--- spectrum/src/frontends/slack/SlackRTM.h | 29 +---- spectrum/src/frontends/slack/SlackSession.cpp | 51 +++------ spectrum/src/frontends/slack/SlackSession.h | 4 +- .../src/frontends/slack/SlackUserManager.cpp | 3 +- spectrum_manager/src/APIServer.cpp | 30 ++++++ spectrum_manager/src/APIServer.h | 1 + 11 files changed, 267 insertions(+), 90 deletions(-) create mode 100644 spectrum/src/frontends/slack/SlackIdManager.cpp create mode 100644 spectrum/src/frontends/slack/SlackIdManager.h diff --git a/spectrum/src/frontends/slack/SlackAPI.cpp b/spectrum/src/frontends/slack/SlackAPI.cpp index cbcbb106..7348fcd7 100644 --- a/spectrum/src/frontends/slack/SlackAPI.cpp +++ b/spectrum/src/frontends/slack/SlackAPI.cpp @@ -22,6 +22,7 @@ #include "SlackFrontend.h" #include "SlackUser.h" #include "SlackRTM.h" +#include "SlackIdManager.h" #include "transport/Transport.h" #include "transport/HTTPRequest.h" @@ -70,9 +71,10 @@ DEFINE_LOGGER(logger, "SlackAPI"); NAME = NAME##_tmp.GetString(); \ } -SlackAPI::SlackAPI(Component *component, const std::string &token) : HTTPRequestQueue(component) { +SlackAPI::SlackAPI(Component *component, SlackIdManager *idManager, const std::string &token) : HTTPRequestQueue(component) { m_component = component; m_token = token; + m_idManager = idManager; } SlackAPI::~SlackAPI() { @@ -353,34 +355,45 @@ 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) { +void SlackAPI::handleSlackChannelInvite(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &userId, 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) { +void SlackAPI::handleSlackChannelCreate(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &userId, 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)); + channelsInvite(channelId, userId, boost::bind(&SlackAPI::handleSlackChannelInvite, this, _1, _2, _3, _4, channelId, userId, 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 channels; +void SlackAPI::handleSlackChannelList(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, const std::string &channel, const std::string &userId, CreateChannelCallback callback) { + std::map &channels = m_idManager->getChannels(); 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)); + channelsInvite(channel, userId, boost::bind(&SlackAPI::handleSlackChannelInvite, this, _1, _2, _3, _4, channels[channel].id, userId, callback)); } else { - channelsCreate(channel, boost::bind(&SlackAPI::handleSlackChannelCreate, this, _1, _2, _3, _4, channel, user, callback)); + channelsCreate(channel, boost::bind(&SlackAPI::handleSlackChannelCreate, this, _1, _2, _3, _4, channel, userId, 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)); +void SlackAPI::createChannel(const std::string &channel, const std::string &userId, CreateChannelCallback callback) { + std::string channelId = m_idManager->getId(channel); + if (channelId != channel) { + if (m_idManager->hasMember(channelId, userId)) { + callback(channelId); + } + else { + channelsInvite(channel, userId, boost::bind(&SlackAPI::handleSlackChannelInvite, this, _1, _2, _3, _4, channelId, userId, callback)); + } + } + else { + channelsList(boost::bind(&SlackAPI::handleSlackChannelList, this, _1, _2, _3, _4, channel, userId, callback)); + } } diff --git a/spectrum/src/frontends/slack/SlackAPI.h b/spectrum/src/frontends/slack/SlackAPI.h index f32d4b45..9c4c315b 100644 --- a/spectrum/src/frontends/slack/SlackAPI.h +++ b/spectrum/src/frontends/slack/SlackAPI.h @@ -35,6 +35,7 @@ namespace Transport { class Component; class StorageBackend; class HTTPRequest; +class SlackIdManager; class SlackChannelInfo { public: @@ -68,7 +69,7 @@ class SlackUserInfo { class SlackAPI : public HTTPRequestQueue { public: - SlackAPI(Component *component, const std::string &token); + SlackAPI(Component *component, SlackIdManager *idManager, const std::string &token); virtual ~SlackAPI(); @@ -104,6 +105,7 @@ class SlackAPI : public HTTPRequestQueue { private: Component *m_component; + SlackIdManager *m_idManager; std::string m_token; }; diff --git a/spectrum/src/frontends/slack/SlackIdManager.cpp b/spectrum/src/frontends/slack/SlackIdManager.cpp new file mode 100644 index 00000000..65842c20 --- /dev/null +++ b/spectrum/src/frontends/slack/SlackIdManager.cpp @@ -0,0 +1,77 @@ +/** + * XMPP - libpurple transport + * + * Copyright (C) 2009, Jan Kaluza + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#include "SlackIdManager.h" +#include "SlackFrontend.h" +#include "SlackUser.h" + +#include "transport/Transport.h" +#include "transport/HTTPRequest.h" +#include "transport/Util.h" +#include "transport/WebSocketClient.h" + +#include +#include +#include +#include +#include +#include + +namespace Transport { + +DEFINE_LOGGER(logger, "SlackIdManager"); + +SlackIdManager::SlackIdManager() { + +} + +SlackIdManager::~SlackIdManager() { + +} + +const std::string &SlackIdManager::getName(const std::string &id) { + if (m_users.find(id) == m_users.end()) { + return id; + } + + return m_users[id].name; +} + +const std::string &SlackIdManager::getId(const std::string &name) { + for (std::map::const_iterator it = m_channels.begin(); it != m_channels.end(); ++it) { + if (it->second.name == name) { + return it->second.id; + } + } + + return name; +} + +bool SlackIdManager::hasMember(const std::string &channelId, const std::string &userId) { + if (m_channels.find(channelId) == m_channels.end()) { + return false; + } + + SlackChannelInfo &channel = m_channels[channelId]; + return std::find(channel.members.begin(), channel.members.end(), userId) != channel.members.end(); +} + + +} diff --git a/spectrum/src/frontends/slack/SlackIdManager.h b/spectrum/src/frontends/slack/SlackIdManager.h new file mode 100644 index 00000000..edcbfd59 --- /dev/null +++ b/spectrum/src/frontends/slack/SlackIdManager.h @@ -0,0 +1,101 @@ +/** + * Spectrum 2 Slack Frontend + * + * Copyright (C) 2015, Jan Kaluza + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#pragma once + +#include "SlackAPI.h" + +#include "transport/StorageBackend.h" +#include "rapidjson/document.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Swiften/Network/Timer.h" +#include "Swiften/Version.h" + +#define HAVE_SWIFTEN_3 (SWIFTEN_VERSION >= 0x030000) + +#if HAVE_SWIFTEN_3 +#include +#endif + +#include +#include +#include + +#include + +namespace Transport { + +class SlackIdManager { + public: + SlackIdManager(); + + virtual ~SlackIdManager(); + + std::map &getUsers() { + return m_users; + } + + std::map &getChannels() { + return m_channels; + } + + std::map &getIMs() { + return m_ims; + } + + const std::string &getName(const std::string &id); + const std::string &getId(const std::string &name); + + bool hasMember(const std::string &channelId, const std::string &userId); + + const std::string &getSelfName() { + return m_selfName; + } + + const std::string &getSelfId() { + return m_selfId; + } + + void setSelfName(const std::string &name) { + m_selfName = name; + } + + void setSelfId(const std::string &id) { + m_selfId = id; + } + + private: + std::map m_channels; + std::map m_ims; + std::map m_users; + std::string m_selfName; + std::string m_selfId; +}; + +} diff --git a/spectrum/src/frontends/slack/SlackRTM.cpp b/spectrum/src/frontends/slack/SlackRTM.cpp index 34825636..ed69af49 100644 --- a/spectrum/src/frontends/slack/SlackRTM.cpp +++ b/spectrum/src/frontends/slack/SlackRTM.cpp @@ -21,6 +21,7 @@ #include "SlackRTM.h" #include "SlackFrontend.h" #include "SlackUser.h" +#include "SlackIdManager.h" #include "transport/Transport.h" #include "transport/HTTPRequest.h" @@ -38,11 +39,12 @@ namespace Transport { DEFINE_LOGGER(logger, "SlackRTM"); -SlackRTM::SlackRTM(Component *component, StorageBackend *storageBackend, UserInfo uinfo) : m_uinfo(uinfo) { +SlackRTM::SlackRTM(Component *component, StorageBackend *storageBackend, SlackIdManager *idManager, UserInfo uinfo) : m_uinfo(uinfo) { m_component = component; m_storageBackend = storageBackend; m_counter = 0; m_started = false; + m_idManager = idManager; m_client = new WebSocketClient(component); m_client->onPayloadReceived.connect(boost::bind(&SlackRTM::handlePayloadReceived, this, _1)); m_client->onWebSocketConnected.connect(boost::bind(&SlackRTM::handleWebSocketConnected, this)); @@ -53,7 +55,7 @@ SlackRTM::SlackRTM(Component *component, StorageBackend *storageBackend, UserInf int type = (int) TYPE_STRING; m_storageBackend->getUserSetting(m_uinfo.id, "bot_token", type, m_token); - m_api = new SlackAPI(component, m_token); + m_api = new SlackAPI(component, m_idManager, m_token); std::string url = "https://slack.com/api/rtm.start?"; url += "token=" + Util::urlencode(m_token); @@ -146,14 +148,6 @@ void SlackRTM::sendPing() { m_pingTimer->start(); } -const std::string &SlackRTM::getUserName(const std::string &id) { - if (m_users.find(id) == m_users.end()) { - return id; - } - - return m_users[id].name; -} - void SlackRTM::handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) { if (!ok) { LOG4CXX_ERROR(logger, req->getError()); @@ -182,7 +176,7 @@ void SlackRTM::handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &re return; } - m_selfName = selfName.GetString(); + m_idManager->setSelfName(selfName.GetString()); rapidjson::Value &selfId = self["id"]; if (!selfId.IsString()) { @@ -191,11 +185,11 @@ void SlackRTM::handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &re return; } - m_selfId = selfId.GetString(); + m_idManager->setSelfId(selfId.GetString()); - SlackAPI::getSlackChannelInfo(req, ok, resp, data, m_channels); - SlackAPI::getSlackImInfo(req, ok, resp, data, m_ims); - SlackAPI::getSlackUserInfo(req, ok, resp, data, m_users); + SlackAPI::getSlackChannelInfo(req, ok, resp, data, m_idManager->getChannels()); + SlackAPI::getSlackImInfo(req, ok, resp, data, m_idManager->getIMs()); + SlackAPI::getSlackUserInfo(req, ok, resp, data, m_idManager->getUsers()); std::string u = url.GetString(); LOG4CXX_INFO(logger, "Started RTM, WebSocket URL is " << u); diff --git a/spectrum/src/frontends/slack/SlackRTM.h b/spectrum/src/frontends/slack/SlackRTM.h index 3b9a8f96..9ede1e68 100644 --- a/spectrum/src/frontends/slack/SlackRTM.h +++ b/spectrum/src/frontends/slack/SlackRTM.h @@ -56,10 +56,11 @@ class StorageBackend; class HTTPRequest; class WebSocketClient; class SlackAPI; +class SlackIdManager; class SlackRTM { public: - SlackRTM(Component *component, StorageBackend *storageBackend, UserInfo uinfo); + SlackRTM(Component *component, StorageBackend *storageBackend, SlackIdManager *idManager, UserInfo uinfo); virtual ~SlackRTM(); @@ -69,43 +70,18 @@ class SlackRTM { boost::signal onRTMStarted; - std::map &getUsers() { - return m_users; - } - - std::map &getChannels() { - return m_channels; - } - SlackAPI *getAPI() { return m_api; } boost::signal onMessageReceived; - const std::string &getUserName(const std::string &id); - - const std::string &getSelfName() { - return m_selfName; - } - - const std::string &getSelfId() { - return m_selfId; - } - private: void handlePayloadReceived(const std::string &payload); void handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data); void handleWebSocketConnected(); void handleWebSocketDisconnected(const boost::optional &error); - private: - std::map m_channels; - std::map m_ims; - std::map m_users; - std::string m_selfName; - std::string m_selfId; - private: Component *m_component; StorageBackend *m_storageBackend; @@ -116,6 +92,7 @@ class SlackRTM { Swift::Timer::ref m_pingTimer; SlackAPI *m_api; bool m_started; + SlackIdManager *m_idManager; }; } diff --git a/spectrum/src/frontends/slack/SlackSession.cpp b/spectrum/src/frontends/slack/SlackSession.cpp index ef1ac05f..756bf7a3 100644 --- a/spectrum/src/frontends/slack/SlackSession.cpp +++ b/spectrum/src/frontends/slack/SlackSession.cpp @@ -23,6 +23,7 @@ #include "SlackUser.h" #include "SlackRTM.h" #include "SlackRosterManager.h" +#include "SlackIdManager.h" #include "transport/Transport.h" #include "transport/HTTPRequest.h" @@ -50,7 +51,9 @@ SlackSession::SlackSession(Component *component, StorageBackend *storageBackend, m_component = component; m_storageBackend = storageBackend; - m_rtm = new SlackRTM(component, storageBackend, uinfo); + m_idManager = new SlackIdManager(); + + m_rtm = new SlackRTM(component, storageBackend, m_idManager, uinfo); m_rtm->onRTMStarted.connect(boost::bind(&SlackSession::handleRTMStarted, this)); m_rtm->onMessageReceived.connect(boost::bind(&SlackSession::handleMessageReceived, this, _1, _2, _3, _4, false)); @@ -60,12 +63,13 @@ SlackSession::SlackSession(Component *component, StorageBackend *storageBackend, int type = (int) TYPE_STRING; std::string token; m_storageBackend->getUserSetting(m_uinfo.id, "access_token", type, token); - m_api = new SlackAPI(m_component, token); + m_api = new SlackAPI(m_component, m_idManager, token); } SlackSession::~SlackSession() { delete m_rtm; delete m_api; + delete m_idManager; m_onlineBuddiesTimer->stop(); } @@ -184,7 +188,7 @@ void SlackSession::handleJoinRoomCreated(const std::string &channelId, std::vect void SlackSession::handleJoinMessage(const std::string &message, std::vector &args, bool quiet) { LOG4CXX_INFO(logger, args[1] << ": Going to join the room, checking the ID of channel " << args[5]); - m_api->createChannel(args[5], m_rtm->getSelfId(), boost::bind(&SlackSession::handleJoinRoomCreated, this, _1, args)); + m_api->createChannel(args[5], m_idManager->getSelfId(), boost::bind(&SlackSession::handleJoinRoomCreated, this, _1, args)); } void SlackSession::handleSlackChannelCreated(const std::string &channelId) { @@ -198,37 +202,14 @@ void SlackSession::handleSlackChannelCreated(const std::string &channelId) { m_component->getFrontend()->onPresenceReceived(presence); } -void SlackSession::handleLeaveMessage(const std::string &message, std::vector &args, bool quiet) { - // .spectrum2 leave.room channel - std::string slackChannel = SlackAPI::SlackObjectToPlainText(args[2], true); - std::string to = m_channel2jid[slackChannel]; +void SlackSession::leaveRoom(const std::string &channel) { + std::string channelId = m_idManager->getId(channel); + std::string to = m_channel2jid[channel]; if (to.empty()) { m_rtm->sendMessage(m_ownerChannel, "Spectrum 2 is not configured to transport this Slack channel."); return; } - std::string rooms = ""; - int type = (int) TYPE_STRING; - m_storageBackend->getUserSetting(m_uinfo.id, "rooms", type, rooms); - - std::vector commands; - boost::split(commands, rooms, boost::is_any_of("\n")); - rooms = ""; - - BOOST_FOREACH(const std::string &command, commands) { - if (command.size() > 5) { - std::vector args2; - boost::split(args2, command, boost::is_any_of(" ")); - if (args2.size() == 6) { - if (slackChannel != SlackAPI::SlackObjectToPlainText(args2[5], true)) { - rooms += command + "\n"; - } - } - } - } - - m_storageBackend->updateUserSetting(m_uinfo.id, "rooms", rooms); - Swift::Presence::ref presence = Swift::Presence::create(); presence->setFrom(Swift::JID("", m_uinfo.jid, "default")); presence->setTo(Swift::JID(to + "/" + m_uinfo.uin)); @@ -264,7 +245,7 @@ void SlackSession::handleRegisterMessage(const std::string &message, std::vector void SlackSession::handleMessageReceived(const std::string &channel, const std::string &user, const std::string &message, const std::string &ts, bool quiet) { if (m_ownerChannel != channel) { std::string to = m_channel2jid[channel]; - if (m_rtm->getUserName(user) == m_rtm->getSelfName()) { + if (m_idManager->getName(user) == m_idManager->getSelfName()) { return; } @@ -273,7 +254,7 @@ void SlackSession::handleMessageReceived(const std::string &channel, const std:: msg->setType(Swift::Message::Groupchat); msg->setTo(to); msg->setFrom(Swift::JID("", m_uinfo.jid, "default")); - msg->setBody("<" + m_rtm->getUserName(user) + "> " + message); + msg->setBody("<" + m_idManager->getName(user) + "> " + message); m_component->getFrontend()->onMessageReceived(msg); } else { @@ -309,7 +290,7 @@ void SlackSession::handleMessageReceived(const std::string &channel, const std:: boost::shared_ptr msg(new Swift::Message()); msg->setTo(b->getJID()); msg->setFrom(Swift::JID("", m_uinfo.jid, "default")); - msg->setBody("<" + m_rtm->getUserName(user) + "> " + message); + msg->setBody("<" + m_idManager->getName(user) + "> " + message); m_component->getFrontend()->onMessageReceived(msg); } } @@ -328,7 +309,7 @@ void SlackSession::handleMessageReceived(const std::string &channel, const std:: handleJoinMessage(message, args, quiet); } else if (args[1] == "leave.room" && args.size() == 3) { - handleLeaveMessage(message, args, quiet); +// handleLeaveMessage(message, args, quiet); } else if (args[1] == "register" && args.size() == 5) { handleRegisterMessage(message, args, quiet); @@ -435,7 +416,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->createChannel(m_slackChannel, m_rtm->getSelfId(), boost::bind(&SlackSession::handleSlackChannelCreated, this, _1)); + m_api->createChannel(m_slackChannel, m_idManager->getSelfId(), boost::bind(&SlackSession::handleSlackChannelCreated, this, _1)); } else { std::string msg; @@ -465,7 +446,7 @@ void SlackSession::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document & } void SlackSession::handleRTMStarted() { - std::map &users = m_rtm->getUsers(); + std::map &users = m_idManager->getUsers(); for (std::map::iterator it = users.begin(); it != users.end(); it++) { SlackUserInfo &info = it->second; if (info.isPrimaryOwner) { diff --git a/spectrum/src/frontends/slack/SlackSession.h b/spectrum/src/frontends/slack/SlackSession.h index e917d25a..7babf6eb 100644 --- a/spectrum/src/frontends/slack/SlackSession.h +++ b/spectrum/src/frontends/slack/SlackSession.h @@ -40,6 +40,7 @@ class HTTPRequest; class SlackRTM; class SlackAPI; class User; +class SlackIdManager; class SlackSession { public: @@ -61,7 +62,7 @@ class SlackSession { void handleConnected(); void handleJoinMessage(const std::string &message, std::vector &args, bool quiet = false); - void handleLeaveMessage(const std::string &message, std::vector &args, bool quiet = false); + void leaveRoom(const std::string &channel); void handleRegisterMessage(const std::string &message, std::vector &args, bool quiet = false); private: @@ -90,6 +91,7 @@ class SlackSession { bool m_disconnected; std::string m_ownerId; SlackAPI *m_api; + SlackIdManager *m_idManager; }; } diff --git a/spectrum/src/frontends/slack/SlackUserManager.cpp b/spectrum/src/frontends/slack/SlackUserManager.cpp index 6e687b03..4a8dbb25 100644 --- a/spectrum/src/frontends/slack/SlackUserManager.cpp +++ b/spectrum/src/frontends/slack/SlackUserManager.cpp @@ -185,8 +185,7 @@ bool SlackUserManager::handleAdminMessage(Swift::Message::ref message) { SlackUser *user = static_cast(getUser(args[1])); if (user) { - // TODO -// user->getSession()->handleJoinMessage("", args, true); + user->getSession()->leaveRoom(args[2]); } message->setBody("Left the room"); return true; diff --git a/spectrum_manager/src/APIServer.cpp b/spectrum_manager/src/APIServer.cpp index 1a83aaa2..1b51e95b 100644 --- a/spectrum_manager/src/APIServer.cpp +++ b/spectrum_manager/src/APIServer.cpp @@ -306,6 +306,33 @@ void APIServer::serve_instances_join_room(Server *server, Server::session *sessi } } +void APIServer::serve_instances_leave_room(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) { + std::string uri(hm->uri.p, hm->uri.len); + std::string instance = uri.substr(uri.rfind("/") + 1); + + UserInfo info; + m_storage->getUser(session->user, info); + + std::string username = ""; + int type = (int) TYPE_STRING; + m_storage->getUserSetting(info.id, instance, type, username); + + if (username.empty()) { + send_ack(conn, true, "You are not registered to this Spectrum 2 instance."); + return; + } + + std::string frontend_room = get_http_var(hm, "frontend_room"); + std::string response = server->send_command(instance, "leave_room " + username + " " + frontend_room); + + if (response.find("Left the room") == std::string::npos) { + send_ack(conn, true, response); + } + else { + send_ack(conn, false, response); + } +} + void APIServer::serve_instances_join_room_form(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) { // So far we support just Slack here. For XMPP, it is up to user to initiate the join room request. Document json; @@ -436,6 +463,9 @@ void APIServer::handleRequest(Server *server, Server::session *sess, struct mg_c else if (has_prefix(&hm->uri, "/api/v1/instances/list_rooms/")) { serve_instances_list_rooms(server, sess, conn, hm); } + else if (has_prefix(&hm->uri, "/api/v1/instances/leave_room/")) { + serve_instances_leave_room(server, sess, conn, hm); + } else if (has_prefix(&hm->uri, "/api/v1/users/remove/")) { serve_users_remove(server, sess, conn, hm); } diff --git a/spectrum_manager/src/APIServer.h b/spectrum_manager/src/APIServer.h index f81da9c3..4ec49515 100644 --- a/spectrum_manager/src/APIServer.h +++ b/spectrum_manager/src/APIServer.h @@ -61,6 +61,7 @@ class APIServer { void serve_instances_list_rooms(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); void serve_instances_join_room(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); void serve_instances_join_room_form(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); + void serve_instances_leave_room(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); void serve_users(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); void serve_users_add(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); void serve_users_remove(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);