浏览代码

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).
master
Дамян Минков 5 年前
父节点
当前提交
b3a2905849
没有帐户链接到提交者的电子邮件
共有 1 个文件被更改,包括 88 次插入24 次删除
  1. 88
    24
      resources/prosody-plugins/mod_muc_lobby_rooms.lua

+ 88
- 24
resources/prosody-plugins/mod_muc_lobby_rooms.lua 查看文件

16
 --     muc_room_default_public_jids = true
16
 --     muc_room_default_public_jids = true
17
 --
17
 --
18
 -- we use async to detect Prosody 0.10 and earlier
18
 -- we use async to detect Prosody 0.10 and earlier
19
-local have_async = pcall(require, "util.async");
19
+local have_async = pcall(require, 'util.async');
20
 
20
 
21
 if not have_async then
21
 if not have_async then
22
-    module:log("warn", "Lobby rooms will not work with Prosody version 0.10 or less.");
22
+    module:log('warn', 'Lobby rooms will not work with Prosody version 0.10 or less.');
23
     return;
23
     return;
24
 end
24
 end
25
 
25
 
26
+local formdecode = require "util.http".formdecode;
26
 local jid_split = require 'util.jid'.split;
27
 local jid_split = require 'util.jid'.split;
27
 local jid_bare = require 'util.jid'.bare;
28
 local jid_bare = require 'util.jid'.bare;
29
+local json = require 'util.json';
28
 local filters = require 'util.filters';
30
 local filters = require 'util.filters';
29
 local st = require 'util.stanza';
31
 local st = require 'util.stanza';
30
 local MUC_NS = 'http://jabber.org/protocol/muc';
32
 local MUC_NS = 'http://jabber.org/protocol/muc';
31
 local DISCO_INFO_NS = 'http://jabber.org/protocol/disco#info';
33
 local DISCO_INFO_NS = 'http://jabber.org/protocol/disco#info';
32
 local DISPLAY_NAME_REQUIRED_FEATURE = 'http://jitsi.org/protocol/lobbyrooms#displayname_required';
34
 local DISPLAY_NAME_REQUIRED_FEATURE = 'http://jitsi.org/protocol/lobbyrooms#displayname_required';
33
 local LOBBY_IDENTITY_TYPE = 'lobbyrooms';
35
 local LOBBY_IDENTITY_TYPE = 'lobbyrooms';
36
+local NOTIFY_JSON_MESSAGE_TYPE = 'lobby-notify';
37
+local NOTIFY_LOBBY_ENABLED = 'LOBBY-ENABLED';
38
+local NOTIFY_LOBBY_ACCESS_GRANTED = 'LOBBY-ACCESS-GRANTED';
39
+local NOTIFY_LOBBY_ACCESS_DENIED = 'LOBBY-ACCESS-DENIED';
34
 
40
 
35
-local is_healthcheck_room = module:require "util".is_healthcheck_room;
41
+local is_healthcheck_room = module:require 'util'.is_healthcheck_room;
36
 
42
 
37
 local main_muc_component_config = module:get_option_string('main_muc');
43
 local main_muc_component_config = module:get_option_string('main_muc');
38
 if main_muc_component_config == nil then
44
 if main_muc_component_config == nil then
48
 local whitelist;
54
 local whitelist;
49
 local check_display_name_required;
55
 local check_display_name_required;
50
 local function load_config()
56
 local function load_config()
51
-    whitelist = module:get_option_set("muc_lobby_whitelist", {});
57
+    whitelist = module:get_option_set('muc_lobby_whitelist', {});
52
     check_display_name_required
58
     check_display_name_required
53
-        = module:get_option_boolean("muc_lobby_check_display_name_required", true);
59
+        = module:get_option_boolean('muc_lobby_check_display_name_required', true);
54
 end
60
 end
55
 load_config();
61
 load_config();
56
 
62
 
57
 local lobby_muc_service;
63
 local lobby_muc_service;
58
 local main_muc_service;
64
 local main_muc_service;
59
 
65
 
60
-function check_self_status(muc_x)
66
+-- Checks whether there is status in the <x node
67
+function check_status(muc_x, status)
61
     if not muc_x then
68
     if not muc_x then
62
         return false;
69
         return false;
63
     end
70
     end
64
 
71
 
65
-    for status in muc_x:childtags('status') do
66
-        if status.attr.code == '110' then
72
+    for statusNode in muc_x:childtags('status') do
73
+        if statusNode.attr.code == status then
67
             return true;
74
             return true;
68
         end
75
         end
69
     end
76
     end
72
     return false;
78
     return false;
73
 end
79
 end
74
 
80
 
