comparison mod_muc_activity/mod_muc_activity.lua @ 6110:9db1529c06c2

Merge upstream
author tmolitor <thilo@eightysoft.de>
date Sun, 05 Jan 2025 17:50:02 +0100
parents a0429c322454
children
comparison
equal deleted inserted replaced
6109:4cb1cad2badd 6110:9db1529c06c2
1 local os_time = os.time;
2 local math_floor = math.floor;
3 local store = module:open_store("muc_activity", "keyval");
4
5 local accumulator = {};
6
7 local field_var = "{urn:xmpp:muc-activity}message-activity";
8
9 module:hook("muc-room-destroyed", function (event)
10 local jid = event.room.jid;
11 module:log("debug", "deleting activity data for destroyed muc %s", jid);
12 store:set_keys(jid, {});
13 accumulator[jid] = nil;
14 end)
15
16 module:hook("muc-occupant-groupchat", function (event)
17 local jid = event.room.jid;
18 if not event.room:get_persistent() then
19 -- we do not count stanzas in non-persistent rooms
20 if accumulator[jid] then
21 -- if we have state for the room, drop it.
22 store:set_keys(jid, {});
23 accumulator[jid] = nil;
24 end
25
26 return
27 end
28
29 if event.stanza:get_child("body") == nil then
30 -- we do not count stanzas without body.
31 return
32 end
33
34 module:log("debug", "counting stanza for MUC activity in %s", jid);
35 accumulator[jid] = (accumulator[jid] or 0) + 1;
36 end)
37
38 local function shift(data)
39 for i = 1, 23 do
40 data[i] = data[i+1]
41 end
42 end
43
44 local function accumulate(data)
45 if data == nil then
46 return 0;
47 end
48 local accum = 0;
49 for i = 1, 24 do
50 local v = data[i];
51 if v ~= nil then
52 accum = accum + v
53 end
54 end
55 return accum;
56 end
57
58 module:hourly("muc-activity-shift", function ()
59 module:log("info", "shifting MUC activity store forward by one hour");
60 for jid in store:users() do
61 local data = store:get(jid);
62 local new = accumulator[jid] or 0;
63 shift(data);
64 data[24] = new;
65 accumulator[jid] = nil;
66 store:set(jid, data);
67 end
68
69 -- All remaining entries in the accumulator are non-existent in the store,
70 -- otherwise they would have been removed earlier.
71 for jid, count in pairs(accumulator) do
72 store:set(jid, { [24] = count });
73 end
74 accumulator = {};
75 end)
76
77 module:hook("muc-disco#info", function(event)
78 local room = event.room;
79 local jid = room.jid;
80 if not room:get_persistent() or not room:get_public() or room:get_members_only() or room:get_password() ~= nil then
81 module:log("debug", "%s is not persistent or not public, not injecting message activity", jid);
82 return;
83 end
84 local count = accumulate(store:get(jid)) / 24.0;
85 table.insert(event.form, { name = field_var, label = "Message activity" });
86 event.formdata[field_var] = tostring(count);
87 end);