changeset 13906:0749265327d2

MUC: Record timestamps and actors of affiliation changes This can help with things like timed affiliations or cleanup of obsolete affiliations, identifying recent changes (e.g. to allow efficient catch-up by clients when changes to the affiliation list occur), and such, as well as simple auditing.
author Matthew Wild <mwild1@gmail.com>
date Thu, 26 Jun 2025 14:39:45 +0100
parents 0e346f4d88ce
children 827a4cfd3fad
files plugins/muc/muc.lib.lua
diffstat 1 files changed, 11 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/muc/muc.lib.lua	Fri Jun 20 18:55:39 2025 +0100
+++ b/plugins/muc/muc.lib.lua	Thu Jun 26 14:39:45 2025 +0100
@@ -24,6 +24,7 @@
 local base64 = require "prosody.util.encodings".base64;
 local hmac_sha256 = require "prosody.util.hashes".hmac_sha256;
 local new_id = require "prosody.util.id".medium;
+local time = require "prosody.util.time";
 
 local log = module._log;
 
@@ -1457,6 +1458,15 @@
 		end
 	end
 
+	data = data or {};
+	local old_data = self._affiliation_data[jid];
+
+	local current_time = time.now();
+	data.affiliation_created_at = old_data and old_data.affiliation_created_at or current_time;
+	data.affiliation_created_by = old_data and old_data.affiliation_created_by or actor;
+	data.affiliation_modified_at = current_time;
+	data.affiliation_modified_by = actor;
+
 	local event_data = {
 		room = self;
 		actor = actor;
@@ -1464,7 +1474,7 @@
 		affiliation = affiliation or "none";
 		reason = reason;
 		previous_affiliation = target_affiliation or "none";
-		data = data and data or nil; -- coerce false to nil
+		data = data;
 		previous_data = self._affiliation_data[jid] or nil;
 	};
 
@@ -1473,10 +1483,6 @@
 		local err = event_data.error or { type = "cancel", condition = "not-allowed" };
 		return nil, err.type, err.condition;
 	end
-	if affiliation and not data and event_data.data then
-		-- Allow handlers to add data when none was going to be set
-		data = event_data.data;
-	end
 
 	-- Set in 'database'
 	self._affiliations[jid] = affiliation;