81
+function broadcast_json_msg(room, from, json_msg)
82
+    json_msg.type = NOTIFY_JSON_MESSAGE_TYPE;
83
+
84
+    local occupant = room:get_occupant_by_real_jid(from);
85
+    if occupant then
86
+        room:broadcast_message(
87
+            st.message({ type = 'groupchat', from = occupant.nick })
88
+              :tag('json-message', {xmlns='http://jitsi.org/jitmeet'})
89
+              :text(json.encode(json_msg)):up());
90
+    end
91
+end
92
+
93
+-- Sends a json message notifying for lobby enabled/disable
94
+-- the message from is the actor that did the operation
95
+function notify_lobby_enabled(room, actor, value)
96
+    broadcast_json_msg(room, actor, {
97
+        event = NOTIFY_LOBBY_ENABLED,
98
+        value = value
99
+    });
100
+end
101
+
102
+-- Sends a json message notifying that the jid was granted/denied access in lobby
103
+-- the message from is the actor that did the operation
104
+function notify_lobby_access(room, actor, jid, granted)
105
+    local notify_json = {
106
+        value = jid
107
+    };
108
+    if granted then
109
+        notify_json.event = NOTIFY_LOBBY_ACCESS_GRANTED;
110
+    else
111
+        notify_json.event = NOTIFY_LOBBY_ACCESS_DENIED;
112
+    end
113
+
114
+    broadcast_json_msg(room, actor, notify_json);
115
+end
116
+
75
 function filter_stanza(stanza)
117
 function filter_stanza(stanza)
76
     if not stanza.attr or not stanza.attr.from or not main_muc_service then
118
     if not stanza.attr or not stanza.attr.from or not main_muc_service then
77
         return stanza;
119
         return stanza;
83
         if stanza.name == 'presence' then
125
         if stanza.name == 'presence' then
84
             local muc_x = stanza:get_child('x', MUC_NS..'#user');
126
             local muc_x = stanza:get_child('x', MUC_NS..'#user');
85
 
127
 
86
-            if muc_x and check_self_status(muc_x) then
128
+            if check_status(muc_x, '110') then
87
                 return stanza;
129
                 return stanza;
88
             end
130
             end
89
 
131
 
142
 
184
 
143
     -- Tag the disco#info response with a feature that display name is required
185
     -- Tag the disco#info response with a feature that display name is required
144
     -- when the conference name from the web request has a lobby enabled.
186
     -- when the conference name from the web request has a lobby enabled.
