瀏覽代碼

feat: new prosody module to report census of all rooms (#9901)

* new prosody module to report census of all rooms

* changed to use util to check if it's a test room

* improved docs

* more doc improvements

* updated to use muc_domain_prefix

* facepalm
master
scott boone 3 年之前
父節點
當前提交
b7cb0a44f2
No account linked to committer's email address
共有 1 個文件被更改,包括 79 次插入0 次删除
  1. 79
    0
      resources/prosody-plugins/mod_muc_census.lua

+ 79
- 0
resources/prosody-plugins/mod_muc_census.lua 查看文件

@@ -0,0 +1,79 @@
1
+-- provides an http endpoint at /room-census that reports list of rooms with the
2
+-- number of members and created date in this JSON format:
3
+--
4
+--     {
5
+--         "room_census": [
6
+--             {
7
+--                 "room_name": "<muc name>",
8
+--                 "participants": <# participants>,
9
+--                 "created_time": <unix timestamp>,
10
+--             },
11
+--             ...
12
+--         ]
13
+--     }
14
+-- 
15
+-- to activate, add "muc_census" to the modules_enabled table in prosody.cfg.lua
16
+-- 
17
+-- warning: this module is unprotected and intended for server admin use only.
18
+-- when enabled, make sure to secure the endpoint at the web server or via
19
+-- network filters
20
+
21
+local jid = require "util.jid";
22
+local json = require "util.json";
23
+local iterators = require "util.iterators";
24
+local util = module:require "util";
25
+local is_healthcheck_room = util.is_healthcheck_room;
26
+
27
+local have_async = pcall(require, "util.async");
28
+if not have_async then
29
+    module:log("error", "requires a version of Prosody with util.async");
30
+    return;
31
+end
32
+
33
+local async_handler_wrapper = module:require "util".async_handler_wrapper;
34
+
35
+local tostring = tostring;
36
+
37
+-- required parameter for custom muc component prefix, defaults to "conference"
38
+local muc_domain_prefix = module:get_option_string("muc_mapper_domain_prefix", "conference");
39
+
40
+--- handles request to get number of participants in all rooms
41
+-- @return GET response
42
+function handle_get_room_census(event)
43
+    local host_session = prosody.hosts[muc_domain_prefix .. "." .. tostring(module.host)]
44
+    if not host_session or not host_session.modules.muc then
45
+        return { status_code = 400; }
46
+    end
47
+
48
+    room_data = {}
49
+    for room in host_session.modules.muc.each_room() do
50
+        if not is_healthcheck_room(room.jid) then
51
+            local occupants = room._occupants;
52
+            if occupants then
53
+                participant_count = iterators.count(room:each_occupant()) - 1; -- subtract focus
54
+            else
55
+                participant_count = 0
56
+            end
57
+            table.insert(room_data, {
58
+                room_name = room.jid;
59
+                participants = participant_count;
60
+                created_time = room.created_timestamp;
61
+            });
62
+        end
63
+    end
64
+
65
+    census_resp = json.encode({
66
+        room_census = room_data;
67
+    });
68
+    return { status_code = 200; body = census_resp }
69
+end
70
+
71
+function module.load()
72
+    module:depends("http");
73
+        module:provides("http", {
74
+                default_path = "/";
75
+                route = {
76
+                        ["GET room-census"] = function (event) return async_handler_wrapper(event,handle_get_room_census) end;
77
+                };
78
+        });
79
+end

Loading…
取消
儲存