Changeset

8539:41b5f070d7bb

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Sat, 24 Feb 2018 11:51:43 +0100
parents 8537:d7f31badd359 (current diff) 8538:3eb4cafb3b64 (diff)
children 8540:ba9fd886b34d
files
diffstat 2 files changed, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_mam/mamprefs.lib.lua	Sat Feb 24 11:50:08 2018 +0100
+++ b/plugins/mod_mam/mamprefs.lib.lua	Sat Feb 24 11:51:43 2018 +0100
@@ -14,6 +14,7 @@
 if global_default_policy ~= "roster" then
 	global_default_policy = module:get_option_boolean("default_archive_policy", global_default_policy);
 end
+local smart_enable = module:get_option_boolean("mam_smart_enable", false);
 
 do
 	-- luacheck: ignore 211/prefs_format
@@ -30,17 +31,37 @@
 local archive_store = module:get_option_string("archive_store", "archive");
 local prefs = module:open_store(archive_store .. "_prefs");
 
-local function get_prefs(user)
+local function get_prefs(user, explicit)
 	local user_sessions = sessions[user];
 	local user_prefs = user_sessions and user_sessions.archive_prefs
 	if not user_prefs then
+		-- prefs not cached
 		user_prefs = prefs:get(user);
+		if not user_prefs then
+			-- prefs not set
+			if smart_enable and explicit then
+				-- a mam-capable client was involved in this action, set defaults
+				user_prefs = { [false] = global_default_policy };
+				prefs:set(user, user_prefs);
+			end
+		end
 		if user_sessions then
+			-- cache settings if they originate from user action
 			user_sessions.archive_prefs = user_prefs;
 		end
+		if not user_prefs then
+			if smart_enable then
+				-- not yet enabled, either explicitly or "smart"
+				user_prefs = { [false] = false };
+			else
+				-- no explicit settings, return defaults
+				user_prefs = { [false] = global_default_policy };
+			end
+		end
 	end
-	return user_prefs or { [false] = global_default_policy };
+	return user_prefs;
 end
+
 local function set_prefs(user, user_prefs)
 	local user_sessions = sessions[user];
 	if user_sessions then
--- a/plugins/mod_mam/mod_mam.lua	Sat Feb 24 11:50:08 2018 +0100
+++ b/plugins/mod_mam/mod_mam.lua	Sat Feb 24 11:51:43 2018 +0100
@@ -76,7 +76,7 @@
 			return true;
 		end
 	end
-	local prefs = prefs_to_stanza(get_prefs(user));
+	local prefs = prefs_to_stanza(get_prefs(user, true));
 	local reply = st.reply(stanza):add_child(prefs);
 	origin.send(reply);
 	return true;
@@ -92,6 +92,7 @@
 -- Serve form
 module:hook("iq-get/self/"..xmlns_mam..":query", function(event)
 	local origin, stanza = event.origin, event.stanza;
+	get_prefs(origin.username, true);
 	origin.send(st.reply(stanza):query(xmlns_mam):add_child(query_form:form()));
 	return true;
 end);
@@ -102,6 +103,7 @@
 	local query = stanza.tags[1];
 	local qid = query.attr.queryid;
 
+	get_prefs(origin.username, true);
 	schedule_cleanup(origin.username);
 
 	-- Search query parameters