Comparison

mod_archive/mod_archive.lua @ 226:7fe326037070

mod_archive: exit for loop as soon as possible.
author shinysky<shinysky1986(AT)gmail.com>
date Thu, 29 Jul 2010 17:05:23 +0800
parent 225:95d4d53f09a1
child 228:56103bdb068c
comparison
equal deleted inserted replaced
225:95d4d53f09a1 226:7fe326037070
104 local utc_datetime = date_time(utc); 104 local utc_datetime = date_time(utc);
105 if data then 105 if data then
106 -- TODO assuming the collection list are in REVERSE chronological order 106 -- TODO assuming the collection list are in REVERSE chronological order
107 for k, v in ipairs(data) do 107 for k, v in ipairs(data) do
108 local collection = st.deserialize(v); 108 local collection = st.deserialize(v);
109 if thread and collection.attr["thread"] == thread:get_text() 109 local do_save = function()
110 or
111 not thread
112 and collection.attr["with"] == with
113 and os.difftime(utc_secs, date_parse(collection.attr["start"])) < 14400 then
114 local dt = 1; 110 local dt = 1;
115 for i = #collection, 1, -1 do 111 for i = #collection, 1, -1 do
116 local s = collection[i].attr["utc_secs"]; 112 local s = collection[i].attr["utc_secs"];
117 if s then 113 if s then
118 dt = os.difftime(utc_secs, tonumber(s)); 114 dt = os.difftime(utc_secs, tonumber(s));
123 local ver = tonumber(collection.attr["version"]) + 1; 119 local ver = tonumber(collection.attr["version"]) + 1;
124 collection.attr["version"] = tostring(ver); 120 collection.attr["version"] = tostring(ver);
125 collection.attr["access"] = utc_datetime; 121 collection.attr["access"] = utc_datetime;
126 data[k] = collection; 122 data[k] = collection;
127 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data)); 123 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data));
128 return; 124 end
125 if thread then
126 if collection.attr["thread"] == thread:get_text() then
127 do_save();
128 return;
129 end
130 else
131 local dt = os.difftime(utc_secs, date_parse(collection.attr["start"]));
132 if dt >= 14400 then break end
133 if collection.attr["with"] == with then
134 do_save();
135 return;
136 end
129 end 137 end
130 end 138 end
131 end 139 end
132 -- not found, create new collection 140 -- not found, create new collection
133 local collection = st.stanza('chat', {with=with, start=utc_datetime, thread=thread and thread:get_text() or nil, version='0', access=utc_datetime}); 141 local collection = st.stanza('chat', {with=with, start=utc_datetime, thread=thread and thread:get_text() or nil, version='0', access=utc_datetime});
328 return false; 336 return false;
329 end 337 end
330 local elem = stanza.tags[1]; 338 local elem = stanza.tags[1];
331 local node, host = origin.username, origin.host; 339 local node, host = origin.username, origin.host;
332 local data = load_prefs(node, host); 340 local data = load_prefs(node, host);
333 if not data then 341 if not data then -- TODO create new pref?
334 return false; 342 return false;
335 end 343 end
336 local setting = data:child_with_name(elem.name) 344 local setting = data:child_with_name(elem.name)
337 for k, v in pairs(elem.attr) do 345 for k, v in pairs(elem.attr) do
338 setting.attr[k] = v; 346 setting.attr[k] = v;