mod_storage_memory: Add map store methods to archive store
authorKim Alvefur <zash@zash.se>
Mon, 11 May 2020 23:22:25 +0200
changeset 10843 018acdaf374f
parent 10842 f26f2ec33f1e
child 10844 a83bfb266b15
mod_storage_memory: Add map store methods to archive store
plugins/mod_storage_memory.lua
--- a/plugins/mod_storage_memory.lua	Mon May 11 22:32:28 2020 +0200
+++ b/plugins/mod_storage_memory.lua	Mon May 11 23:22:25 2020 +0200
@@ -167,6 +167,37 @@
 	end, count;
 end
 
+function archive_store:get(username, wanted_key)
+	local items = self.store[username or NULL];
+	if not items then return nil, "item-not-found"; end
+	local i = items[wanted_key];
+	if not i then return nil, "item-not-found"; end
+	local item = items[i];
+	return item.value(), item.when, item.with;
+end
+
+function archive_store:set(username, wanted_key, new_value, new_when, new_with)
+	local items = self.store[username or NULL];
+	if not items then return nil, "item-not-found"; end
+	local i = items[wanted_key];
+	if not i then return nil, "item-not-found"; end
+	local item = items[i];
+
+	if is_stanza(new_value) then
+		new_value = st.preserialize(new_value);
+		item.value = envload("return xml"..serialize(new_value), "=(stanza)", { xml = st.deserialize })
+	else
+		item.value = envload("return "..serialize(new_value), "=(data)", {});
+	end
+	if new_when then
+		item.when = new_when;
+	end
+	if new_with then
+		item.with = new_when;
+	end
+	return true;
+end
+
 function archive_store:summary(username, query)
 	local iter, err = self:find(username, query)
 	if not iter then return iter, err; end