Software / code / prosody-modules
Comparison
mod_map/mod_map.lua @ 3496:262e68821f3f
mod_map: Experimental module exposing MAM summary
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 25 Feb 2019 15:52:46 +0100 |
| child | 3650:30743ae1fe1a |
comparison
equal
deleted
inserted
replaced
| 3495:5567098a7f91 | 3496:262e68821f3f |
|---|---|
| 1 | |
| 2 local st = require "util.stanza"; | |
| 3 local jid_bare = require "util.jid".bare; | |
| 4 local rsm = require "util.rsm"; | |
| 5 local dataform = require "util.dataforms".new; | |
| 6 | |
| 7 local archive = module:open_store("archive", "archive"); | |
| 8 | |
| 9 local query_form = dataform { | |
| 10 { name = "with"; type = "jid-single"; }; | |
| 11 { name = "start"; type = "text-single" }; | |
| 12 { name = "end"; type = "text-single"; }; | |
| 13 }; | |
| 14 | |
| 15 if not archive.summary then | |
| 16 module:log("error", "The archive:summary() API is not supported by %s", archive._provided_by); | |
| 17 return | |
| 18 end | |
| 19 | |
| 20 module:hook("iq-get/self/xmpp:prosody.im/mod_map:summary", function(event) | |
| 21 local origin, stanza = event.origin, event.stanza; | |
| 22 | |
| 23 local query = stanza.tags[1]; | |
| 24 | |
| 25 -- Search query parameters | |
| 26 local qwith, qstart, qend; | |
| 27 local form = query:get_child("x", "jabber:x:data"); | |
| 28 if form then | |
| 29 local err; | |
| 30 form, err = query_form:data(form); | |
| 31 if err then | |
| 32 origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err)))); | |
| 33 return true; | |
| 34 end | |
| 35 qwith, qstart, qend = form["with"], form["start"], form["end"]; | |
| 36 qwith = qwith and jid_bare(qwith); -- dataforms does jidprep | |
| 37 end | |
| 38 | |
| 39 local qset = rsm.get(query); | |
| 40 local qmax = qset and qset.max; | |
| 41 local before, after = qset and qset.before, qset and qset.after; | |
| 42 if type(before) ~= "string" then before = nil; end | |
| 43 | |
| 44 local summary = archive:summary(origin.username, { | |
| 45 start = qstart; ["end"] = qend; -- Time range | |
| 46 with = qwith; | |
| 47 limit = qmax; | |
| 48 before = before; after = after; | |
| 49 }); | |
| 50 if not summary then | |
| 51 module:send(st.error_reply(stanza, "wait", "internal-server-error")); | |
| 52 return true; | |
| 53 end | |
| 54 | |
| 55 local reply = st.reply(stanza); | |
| 56 reply:tag("summary", { xmlns = "xmpp:prosody.im/mod_map" }); | |
| 57 for jid, count in pairs(summary) do | |
| 58 reply:tag("item", { jid = jid }); | |
| 59 if type(count) == "number" then | |
| 60 reply:text_tag("count", ("%d"):format(count)); | |
| 61 end | |
| 62 reply:up(); | |
| 63 end | |
| 64 | |
| 65 module:send(reply); | |
| 66 return true; | |
| 67 end); |