Browse Source

feat(reservations) add integration with mod_muc_max_occupants

master
Shawn 3 years ago
parent
commit
f8628dfeef
1 changed files with 42 additions and 4 deletions
  1. 42
    4
      resources/prosody-plugins/mod_reservations.lua

+ 42
- 4
resources/prosody-plugins/mod_reservations.lua View File

32
 --      * set "reservations_api_should_retry_for_code" to a function that takes an HTTP response code and
32
 --      * set "reservations_api_should_retry_for_code" to a function that takes an HTTP response code and
33
 --        returns true if API call should be retried. By default, retries are done for 5XX
33
 --        returns true if API call should be retried. By default, retries are done for 5XX
34
 --        responses. Timeouts are never retried, and HTTP call failures are always retried.
34
 --        responses. Timeouts are never retried, and HTTP call failures are always retried.
35
+--      * set "reservations_enable_max_occupants" to true to enable integration with
36
+--        mod_muc_max_occupants. Setting thia will allow optional "max_occupants"
37
+--        payload from API to influence max occupants allowed for a given room.
35
 --
38
 --
36
 --
39
 --
37
 --  Example config:
40
 --  Example config:
71
 local api_timeout = module:get_option("reservations_api_timeout", 20);
74
 local api_timeout = module:get_option("reservations_api_timeout", 20);
72
 local api_retry_count = tonumber(module:get_option("reservations_api_retry_count", 3));
75
 local api_retry_count = tonumber(module:get_option("reservations_api_retry_count", 3));
73
 local api_retry_delay = tonumber(module:get_option("reservations_api_retry_delay", 3));
76
 local api_retry_delay = tonumber(module:get_option("reservations_api_retry_delay", 3));
77
+local max_occupants_enabled = module:get_option("reservations_enable_max_occupants", false);
74
 
78
 
75
 
79
 
76
 -- Option for user to control HTTP response codes that will result in a retry.
80
 -- Option for user to control HTTP response codes that will result in a retry.
244
 end
248
 end
245
 
249
 
246
 --- Updates status and initiates event routing. Called internally when API call complete.
250
 --- Updates status and initiates event routing. Called internally when API call complete.
247
-function RoomReservation:set_status_success(start_time, duration, mail_owner, conflict_id)
251
+function RoomReservation:set_status_success(start_time, duration, mail_owner, conflict_id, max_occupants)
248
     module:log("info", "Reservation created successfully for %s", self.room_jid);
252
     module:log("info", "Reservation created successfully for %s", self.room_jid);
249
     self.meta = {
253
     self.meta = {
250
         status = STATUS.SUCCESS;
254
         status = STATUS.SUCCESS;
255
         error_text = nil;
259
         error_text = nil;
256
         error_code = nil;
260
         error_code = nil;
257
     }
261
     }
262
+    if max_occupants_enabled and max_occupants then
263
+        self.meta.max_occupants = max_occupants
264
+    end
258
     self:route_pending_events()
265
     self:route_pending_events()
259
 end
266
 end
260
 
267
 
362
 
369
 
363
 --- Parses and validates HTTP response body for conference payload
370
 --- Parses and validates HTTP response body for conference payload
364
 --  Ref: https://github.com/jitsi/jicofo/blob/master/doc/reservation.md
371
 --  Ref: https://github.com/jitsi/jicofo/blob/master/doc/reservation.md
372
+--  @return nil if invalid, or table with payload parsed from JSON response
365
 function RoomReservation:parse_conference_response(response_body)
373
 function RoomReservation:parse_conference_response(response_body)
366
     local data = json.decode(response_body);
374
     local data = json.decode(response_body);
367
 
375
 
372
     end
379
     end
373
 
380
 
374
     if data.name == nil or data.name:lower() ~= self:get_room_name() then
381
     if data.name == nil or data.name:lower() ~= self:get_room_name() then
375
-        module:log("error", "Missing or mismathing room name - %s", data.name);
382
+        module:log("error", "Missing or mismatching room name - %s", data.name);
376
         return;
383
         return;
377
     end
384
     end
378
 
385
 
393
     end
400
     end
394
     data.duration = duration;
401
     data.duration = duration;
395
 
402
 
403
+    -- if optional max_occupants field set, cast to number
404
+    if data.max_occupants ~= nil then
405
+        local max_occupants = tonumber(data.max_occupants)
406
+        if max_occupants == nil or max_occupants < 1 then
407
+            -- N.B. invalid max_occupants rejected even if max_occupants_enabled=false
408
+            module:log("error", "Invalid value for max_occupants - %s", data.max_occupants);
409
+            return;
410
+        end
411
+        data.max_occupants = max_occupants
412
+    end
413
+
396
     local start_time = datetime.parse(data.start_time);  -- N.B. we lose milliseconds portion of the date
414
     local start_time = datetime.parse(data.start_time);  -- N.B. we lose milliseconds portion of the date
397
     if start_time == nil then
415
     if start_time == nil then
398
         module:log("error", "Missing or invalid start_time - %s", data.start_time);
416
         module:log("error", "Missing or invalid start_time - %s", data.start_time);
440
         module:log("error", "API returned success code but invalid payload");
458
         module:log("error", "API returned success code but invalid payload");
441
         self:set_status_failed(500, 'Invalid response from reservation server');
459
         self:set_status_failed(500, 'Invalid response from reservation server');
442
     else
460
     else
443
-        self:set_status_success(data.start_time, data.duration, data.mail_owner, data.id)
461
+        self:set_status_success(data.start_time, data.duration, data.mail_owner, data.id, data.max_occupants)
444
     end
462
     end
445
 end
463
 end
446
 
464
 
573
     end
591
     end
574
 end
592
 end
575
 
593
 
594
+--- If max_occupants_enabled, update room max_occupants if returned by API
595
+local function room_created(event)
596
+    local res;
597
+    local room = event.room
598
+
599
+    if max_occupants_enabled and not is_healthcheck_room(room.jid) then
600
+        res = reservations[room.jid]
601
+
602
+        if res and res.meta.max_occupants ~= nil then
603
+            module:log("info", "Setting max_occupants %d for room %s", res.meta.max_occupants, room.jid);
604
+            room._data.max_occupants = res.meta.max_occupants
605
+        end
606
+    end
607
+end
576
 
608
 
577
 function process_host(host)
609
 function process_host(host)
578
     if host == muc_component_host then -- the conference muc component
610
     if host == muc_component_host then -- the conference muc component
579
-        module:log("info", "Hook to muc events on %s", host);
611
+        module:log("info", "Hook to muc-room-destroyed on %s", host);
580
         module:context(host):hook("muc-room-destroyed", room_destroyed, -1);
612
         module:context(host):hook("muc-room-destroyed", room_destroyed, -1);
613
+
614
+        if max_occupants_enabled then
615
+            module:log("info", "Hook to muc-room-created on %s (mod_muc_max_occupants integration enabled)", host);
616
+            module:context(host):hook("muc-room-created", room_created);
617
+        end
581
     end
618
     end
582
 end
619
 end
583
 
620
 

Loading…
Cancel
Save