145
-    host_module:hook("host-disco-info-node", function (event)
187
+    host_module:hook('host-disco-info-node', function (event)
146
         local session, reply, node = event.origin, event.reply, event.node;
188
         local session, reply, node = event.origin, event.reply, event.node;
147
         if node == LOBBY_IDENTITY_TYPE
189
         if node == LOBBY_IDENTITY_TYPE
148
             and session.jitsi_web_query_room
190
             and session.jitsi_web_query_room
151
             local room = main_muc_service.get_room_from_jid(
193
             local room = main_muc_service.get_room_from_jid(
152
                 jid_bare(session.jitsi_web_query_room .. '@' .. main_muc_component_config));
194
                 jid_bare(session.jitsi_web_query_room .. '@' .. main_muc_component_config));
153
             if room and room._data.lobbyroom then
195
             if room and room._data.lobbyroom then
154
-                reply:tag("feature", { var = DISPLAY_NAME_REQUIRED_FEATURE }):up();
196
+                reply:tag('feature', { var = DISPLAY_NAME_REQUIRED_FEATURE }):up();
155
             end
197
             end
156
         end
198
         end
157
         event.exists = true;
199
         event.exists = true;
173
 
215
 
174
         return 'none';
216
         return 'none';
175
     end
217
     end
218
+
219
+    -- listens for kicks in lobby room, 307 is the status for kick according to xep-0045
220
+    host_module:hook('muc-broadcast-presence', function (event)
221
+        local actor, occupant, room, x = event.actor, event.occupant, event.room, event.x;
222
+        if check_status(x, '307') then
223
+            -- we need to notify in the main room
224
+            notify_lobby_access(room.main_room, actor, occupant.jid, false);
225
+        end
226
+    end);
176
 end
227
 end
177
 
228
 
178
 -- process or waits to process the lobby muc component
229
 -- process or waits to process the lobby muc component
199
 
250
 
200
     -- hooks when lobby is enabled to create its room, only done here or by admin
251
     -- hooks when lobby is enabled to create its room, only done here or by admin
201
     host_module:hook('muc-config-submitted', function(event)
252
     host_module:hook('muc-config-submitted', function(event)
202
-        local room = event.room;
253
+        local actor, room = event.actor, event.room;
203
         local members_only = event.fields['muc#roomconfig_membersonly'] and true or nil;
254
         local members_only = event.fields['muc#roomconfig_membersonly'] and true or nil;
204
         if members_only then
255
         if members_only then
205
             local node = jid_split(room.jid);
256
             local node = jid_split(room.jid);
209
                 local new_room = lobby_muc_service.create_room(lobby_room_jid);
260
                 local new_room = lobby_muc_service.create_room(lobby_room_jid);
210
                 new_room.main_room = room;
261
                 new_room.main_room = room;
211
                 room._data.lobbyroom = new_room;
262
                 room._data.lobbyroom = new_room;
212
-                event.status_codes["104"] = true;
263
+                event.status_codes['104'] = true;
264
+                notify_lobby_enabled(room, actor, true);
213
             end
265
             end
214
         elseif room._data.lobbyroom then
266
         elseif room._data.lobbyroom then
215
             room._data.lobbyroom:destroy(room.jid, 'Lobby room closed.');
267
             room._data.lobbyroom:destroy(room.jid, 'Lobby room closed.');
216
             room._data.lobbyroom = nil;
268
             room._data.lobbyroom = nil;
269
+            notify_lobby_enabled(room, actor, false);
217
         end
270
         end
218
     end);
271
     end);
219
-    host_module:hook("muc-room-destroyed",function(event)
272
+    host_module:hook('muc-room-destroyed',function(event)
220
         local room = event.room;
273
         local room = event.room;
221
         if room._data.lobbyroom then
274
         if room._data.lobbyroom then
222
             room._data.lobbyroom:destroy(nil, 'Lobby room closed.');
275
             room._data.lobbyroom:destroy(nil, 'Lobby room closed.');
223
             room._data.lobbyroom = nil;
276
             room._data.lobbyroom = nil;
224
         end
277
         end
225
     end);
278
     end);
226
-    host_module:hook("muc-disco#info", function (event)
279
+    host_module:hook('muc-disco#info', function (event)
227
         local room = event.room;
280
         local room = event.room;
228
         if (room._data.lobbyroom and room:get_members_only()) then
281
         if (room._data.lobbyroom and room:get_members_only()) then
229
             table.insert(event.form, {
282
             table.insert(event.form, {
230
-                name = "muc#roominfo_lobbyroom";
231
-                label = "Lobby room jid";
232
-                value = "";
283
+                name = 'muc#roominfo_lobbyroom';
284
+                label = 'Lobby room jid';
285
+                value = '';
233
             });
286
             });
234
-            event.formdata["muc#roominfo_lobbyroom"] = room._data.lobbyroom.jid;
287
+            event.formdata['muc#roominfo_lobbyroom'] = room._data.lobbyroom.jid;
235
         end
288
         end
236
     end);
289
     end);
237
 
290
 
242
             return;
295
             return;
243
         end
296
         end
244
 
297
 
245
-        local join = stanza:get_child("x", MUC_NS);
298
+        local join = stanza:get_child('x', MUC_NS);
246
         if not join then
299
         if not join then
247
             return;
300
             return;
248
         end
301
         end
266
             local affiliation = room:get_affiliation(invitee);
319
             local affiliation = room:get_affiliation(invitee);
267
             if not affiliation or affiliation == 0 then
320
             if not affiliation or affiliation == 0 then
268
                 event.occupant.role = 'participant';
321
                 event.occupant.role = 'participant';
269
-                room:set_affiliation(true, invitee_bare_jid, "member");
322
+                room:set_affiliation(true, invitee_bare_jid, 'member');
270
                 room:save();
323
                 room:save();
271
 
324
 
272
                 return;
325
                 return;
285
             return true;
338
             return true;
286
         end
339
         end
287
     end, -4); -- the default hook on members_only module is on -5
340
     end, -4); -- the default hook on members_only module is on -5
341
+
342
+    -- listens for invites for participants to join the main room
343
+    host_module:hook('muc-invite', function(event)
344
+        local room, stanza = event.room, event.stanza;
345
+        local invitee = stanza.attr.to;
346
+        local from = stanza:get_child('x', 'http://jabber.org/protocol/muc#user')
347
+            :get_child('invite').attr.from;
348
+
349
+        notify_lobby_access(room, from, invitee, true);
350
+    end);
288
 end);
351
 end);
289
 
352
 
290
 -- Extract 'room' param from URL when session is created
353
 -- Extract 'room' param from URL when session is created
301
         local params = formdecode(query);
364
         local params = formdecode(query);
302
         -- The room name and optional prefix from the web query
365
         -- The room name and optional prefix from the web query
303
         session.jitsi_web_query_room = params.room;
366
         session.jitsi_web_query_room = params.room;
304
-        session.jitsi_web_query_prefix = params.prefix or "";
367
+        session.jitsi_web_query_prefix = params.prefix or '';
305
     end
368
     end
306
 end
369
 end
307
 
370
 
308
-module:hook_global("bosh-session", update_session);
309
-module:hook_global("websocket-session", update_session);
371
+module:hook_global('bosh-session', update_session);
372
+module:hook_global('websocket-session', update_session);
310
 module:hook_global('config-reloaded', load_config);
373
 module:hook_global('config-reloaded', load_config);

正在加载...
取消
保存