# HG changeset patch # User Thijs Alkemade # Date 1359304456 -3600 # Node ID 8a1beff848c17cd6bcfa763c47f58d1a4bfe1e81 # Parent 490cb9161c81350923099be888fce7a9bbd20282 mod_mam: Implemented an empty , to request the last n items in the archive. diff -r 490cb9161c81 -r 8a1beff848c1 mod_mam/mod_mam.lua --- a/mod_mam/mod_mam.lua Sat Jan 26 04:34:05 2013 +0500 +++ b/mod_mam/mod_mam.lua Sun Jan 27 17:34:16 2013 +0100 @@ -23,6 +23,7 @@ local tostring = tostring; local time_now = os.time; local m_min = math.min; +local t_insert = table.insert; local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse; local uuid = require "util.uuid".generate; local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50); @@ -165,9 +166,12 @@ local first, last, index; local n = 0; local start = qset and qset.index or 1; + local results = {}; + -- An empty means: give the last n items. So we loop backwards. + local reverse = qset and qset.before or false; module:log("debug", "Loaded %d items, about to filter", #data); - for i=start,#data do + for i=(reverse and #data or start),(reverse and start or #data),(reverse and -1 or 1) do local item = data[i]; local when, with, resource = item.when, item.with, item.resource; local id = item.id; @@ -195,7 +199,11 @@ local orig_stanza = st.deserialize(item.stanza); orig_stanza.attr.xmlns = "jabber:client"; fwd_st:add_child(orig_stanza); - origin.send(fwd_st); + if reverse then + t_insert(results, 1, fwd_st); + else + results[#results + 1] = fwd_st; + end if not first then index = i; first = id; @@ -219,13 +227,16 @@ break end end + for _,v in pairs(results) do + origin.send(v); + end -- That's all folks! module:log("debug", "Archive query %s completed", tostring(qid)); local reply = st.reply(stanza); if last then -- This is a bit redundant, isn't it? - reply:query(xmlns_mam):add_child(rsm.generate{first = first, last = last, count = n}); + reply:query(xmlns_mam):add_child(rsm.generate{first = (reverse and last or first), last = (reverse and first or last), count = n}); end origin.send(reply); return true