comparison mod_mam_muc/mod_mam_muc.lua @ 1691:1a8c791d365f

mod_mam_muc: d20cfc5ba827 .. cd87a2eba8f2 here too (uh, duplicated code)
author Kim Alvefur <zash@zash.se>
date Sun, 03 May 2015 14:05:43 +0200
parents c77e9522dc66
children db8b256f51ff
comparison
equal deleted inserted replaced
1690:8c0fbc685364 1691:1a8c791d365f
137 }; 137 };
138 138
139 -- Serve form 139 -- Serve form
140 module:hook("iq-get/bare/"..xmlns_mam..":query", function(event) 140 module:hook("iq-get/bare/"..xmlns_mam..":query", function(event)
141 local origin, stanza = event.origin, event.stanza; 141 local origin, stanza = event.origin, event.stanza;
142 return origin.send(st.reply(stanza):add_child(query_form:form())); 142 origin.send(st.reply(stanza):add_child(query_form:form()));
143 return true;
143 end); 144 end);
144 145
145 -- Handle archive queries 146 -- Handle archive queries
146 module:hook("iq-set/bare/"..xmlns_mam..":query", function(event) 147 module:hook("iq-set/bare/"..xmlns_mam..":query", function(event)
147 local origin, stanza = event.origin, event.stanza; 148 local origin, stanza = event.origin, event.stanza;
170 local form = query:get_child("x", "jabber:x:data"); 171 local form = query:get_child("x", "jabber:x:data");
171 if form then 172 if form then
172 local err; 173 local err;
173 form, err = query_form:data(form); 174 form, err = query_form:data(form);
174 if err then 175 if err then
175 return origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err)))) 176 origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err))));
177 return true;
176 end 178 end
177 qstart, qend = form["start"], form["end"]; 179 qstart, qend = form["start"], form["end"];
178 end 180 end
179 181
180 if qstart or qend then -- Validate timestamps 182 if qstart or qend then -- Validate timestamps
195 if type(before) ~= "string" then before = nil; end 197 if type(before) ~= "string" then before = nil; end
196 198
197 -- Load all the data! 199 -- Load all the data!
198 local data, err = archive:find(room_node, { 200 local data, err = archive:find(room_node, {
199 start = qstart; ["end"] = qend; -- Time range 201 start = qstart; ["end"] = qend; -- Time range
200 limit = qmax; 202 limit = qmax + 1;
201 before = before; after = after; 203 before = before; after = after;
202 reverse = reverse; 204 reverse = reverse;
203 total = true; 205 total = true;
204 with = "message<groupchat"; 206 with = "message<groupchat";
205 }); 207 });
206 208
207 if not data then 209 if not data then
208 return origin.send(st.error_reply(stanza, "cancel", "internal-server-error")); 210 return origin.send(st.error_reply(stanza, "cancel", "internal-server-error"));
209 end 211 end
210 local count = err; 212 local total = err;
211 213
212 origin.send(st.reply(stanza)) 214 origin.send(st.reply(stanza))
213 local msg_reply_attr = { to = stanza.attr.from, from = stanza.attr.to }; 215 local msg_reply_attr = { to = stanza.attr.from, from = stanza.attr.to };
214 216
217 local results = {};
218
215 -- Wrap it in stuff and deliver 219 -- Wrap it in stuff and deliver
216 local fwd_st, first, last; 220 local first, last;
221 local count = 0;
222 local complete = "true";
217 for id, item, when in data do 223 for id, item, when in data do
218 fwd_st = st.message(msg_reply_attr) 224 count = count + 1;
225 if count > qmax then
226 complete = nil;
227 break;
228 end
229 local fwd_st = st.message(msg_reply_attr)
219 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }) 230 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id })
220 :tag("forwarded", { xmlns = xmlns_forward }) 231 :tag("forwarded", { xmlns = xmlns_forward })
221 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up(); 232 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up();
222 233
223 if not is_stanza(item) then 234 if not is_stanza(item) then
227 fwd_st:add_child(item); 238 fwd_st:add_child(item);
228 239
229 if not first then first = id; end 240 if not first then first = id; end
230 last = id; 241 last = id;
231 242
232 origin.send(fwd_st); 243 if reverse then
233 end 244 results[count] = fwd_st;
245 else
246 origin.send(fwd_st);
247 end
248 end
249 if reverse then
250 for i = #results, 1, -1 do
251 origin.send(results[i]);
252 end
253 end
254
234 -- That's all folks! 255 -- That's all folks!
235 module:log("debug", "Archive query %s completed", tostring(qid)); 256 module:log("debug", "Archive query %s completed", tostring(qid));
236 257
237 if reverse then first, last = last, first; end 258 if reverse then first, last = last, first; end
238 return origin.send(st.message(msg_reply_attr) 259 origin.send(st.message(msg_reply_attr)
239 :tag("fin", { xmlns = xmlns_mam, queryid = qid }) 260 :tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete })
240 :add_child(rsm.generate { 261 :add_child(rsm.generate {
241 first = first, last = last, count = count })); 262 first = first, last = last, count = total }));
263 return true;
242 end); 264 end);
243 265
244 module:hook("muc-get-history", function (event) 266 module:hook("muc-get-history", function (event)
245 local room = event.room; 267 local room = event.room;
246 if not logging_enabled(room) then return end 268 if not logging_enabled(room) then return end