Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

mod_muc_size.lua 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 room = get_room_from_jid(room_name);
  39. local participant_count = 0;
  40. log("debug", "Querying room %s", tostring(room_name));
  41. if room then
  42. local occupants = room._occupants;
  43. if occupants then
  44. participant_count = iterators.count(room:each_occupant());
  45. end
  46. log("debug", "there are %s occupants in room", tostring(participant_count));
  47. else
  48. log("debug", "no such room exists");
  49. end
  50. if participant_count > 1 then
  51. participant_count = participant_count - 1;
  52. end
  53. local GET_response = {
  54. headers = {
  55. content_type = "application/json";
  56. };
  57. body = [[{"participants":]]..participant_count..[[}]];
  58. };
  59. return GET_response;
  60. end
  61. function handle_get_room (event)
  62. local params = parse(event.request.url.query);
  63. local room_name = params["room"];
  64. local room = get_room_from_jid(room_name);
  65. local participant_count = 0;
  66. local occupants_json = array();
  67. log("debug", "Querying room %s", tostring(room_name));
  68. if room then
  69. local occupants = room._occupants;
  70. if occupants then
  71. participant_count = iterators.count(room:each_occupant());
  72. for _, occupant in room:each_occupant() do
  73. -- filter focus as we keep it as hidden participant
  74. if string.sub(occupant.nick,-string.len("/focus"))~="/focus" then
  75. for _, pr in occupant:each_session() do
  76. local nick = pr:get_child_text("nick", "http://jabber.org/protocol/nick") or "";
  77. local email = pr:get_child_text("email") or "";
  78. occupants_json:push({
  79. jid = tostring(occupant.nick),
  80. email = tostring(email),
  81. display_name = tostring(nick)});
  82. end
  83. end
  84. end
  85. end
  86. log("debug", "there are %s occupants in room", tostring(participant_count));
  87. else
  88. log("debug", "no such room exists");
  89. end
  90. if participant_count > 1 then
  91. participant_count = participant_count - 1;
  92. end
  93. local GET_response = {
  94. headers = {
  95. content_type = "application/json";
  96. };
  97. body = json.encode(occupants_json);
  98. };
  99. return GET_response;
  100. end;
  101. function module.add_host(module)
  102. module:depends("http");
  103. module:provides("http", {
  104. default_path = "/";
  105. route = {
  106. ["GET room-size"] = handle_get_room_size;
  107. ["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end;
  108. ["GET room"] = handle_get_room;
  109. };
  110. });
  111. end