fixes async_handler_wrapper (#5001)
* fixes async_handler_wrapper adds missing runner variable from async to async_handler_wrapper removes redundant have_async definition in wrap_async_run, defined at top of module * only use async handler wrapper, remove async_wrap_run
This commit is contained in:
parent
1cde7e63c7
commit
710307725b
|
@ -11,7 +11,7 @@ if not have_async then
|
||||||
return;
|
return;
|
||||||
end
|
end
|
||||||
|
|
||||||
local wrap_async_run = module:require "util".wrap_async_run;
|
local async_handler_wrapper = module:require "util".async_handler_wrapper;
|
||||||
|
|
||||||
-- Options
|
-- Options
|
||||||
local poltergeist_component
|
local poltergeist_component
|
||||||
|
@ -175,7 +175,7 @@ module:hook(
|
||||||
-- @return GET response, containing a json with response details
|
-- @return GET response, containing a json with response details
|
||||||
function handle_create_poltergeist (event)
|
function handle_create_poltergeist (event)
|
||||||
if (not event.request.url.query) then
|
if (not event.request.url.query) then
|
||||||
return 400;
|
return { status_code = 400; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local params = parse(event.request.url.query);
|
local params = parse(event.request.url.query);
|
||||||
|
@ -189,7 +189,7 @@ function handle_create_poltergeist (event)
|
||||||
local session = {};
|
local session = {};
|
||||||
|
|
||||||
if not verify_token(params["token"], room_name, group, session) then
|
if not verify_token(params["token"], room_name, group, session) then
|
||||||
return 403;
|
return { status_code = 403; };
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If the provided room conference doesn't exist then we
|
-- If the provided room conference doesn't exist then we
|
||||||
|
@ -197,7 +197,7 @@ function handle_create_poltergeist (event)
|
||||||
local room = get_room(room_name, group);
|
local room = get_room(room_name, group);
|
||||||
if (not room) then
|
if (not room) then
|
||||||
log("error", "no room found %s", room_name);
|
log("error", "no room found %s", room_name);
|
||||||
return 404;
|
return { status_code = 404; };
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If the poltergiest is already in the conference then it will
|
-- If the poltergiest is already in the conference then it will
|
||||||
|
@ -210,7 +210,7 @@ function handle_create_poltergeist (event)
|
||||||
username,
|
username,
|
||||||
room_name
|
room_name
|
||||||
);
|
);
|
||||||
return 202;
|
return { status_code = 202; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local context = {
|
local context = {
|
||||||
|
@ -228,7 +228,7 @@ function handle_create_poltergeist (event)
|
||||||
end
|
end
|
||||||
|
|
||||||
poltergeist.add_to_muc(room, user_id, name, avatar, context, status, resources)
|
poltergeist.add_to_muc(room, user_id, name, avatar, context, status, resources)
|
||||||
return 200;
|
return { status_code = 200; };
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Handles request for updating poltergeists status
|
--- Handles request for updating poltergeists status
|
||||||
|
@ -236,7 +236,7 @@ end
|
||||||
-- @return GET response, containing a json with response details
|
-- @return GET response, containing a json with response details
|
||||||
function handle_update_poltergeist (event)
|
function handle_update_poltergeist (event)
|
||||||
if (not event.request.url.query) then
|
if (not event.request.url.query) then
|
||||||
return 400;
|
return { status_code = 400; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local params = parse(event.request.url.query);
|
local params = parse(event.request.url.query);
|
||||||
|
@ -252,18 +252,18 @@ function handle_update_poltergeist (event)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not verify_token(params["token"], room_name, group, {}) then
|
if not verify_token(params["token"], room_name, group, {}) then
|
||||||
return 403;
|
return { status_code = 403; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local room = get_room(room_name, group);
|
local room = get_room(room_name, group);
|
||||||
if (not room) then
|
if (not room) then
|
||||||
log("error", "no room found %s", room_name);
|
log("error", "no room found %s", room_name);
|
||||||
return 404;
|
return { status_code = 404; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local username = poltergeist.get_username(room, user_id);
|
local username = poltergeist.get_username(room, user_id);
|
||||||
if (not username) then
|
if (not username) then
|
||||||
return 404;
|
return { status_code = 404; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local call_details = {
|
local call_details = {
|
||||||
|
@ -273,11 +273,11 @@ function handle_update_poltergeist (event)
|
||||||
|
|
||||||
local nick = poltergeist.create_nick(username);
|
local nick = poltergeist.create_nick(username);
|
||||||
if (not poltergeist.occupies(room, nick)) then
|
if (not poltergeist.occupies(room, nick)) then
|
||||||
return 404;
|
return { status_code = 404; };
|
||||||
end
|
end
|
||||||
|
|
||||||
poltergeist.update(room, nick, status, call_details);
|
poltergeist.update(room, nick, status, call_details);
|
||||||
return 200;
|
return { status_code = 200; };
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Handles remove poltergeists
|
--- Handles remove poltergeists
|
||||||
|
@ -285,7 +285,7 @@ end
|
||||||
-- @return GET response, containing a json with response details
|
-- @return GET response, containing a json with response details
|
||||||
function handle_remove_poltergeist (event)
|
function handle_remove_poltergeist (event)
|
||||||
if (not event.request.url.query) then
|
if (not event.request.url.query) then
|
||||||
return 400;
|
return { status_code = 400; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local params = parse(event.request.url.query);
|
local params = parse(event.request.url.query);
|
||||||
|
@ -294,27 +294,27 @@ function handle_remove_poltergeist (event)
|
||||||
local group = params["group"];
|
local group = params["group"];
|
||||||
|
|
||||||
if not verify_token(params["token"], room_name, group, {}) then
|
if not verify_token(params["token"], room_name, group, {}) then
|
||||||
return 403;
|
return { status_code = 403; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local room = get_room(room_name, group);
|
local room = get_room(room_name, group);
|
||||||
if (not room) then
|
if (not room) then
|
||||||
log("error", "no room found %s", room_name);
|
log("error", "no room found %s", room_name);
|
||||||
return 404;
|
return { status_code = 404; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local username = poltergeist.get_username(room, user_id);
|
local username = poltergeist.get_username(room, user_id);
|
||||||
if (not username) then
|
if (not username) then
|
||||||
return 404;
|
return { status_code = 404; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local nick = poltergeist.create_nick(username);
|
local nick = poltergeist.create_nick(username);
|
||||||
if (not poltergeist.occupies(room, nick)) then
|
if (not poltergeist.occupies(room, nick)) then
|
||||||
return 404;
|
return { status_code = 404; };
|
||||||
end
|
end
|
||||||
|
|
||||||
poltergeist.remove(room, nick, false);
|
poltergeist.remove(room, nick, false);
|
||||||
return 200;
|
return { status_code = 200; };
|
||||||
end
|
end
|
||||||
|
|
||||||
log("info", "Loading poltergeist service");
|
log("info", "Loading poltergeist service");
|
||||||
|
@ -323,8 +323,8 @@ module:provides("http", {
|
||||||
default_path = "/";
|
default_path = "/";
|
||||||
name = "poltergeist";
|
name = "poltergeist";
|
||||||
route = {
|
route = {
|
||||||
["GET /poltergeist/create"] = function (event) return wrap_async_run(event,handle_create_poltergeist) end;
|
["GET /poltergeist/create"] = function (event) return async_handler_wrapper(event,handle_create_poltergeist) end;
|
||||||
["GET /poltergeist/update"] = function (event) return wrap_async_run(event,handle_update_poltergeist) end;
|
["GET /poltergeist/update"] = function (event) return async_handler_wrapper(event,handle_update_poltergeist) end;
|
||||||
["GET /poltergeist/remove"] = function (event) return wrap_async_run(event,handle_remove_poltergeist) end;
|
["GET /poltergeist/remove"] = function (event) return async_handler_wrapper(event,handle_remove_poltergeist) end;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
@ -14,7 +14,7 @@ if not have_async then
|
||||||
return;
|
return;
|
||||||
end
|
end
|
||||||
|
|
||||||
local wrap_async_run = module:require "util".wrap_async_run;
|
local async_handler_wrapper = module:require "util".async_handler_wrapper;
|
||||||
|
|
||||||
local tostring = tostring;
|
local tostring = tostring;
|
||||||
local neturl = require "net.url";
|
local neturl = require "net.url";
|
||||||
|
@ -79,7 +79,7 @@ end
|
||||||
-- tha value is without counting the focus.
|
-- tha value is without counting the focus.
|
||||||
function handle_get_room_size(event)
|
function handle_get_room_size(event)
|
||||||
if (not event.request.url.query) then
|
if (not event.request.url.query) then
|
||||||
return 400;
|
return { status_code = 400; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local params = parse(event.request.url.query);
|
local params = parse(event.request.url.query);
|
||||||
|
@ -95,7 +95,7 @@ function handle_get_room_size(event)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not verify_token(params["token"], room_address) then
|
if not verify_token(params["token"], room_address) then
|
||||||
return 403;
|
return { status_code = 403; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local room = get_room_from_jid(room_address);
|
local room = get_room_from_jid(room_address);
|
||||||
|
@ -112,14 +112,14 @@ function handle_get_room_size(event)
|
||||||
"there are %s occupants in room", tostring(participant_count));
|
"there are %s occupants in room", tostring(participant_count));
|
||||||
else
|
else
|
||||||
log("debug", "no such room exists");
|
log("debug", "no such room exists");
|
||||||
return 404;
|
return { status_code = 404; };
|
||||||
end
|
end
|
||||||
|
|
||||||
if participant_count > 1 then
|
if participant_count > 1 then
|
||||||
participant_count = participant_count - 1;
|
participant_count = participant_count - 1;
|
||||||
end
|
end
|
||||||
|
|
||||||
return [[{"participants":]]..participant_count..[[}]];
|
return { status_code = 200; body = [[{"participants":]]..participant_count..[[}]] };
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Handles request for retrieving the room participants details
|
--- Handles request for retrieving the room participants details
|
||||||
|
@ -127,7 +127,7 @@ end
|
||||||
-- @return GET response, containing a json with participants details
|
-- @return GET response, containing a json with participants details
|
||||||
function handle_get_room (event)
|
function handle_get_room (event)
|
||||||
if (not event.request.url.query) then
|
if (not event.request.url.query) then
|
||||||
return 400;
|
return { status_code = 400; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local params = parse(event.request.url.query);
|
local params = parse(event.request.url.query);
|
||||||
|
@ -142,7 +142,7 @@ function handle_get_room (event)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not verify_token(params["token"], room_address) then
|
if not verify_token(params["token"], room_address) then
|
||||||
return 403;
|
return { status_code = 403; };
|
||||||
end
|
end
|
||||||
|
|
||||||
local room = get_room_from_jid(room_address);
|
local room = get_room_from_jid(room_address);
|
||||||
|
@ -173,14 +173,14 @@ function handle_get_room (event)
|
||||||
"there are %s occupants in room", tostring(participant_count));
|
"there are %s occupants in room", tostring(participant_count));
|
||||||
else
|
else
|
||||||
log("debug", "no such room exists");
|
log("debug", "no such room exists");
|
||||||
return 404;
|
return { status_code = 404; };
|
||||||
end
|
end
|
||||||
|
|
||||||
if participant_count > 1 then
|
if participant_count > 1 then
|
||||||
participant_count = participant_count - 1;
|
participant_count = participant_count - 1;
|
||||||
end
|
end
|
||||||
|
|
||||||
return json.encode(occupants_json);
|
return { status_code = 200; body = json.encode(occupants_json); };
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function module.load()
|
function module.load()
|
||||||
|
@ -188,9 +188,9 @@ function module.load()
|
||||||
module:provides("http", {
|
module:provides("http", {
|
||||||
default_path = "/";
|
default_path = "/";
|
||||||
route = {
|
route = {
|
||||||
["GET room-size"] = function (event) return wrap_async_run(event,handle_get_room_size) end;
|
["GET room-size"] = function (event) return async_handler_wrapper(event,handle_get_room_size) end;
|
||||||
["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end;
|
["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end;
|
||||||
["GET room"] = function (event) return wrap_async_run(event,handle_get_room) end;
|
["GET room"] = function (event) return async_handler_wrapper(event,handle_get_room) end;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,36 +59,14 @@ function get_room_from_jid(room_jid)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function async_handler_wrapper(event, handler)
|
||||||
function wrap_async_run(event,handler)
|
|
||||||
local have_async = pcall(require, "util.async");
|
|
||||||
if not have_async then
|
if not have_async then
|
||||||
module:log("error", "requires a version of Prosody with util.async");
|
module:log("error", "requires a version of Prosody with util.async");
|
||||||
return nil;
|
return nil;
|
||||||
end
|
end
|
||||||
|
|
||||||
local runner = async.runner;
|
local runner = async.runner;
|
||||||
-- Grab a local response so that we can send the http response when
|
|
||||||
-- the handler is done.
|
|
||||||
local response = event.response;
|
|
||||||
local async_func = runner(function (event)
|
|
||||||
local result = handler(event);
|
|
||||||
-- if it is a number, it is a status code, else it is the body
|
|
||||||
-- result we will return
|
|
||||||
if (tonumber(result) ~= nil) then
|
|
||||||
response.status_code = result;
|
|
||||||
else
|
|
||||||
response.body = result;
|
|
||||||
end;
|
|
||||||
-- Send the response to the waiting http client.
|
|
||||||
response:send();
|
|
||||||
end)
|
|
||||||
async_func:run(event)
|
|
||||||
-- return true to keep the client http connection open.
|
|
||||||
return true;
|
|
||||||
end
|
|
||||||
|
|
||||||
function async_handler_wrapper(event, handler)
|
|
||||||
-- Grab a local response so that we can send the http response when
|
-- Grab a local response so that we can send the http response when
|
||||||
-- the handler is done.
|
-- the handler is done.
|
||||||
local response = event.response;
|
local response = event.response;
|
||||||
|
@ -197,7 +175,6 @@ end
|
||||||
return {
|
return {
|
||||||
is_feature_allowed = is_feature_allowed;
|
is_feature_allowed = is_feature_allowed;
|
||||||
get_room_from_jid = get_room_from_jid;
|
get_room_from_jid = get_room_from_jid;
|
||||||
wrap_async_run = wrap_async_run;
|
|
||||||
async_handler_wrapper = async_handler_wrapper;
|
async_handler_wrapper = async_handler_wrapper;
|
||||||
room_jid_match_rewrite = room_jid_match_rewrite;
|
room_jid_match_rewrite = room_jid_match_rewrite;
|
||||||
update_presence_identity = update_presence_identity;
|
update_presence_identity = update_presence_identity;
|
||||||
|
|
Loading…
Reference in New Issue