changeset 4861:5fadb991003d

mod_groups_muc_bookmarks: Switch to XEP-0402 Rationale: Look at the diffstat
author Kim Alvefur <zash@zash.se>
date Sat, 08 Jan 2022 02:57:36 +0100
parents 4a34ed2bb9a2
children 9a8a43d0faed
files mod_groups_muc_bookmarks/mod_groups_muc_bookmarks.lua
diffstat 1 files changed, 24 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/mod_groups_muc_bookmarks/mod_groups_muc_bookmarks.lua	Sat Jan 08 02:28:16 2022 +0100
+++ b/mod_groups_muc_bookmarks/mod_groups_muc_bookmarks.lua	Sat Jan 08 02:57:36 2022 +0100
@@ -5,63 +5,46 @@
 local mod_groups = module:depends("groups_internal")
 local mod_pep = module:depends("pep")
 
-local PUBSUB_NODE_XEP0048 = "storage:bookmarks";
-local XMLNS_XEP0048 = "storage:bookmarks";
+local XMLNS_BM2 = "urn:xmpp:bookmarks:1";
 local XMLNS_XEP0060 = "http://jabber.org/protocol/pubsub";
 
 local default_options = {
 	["persist_items"] = true;
+	["max_items"] = "max";
+	["send_last_published_item"] = "never";
 	["access_model"] = "whitelist";
 };
 
 local function get_current_bookmarks(jid, service)
-	local ok, id, item = service:get_last_item(PUBSUB_NODE_XEP0048, jid)
-	if not ok or id == nil then
-		if id == "item-not-found" or id == nil then
-			-- return empty
-			return st.stanza("storage", { xmlns = XMLNS_XEP0048 });
-		end
-		return nil, result
-	end
-	-- first item is the actual storage element
-	local hit = item:get_child("storage", XMLNS_XEP0048);
-	if not hit then
-		return nil, "internal-server-error"
-	end
-	return hit
+	local ok, items = service:get_items(XMLNS_BM2, jid)
+	if not ok then return nil, items; end
+	return items or {};
 end
 
-local function update_bookmarks(jid, service, storage)
-	local item = st.stanza("item", { xmlns = XMLNS_XEP0060, id = "current" }):add_child(storage)
-	module:log("debug", "updating bookmarks with %q", item)
-	local ok, err = service:publish(
-		PUBSUB_NODE_XEP0048,
-		jid,
-		"current",
-		item,
-		default_options
-	)
-	if not ok then
+local function update_bookmark(jid, service, room, bookmark)
+	local ok, err = service:publish(XMLNS_BM2, jid, room, bookmark, default_options);
+	if ok then
+		module:log("debug", "found existing matching bookmark, updated")
+	else
 		module:log("error", "failed to update bookmarks: %s", err)
 	end
 end
 
 local function find_matching_bookmark(storage, room)
-	for node in storage:childtags("conference") do
-		if node.attr.jid == room then
-			return node
-		end
-	end
-	return nil
+	return storage[room];
 end
 
 local function inject_bookmark(jid, room, autojoin, name)
 	local pep_service = mod_pep.get_pep_service(jid_split(jid))
 
-	autojoin = autojoin or false and true
-	local current = get_current_bookmarks(jid, pep_service)
-	local existing = find_matching_bookmark(current, room)
-	if existing then
+	local current, err = get_current_bookmarks(jid, pep_service);
+	if err then
+		module:log("error", "Could not retrieve existing bookmarks for %s: %s", jid, err);
+		return;
+	end
+	local found = find_matching_bookmark(current, room)
+	if found then
+		local existing = found:get_child("conference", XMLNS_BM2);
 		if autojoin ~= nil then
 			existing.attr.autojoin = autojoin and "true" or "false"
 		end
@@ -71,44 +54,19 @@
 				existing.attr.name = name
 			end
 		end
-		done = true
-		module:log("debug", "found existing matching bookmark, updated")
 	else
 		module:log("debug", "no existing bookmark found, adding new")
-		current:tag("conference", {
-			name = name,
-			autojoin = autojoin and "true" or "false",
-			jid = room,
-			xmlns = XMLNS_XEP0048,
-		})
+		found = st.stanza("item", { xmlns = XMLNS_XEP0060; id = room })
+			:tag("conference", { xmlns = XMLNS_BM2; name = name; autojoin = autojoin and "true" or "false"; })
 	end
 
-	update_bookmarks(jid, pep_service, current)
+	update_bookmark(jid, pep_service, current, room, found)
 end
 
 local function remove_bookmark(jid, room, autojoin, name)
 	local pep_service = mod_pep.get_pep_service(jid_split(jid))
 
-	autojoin = autojoin or false and true
-	local current = get_current_bookmarks(jid, pep_service)
-	if not current then
-		return
-	end
-	current:maptags(function (node)
-		if node.attr.xmlns and node.attr.xmlns ~= XMLNS_XEP0048 then
-			return node
-		end
-		if node.name ~= "conference" then
-			return node
-		end
-		if node.attr.jid == room then
-			-- remove matching bookmark
-			return nil
-		end
-		return node
-	end)
-
-	update_bookmarks(jid, pep_service, current)
+	return pep_service:retract(XMLNS_BM2, jid, room, st.stanza("retract", { id = room }));
 end
 
 local function handle_user_added(event)