Mercurial > prosody-modules
annotate mod_http_user_count/mod_http_user_count.lua @ 4974:807007913f67
mod_log_json: Prefer native Lua table.pack over Prosody util.table one
Prosody is removing support for Lua 5.1, which was the reason for
util.table.pack to exist in the first place, since Lua 5.2+ provides
table.pack. In prosody rev 5eaf77114fdb everything was switched over to
use table.pack, opening the door for removing util.table.pack at some
point. This change here is to prepare for that future eventuality.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 11 Jul 2022 20:08:41 +0200 |
parents | a45f2f79e99b |
children |
rev | line source |
---|---|
1263
fe29627a5ed8
mod_http_user_count: HTTP module to report the current number of online users or sessions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local it = require "util.iterators"; |
1830
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
2 local jid_split = require "util.jid".prepped_split; |
1263
fe29627a5ed8
mod_http_user_count: HTTP module to report the current number of online users or sessions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
fe29627a5ed8
mod_http_user_count: HTTP module to report the current number of online users or sessions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 module:depends("http"); |
fe29627a5ed8
mod_http_user_count: HTTP module to report the current number of online users or sessions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
1830
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
6 local function check_muc(jid) |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
7 local room_name, host = jid_split(jid); |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
8 if not hosts[host] then |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
9 return nil, "No such host: "..host; |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
10 elseif not hosts[host].modules.muc then |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
11 return nil, "Host '"..host.."' is not a MUC service"; |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
12 end |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
13 return room_name, host; |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
14 end |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
15 |
1263
fe29627a5ed8
mod_http_user_count: HTTP module to report the current number of online users or sessions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 module:provides("http", { |
fe29627a5ed8
mod_http_user_count: HTTP module to report the current number of online users or sessions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 route = { |
fe29627a5ed8
mod_http_user_count: HTTP module to report the current number of online users or sessions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 ["GET /sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end; |
fe29627a5ed8
mod_http_user_count: HTTP module to report the current number of online users or sessions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 ["GET /users"] = function () return tostring(it.count(it.keys(prosody.bare_sessions))); end; |
1830
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
20 ["GET /host"] = function () return tostring(it.count(it.keys(prosody.hosts[module.host].sessions))); end; |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
21 ["GET /room/*"] = function (request, room_jid) |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
22 local name, host = check_muc(room_jid); |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
23 if not name then |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
24 return "0"; |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
25 end |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
26 local room = prosody.hosts[host].modules.muc.rooms[name.."@"..host]; |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
27 if not room then |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
28 return "0"; |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
29 end |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
30 return tostring(it.count(it.keys(room._occupants))); |
a45f2f79e99b
mod_http_user_count: Support for host and MUC session counts
Matthew Wild <mwild1@gmail.com>
parents:
1263
diff
changeset
|
31 end; |
1263
fe29627a5ed8
mod_http_user_count: HTTP module to report the current number of online users or sessions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 }; |
fe29627a5ed8
mod_http_user_count: HTTP module to report the current number of online users or sessions
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 }); |