diff options
author | Kim Alvefur <zash@zash.se> | 2021-10-25 21:45:46 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2021-10-25 21:45:46 +0200 |
commit | 851127ecd70c9c7e5e5c13565d26b908a020b69e (patch) | |
tree | 966bb4c6d5acfcedb67e8dca2695fe647e783b72 | |
parent | 7a272b16703fd26c19f6f2519229b1ca539377ed (diff) | |
download | prosody-851127ecd70c9c7e5e5c13565d26b908a020b69e.tar.gz prosody-851127ecd70c9c7e5e5c13565d26b908a020b69e.zip |
util.dataforms: Add support for datetime field types via XEP-0122
-rw-r--r-- | spec/util_dataforms_spec.lua | 14 | ||||
-rw-r--r-- | util/dataforms.lua | 8 |
2 files changed, 22 insertions, 0 deletions
diff --git a/spec/util_dataforms_spec.lua b/spec/util_dataforms_spec.lua index d2d1264a..f796b30d 100644 --- a/spec/util_dataforms_spec.lua +++ b/spec/util_dataforms_spec.lua @@ -446,6 +446,20 @@ describe("util.dataforms", function () assert.table(e); assert.string(e.number); end); + + describe("datetime", function () + local f = dataforms.new { { name = "when"; type = "text-single"; datatype = "xs:dateTime" } } -- luacheck: ignore 431 + + it("works", function () + local x = f:form({ when = "2008-08-22T21:09:00Z" }); + assert.equal("2008-08-22T21:09:00Z", x:find("field/value#")) + local d, e = f:data(x); + assert.is_nil(e); + assert.same({ when = 1219439340 }, d); + end); + + end) + end); describe("media element", function () it("produced media element correctly", function () diff --git a/util/dataforms.lua b/util/dataforms.lua index 25e198df..79559b22 100644 --- a/util/dataforms.lua +++ b/util/dataforms.lua @@ -14,6 +14,7 @@ local tostring = tostring; local t_concat = table.concat; local st = require "util.stanza"; local jid_prep = require "util.jid".prep; +local datetime = require "util.datetime"; local _ENV = nil; -- luacheck: std none @@ -321,6 +322,13 @@ data_validators["pubsub:integer-or-max"] = end end +data_validators["xs:dateTime"] = + function(data, field) -- luacheck: ignore 212/field + local n = datetime.parse(data); + if not n then return false, "invalid timestamp"; end + return true, n; + end + local function get_form_type(form) if not st.is_stanza(form) then |