Web interface: working register/unregister

This commit is contained in:
Jan Kaluza 2016-01-20 17:48:17 +01:00
parent af6c160261
commit 91fae24a09
5 changed files with 70 additions and 3 deletions

View file

@ -179,6 +179,49 @@ void APIServer::serve_instances_unregister(Server *server, Server::session *sess
}
}
void APIServer::serve_instances_register(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);
std::string jid = get_http_var(hm, "jid");
std::string uin = get_http_var(hm, "uin");
std::string password = get_http_var(hm, "password");
if (jid.empty() || uin.empty() || password.empty()) {
send_ack(conn, true, "Insufficient data.");
}
else {
std::string response = server->send_command(instance, "register " + jid + " " + uin + " " + password);
if (!response.empty()) {
std::string value = jid;
int type = (int) TYPE_STRING;
m_storage->updateUserSetting(info.id, instance, value);
}
else {
send_ack(conn, true, response);
return;
}
Document json;
json.SetObject();
json.AddMember("error", false, json.GetAllocator());
response = server->send_command(instance, "get_oauth2_url " + jid);
if (!response.empty()) {
json.AddMember("oauth2_url", response.c_str(), 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);
@ -211,11 +254,14 @@ void APIServer::handleRequest(Server *server, Server::session *sess, struct mg_c
serve_instances_stop(server, sess, conn, hm);
}
else if (has_prefix(&hm->uri, "/api/v1/instances/unregister/")) {
serve_instances_stop(server, sess, conn, hm);
serve_instances_unregister(server, sess, conn, hm);
}
else if (has_prefix(&hm->uri, "/api/v1/instances/register_form/")) {
serve_instances_register_form(server, sess, conn, hm);
}
else if (has_prefix(&hm->uri, "/api/v1/instances/register/")) {
serve_instances_register(server, sess, conn, hm);
}
else if (mg_vcmp(&hm->uri, "/api/v1/instances") == 0) {
serve_instances(server, sess, conn, hm);
}

View file

@ -56,6 +56,7 @@ class APIServer {
void serve_instances_start(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
void serve_instances_stop(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm);
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 send_json(struct mg_connection *conn, const Document &d);
void send_ack(struct mg_connection *conn, bool error, const std::string &message);

View file

@ -18,7 +18,7 @@
</label>
<label>
<span>&nbsp;</span>
<input type="submit" class="button" value="Register" />
<input type="submit" class="button_command" value="Register" />
</label>
<input type="hidden" name="instance" id="instance" value=""></input>
</form><br/>

View file

@ -67,6 +67,21 @@ function getQueryParams(qs) {
function fill_instances_register_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 ={
"jid": $("#jid").val(),
"uin": $("#uin").val(),
"password": $("#password").val()
};
$.post($.cookie("base_location") + "api/v1/instances/register/" + $("#instance").val(), postdata, function(data) {
window.location.replace("index.shtml");
});
})
$.get($.cookie("base_location") + "api/v1/instances/register_form/" + query.id, function(data) {
$("#jid_desc").html(data.username_label + ":");

View file

@ -292,7 +292,12 @@ std::string Server::send_command(const std::string &jid, const std::string &cmd)
eventLoop.runOnce();
}
return get_response();
std::string response = get_response();
if (response == "Empty response") {
return "";
}
return response;
}
void Server::serve_onlineusers(struct mg_connection *conn, struct http_message *hm) {