Changeset

12082:e87563fefd85

mod_storage_xep0227: Replace custom tag-removal helpers with :remove_children()
author Matthew Wild <mwild1@gmail.com>
date Mon, 20 Dec 2021 15:39:26 +0000
parents 12081:6cc3135138d7
children 12083:ec21e379c145
files plugins/mod_storage_xep0227.lua
diffstat 1 files changed, 9 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_storage_xep0227.lua	Mon Dec 20 00:21:04 2021 +0100
+++ b/plugins/mod_storage_xep0227.lua	Mon Dec 20 15:39:26 2021 +0000
@@ -3,7 +3,6 @@
 local setmetatable = setmetatable;
 local tostring = tostring;
 local next, unpack = next, table.unpack or unpack; --luacheck: ignore 113/unpack
-local t_remove = table.remove;
 local os_remove = os.remove;
 local io_open = io.open;
 local jid_bare = require "util.jid".bare;
@@ -66,18 +65,6 @@
 		:tag("host", {jid=host})
 			:tag("user", {name = user});
 end
-local function removeFromArray(arr, value)
-	for i,item in ipairs(arr) do
-		if item == value then
-			t_remove(arr, i);
-			return;
-		end
-	end
-end
-local function removeStanzaChild(s, child)
-	removeFromArray(s.tags, child);
-	removeFromArray(s, child);
-end
 
 local function hex_to_base64(s)
 	return base64.encode(hex.from(s));
@@ -171,16 +158,13 @@
 		local xml = getXml(user, self.host);
 		local usere = xml and getUserElement(xml);
 		if usere then
-			local vcard = usere:get_child("vCard", 'vcard-temp');
-			if vcard then
-				removeStanzaChild(usere, vcard);
-			elseif not data then
+			usere:remove_children("vCard", "vcard-temp");
+			if not data then
+				-- No data to set, old one deleted, success
 				return true;
 			end
-			if data then
-				vcard = st.deserialize(data);
-				usere:add_child(vcard);
-			end
+			local vcard = st.deserialize(data);
+			usere:add_child(vcard);
 			return setXml(user, self.host, xml);
 		end
 		return true;
@@ -204,10 +188,9 @@
 		local xml = getXml(user, self.host);
 		local usere = xml and getUserElement(xml);
 		if usere then
-			local private = usere:get_child("query", 'jabber:iq:private');
-			if private then removeStanzaChild(usere, private); end
+			usere:remove_children("query", "jabber:iq:private");
 			if data and next(data) ~= nil then
-				private = st.stanza("query", {xmlns='jabber:iq:private'});
+				local private = st.stanza("query", {xmlns='jabber:iq:private'});
 				for _,tag in pairs(data) do
 					private:add_child(st.deserialize(tag));
 				end
@@ -254,8 +237,7 @@
 		local xml = getXml(user, self.host);
 		local usere = xml and getUserElement(xml);
 		if usere then
-			local roster = usere:get_child("query", 'jabber:iq:roster');
-			if roster then removeStanzaChild(usere, roster); end
+			usere:remove_children("query", "jabber:iq:roster");
 			usere:maptags(function (tag)
 				if tag.attr.xmlns == "jabber:client" and tag.name == "presence" and tag.attr.type == "subscribe" then
 					return nil;
@@ -263,7 +245,7 @@
 				return tag;
 			end);
 			if data and next(data) ~= nil then
-				roster = st.stanza("query", {xmlns='jabber:iq:roster'});
+				local roster = st.stanza("query", {xmlns='jabber:iq:roster'});
 				usere:add_child(roster);
 				for jid, item in pairs(data) do
 					if jid then