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_size.lua 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. -- Prosody IM
  2. -- Copyright (C) 2017 Atlassian
  3. --
  4. -- This project is MIT/X11 licensed. Please see the
  5. -- COPYING file in the source package for more information.
  6. --
  7. -- This module requires net-url module
  8. -- Install it using #luarocks install net-url
  9. module:set_global(); -- Global module
  10. local split_jid = require "util.jid".split;
  11. local st = require "util.stanza";
  12. local it = require "util.iterators";
  13. local json = require "util.json";
  14. local iterators = require "util.iterators";
  15. local array = require"util.array";
  16. local tostring = tostring;
  17. local neturl = require "net.url";
  18. local parse = neturl.parseQuery;
  19. function get_room_from_jid(jid)
  20. local node, host = split_jid(jid);
  21. local component = hosts[host];
  22. if component then
  23. local muc = component.modules.muc
  24. if muc and rawget(muc,"rooms") then
  25. -- We're running 0.9.x or 0.10 (old MUC API)
  26. return muc.rooms[jid];
  27. elseif muc and rawget(muc,"get_room_from_jid") then
  28. -- We're running >0.10 (new MUC API)
  29. return muc.get_room_from_jid(jid);
  30. else
  31. return
  32. end
  33. end
  34. end
  35. function handle_get_room_size(event)
  36. local params = parse(event.request.url.query);
  37. local room_name = params["room"];
  38. local domain_name = params["domain"];
  39. local room_address = room_name .. "@" .. "conference." .. domain_name;
  40. local room = get_room_from_jid(room_address);
  41. local participant_count = 0;
  42. log("debug", "Querying room %s", tostring(room_address));
  43. if room then
  44. local occupants = room._occupants;
  45. if occupants then
  46. participant_count = iterators.count(room:each_occupant());
  47. end
  48. log("debug", "there are %s occupants in room", tostring(participant_count));
  49. else
  50. log("debug", "no such room exists");
  51. end
  52. if participant_count > 1 then
  53. participant_count = participant_count - 1;
  54. end
  55. local GET_response = {
  56. headers = {
  57. content_type = "application/json";
  58. };
  59. body = [[{"participants":]]..participant_count..[[}]];
  60. };
  61. return GET_response;
  62. end
  63. function handle_get_room (event)
  64. local params = parse(event.request.url.query);
  65. local room_name = params["room"];
  66. local domain_name = params["domain"];
  67. local room_address = room_name .. "@" .. "conference." .. domain_name;
  68. local room = get_room_from_jid(room_address);
  69. local participant_count = 0;
  70. local occupants_json = array();
  71. log("debug", "Querying room %s", tostring(room_address));
  72. if room then
  73. local occupants = room._occupants;
  74. if occupants then
  75. participant_count = iterators.count(room:each_occupant());
  76. for _, occupant in room:each_occupant() do
  77. -- filter focus as we keep it as hidden participant
  78. if string.sub(occupant.nick,-string.len("/focus"))~="/focus" then
  79. for _, pr in occupant:each_session() do
  80. local nick = pr:get_child_text("nick", "http://jabber.org/protocol/nick") or "";
  81. local email = pr:get_child_text("email") or "";
  82. occupants_json:push({
  83. jid = tostring(occupant.nick),
  84. email = tostring(email),
  85. display_name = tostring(nick)});
  86. end
  87. end
  88. end
  89. end
  90. log("debug", "there are %s occupants in room", tostring(participant_count));
  91. else
  92. log("debug", "no such room exists");
  93. end
  94. if participant_count > 1 then
  95. participant_count = participant_count - 1;
  96. end
  97. local GET_response = {
  98. headers = {
  99. content_type = "application/json";
  100. };
  101. body = json.encode(occupants_json);
  102. };
  103. return GET_response;
  104. end;
  105. function module.add_host(module)
  106. module:depends("http");
  107. module:provides("http", {
  108. default_path = "/";
  109. route = {
  110. ["GET room-size"] = handle_get_room_size;
  111. ["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end;
  112. ["GET room"] = handle_get_room;
  113. };
  114. });
  115. end