Просмотр исходного кода

feat(reservations) add integration with mod_muc_max_occupants

master
Shawn 3 лет назад
Родитель
Сommit
f8628dfeef
1 измененных файлов: 42 добавлений и 4 удалений
  1. 42
    4
      resources/prosody-plugins/mod_reservations.lua

+ 42
- 4
resources/prosody-plugins/mod_reservations.lua Просмотреть файл

@@ -32,6 +32,9 @@
32 32
 --      * set "reservations_api_should_retry_for_code" to a function that takes an HTTP response code and
33 33
 --        returns true if API call should be retried. By default, retries are done for 5XX
34 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 40
 --  Example config:
@@ -71,6 +74,7 @@ local api_headers = module:get_option("reservations_api_headers");
71 74
 local api_timeout = module:get_option("reservations_api_timeout", 20);
72 75
 local api_retry_count = tonumber(module:get_option("reservations_api_retry_count", 3));
73 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 80
 -- Option for user to control HTTP response codes that will result in a retry.
@@ -244,7 +248,7 @@ function RoomReservation:enqueue_or_route_event(event)
244 248
 end
245 249
 
246 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 252
     module:log("info", "Reservation created successfully for %s", self.room_jid);
249 253
     self.meta = {
250 254
         status = STATUS.SUCCESS;
@@ -255,6 +259,9 @@ function RoomReservation:set_status_success(start_time, duration, mail_owner, co
255 259
         error_text = nil;
256 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 265
     self:route_pending_events()
259 266
 end
260 267
 
@@ -362,7 +369,7 @@ end
362 369
 
363 370
 --- Parses and validates HTTP response body for conference payload
364 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 373
 function RoomReservation:parse_conference_response(response_body)
366 374
     local data = json.decode(response_body);
367 375
 
@@ -372,7 +379,7 @@ function RoomReservation:parse_conference_response(response_body)
372 379
     end
373 380
 
374 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 383
         return;
377 384
     end
378 385
 
@@ -393,6 +400,17 @@ function RoomReservation:parse_conference_response(response_body)
393 400
     end
394 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 414
     local start_time = datetime.parse(data.start_time);  -- N.B. we lose milliseconds portion of the date
397 415
     if start_time == nil then
398 416
         module:log("error", "Missing or invalid start_time - %s", data.start_time);
@@ -440,7 +458,7 @@ function RoomReservation:handler_conference_data_returned_from_api(response_body
440 458
         module:log("error", "API returned success code but invalid payload");
441 459
         self:set_status_failed(500, 'Invalid response from reservation server');
442 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 462
     end
445 463
 end
446 464
 
@@ -573,11 +591,30 @@ local function room_destroyed(event)
573 591
     end
574 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 609
 function process_host(host)
578 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 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 618
     end
582 619
 end
583 620
 

Загрузка…
Отмена
Сохранить