aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mod_pubsub/mod_pubsub.lua
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/mod_pubsub/mod_pubsub.lua')
-rw-r--r--plugins/mod_pubsub/mod_pubsub.lua85
1 files changed, 60 insertions, 25 deletions
diff --git a/plugins/mod_pubsub/mod_pubsub.lua b/plugins/mod_pubsub/mod_pubsub.lua
index c13630c9..ef31f326 100644
--- a/plugins/mod_pubsub/mod_pubsub.lua
+++ b/plugins/mod_pubsub/mod_pubsub.lua
@@ -4,6 +4,7 @@ local jid_bare = require "util.jid".bare;
local usermanager = require "core.usermanager";
local new_id = require "util.id".medium;
local storagemanager = require "core.storagemanager";
+local xtemplate = require "util.xtemplate";
local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
@@ -39,16 +40,32 @@ end
-- get(node_name)
-- users(): iterator over (node_name)
+local max_max_items = module:get_option_number("pubsub_max_items", 256);
+
+local function tonumber_max_items(n)
+ if n == "max" then
+ return max_max_items;
+ end
+ return tonumber(n);
+end
+
+for _, field in ipairs(lib_pubsub.node_config_form) do
+ if field.var == "pubsub#max_items" then
+ field.range_max = max_max_items;
+ break;
+ end
+end
local node_store = module:open_store(module.name.."_nodes");
-local function create_simple_itemstore(node_config, node_name)
+local function create_simple_itemstore(node_config, node_name) --> util.cache like object
local driver = storagemanager.get_driver(module.host, "pubsub_data");
local archive = driver:open("pubsub_"..node_name, "archive");
- return lib_pubsub.archive_itemstore(archive, node_config, nil, node_name);
+ local max_items = tonumber_max_items(node_config["max_items"]);
+ return lib_pubsub.archive_itemstore(archive, max_items, nil, node_name);
end
-function simple_broadcast(kind, node, jids, item, actor, node_obj)
+function simple_broadcast(kind, node, jids, item, actor, node_obj, service) --luacheck: ignore 431/service
if node_obj then
if node_obj.config["notify_"..kind] == false then
return;
@@ -65,8 +82,10 @@ function simple_broadcast(kind, node, jids, item, actor, node_obj)
if node_obj and node_obj.config.include_payload == false then
item:maptags(function () return nil; end);
end
- if expose_publisher and actor then
- item.attr.publisher = actor
+ if not expose_publisher then
+ item.attr.publisher = nil;
+ elseif not item.attr.publisher and actor ~= true then
+ item.attr.publisher = service.config.normalize_jid(actor);
end
end
end
@@ -75,17 +94,17 @@ function simple_broadcast(kind, node, jids, item, actor, node_obj)
local msg_type = node_obj and node_obj.config.notification_type or "headline";
local message = st.message({ from = module.host, type = msg_type, id = id })
:tag("event", { xmlns = xmlns_pubsub_event })
- :tag(kind, { node = node })
+ :tag(kind, { node = node });
if item then
message:add_child(item);
end
local summary;
- -- Compose a sensible textual representation of at least Atom payloads
if item and item.tags[1] then
local payload = item.tags[1];
- summary = module:fire_event("pubsub-summary/"..payload.attr.xmlns, {
+ local payload_type = node_obj and node_obj.config.payload_type or payload.attr.xmlns;
+ summary = module:fire_event("pubsub-summary/"..payload_type, {
kind = kind, node = node, jids = jids, actor = actor, item = item, payload = payload,
});
end
@@ -100,12 +119,12 @@ function simple_broadcast(kind, node, jids, item, actor, node_obj)
end
end
-local max_max_items = module:get_option_number("pubsub_max_items", 256);
-function check_node_config(node, actor, new_config) -- luacheck: ignore 212/actor 212/node
- if (new_config["max_items"] or 1) > max_max_items then
+function check_node_config(node, actor, new_config) -- luacheck: ignore 212/node 212/actor
+ if (tonumber_max_items(new_config["max_items"]) or 1) > max_max_items then
return false;
end
- if new_config["access_model"] ~= "whitelist" and new_config["access_model"] ~= "open" then
+ if new_config["access_model"] ~= "whitelist"
+ and new_config["access_model"] ~= "open" then
return false;
end
return true;
@@ -115,19 +134,18 @@ function is_item_stanza(item)
return st.is_stanza(item) and item.attr.xmlns == xmlns_pubsub and item.name == "item" and #item.tags == 1;
end
-module:hook("pubsub-summary/http://www.w3.org/2005/Atom", function (event)
- local payload = event.payload;
- local title = payload:get_child_text("title");
- local summary = payload:get_child_text("summary");
- if not summary and title then
- local author = payload:find("author/name#");
- summary = title;
- if author then
- summary = author .. " posted " .. summary;
- end
- end
- return summary;
-end, -1);
+-- Compose a textual representation of Atom payloads
+local summary_templates = module:get_option("pubsub_summary_templates", {
+ ["http://www.w3.org/2005/Atom"] = "{summary|or{{author/name|and{{author/name} posted }}{title}}}";
+})
+
+for pubsub_type, template in pairs(summary_templates) do
+ module:hook("pubsub-summary/"..pubsub_type, function (event)
+ local payload = event.payload;
+ return xtemplate.render(template, payload, tostring);
+ end, -1);
+end
+
module:hook("iq/host/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
module:hook("iq/host/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
@@ -172,6 +190,17 @@ end
function set_service(new_service)
service = new_service;
+ service.config.autocreate_on_publish = autocreate_on_publish;
+ service.config.autocreate_on_subscribe = autocreate_on_subscribe;
+ service.config.expose_publisher = expose_publisher;
+
+ service.config.nodestore = node_store;
+ service.config.itemstore = create_simple_itemstore;
+ service.config.broadcaster = simple_broadcast;
+ service.config.itemcheck = is_item_stanza;
+ service.config.check_node_config = check_node_config;
+ service.config.get_affiliation = get_affiliation;
+
module.environment.service = service;
add_disco_features_from_service(service);
end
@@ -190,7 +219,12 @@ function module.load()
set_service(pubsub.new({
autocreate_on_publish = autocreate_on_publish;
autocreate_on_subscribe = autocreate_on_subscribe;
+ expose_publisher = expose_publisher;
+ node_defaults = {
+ ["persist_items"] = true;
+ };
+ max_items = max_max_items;
nodestore = node_store;
itemstore = create_simple_itemstore;
broadcaster = simple_broadcast;
@@ -198,6 +232,7 @@ function module.load()
check_node_config = check_node_config;
get_affiliation = get_affiliation;
+ jid = module.host;
normalize_jid = jid_bare;
}));
end