Web interface list joined room for slack backend
This commit is contained in:
parent
4e8042885f
commit
0363e74dd6
5 changed files with 167 additions and 33 deletions
|
@ -30,6 +30,7 @@
|
|||
#include "transport/Logging.h"
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
namespace Transport {
|
||||
|
||||
|
@ -105,23 +106,24 @@ void SlackUserManager::handleUserCreated(User *user) {
|
|||
}
|
||||
|
||||
bool SlackUserManager::handleAdminMessage(Swift::Message::ref message) {
|
||||
if (message->getBody().find("get_user_configuration") == 0) {
|
||||
// std::string body = message->getBody();
|
||||
// std::vector<std::string> args;
|
||||
// boost::split(args, body, boost::is_any_of(" "));
|
||||
// if (args.size() == 2) {
|
||||
// UserInfo uinfo;
|
||||
// if (!m_storageBackend->getUser(args[1], uinfo)) {
|
||||
// message->setBody("Error: Unknown user");
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// std::string rooms = "";
|
||||
// int type = (int) TYPE_STRING;
|
||||
// m_storageBackend->getUserSetting(uinfo.id, "rooms", type, rooms);
|
||||
//
|
||||
// m_storageBackend->getUserSetting(uinfo.id, "slack_channel", type, m_slackChannel);
|
||||
// }
|
||||
if (message->getBody().find("list_rooms") == 0) {
|
||||
std::string body = message->getBody();
|
||||
std::vector<std::string> args;
|
||||
boost::split(args, body, boost::is_any_of(" "));
|
||||
if (args.size() == 2) {
|
||||
UserInfo uinfo;
|
||||
if (!m_storageBackend->getUser(args[1], uinfo)) {
|
||||
message->setBody("Error: Unknown user");
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string rooms = "";
|
||||
int type = (int) TYPE_STRING;
|
||||
m_storageBackend->getUserSetting(uinfo.id, "rooms", type, rooms);
|
||||
|
||||
message->setBody(rooms);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (message->getBody().find("join_room ") == 0) {
|
||||
std::string body = message->getBody();
|
||||
|
@ -148,6 +150,48 @@ bool SlackUserManager::handleAdminMessage(Swift::Message::ref message) {
|
|||
return true;
|
||||
}
|
||||
}
|
||||
else if (message->getBody().find("leave_room ") == 0) {
|
||||
std::string body = message->getBody();
|
||||
std::vector<std::string> args;
|
||||
boost::split(args, body, boost::is_any_of(" "));
|
||||
if (args.size() == 3) {
|
||||
UserInfo uinfo;
|
||||
if (!m_storageBackend->getUser(args[1], uinfo)) {
|
||||
message->setBody("Error: Unknown user");
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string rooms = "";
|
||||
int type = (int) TYPE_STRING;
|
||||
m_storageBackend->getUserSetting(uinfo.id, "rooms", type, rooms);
|
||||
|
||||
std::vector<std::string> commands;
|
||||
boost::split(commands, rooms, boost::is_any_of("\n"));
|
||||
rooms = "";
|
||||
|
||||
BOOST_FOREACH(const std::string &command, commands) {
|
||||
if (command.size() > 5) {
|
||||
std::vector<std::string> args2;
|
||||
boost::split(args2, command, boost::is_any_of(" "));
|
||||
if (args2.size() == 6) {
|
||||
if (args[2] != args2[5]) {
|
||||
rooms += command + "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_storageBackend->updateUserSetting(uinfo.id, "rooms", rooms);
|
||||
|
||||
SlackUser *user = static_cast<SlackUser *>(getUser(args[1]));
|
||||
if (user) {
|
||||
// TODO
|
||||
// user->getSession()->handleJoinMessage("", args, true);
|
||||
}
|
||||
message->setBody("Left the room");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -128,6 +128,54 @@ void APIServer::serve_instances(Server *server, Server::session *session, struct
|
|||
send_json(conn, json);
|
||||
}
|
||||
|
||||
void APIServer::serve_instances_list_rooms(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 response = server->send_command(instance, "list_rooms " + username);
|
||||
|
||||
std::vector<std::string> commands;
|
||||
boost::split(commands, response, boost::is_any_of("\n"));
|
||||
|
||||
Document json;
|
||||
json.SetObject();
|
||||
json.AddMember("error", 0, json.GetAllocator());
|
||||
|
||||
std::vector<std::vector<std::string> > tmp;
|
||||
Value rooms(kArrayType);
|
||||
BOOST_FOREACH(const std::string &command, commands) {
|
||||
if (command.size() > 5) {
|
||||
std::vector<std::string> args2;
|
||||
boost::split(args2, command, boost::is_any_of(" "));
|
||||
if (args2.size() == 6) {
|
||||
tmp.push_back(args2);
|
||||
Value room;
|
||||
room.SetObject();
|
||||
room.AddMember("name", tmp.back()[2].c_str(), json.GetAllocator());
|
||||
room.AddMember("legacy_room", tmp.back()[3].c_str(), json.GetAllocator());
|
||||
room.AddMember("legacy_server", tmp.back()[4].c_str(), json.GetAllocator());
|
||||
room.AddMember("frontend_room", tmp.back()[5].c_str(), json.GetAllocator());
|
||||
rooms.PushBack(room, json.GetAllocator());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
json.AddMember("rooms", rooms, json.GetAllocator());
|
||||
send_json(conn, json);
|
||||
}
|
||||
|
||||
void APIServer::serve_instances_start(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
|
||||
ALLOW_ONLY_ADMIN();
|
||||
|
||||
|
@ -381,6 +429,9 @@ void APIServer::handleRequest(Server *server, Server::session *sess, struct mg_c
|
|||
else if (has_prefix(&hm->uri, "/api/v1/instances/join_room/")) {
|
||||
serve_instances_join_room(server, sess, conn, hm);
|
||||
}
|
||||
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/users/remove/")) {
|
||||
serve_users_remove(server, sess, conn, hm);
|
||||
}
|
||||
|
|
|
@ -58,6 +58,7 @@ class APIServer {
|
|||
void serve_instances_unregister(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
|
||||
void serve_instances_register(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
|
||||
void serve_instances_register_form(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
|
||||
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_users(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
|
||||
|
|
9
spectrum_manager/src/html/instances/list_rooms.shtml
Normal file
9
spectrum_manager/src/html/instances/list_rooms.shtml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<!--#include virtual="/header.shtml" -->
|
||||
|
||||
<script type="text/javascript">
|
||||
$(function() {
|
||||
show_list_rooms();
|
||||
});
|
||||
</script>
|
||||
|
||||
<!--#include virtual="/footer.shtml" -->
|
|
@ -1,3 +1,17 @@
|
|||
function getQueryParams(qs) {
|
||||
qs = qs.split('+').join(' ');
|
||||
|
||||
var params = {},
|
||||
tokens,
|
||||
re = /[?&]?([^=]+)=([^&]*)/g;
|
||||
|
||||
while (tokens = re.exec(qs)) {
|
||||
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
function show_instances() {
|
||||
$.get($.cookie("base_location") + "api/v1/instances", function(data) {
|
||||
$("#main_content").html("<h2>List of Spectrum 2 instances</h2><table id='main_result'><tr><th>Name<th>Status</th><th>Actions</th></tr>");
|
||||
|
@ -37,6 +51,7 @@ function show_instances() {
|
|||
row += '<td>';
|
||||
if (command == 'unregister' && instance.frontend == "slack") {
|
||||
row += '<a href="' + $.cookie("base_location") + 'instances/join_room.shtml?id=' + instance.id + '">Join room</a> | ';
|
||||
row += '<a href="' + $.cookie("base_location") + 'instances/list_rooms.shtml?id=' + instance.id + '">List joined rooms</a> | ';
|
||||
}
|
||||
row += '<a class="button_command" href="' + $.cookie("base_location") + 'api/v1/instances/' + command + '/' + instance.id + '">' + command + '</a>';
|
||||
row += '</td></tr>';
|
||||
|
@ -55,6 +70,34 @@ function show_instances() {
|
|||
});
|
||||
}
|
||||
|
||||
function show_list_rooms() {
|
||||
var query = getQueryParams(document.location.search);
|
||||
$.get($.cookie("base_location") + "api/v1/instances/list_rooms/" + query.id, function(data) {
|
||||
$("#main_content").html("<h2>Joined rooms</h2><table id='main_result'><tr><th>" + data.frontend_room_label + "</th><th>" + data.legacy_room_label + "</th><th>" + data.legacy_server_label + "</th><th>" + data.name_label + "</th><th>Actions</th></tr>");
|
||||
|
||||
$.each(data.room, function(i, instance) {
|
||||
var row = '<tr>';
|
||||
row += '<td>' + room.frontend_room + '</td>';
|
||||
row += '<td>' + room.legacy_room + '</td>';
|
||||
row += '<td>' + room.legacy_server + '</td>';
|
||||
row += '<td>' + room.name + '</td>';
|
||||
row += '<td><a class="button_command" href="' + $.cookie("base_location") + 'api/v1/instances/leave_room/' + instance.id + '?frontend_room=' + room.frontend_room + '">Leave</a></td>';
|
||||
row += '</tr>';
|
||||
|
||||
$("#main_result > tbody:last-child").append(row);
|
||||
$(".button_command").click(function(e) {
|
||||
e.preventDefault();
|
||||
$(this).parent().empty().progressbar( {value: false} ).css('height', '1em');
|
||||
|
||||
var url = $(this).attr('href');
|
||||
$.get(url, function(data) {
|
||||
show_instances();
|
||||
});
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function show_users() {
|
||||
var admin = $.cookie("admin") == "1";
|
||||
if (!admin) {
|
||||
|
@ -83,20 +126,6 @@ function show_users() {
|
|||
});
|
||||
}
|
||||
|
||||
function getQueryParams(qs) {
|
||||
qs = qs.split('+').join(' ');
|
||||
|
||||
var params = {},
|
||||
tokens,
|
||||
re = /[?&]?([^=]+)=([^&]*)/g;
|
||||
|
||||
while (tokens = re.exec(qs)) {
|
||||
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
function fill_instances_join_room_form() {
|
||||
var query = getQueryParams(document.location.search);
|
||||
$("#instance").attr("value", query.id);
|
||||
|
@ -121,12 +150,12 @@ function fill_instances_join_room_form() {
|
|||
$("#name_desc").html(data.name_label + ":");
|
||||
$("#legacy_room_desc").html(data.legacy_room_label + ":");
|
||||
$("#legacy_server_desc").html(data.legacy_server_label + ":");
|
||||
$("#frontend_room_desc").html(data.frontend_room_desc + ":");
|
||||
$("#frontend_room_desc").html(data.frontend_room_label + ":");
|
||||
|
||||
$("#name").attr("placeholder", data.name_label + ":");
|
||||
$("#legacy_room").attr("placeholder", data.legacy_room_label + ":");
|
||||
$("#legacy_server").attr("placeholder", data.legacy_server_label + ":");
|
||||
$("#frontend_room").attr("placeholder", data.frontend_room_desc + ":");
|
||||
$("#frontend_room").attr("placeholder", data.frontend_room_label + ":");
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue