changeset 1689:06f9ab0c078c

Merge
author Kim Alvefur <zash@zash.se>
date Sun, 03 May 2015 13:42:56 +0200
parents cd87a2eba8f2 (diff) 752d52d61186 (current diff)
children 8c0fbc685364
files
diffstat 1 files changed, 39 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/mod_mam/mod_mam.lua	Wed Apr 29 07:24:07 2015 -0500
+++ b/mod_mam/mod_mam.lua	Sun May 03 13:42:56 2015 +0200
@@ -52,16 +52,18 @@
 	if stanza.attr.type == "get" then
 		local prefs = prefs_to_stanza(get_prefs(user));
 		local reply = st.reply(stanza):add_child(prefs);
-		return origin.send(reply);
+		origin.send(reply);
 	else -- type == "set"
 		local new_prefs = stanza:get_child("prefs", xmlns_mam);
 		local prefs = prefs_from_stanza(new_prefs);
 		local ok, err = set_prefs(user, prefs);
 		if not ok then
-			return origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err)));
+			origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err)));
+		else
+			origin.send(st.reply(stanza));
 		end
-		return origin.send(st.reply(stanza));
 	end
+	return true;
 end);
 
 local query_form = dataform {
@@ -74,7 +76,8 @@
 -- Serve form
 module:hook("iq-get/self/"..xmlns_mam..":query", function(event)
 	local origin, stanza = event.origin, event.stanza;
-	return origin.send(st.reply(stanza):add_child(query_form:form()));
+	origin.send(st.reply(stanza):add_child(query_form:form()));
+	return true;
 end);
 
 -- Handle archive queries
@@ -90,7 +93,8 @@
 		local err;
 		form, err = query_form:data(form);
 		if err then
-			return origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err))))
+			origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err))))
+			return true;
 		end
 		qwith, qstart, qend = form["with"], form["start"], form["end"];
 		qwith = qwith and jid_bare(qwith); -- dataforms does jidprep
@@ -120,24 +124,34 @@
 	local data, err = archive:find(origin.username, {
 		start = qstart; ["end"] = qend; -- Time range
 		with = qwith;
-		limit = qmax;
+		limit = qmax + 1;
 		before = before; after = after;
 		reverse = reverse;
 		total = true;
 	});
 
 	if not data then
-		return origin.send(st.error_reply(stanza, "cancel", "internal-server-error", err));
+		origin.send(st.error_reply(stanza, "cancel", "internal-server-error", err));
+		return true;
 	end
-	local count = err;
+	local total = err;
 
 	origin.send(st.reply(stanza))
 	local msg_reply_attr = { to = stanza.attr.from, from = stanza.attr.to };
 
+	local results = {};
+
 	-- Wrap it in stuff and deliver
-	local fwd_st, first, last;
+	local first, last;
+	local count = 0;
+	local complete = "true";
 	for id, item, when in data do
-		fwd_st = st.message(msg_reply_attr)
+		count = count + 1;
+		if count > qmax then
+			complete = nil;
+			break;
+		end
+		local fwd_st = st.message(msg_reply_attr)
 			:tag("result", { xmlns = xmlns_mam, queryid = qid, id = id })
 				:tag("forwarded", { xmlns = xmlns_forward })
 					:tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up();
@@ -151,16 +165,27 @@
 		if not first then first = id; end
 		last = id;
 
-		origin.send(fwd_st);
+		if reverse then
+			results[count] = fwd_st;
+		else
+			origin.send(fwd_st);
+		end
 	end
+	if reverse then
+		for i = #results, 1, -1 do
+			origin.send(results[i]);
+		end
+	end
+
 	-- That's all folks!
 	module:log("debug", "Archive query %s completed", tostring(qid));
 
 	if reverse then first, last = last, first; end
-	return origin.send(st.message(msg_reply_attr)
-		:tag("fin", { xmlns = xmlns_mam, queryid = qid })
+	origin.send(st.message(msg_reply_attr)
+		:tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete })
 			:add_child(rsm.generate {
-				first = first, last = last, count = count }));
+				first = first, last = last, count = total }));
+	return true;
 end);
 
 local function has_in_roster(user, who)