comparison mod_muc_mav/mod_muc_mav.lua @ 6543:19bee2a6436a

mod_muc_mav: allow to work even in very large rooms Tested on a MUC with almost 5000 affiliations
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Tue, 19 May 2026 13:31:35 -0500
parents d7162714f8b2
children
comparison
equal deleted inserted replaced
6542:d7162714f8b2 6543:19bee2a6436a
10 local xmlns_muc_user = "http://jabber.org/protocol/muc#user"; 10 local xmlns_muc_user = "http://jabber.org/protocol/muc#user";
11 local xmlns_occupant_id = "urn:xmpp:occupant-id:0"; 11 local xmlns_occupant_id = "urn:xmpp:occupant-id:0";
12 12
13 local mav_versions = {} 13 local mav_versions = {}
14 14
15 local function get_affiliations(from, room) 15 local function get_affiliations(from, room, hash_only)
16 local mav_since = nil
16 local jid_affiliations = {} 17 local jid_affiliations = {}
18 local xs = {}
17 local x = st.stanza("x", {xmlns = xmlns_muc_user;}); 19 local x = st.stanza("x", {xmlns = xmlns_muc_user;});
20 local count = 0
18 for jid, affiliation, affiliation_data in room:each_affiliation() do 21 for jid, affiliation, affiliation_data in room:each_affiliation() do
19 table.insert(jid_affiliations, jid.."\0"..affiliation); 22 table.insert(jid_affiliations, jid.."\0"..affiliation);
20 local occupant_id = room:get_occupant_id_from_jid(jid); 23 if not hash_only then
21 local nick = affiliation_data and affiliation_data.reserved_nickname or nil; 24 local occupant_id = room:get_occupant_id_from_jid(jid);
22 local show_jid = (not room:is_anonymous_for(from) or util_jid.bare(from) == jid) and jid or nil 25 local nick = affiliation_data and affiliation_data.reserved_nickname or nil;
23 x:tag("item", {affiliation = affiliation or "none"; jid = show_jid; nick = nick;}) 26 local show_jid = (not room:is_anonymous_for(from) or util_jid.bare(from) == jid) and jid or nil
24 :tag("occupant-id", { xmlns = xmlns_occupant_id, id = occupant_id }):up():up(); 27 x:tag("item", {affiliation = affiliation or "none"; jid = show_jid; nick = nick;})
28 :tag("occupant-id", { xmlns = xmlns_occupant_id, id = occupant_id }):up():up();
29 count = count + 1
30 if count > 400 then
31 table.sort(jid_affiliations)
32 local hash_input = table.concat(jid_affiliations, "\0")
33 local hash = base64.encode(hashes.sha256(hash_input))
34 x:tag("mav", {xmlns = xmlns_mav; since = mav_since; ["until"] = hash;}):up()
35 table.insert(xs, x)
36 x = st.stanza("x", {xmlns = xmlns_muc_user;})
37 count = 0
38 mav_since = hash
39 end
40 end
25 end 41 end
26 table.sort(jid_affiliations); 42 table.sort(jid_affiliations);
27 local hash_input = table.concat(jid_affiliations, "\0"); 43 local hash_input = table.concat(jid_affiliations, "\0");
28 local hash = base64.encode(hashes.sha256(hash_input)) 44 local hash = base64.encode(hashes.sha256(hash_input))
29 x:tag("mav", {xmlns = xmlns_mav; ["until"] = hash;}):up(); 45 if not hash_only and (count > 0 or #xs < 1) then
30 return hash, x; 46 x:tag("mav", {xmlns = xmlns_mav; since = mav_since; ["until"] = hash;}):up();
47 table.insert(xs, x);
48 end
49 return hash, xs;
31 end 50 end
32 51
33 local function handle_stanza_with_x(room, stanza, mav_current) 52 local function handle_stanza_with_x(room, stanza, mav_current)
34 local muc_x = stanza:get_child("x", xmlns_muc_user); 53 local muc_x = stanza:get_child("x", xmlns_muc_user);
35 local is_initial = false; 54 local is_initial = false;
37 if child.attr.code == "110" then is_initial = true; end; 56 if child.attr.code == "110" then is_initial = true; end;
38 end 57 end
39 if is_initial then 58 if is_initial then
40 muc_x:tag("mav", {xmlns = xmlns_mav; ["until"] = mav_current;}):up(); 59 muc_x:tag("mav", {xmlns = xmlns_mav; ["until"] = mav_current;}):up();
41 else 60 else
42 local mav_new = get_affiliations(stanza.attr.from, room); 61 -- TODO: stop recomputing the hash on every stanza in the presence flood
62 local mav_new = get_affiliations(stanza.attr.from, room, true);
43 if mav_current ~= mav_new then 63 if mav_current ~= mav_new then
44 module:log("info", "Update version of %s from %s to %s in affiliation update", stanza.attr.to, mav_current, mav_new); 64 module:log("info", "Update version of %s from %s to %s in affiliation update", stanza.attr.to, mav_current, mav_new);
45 muc_x:tag("mav", {xmlns = xmlns_mav; ["since"] = mav_current; ["until"] = mav_new; }):up(); 65 muc_x:tag("mav", {xmlns = xmlns_mav; ["since"] = mav_current; ["until"] = mav_new; }):up();
46 mav_versions[room.jid.."\0"..stanza.attr.to] = mav_new; 66 mav_versions[room.jid.."\0"..stanza.attr.to] = mav_new;
47 end 67 end
79 if not muc_x then return; end 99 if not muc_x then return; end
80 local mav_child = muc_x:get_child("mav", xmlns_mav); 100 local mav_child = muc_x:get_child("mav", xmlns_mav);
81 if not mav_child then return; end 101 if not mav_child then return; end
82 local mav_since = mav_child.attr["since"]; 102 local mav_since = mav_child.attr["since"];
83 mav_versions[room.jid.."\0"..stanza.attr.from] = mav_since; 103 mav_versions[room.jid.."\0"..stanza.attr.from] = mav_since;
84 local mav_current, x = get_affiliations(stanza.attr.from, room); 104 local mav_current, xs = get_affiliations(stanza.attr.from, room);
85 if mav_current ~= mav_since then 105 if mav_current ~= mav_since then
86 -- TODO: Support for diffs 106 -- TODO: Support for diffs
87 -- Send full response 107 -- Send full response
88 module:log("info", "Old version of %s is %s, sending full response for %s", stanza.attr.from, mav_since, mav_current); 108 module:log("info", "Old version of %s is %s, sending full response for %s", stanza.attr.from, mav_since, mav_current);
89 local announce_msg = st.message({ from = room.jid, to = stanza.attr.from }):add_child(x); 109 for _, x in ipairs(xs) do
90 room:route_stanza(announce_msg); 110 local announce_msg = st.message({ from = room.jid, to = stanza.attr.from }):add_child(x);
111 room:route_stanza(announce_msg);
112 end
91 mav_versions[room.jid.."\0"..stanza.attr.from] = mav_current; 113 mav_versions[room.jid.."\0"..stanza.attr.from] = mav_current;
92 else 114 else
93 module:log("info", "Old version of %s is %s, still current", stanza.attr.from, mav_since); 115 module:log("info", "Old version of %s is %s, still current", stanza.attr.from, mav_since);
94 end 116 end
95 end, -100); 117 end, -100);