Software /
code /
prosody-modules
Comparison
mod_archive/mod_archive.lua @ 196:a1c2677257da
mod_archive: XEP-0059 Result Set Management for Retrieving a List of Collections is DONE
author | shinysky<shinysky1986(AT)gmail.com> |
---|---|
date | Wed, 07 Jul 2010 17:31:31 +0800 |
parent | 195:4d9ed6374a1f |
child | 198:a3b5810de3e4 |
comparison
equal
deleted
inserted
replaced
195:4d9ed6374a1f | 196:a1c2677257da |
---|---|
335 | 335 |
336 local function filter_end(endtime, coll_start) | 336 local function filter_end(endtime, coll_start) |
337 return not endtime or endtime >= coll_start; | 337 return not endtime or endtime >= coll_start; |
338 end | 338 end |
339 | 339 |
340 local function find_coll(resset, uid) | |
341 for i, c in ipairs(resset) do | |
342 if gen_uid(c) == uid then | |
343 return i; | |
344 end | |
345 end | |
346 return nil; | |
347 end | |
348 | |
340 local function list_handler(event) | 349 local function list_handler(event) |
341 local origin, stanza = event.origin, event.stanza; | 350 local origin, stanza = event.origin, event.stanza; |
342 local node, host = origin.username, origin.host; | 351 local node, host = origin.username, origin.host; |
343 local data = dm.list_load(node, host, ARCHIVE_DIR); | 352 local data = dm.list_load(node, host, ARCHIVE_DIR); |
344 local elem = stanza.tags[1]; | 353 local elem = stanza.tags[1]; |
353 table.insert(resset, collection); | 362 table.insert(resset, collection); |
354 end | 363 end |
355 end | 364 end |
356 end | 365 end |
357 local reply = st.reply(stanza):tag('list', {xmlns='urn:xmpp:archive'}); | 366 local reply = st.reply(stanza):tag('list', {xmlns='urn:xmpp:archive'}); |
358 if table.getn(resset) > 0 then | 367 local count = table.getn(resset); |
368 if count > 0 then | |
359 local max = elem.tags[1]:child_with_name("max"); | 369 local max = elem.tags[1]:child_with_name("max"); |
360 if max then | 370 if max then |
361 max = tonumber(max:get_text()); | 371 max = tonumber(max:get_text()); |
362 else max = 100; end | 372 else max = 100; end |
363 local after = elem.tags[1]:child_with_name("after"); | 373 local after = elem.tags[1]:child_with_name("after"); |
364 -- local before = elem.tags[1]:child_with_name("before"); | 374 local before = elem.tags[1]:child_with_name("before"); |
365 -- local index = elem.tags[1]:child_with_name("index"); | 375 local index = elem.tags[1]:child_with_name("index"); |
366 if after then after = after:get_text(); end | 376 local s, e = 1, 1+max; |
367 local found = false; | 377 if after then |
368 local first, last = nil, nil; | 378 after = after:get_text(); |
379 s = find_coll(resset, after); | |
380 if not s then -- not found | |
381 origin.send(st.error_reply(stanza, "cancel", "item-not-found")); | |
382 return true; | |
383 end | |
384 s = s + 1; | |
385 e = s + max; | |
386 elseif before then | |
387 before = before:get_text(); | |
388 if not before or before == '' then -- the last page | |
389 e = count + 1; | |
390 s = e - max; | |
391 else | |
392 e = find_coll(resset, before); | |
393 if not e then -- not found | |
394 origin.send(st.error_reply(stanza, "cancel", "item-not-found")); | |
395 return true; | |
396 end | |
397 s = e - max; | |
398 end | |
399 elseif index then | |
400 s = tonumber(index:get_text()) + 1; -- 0-based | |
401 e = s + max; | |
402 end | |
403 if s < 1 then s = 1; end | |
404 if e > count + 1 then e = count + 1; end | |
369 -- Assuming result set is sorted. | 405 -- Assuming result set is sorted. |
370 for i, c in ipairs(resset) do | 406 for i = s, e-1 do |
371 if after and not found then | 407 reply:add_child(st.stanza('chat', resset[i].attr)); |
372 if gen_uid(c) == after then | |
373 found = true; | |
374 end | |
375 elseif max > 0 then | |
376 if not first then first = i; end | |
377 last = i; | |
378 local chat = st.stanza('chat', c.attr); | |
379 reply:add_child(chat); | |
380 max = max - 1; | |
381 else break; end | |
382 end | 408 end |
383 local set = st.stanza('set', {xmlns='http://jabber.org/protocol/rsm'}); | 409 local set = st.stanza('set', {xmlns='http://jabber.org/protocol/rsm'}); |
384 if first then | 410 if s <= e-1 then |
385 set:tag('first', {index=first-1}):text(gen_uid(resset[first])):up() | 411 set:tag('first', {index=s-1}):text(gen_uid(resset[s])):up() |
386 :tag('last'):text(gen_uid(resset[last])):up(); | 412 :tag('last'):text(gen_uid(resset[e-1])):up(); |
387 end | 413 end |
388 set:tag('count'):text(tostring(table.getn(resset))):up(); | 414 set:tag('count'):text(tostring(count)):up(); |
389 reply:add_child(set); | 415 reply:add_child(set); |
390 end | 416 end |
391 origin.send(reply); | 417 origin.send(reply); |
392 return true; | 418 return true; |
393 end | 419 end |