You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mod_muc_census.lua 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. local jid = require "util.jid";
  21. local json = require "util.json";
  22. local iterators = require "util.iterators";
  23. local util = module:require "util";
  24. local is_healthcheck_room = util.is_healthcheck_room;
  25. local have_async = pcall(require, "util.async");
  26. if not have_async then
  27. module:log("error", "requires a version of Prosody with util.async");
  28. return;
  29. end
  30. local async_handler_wrapper = module:require "util".async_handler_wrapper;
  31. local tostring = tostring;
  32. -- required parameter for custom muc component prefix, defaults to "conference"
  33. local muc_domain_prefix = module:get_option_string("muc_mapper_domain_prefix", "conference");
  34. --- handles request to get number of participants in all rooms
  35. -- @return GET response
  36. function handle_get_room_census(event)
  37. local host_session = prosody.hosts[muc_domain_prefix .. "." .. tostring(module.host)]
  38. if not host_session or not host_session.modules.muc then
  39. return { status_code = 400; }
  40. end
  41. room_data = {}
  42. for room in host_session.modules.muc.each_room() do
  43. if not is_healthcheck_room(room.jid) then
  44. local occupants = room._occupants;
  45. if occupants then
  46. participant_count = iterators.count(room:each_occupant()) - 1; -- subtract focus
  47. else
  48. participant_count = 0
  49. end
  50. table.insert(room_data, {
  51. room_name = room.jid;
  52. participants = participant_count;
  53. created_time = room.created_timestamp;
  54. });
  55. end
  56. end
  57. census_resp = json.encode({
  58. room_census = room_data;
  59. });
  60. return { status_code = 200; body = census_resp }
  61. end
  62. function module.load()
  63. module:depends("http");
  64. module:provides("http", {
  65. default_path = "/";
  66. route = {
  67. ["GET room-census"] = function (event) return async_handler_wrapper(event,handle_get_room_census) end;
  68. };
  69. });
  70. end