comparison mod_archive/mod_archive.lua @ 223:de71a52fc63a

mod_archive: Mapping an message to some collection based on timestamp
author shinysky<shinysky1986(AT)gmail.com>
date Sun, 25 Jul 2010 20:55:04 +0800
parents 6e6a08b0531a
children 96e29ff5fa07
comparison
equal deleted inserted replaced
222:6e6a08b0531a 223:de71a52fc63a
51 local function store_msg(msg, node, host, isfrom) 51 local function store_msg(msg, node, host, isfrom)
52 local body = msg:child_with_name("body"); 52 local body = msg:child_with_name("body");
53 local thread = msg:child_with_name("thread"); 53 local thread = msg:child_with_name("thread");
54 local data = dm.list_load(node, host, ARCHIVE_DIR); 54 local data = dm.list_load(node, host, ARCHIVE_DIR);
55 local tag = (isfrom and "from") or "to"; 55 local tag = (isfrom and "from") or "to";
56 local utc = os_time();
56 if data then 57 if data then
57 for k, v in ipairs(data) do 58 if thread then
58 local collection = st.deserialize(v); 59 for k, v in ipairs(data) do
59 if collection.attr["thread"] == thread:get_text() then 60 local collection = st.deserialize(v);
60 -- TODO figure out secs 61 if collection.attr["thread"] == thread:get_text() then
61 collection:tag(tag, {secs='1', utc=os_time()}):add_child(body); 62 -- TODO figure out secs
63 collection:tag(tag, {secs='1', utc=utc}):add_child(body);
64 local ver = tonumber(collection.attr["version"]) + 1;
65 collection.attr["version"] = tostring(ver);
66 collection.attr["access"] = utc;
67 data[k] = collection;
68 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data));
69 return;
70 end
71 end
72 else -- if the last collection occurs on the same day, then join it
73 -- TODO assuming the collection list are in reverse chronological order
74 local collection = st.deserialize(data[1]);
75 local difftime = os.difftime(date_parse(utc), date_parse(collection.attr["start"]));
76 if difftime < 86400 then -- 60 * 60 * 24
77 collection:tag(tag, {secs='1', utc=utc}):add_child(body);
62 local ver = tonumber(collection.attr["version"]) + 1; 78 local ver = tonumber(collection.attr["version"]) + 1;
63 collection.attr["version"] = tostring(ver); 79 collection.attr["version"] = tostring(ver);
64 collection.attr["access"] = os_time(); 80 collection.attr["access"] = utc;
65 data[k] = collection; 81 data[1] = collection;
66 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data)); 82 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data));
67 return; 83 return;
68 end 84 end
69 end 85 end
70 end 86 end
71 -- not found, create new collection 87 -- not found, create new collection
72 local utc = os_time();
73 local collection = st.stanza('chat', {with = isfrom and msg.attr.to or msg.attr.from, start=utc, thread=thread:get_text(), version='0', access=utc}); 88 local collection = st.stanza('chat', {with = isfrom and msg.attr.to or msg.attr.from, start=utc, thread=thread:get_text(), version='0', access=utc});
74 collection:tag(tag, {secs='0', utc=utc}):add_child(body); 89 collection:tag(tag, {secs='0', utc=utc}):add_child(body);
75 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(collection)); 90 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(collection));
76 end 91 end
77 92
345 end 360 end
346 361
347 ------------------------------------------------------------ 362 ------------------------------------------------------------
348 -- Archive Management 363 -- Archive Management
349 ------------------------------------------------------------ 364 ------------------------------------------------------------
350 local function filter_with(with, coll_with) 365 local function filter_jid(rule, jid)
351 return not with or coll_with:find(with); 366 return not rule or jid.compare(jid, rule);
352 end 367 end
353 368
354 local function filter_start(start, coll_start) 369 local function filter_start(start, coll_start)
355 return not start or start <= coll_start; 370 return not start or start <= coll_start;
356 end 371 end
376 local resset = {} 391 local resset = {}
377 if data then 392 if data then
378 for k, v in ipairs(data) do 393 for k, v in ipairs(data) do
379 local collection = st.deserialize(v); 394 local collection = st.deserialize(v);
380 if collection[1] then -- has children(not deleted) 395 if collection[1] then -- has children(not deleted)
381 local res = filter_with(elem.attr["with"], collection.attr["with"]); 396 local res = filter_jid(elem.attr["with"], collection.attr["with"]);
382 res = res and filter_start(elem.attr["start"], collection.attr["start"]); 397 res = res and filter_start(elem.attr["start"], collection.attr["start"]);
383 res = res and filter_end(elem.attr["end"], collection.attr["start"]); 398 res = res and filter_end(elem.attr["end"], collection.attr["start"]);
384 if res then 399 if res then
385 table.insert(resset, collection); 400 table.insert(resset, collection);
386 end 401 end
533 local count = table.getn(data); 548 local count = table.getn(data);
534 local found = false; 549 local found = false;
535 for i = count, 1, -1 do 550 for i = count, 1, -1 do
536 local collection = st.deserialize(data[i]); 551 local collection = st.deserialize(data[i]);
537 if collection[1] then -- has children(not deleted) 552 if collection[1] then -- has children(not deleted)
538 local res = filter_with(elem.attr["with"], collection.attr["with"]); 553 local res = filter_jid(elem.attr["with"], collection.attr["with"]);
539 res = res and filter_start(elem.attr["start"], collection.attr["start"]); 554 res = res and filter_start(elem.attr["start"], collection.attr["start"]);
540 res = res and filter_end(elem.attr["end"], collection.attr["start"]); 555 res = res and filter_end(elem.attr["end"], collection.attr["start"]);
541 if res then 556 if res then
542 -- table.remove(data, i); 557 -- table.remove(data, i);
543 local temp = st.stanza('chat', collection.attr); 558 local temp = st.stanza('chat', collection.attr);
647 elseif not exactmatch then 662 elseif not exactmatch then
648 if tobool(child.attr['exactmatch']) then 663 if tobool(child.attr['exactmatch']) then
649 if child.attr[k] == v then 664 if child.attr[k] == v then
650 return child; 665 return child;
651 end 666 end
652 elseif filter_with(child.attr[k], v) then 667 elseif filter_jid(child.attr[k], v) then
653 return child; 668 return child;
654 end 669 end
655 end 670 end
656 else 671 else
657 return child; 672 return child;