feat: Sends json messages notifying for lobby actions. (#7209)
* feat: Sends json messages notifying for lobby actions. * squash: Fixes quotes to be consistent. * fix: Fixes attempt to call global 'formdecode' (a nil value).
This commit is contained in:
parent
5f579e9a15
commit
b3a2905849
|
@ -16,23 +16,29 @@
|
|||
-- muc_room_default_public_jids = true
|
||||
--
|
||||
-- we use async to detect Prosody 0.10 and earlier
|
||||
local have_async = pcall(require, "util.async");
|
||||
local have_async = pcall(require, 'util.async');
|
||||
|
||||
if not have_async then
|
||||
module:log("warn", "Lobby rooms will not work with Prosody version 0.10 or less.");
|
||||
module:log('warn', 'Lobby rooms will not work with Prosody version 0.10 or less.');
|
||||
return;
|
||||
end
|
||||
|
||||
local formdecode = require "util.http".formdecode;
|
||||
local jid_split = require 'util.jid'.split;
|
||||
local jid_bare = require 'util.jid'.bare;
|
||||
local json = require 'util.json';
|
||||
local filters = require 'util.filters';
|
||||
local st = require 'util.stanza';
|
||||
local MUC_NS = 'http://jabber.org/protocol/muc';
|
||||
local DISCO_INFO_NS = 'http://jabber.org/protocol/disco#info';
|
||||
local DISPLAY_NAME_REQUIRED_FEATURE = 'http://jitsi.org/protocol/lobbyrooms#displayname_required';
|
||||
local LOBBY_IDENTITY_TYPE = 'lobbyrooms';
|
||||
local NOTIFY_JSON_MESSAGE_TYPE = 'lobby-notify';
|
||||
local NOTIFY_LOBBY_ENABLED = 'LOBBY-ENABLED';
|
||||
local NOTIFY_LOBBY_ACCESS_GRANTED = 'LOBBY-ACCESS-GRANTED';
|
||||
local NOTIFY_LOBBY_ACCESS_DENIED = 'LOBBY-ACCESS-DENIED';
|
||||
|
||||
local is_healthcheck_room = module:require "util".is_healthcheck_room;
|
||||
local is_healthcheck_room = module:require 'util'.is_healthcheck_room;
|
||||
|
||||
local main_muc_component_config = module:get_option_string('main_muc');
|
||||
if main_muc_component_config == nil then
|
||||
|
@ -48,23 +54,23 @@ end
|
|||
local whitelist;
|
||||
local check_display_name_required;
|
||||
local function load_config()
|
||||
whitelist = module:get_option_set("muc_lobby_whitelist", {});
|
||||
whitelist = module:get_option_set('muc_lobby_whitelist', {});
|
||||
check_display_name_required
|
||||
= module:get_option_boolean("muc_lobby_check_display_name_required", true);
|
||||
= module:get_option_boolean('muc_lobby_check_display_name_required', true);
|
||||
end
|
||||
load_config();
|
||||
|
||||
local lobby_muc_service;
|
||||
local main_muc_service;
|
||||
|
||||
-- Checks whether there is self-status 110 of the <x node
|
||||
function check_self_status(muc_x)
|
||||
-- Checks whether there is status in the <x node
|
||||
function check_status(muc_x, status)
|
||||
if not muc_x then
|
||||
return false;
|
||||
end
|
||||
|
||||
for status in muc_x:childtags('status') do
|
||||
if status.attr.code == '110' then
|
||||
for statusNode in muc_x:childtags('status') do
|
||||
if statusNode.attr.code == status then
|
||||
return true;
|
||||
end
|
||||
end
|
||||
|
@ -72,6 +78,42 @@ function check_self_status(muc_x)
|
|||
return false;
|
||||
end
|
||||
|
||||
function broadcast_json_msg(room, from, json_msg)
|
||||
json_msg.type = NOTIFY_JSON_MESSAGE_TYPE;
|
||||
|
||||
local occupant = room:get_occupant_by_real_jid(from);
|
||||
if occupant then
|
||||
room:broadcast_message(
|
||||
st.message({ type = 'groupchat', from = occupant.nick })
|
||||
:tag('json-message', {xmlns='http://jitsi.org/jitmeet'})
|
||||
:text(json.encode(json_msg)):up());
|
||||
end
|
||||
end
|
||||
|
||||
-- Sends a json message notifying for lobby enabled/disable
|
||||
-- the message from is the actor that did the operation
|
||||
function notify_lobby_enabled(room, actor, value)
|
||||
broadcast_json_msg(room, actor, {
|
||||
event = NOTIFY_LOBBY_ENABLED,
|
||||
value = value
|
||||
});
|
||||
end
|
||||
|
||||
-- Sends a json message notifying that the jid was granted/denied access in lobby
|
||||
-- the message from is the actor that did the operation
|
||||
function notify_lobby_access(room, actor, jid, granted)
|
||||
local notify_json = {
|
||||
value = jid
|
||||
};
|
||||
if granted then
|
||||
notify_json.event = NOTIFY_LOBBY_ACCESS_GRANTED;
|
||||
else
|
||||
notify_json.event = NOTIFY_LOBBY_ACCESS_DENIED;
|
||||
end
|
||||
|
||||
broadcast_json_msg(room, actor, notify_json);
|
||||
end
|
||||
|
||||
function filter_stanza(stanza)
|
||||
if not stanza.attr or not stanza.attr.from or not main_muc_service then
|
||||
return stanza;
|
||||
|
@ -83,7 +125,7 @@ function filter_stanza(stanza)
|
|||
if stanza.name == 'presence' then
|
||||
local muc_x = stanza:get_child('x', MUC_NS..'#user');
|
||||
|
||||
if muc_x and check_self_status(muc_x) then
|
||||
if check_status(muc_x, '110') then
|
||||
return stanza;
|
||||
end
|
||||
|
||||
|
@ -142,7 +184,7 @@ function process_lobby_muc_loaded(lobby_muc, host_module)
|
|||
|
||||
-- Tag the disco#info response with a feature that display name is required
|
||||
-- when the conference name from the web request has a lobby enabled.
|
||||
host_module:hook("host-disco-info-node", function (event)
|
||||
host_module:hook('host-disco-info-node', function (event)
|
||||
local session, reply, node = event.origin, event.reply, event.node;
|
||||
if node == LOBBY_IDENTITY_TYPE
|
||||
and session.jitsi_web_query_room
|
||||
|
@ -151,7 +193,7 @@ function process_lobby_muc_loaded(lobby_muc, host_module)
|
|||
local room = main_muc_service.get_room_from_jid(
|
||||
jid_bare(session.jitsi_web_query_room .. '@' .. main_muc_component_config));
|
||||
if room and room._data.lobbyroom then
|
||||
reply:tag("feature", { var = DISPLAY_NAME_REQUIRED_FEATURE }):up();
|
||||
reply:tag('feature', { var = DISPLAY_NAME_REQUIRED_FEATURE }):up();
|
||||
end
|
||||
end
|
||||
event.exists = true;
|
||||
|
@ -173,6 +215,15 @@ function process_lobby_muc_loaded(lobby_muc, host_module)
|
|||
|
||||
return 'none';
|
||||
end
|
||||
|
||||
-- listens for kicks in lobby room, 307 is the status for kick according to xep-0045
|
||||
host_module:hook('muc-broadcast-presence', function (event)
|
||||
local actor, occupant, room, x = event.actor, event.occupant, event.room, event.x;
|
||||
if check_status(x, '307') then
|
||||
-- we need to notify in the main room
|
||||
notify_lobby_access(room.main_room, actor, occupant.jid, false);
|
||||
end
|
||||
end);
|
||||
end
|
||||
|
||||
-- process or waits to process the lobby muc component
|
||||
|
@ -199,7 +250,7 @@ process_host_module(main_muc_component_config, function(host_module, host)
|
|||
|
||||
-- hooks when lobby is enabled to create its room, only done here or by admin
|
||||
host_module:hook('muc-config-submitted', function(event)
|
||||
local room = event.room;
|
||||
local actor, room = event.actor, event.room;
|
||||
local members_only = event.fields['muc#roomconfig_membersonly'] and true or nil;
|
||||
if members_only then
|
||||
local node = jid_split(room.jid);
|
||||
|
@ -209,29 +260,31 @@ process_host_module(main_muc_component_config, function(host_module, host)
|
|||
local new_room = lobby_muc_service.create_room(lobby_room_jid);
|
||||
new_room.main_room = room;
|
||||
room._data.lobbyroom = new_room;
|
||||
event.status_codes["104"] = true;
|
||||
event.status_codes['104'] = true;
|
||||
notify_lobby_enabled(room, actor, true);
|
||||
end
|
||||
elseif room._data.lobbyroom then
|
||||
room._data.lobbyroom:destroy(room.jid, 'Lobby room closed.');
|
||||
room._data.lobbyroom = nil;
|
||||
notify_lobby_enabled(room, actor, false);
|
||||
end
|
||||
end);
|
||||
host_module:hook("muc-room-destroyed",function(event)
|
||||
host_module:hook('muc-room-destroyed',function(event)
|
||||
local room = event.room;
|
||||
if room._data.lobbyroom then
|
||||
room._data.lobbyroom:destroy(nil, 'Lobby room closed.');
|
||||
room._data.lobbyroom = nil;
|
||||
end
|
||||
end);
|
||||
host_module:hook("muc-disco#info", function (event)
|
||||
host_module:hook('muc-disco#info', function (event)
|
||||
local room = event.room;
|
||||
if (room._data.lobbyroom and room:get_members_only()) then
|
||||
table.insert(event.form, {
|
||||
name = "muc#roominfo_lobbyroom";
|
||||
label = "Lobby room jid";
|
||||
value = "";
|
||||
name = 'muc#roominfo_lobbyroom';
|
||||
label = 'Lobby room jid';
|
||||
value = '';
|
||||
});
|
||||
event.formdata["muc#roominfo_lobbyroom"] = room._data.lobbyroom.jid;
|
||||
event.formdata['muc#roominfo_lobbyroom'] = room._data.lobbyroom.jid;
|
||||
end
|
||||
end);
|
||||
|
||||
|
@ -242,7 +295,7 @@ process_host_module(main_muc_component_config, function(host_module, host)
|
|||
return;
|
||||
end
|
||||
|
||||
local join = stanza:get_child("x", MUC_NS);
|
||||
local join = stanza:get_child('x', MUC_NS);
|
||||
if not join then
|
||||
return;
|
||||
end
|
||||
|
@ -266,7 +319,7 @@ process_host_module(main_muc_component_config, function(host_module, host)
|
|||
local affiliation = room:get_affiliation(invitee);
|
||||
if not affiliation or affiliation == 0 then
|
||||
event.occupant.role = 'participant';
|
||||
room:set_affiliation(true, invitee_bare_jid, "member");
|
||||
room:set_affiliation(true, invitee_bare_jid, 'member');
|
||||
room:save();
|
||||
|
||||
return;
|
||||
|
@ -285,6 +338,16 @@ process_host_module(main_muc_component_config, function(host_module, host)
|
|||
return true;
|
||||
end
|
||||
end, -4); -- the default hook on members_only module is on -5
|
||||
|
||||
-- listens for invites for participants to join the main room
|
||||
host_module:hook('muc-invite', function(event)
|
||||
local room, stanza = event.room, event.stanza;
|
||||
local invitee = stanza.attr.to;
|
||||
local from = stanza:get_child('x', 'http://jabber.org/protocol/muc#user')
|
||||
:get_child('invite').attr.from;
|
||||
|
||||
notify_lobby_access(room, from, invitee, true);
|
||||
end);
|
||||
end);
|
||||
|
||||
-- Extract 'room' param from URL when session is created
|
||||
|
@ -301,10 +364,10 @@ function update_session(event)
|
|||
local params = formdecode(query);
|
||||
-- The room name and optional prefix from the web query
|
||||
session.jitsi_web_query_room = params.room;
|
||||
session.jitsi_web_query_prefix = params.prefix or "";
|
||||
session.jitsi_web_query_prefix = params.prefix or '';
|
||||
end
|
||||
end
|
||||
|
||||
module:hook_global("bosh-session", update_session);
|
||||
module:hook_global("websocket-session", update_session);
|
||||
module:hook_global('bosh-session', update_session);
|
||||
module:hook_global('websocket-session', update_session);
|
||||
module:hook_global('config-reloaded', load_config);
|
||||
|
|
Loading…
Reference in New Issue