Fix mod_muc_moderation + anonymize moderation events (#137)

This commit is contained in:
John Livingston 2024-07-29 13:12:25 +02:00
parent 6e92882176
commit c5bcb9fc14
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
2 changed files with 59 additions and 16 deletions

View File

@ -39,6 +39,30 @@ local function remove_actor(event)
end end
end end
local function remove_moderate_actor(event)
local room, announcement, tombstone = event.room, event.announcement, event.tombstone;
if not get_anonymize_moderation_actions(room) then
return;
end
local moderated = announcement:find("{urn:xmpp:fasten:0}apply-to/{urn:xmpp:message-moderate:0}moderated");
if moderated then
module:log("debug", "We must anonymize the moderation announcement for stanza %s", event.stanza_id);
moderated.attr.by = nil;
moderated:remove_children("occupant-id", "urn:xmpp:occupant-id:0");
end
if tombstone then
local moderated = tombstone:get_child("moderated", "urn:xmpp:message-moderate:0");
if moderated then
module:log("debug", "We must anonymize the moderation tombstone for stanza %s", event.stanza_id);
moderated.attr.by = nil;
moderated:remove_children("occupant-id", "urn:xmpp:occupant-id:0");
end
end
end
module:hook("muc-config-submitted/muc#roomconfig_anonymize_moderation_actions", config_submitted); module:hook("muc-config-submitted/muc#roomconfig_anonymize_moderation_actions", config_submitted);
module:hook("muc-config-form", add_form_option, form_position); module:hook("muc-config-form", add_form_option, form_position);
module:hook("muc-broadcast-presence", remove_actor); module:hook("muc-broadcast-presence", remove_actor);
module:hook("muc-moderate-message", remove_moderate_actor);

View File

@ -107,29 +107,48 @@ local function moderate(actor, room_jid, stanza_id, retract, reason)
announcement:add_direct_child(moderated_occupant_id); announcement:add_direct_child(moderated_occupant_id);
end end
announcement:reset();
local tombstone = nil;
if muc_log_archive.set and retract then if muc_log_archive.set and retract then
local tombstone = st.message({ from = original.attr.from, type = "groupchat", id = original.attr.id }) tombstone = st.message({ from = original.attr.from, type = "groupchat", id = original.attr.id })
:tag("moderated", { xmlns = xmlns_moderate, by = actor_nick }) :tag("moderated", { xmlns = xmlns_moderate, by = actor_nick })
:tag("retracted", { xmlns = xmlns_retract, stamp = dt.datetime() }); :tag("retracted", { xmlns = xmlns_retract, stamp = dt.datetime() }):up();
if room.get_occupant_id then
tombstone:add_child(st.stanza("occupant-id", { xmlns = xmlns_occupant_id; id = room:get_occupant_id(actor_occupant) }));
end
tombstone:up();
if room.get_occupant_id then
if moderated_occupant_id then
-- Copy occupant id from moderated message
tombstone:add_child(moderated_occupant_id);
end
end
if reason then if reason then
tombstone:text_tag("reason", reason); tombstone:text_tag("reason", reason);
end end
tombstone:reset();
if room.get_occupant_id then
if actor_occupant then
tombstone:add_child(st.stanza("occupant-id", { xmlns = xmlns_occupant_id; id = room:get_occupant_id(actor_occupant) }));
end
if moderated_occupant_id then
-- Copy occupant id from moderated message
tombstone:add_direct_child(moderated_occupant_id);
end
end
tombstone:reset();
end
-- fire an event, that can be used to cancel the moderation, or modify stanzas.
local event = {
room = room;
announcement = announcement;
tombstone = tombstone;
stanza_id = stanza_id;
retract = retract;
reason = reason;
actor = actor;
actor_nick = actor_nick;
};
if module:fire_event("muc-moderate-message", event) then
-- TODO: allow to change the error message?
return false, "wait", "internal-server-error";
end
if tombstone then
local was_replaced = muc_log_archive:set(room_node, stanza_id, tombstone); local was_replaced = muc_log_archive:set(room_node, stanza_id, tombstone);
if not was_replaced then if not was_replaced then
return false, "wait", "internal-server-error"; return false, "wait", "internal-server-error";