Changeset

10734:136c41a3d03c

mod_mam: Factor out "should we store this" into a function Meant to improve readability and ease further improvements to this algorithm.
author Kim Alvefur <zash@zash.se>
date Tue, 21 Apr 2020 00:53:21 +0200
parents 10733:89e0f5cb60a1
children 10735:f2838ffcc499
files plugins/mod_mam/mod_mam.lua
diffstat 1 files changed, 21 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_mam/mod_mam.lua	Sat Apr 18 19:36:26 2020 +0200
+++ b/plugins/mod_mam/mod_mam.lua	Tue Apr 21 00:53:21 2020 +0200
@@ -263,11 +263,28 @@
 	return stanza;
 end
 
+local function should_store(stanza) --> boolean, reason: string
+	local orig_type = stanza.attr.type or "normal";
+	-- We store chat messages or normal messages that have a body
+	if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then
+		return false, "type";
+	end
+
+	-- or if hints suggest we shouldn't
+	if not stanza:get_child("store", "urn:xmpp:hints") then -- No hint telling us we should store
+		if stanza:get_child("no-permanent-store", "urn:xmpp:hints")
+			or stanza:get_child("no-store", "urn:xmpp:hints") then -- Hint telling us we should NOT store
+			return false, "hint";
+		end
+	end
+
+	return true, "default";
+end
+
 -- Handle messages
 local function message_handler(event, c2s)
 	local origin, stanza = event.origin, event.stanza;
 	local log = c2s and origin.log or module._log;
-	local orig_type = stanza.attr.type or "normal";
 	local orig_from = stanza.attr.from;
 	local orig_to = stanza.attr.to or orig_from;
 	-- Stanza without 'to' are treated as if it was to their own bare jid
@@ -280,21 +297,12 @@
 	-- Filter out <stanza-id> that claim to be from us
 	event.stanza = strip_stanza_id(stanza, store_user);
 
-	-- We store chat messages or normal messages that have a body
-	if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then
-		log("debug", "Not archiving stanza: %s (type)", stanza:top_tag());
+	local should, why = should_store(stanza);
+	if not should then
+		log("debug", "Not archiving stanza: %s (%s)", stanza:top_tag(), why);
 		return;
 	end
 
-	-- or if hints suggest we shouldn't
-	if not stanza:get_child("store", "urn:xmpp:hints") then -- No hint telling us we should store
-		if stanza:get_child("no-permanent-store", "urn:xmpp:hints")
-			or stanza:get_child("no-store", "urn:xmpp:hints") then -- Hint telling us we should NOT store
-			log("debug", "Not archiving stanza: %s (hint)", stanza:top_tag());
-			return;
-		end
-	end
-
 	local clone_for_storage;
 	if not strip_tags:empty() then
 		clone_for_storage = st.clone(stanza);