changeset 3923:3c3d216c6f6d

mod_rest: Add JSON mapping of XEP-0039: Statistics Gathering
author Kim Alvefur <zash@zash.se>
date Fri, 28 Feb 2020 22:38:41 +0100
parents ea59c9455f93
children 88d0f7438f0e
files mod_rest/jsonmap.lib.lua
diffstat 1 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/jsonmap.lib.lua	Fri Feb 28 22:24:49 2020 +0100
+++ b/mod_rest/jsonmap.lib.lua	Fri Feb 28 22:38:41 2020 +0100
@@ -344,6 +344,31 @@
 			return form;
 		end
 	};
+
+	-- XEP-0039: Statistics Gathering
+	stats = { type = "func", xmlns = "http://jabber.org/protocol/stats", tagname = "query",
+		st2json = function (s)
+			local o = array();
+			for stat in s:childtags("stat") do
+				o:push({
+						name = stat.attr.name;
+						unit = stat.attr.unit;
+						value = stat.attr.value;
+					});
+			end
+			return o;
+		end;
+		json2st = function (j)
+			local stats = st.stanza("query", { xmlns = "http://jabber.org/protocol/stats" });
+			if type(j) == "table" then
+				for _, stat in ipairs(j) do
+					stats:tag("stat", { name = stat.name, unit = stat.unit, value = stat.value }):up();
+				end
+			end
+			return stats;
+		end;
+	};
+
 };
 
 local implied_kinds = {