annotate mod_map/mod_map.lua @ 3866:c0df50ce96f0

mod_rest: Handle internal http request errors early and then return Skips over attempted parsing of the payload which usually failed since the body is an error string like "connection refused", so this produced useless errors.
author Kim Alvefur <zash@zash.se>
date Sat, 25 Jan 2020 20:22:12 +0100
parents d0c2f001735f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3496
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local st = require "util.stanza";
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local jid_bare = require "util.jid".bare;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local rsm = require "util.rsm";
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local dataform = require "util.dataforms".new;
3651
95f7291db669 mod_map: Include timestamps of first/last message (if reported)
Kim Alvefur <zash@zash.se>
parents: 3650
diff changeset
6 local datetime = require "util.datetime".datetime;
3496
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local archive = module:open_store("archive", "archive");
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local query_form = dataform {
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 { name = "with"; type = "jid-single"; };
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 { name = "start"; type = "text-single" };
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 { name = "end"; type = "text-single"; };
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 };
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 if not archive.summary then
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 module:log("error", "The archive:summary() API is not supported by %s", archive._provided_by);
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 return
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 end
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 module:hook("iq-get/self/xmpp:prosody.im/mod_map:summary", function(event)
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local origin, stanza = event.origin, event.stanza;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local query = stanza.tags[1];
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 -- Search query parameters
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local qwith, qstart, qend;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local form = query:get_child("x", "jabber:x:data");
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 if form then
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local err;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 form, err = query_form:data(form);
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 if err then
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err))));
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 return true;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 end
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 qwith, qstart, qend = form["with"], form["start"], form["end"];
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 qwith = qwith and jid_bare(qwith); -- dataforms does jidprep
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local qset = rsm.get(query);
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local qmax = qset and qset.max;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 local before, after = qset and qset.before, qset and qset.after;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 if type(before) ~= "string" then before = nil; end
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 local summary = archive:summary(origin.username, {
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 start = qstart; ["end"] = qend; -- Time range
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 with = qwith;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 limit = qmax;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 before = before; after = after;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 });
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 if not summary then
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 module:send(st.error_reply(stanza, "wait", "internal-server-error"));
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 return true;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 local reply = st.reply(stanza);
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 reply:tag("summary", { xmlns = "xmpp:prosody.im/mod_map" });
3650
30743ae1fe1a mod_map: Update for new API in prosody 1e2b444acb72
Kim Alvefur <zash@zash.se>
parents: 3496
diff changeset
58 for jid, count in pairs(summary.counts) do
3496
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 reply:tag("item", { jid = jid });
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 if type(count) == "number" then
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 reply:text_tag("count", ("%d"):format(count));
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end
3651
95f7291db669 mod_map: Include timestamps of first/last message (if reported)
Kim Alvefur <zash@zash.se>
parents: 3650
diff changeset
63 if summary.earliest and summary.earliest[jid] then
95f7291db669 mod_map: Include timestamps of first/last message (if reported)
Kim Alvefur <zash@zash.se>
parents: 3650
diff changeset
64 reply:text_tag("start", datetime(summary.earliest[jid]));
95f7291db669 mod_map: Include timestamps of first/last message (if reported)
Kim Alvefur <zash@zash.se>
parents: 3650
diff changeset
65 end
95f7291db669 mod_map: Include timestamps of first/last message (if reported)
Kim Alvefur <zash@zash.se>
parents: 3650
diff changeset
66 if summary.latest and summary.latest[jid] then
95f7291db669 mod_map: Include timestamps of first/last message (if reported)
Kim Alvefur <zash@zash.se>
parents: 3650
diff changeset
67 reply:text_tag("end", datetime(summary.latest[jid]));
95f7291db669 mod_map: Include timestamps of first/last message (if reported)
Kim Alvefur <zash@zash.se>
parents: 3650
diff changeset
68 end
3652
d0c2f001735f mod_map: Include last body text
Kim Alvefur <zash@zash.se>
parents: 3651
diff changeset
69 if summary.body and summary.body[jid] then
d0c2f001735f mod_map: Include last body text
Kim Alvefur <zash@zash.se>
parents: 3651
diff changeset
70 reply:text_tag("body", summary.body[jid]);
d0c2f001735f mod_map: Include last body text
Kim Alvefur <zash@zash.se>
parents: 3651
diff changeset
71 end
3496
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 reply:up();
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 module:send(reply);
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 return true;
262e68821f3f mod_map: Experimental module exposing MAM summary
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 end);