Changeset

9136:07fc2c5c0c2e

Merge jonasw->trunk
author Kim Alvefur <zash@zash.se>
date Mon, 06 Aug 2018 16:58:21 +0200
parents 9134:4aa786a180f5 (diff) 9135:c68d62ae9cb7 (current diff)
children 9137:4803d2feeecb
files plugins/mod_pubsub/pubsub.lib.lua
diffstat 7 files changed, 103 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES	Mon Aug 06 16:27:05 2018 +0200
+++ b/CHANGES	Mon Aug 06 16:58:21 2018 +0200
@@ -14,7 +14,9 @@
 -   PubSub features
     -   Persistence
     -   Affiliations
-    -   mod\_pep\_plus
+    -   Access models
+    -   "publish-options"
+-   PEP now uses our pubsub code and now shares the above features
 -   Asynchronous operations
 -   Busted for tests
 -   mod\_muc\_mam (XEP-0313 in groupchats)
--- a/plugins/mod_pep.lua	Mon Aug 06 16:27:05 2018 +0200
+++ b/plugins/mod_pep.lua	Mon Aug 06 16:58:21 2018 +0200
@@ -81,12 +81,17 @@
 	function store:set(node, data)
 		if data then
 			-- Save the data without subscriptions
-			-- TODO Save explicit subscriptions maybe?
+			local subscribers = {};
+			for jid, sub in pairs(data.subscribers) do
+				if type(sub) ~= "table" or not sub.presence then
+					subscribers[jid] = sub;
+				end
+			end
 			data = {
 				name = data.name;
 				config = data.config;
 				affiliations = data.affiliations;
-				subscribers = {};
+				subscribers = subscribers;
 			};
 		end
 		return node_config:set(username, node, data);
@@ -353,7 +358,7 @@
 	end
 
 	for node in nodes - current do
-		service:add_subscription(node, recipient, recipient);
+		service:add_subscription(node, recipient, recipient, { presence = true });
 		resend_last_item(recipient, node, service);
 	end
 
--- a/plugins/mod_pubsub/mod_pubsub.lua	Mon Aug 06 16:27:05 2018 +0200
+++ b/plugins/mod_pubsub/mod_pubsub.lua	Mon Aug 06 16:58:21 2018 +0200
@@ -150,6 +150,10 @@
 	end
 end
 
+function get_service()
+	return service;
+end
+
 function set_service(new_service)
 	service = new_service;
 	module.environment.service = service;
@@ -196,6 +200,7 @@
 				publish = true;
 				retract = true;
 				get_nodes = true;
+				get_configuration = true;
 
 				subscribe = true;
 				unsubscribe = true;
@@ -220,6 +225,7 @@
 				delete = true;
 				get_nodes = true;
 				configure = true;
+				get_configuration = true;
 
 				subscribe = true;
 				unsubscribe = true;
--- a/plugins/mod_pubsub/pubsub.lib.lua	Mon Aug 06 16:27:05 2018 +0200
+++ b/plugins/mod_pubsub/pubsub.lib.lua	Mon Aug 06 16:58:21 2018 +0200
@@ -12,8 +12,6 @@
 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
 
-local enable_publish_options = module:get_option_boolean("enable_publish_options", false);
-
 local _M = {};
 
 local handlers = {};
@@ -86,6 +84,16 @@
 	};
 	{
 		type = "list-single";
+		name = "pubsub#publish_model";
+		label = "Specify the publisher model";
+		options = {
+			{ value = "publishers" };
+			{ value = "subscribers" };
+			{ value = "open" };
+		};
+	};
+	{
+		type = "list-single";
 		name = "pubsub#notification_type";
 		label = "Specify the delivery style for notifications";
 		options = {
@@ -131,6 +139,7 @@
 	persist_items = "pubsub#persist_items";
 	notification_type = "pubsub#notification_type";
 	access_model = "pubsub#access_model";
+	publish_model = "pubsub#publish_model";
 };
 local reverse_config_field_map = {};
 for k, v in pairs(config_field_map) do reverse_config_field_map[v] = k; end
@@ -144,6 +153,7 @@
 		["pubsub#persist_items"] = node_config["persist_items"];
 		["pubsub#notification_type"] = node_config["notification_type"];
 		["pubsub#access_model"] = node_config["access_model"];
+		["pubsub#publish_model"] = node_config["publish_model"];
 	}
 end
 
@@ -152,6 +162,9 @@
 	for config_field, config_value in pairs(config) do
 		local mapped_name = reverse_config_field_map[config_field];
 		if mapped_name then
+			-- FIXME: The intention is to add "subtype" support to
+			-- util.dataforms, which will remove the need for this
+			-- ugly hack
 			if mapped_name == "max_items" then
 				config_value = tonumber(config_value);
 			end
@@ -183,7 +196,7 @@
 	get_items = { "retrieve-items" };
 	get_subscriptions = { "retrieve-subscriptions" };
 	node_defaults = { "retrieve-default" };
-	publish = { "publish", "multi-items", enable_publish_options and "publish-options" or nil };
+	publish = { "publish", "multi-items", "publish-options" };
 	purge = { "purge-nodes" };
 	retract = { "delete-items", "retract-items" };
 	set_node_config = { "config-node" };
@@ -548,7 +561,7 @@
 		return true;
 	end
 	local publish_options = stanza.tags[1]:get_child("publish-options");
-	if enable_publish_options and publish_options then
+	if publish_options then
 		-- Ensure that the node configuration matches the values in publish-options
 		local publish_options_form = publish_options:get_child("x", "jabber:x:data");
 		local required_config = config_from_xep0060(node_config_form:data(publish_options_form), true);
