diff --git a/spectrum_manager/src/APIServer.cpp b/spectrum_manager/src/APIServer.cpp index 5af0fccd..357535dd 100644 --- a/spectrum_manager/src/APIServer.cpp +++ b/spectrum_manager/src/APIServer.cpp @@ -119,6 +119,7 @@ void APIServer::serve_instances(Server *server, Server::session *session, struct usernames.push_back(username); instance.AddMember("registered", !username.empty(), json.GetAllocator()); instance.AddMember("username", usernames.back().c_str(), json.GetAllocator()); + instance.AddMember("frontend", is_slack(m_config, id) ? "slack" : "xmpp", json.GetAllocator()); instances.PushBack(instance, json.GetAllocator()); } @@ -221,6 +222,50 @@ void APIServer::serve_instances_register(Server *server, Server::session *sessio } } +void APIServer::serve_instances_join_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 name = get_http_var(hm, "name"); + std::string legacy_room = get_http_var(hm, "legacy_room"); + std::string legacy_server = get_http_var(hm, "legacy_server"); + std::string frontend_room = get_http_var(hm, "frontend_room"); + + std::string response = server->send_command(instance, "join_room " + + username + " " + name + " " + legacy_room + " " + legacy_server + " " + frontend_room); + + if (response.find("Joined 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; + json.SetObject(); + json.AddMember("error", 0, json.GetAllocator()); + json.AddMember("name_label", "Nickname in 3rd-party room", json.GetAllocator()); + json.AddMember("legacy_room_label", "3rd-party room name", json.GetAllocator()); + json.AddMember("legacy_server_label", "3rd-party server", json.GetAllocator()); + json.AddMember("frontend_room_label", "Slack channel", json.GetAllocator()); + send_json(conn, json); +} + void APIServer::serve_instances_register_form(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); @@ -330,6 +375,12 @@ void APIServer::handleRequest(Server *server, Server::session *sess, struct mg_c else if (has_prefix(&hm->uri, "/api/v1/instances/register/")) { serve_instances_register(server, sess, conn, hm); } + else if (has_prefix(&hm->uri, "/api/v1/instances/join_room_form/")) { + serve_instances_join_room_form(server, sess, conn, hm); + } + 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/users/remove/")) { serve_users_remove(server, sess, conn, hm); } diff --git a/spectrum_manager/src/APIServer.h b/spectrum_manager/src/APIServer.h index e649dd07..9971ecd0 100644 --- a/spectrum_manager/src/APIServer.h +++ b/spectrum_manager/src/APIServer.h @@ -58,6 +58,8 @@ 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_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); 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); diff --git a/spectrum_manager/src/html/instances/join_room.shtml b/spectrum_manager/src/html/instances/join_room.shtml new file mode 100644 index 00000000..a9bcbe29 --- /dev/null +++ b/spectrum_manager/src/html/instances/join_room.shtml @@ -0,0 +1,37 @@ + + +

Join room

+
+

Join room + Join the room using this Spectrum 2 instance. +

+ + + + + + +

+ + + + diff --git a/spectrum_manager/src/html/js/app.js b/spectrum_manager/src/html/js/app.js index b4098066..613049d9 100644 --- a/spectrum_manager/src/html/js/app.js +++ b/spectrum_manager/src/html/js/app.js @@ -34,7 +34,12 @@ function show_instances() { $("#main_result > tbody:last-child").append(row); } else { - row += '' + command + '' + ''; + row += ''; + if (command == 'unregister' && instance.frontend == "slack") { + row += 'Join room | '; + } + row += '' + command + ''; + row += ''; $("#main_result > tbody:last-child").append(row); $(".button_command").click(function(e) { e.preventDefault(); @@ -92,6 +97,39 @@ function getQueryParams(qs) { return params; } +function fill_instances_join_room_form() { + var query = getQueryParams(document.location.search); + $("#instance").attr("value", query.id); + + $(".button_command").click(function(e) { + e.preventDefault(); + $(this).parent().empty().progressbar( {value: false} ).css('height', '1em'); + + var postdata ={ + "name": $("#name").val(), + "legacy_room": $("#legacy_room").val(), + "legacy_server": $("#legacy_server").val() + "frontend_room": $("#frontend_room").val() + }; + + $.post($.cookie("base_location") + "api/v1/instances/join_room/" + $("#instance").val(), postdata, function(data) { + window.location.replace("index.shtml"); + }); + }) + + $.get($.cookie("base_location") + "api/v1/instances/join_room_form/" + query.id, function(data) { + $("#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 + ":"); + + $("#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 + ":"); + }); +} + function fill_instances_register_form() { var query = getQueryParams(document.location.search); $("#instance").attr("value", query.id);