Comparison

mod_storage_xmlarchive/mod_storage_xmlarchive.lua @ 2290:4786bf0a9334

mod_storage_xmlarchive: Determine if a message is the first of day by checking if the list file exists before
author Kim Alvefur <zash@zash.se>
date Tue, 23 Aug 2016 23:39:57 +0200
parent 2289:aa984980a4dc
child 2396:544ee057a23f
comparison
equal deleted inserted replaced
2289:aa984980a4dc 2290:4786bf0a9334
3 -- 3 --
4 -- This file is MIT/X11 licensed. 4 -- This file is MIT/X11 licensed.
5 -- 5 --
6 -- luacheck: ignore unused self 6 -- luacheck: ignore unused self
7 7
8 local lfs = require "lfs";
8 local dm = require "core.storagemanager".olddm; 9 local dm = require "core.storagemanager".olddm;
9 local hmac_sha256 = require"util.hashes".hmac_sha256; 10 local hmac_sha256 = require"util.hashes".hmac_sha256;
10 local st = require"util.stanza"; 11 local st = require"util.stanza";
11 local dt = require"util.datetime"; 12 local dt = require"util.datetime";
12 local new_stream = require "util.xmppstream".new; 13 local new_stream = require "util.xmppstream".new;
35 local ok, err = dm.append_raw(username.."@"..day, module.host, self.store, "xml", data); 36 local ok, err = dm.append_raw(username.."@"..day, module.host, self.store, "xml", data);
36 if not ok then 37 if not ok then
37 return nil, err; 38 return nil, err;
38 end 39 end
39 40
41 -- If the day-file is missing then we need to add it to the list of days
42 local first_of_day = not lfs.attributes(dm.getpath(username .. "@" .. day, module.host, self.store, "list"));
43
40 local offset = ok and err; 44 local offset = ok and err;
41 45
42 local id = day .. "-" .. hmac_sha256(username.."@"..day.."+"..offset, data, true):sub(-16); 46 local id = day .. "-" .. hmac_sha256(username.."@"..day.."+"..offset, data, true):sub(-16);
43 ok, err = dm.list_append(username.."@"..day, module.host, self.store, { id = id, when = dt.datetime(when), with = with, offset = offset, length = #data }); 47 ok, err = dm.list_append(username.."@"..day, module.host, self.store, { id = id, when = dt.datetime(when), with = with, offset = offset, length = #data });
44 if offset == 0 then 48 if ok and first_of_day then
45 -- means the message is at the beginnig of the file, so it's a new day
46 -- so we add this new day to the "index"
47 ok, err = dm.list_append(username, module.host, self.store, day); 49 ok, err = dm.list_append(username, module.host, self.store, day);
48 end 50 end
49 if not ok then 51 if not ok then
50 return nil, err; 52 return nil, err;
51 end 53 end