@@ -642,18 +655,12 @@
 		return true;
 	end
 
-	if not service:may(node, stanza.attr.from, "configure") then
-		origin.send(pubsub_error_reply(stanza, "forbidden"));
+	local ok, node_config = service:get_node_config(node, stanza.attr.from);
+	if not ok then
+		origin.send(pubsub_error_reply(stanza, node_config));
 		return true;
 	end
 
-	local node_obj = service.nodes[node];
-	if not node_obj then
-		origin.send(pubsub_error_reply(stanza, "item-not-found"));
-		return true;
-	end
-
-	local node_config = node_obj.config;
 	local pubsub_form_data = config_to_xep0060(node_config);
 	local reply = st.reply(stanza)
 		:tag("pubsub", { xmlns = xmlns_pubsub_owner })
@@ -678,7 +685,12 @@
 		origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing dataform"));
 		return true;
 	end
-	local form_data, err = node_config_form:data(config_form);
+	local ok, old_config = service:get_node_config(node, stanza.attr.from);
+	if not ok then
+		origin.send(pubsub_error_reply(stanza, old_config));
+		return true;
+	end
+	local form_data, err = node_config_form:data(config_form, old_config);
 	if not form_data then
 		origin.send(st.error_reply(stanza, "modify", "bad-request", err));
 		return true;
--- a/spec/util_dataforms_spec.lua	Mon Aug 06 16:27:05 2018 +0200
+++ b/spec/util_dataforms_spec.lua	Mon Aug 06 16:58:21 2018 +0200
@@ -347,5 +347,44 @@
 			assert.truthy(f:find("field/option"));
 		end);
 	end);
+
+	describe("using current values in place of missing fields", function ()
+		it("gets back the previous values when given an empty form", function ()
+			local current = {
+				["list-multi-field"] = {
+					"list-multi-option-value#2";
+				};
+				["list-single-field"] = "list-single-value#2";
+				["hidden-field"] = "hidden-value";
+				["boolean-field"] = false;
+				["text-multi-field"] = "words\ngo\nhere";
+				["jid-single-field"] = "alice@example.com";
+				["text-private-field"] = "hunter2";
+				["text-single-field"] = "text-single-value";
+				["jid-multi-field"] = {
+					"bob@example.net";
+				};
+			};
+			local expect = {
+				-- FORM_TYPE = "xmpp:prosody.im/spec/util.dataforms#1"; -- does this need to be included?
+				["list-multi-field"] = {
+					"list-multi-option-value#2";
+				};
+				["list-single-field"] = "list-single-value#2";
+				["hidden-field"] = "hidden-value";
+				["boolean-field"] = false;
+				["text-multi-field"] = "words\ngo\nhere";
+				["jid-single-field"] = "alice@example.com";
+				["text-private-field"] = "hunter2";
+				["text-single-field"] = "text-single-value";
+				["jid-multi-field"] = {
+					"bob@example.net";
+				};
+			};
+			local data, err = some_form:data(st.stanza("x", {xmlns="jabber:x:data"}), current);
+			assert.is.table(data, err);
+			assert.same(expect, data, "got back the same data");
+		end);
+	end);
 end);
 
--- a/util/dataforms.lua	Mon Aug 06 16:27:05 2018 +0200
+++ b/util/dataforms.lua	Mon Aug 06 16:58:21 2018 +0200
@@ -142,7 +142,7 @@
 
 local field_readers = {};
 
-function form_t.data(layout, stanza)
+function form_t.data(layout, stanza, current)
 	local data = {};
 	local errors = {};
 	local present = {};
@@ -157,7 +157,9 @@
 		end
 
 		if not tag then
-			if field.required then
+			if current and current[field.name] ~= nil then
+				data[field.name] = current[field.name];
+			elseif field.required then
 				errors[field.name] = "Required value missing";
 			end
 		elseif field.name then
--- a/util/pubsub.lua	Mon Aug 06 16:27:05 2018 +0200
+++ b/util/pubsub.lua	Mon Aug 06 16:58:21 2018 +0200
@@ -17,6 +17,7 @@
 	["persist_items"] = false;
 	["max_items"] = 20;
 	["access_model"] = "open";
+	["publish_model"] = "publishers";
 };
 local default_node_config_mt = { __index = default_node_config };
 
@@ -365,7 +366,19 @@
 
 function service:publish(node, actor, id, item)
 	-- Access checking
-	if not self:may(node, actor, "publish") then
+	local may_publish = false;
+
+	if self:may(node, actor, "publish") then
+		may_publish = true;
+	else
+		local node_obj = self.nodes[node];
+		local publish_model = node_obj and node_obj.config.publish_model;
+		if publish_model == "open"
+		or (publish_model == "subscribers" and node_obj.subscribers[actor]) then
+			may_publish = true;
+		end
+	end
+	if not may_publish then
 		return false, "forbidden";
 	end
 	--
@@ -565,6 +578,8 @@
 		return false, "item-not-found";
 	end
 
+	setmetatable(new_config, {__index=self.node_defaults})
+
 	if self.config.check_node_config then
 		local ok = self.config.check_node_config(node, actor, new_config);
 		if not ok then
@@ -573,7 +588,7 @@
 	end
 
 	local old_config = node_obj.config;
-	node_obj.config = setmetatable(new_config, {__index=self.node_defaults});
+	node_obj.config = new_config;
 
 	if self.config.nodestore then
 		local ok, err = save_node_to_store(self, node_obj);