mod_muc_webchat_url: Fix default url
[prosody-modules.git] / mod_mam / fallback_archive.lib.lua
blob96c72b0013ba511dcc73c5a98a6c583225d215ee
1 -- luacheck: ignore 212/self
3 local uuid = require "util.uuid".generate;
4 local store = module:shared("archive");
5 local archive_store = { _provided_by = "mam"; name = "fallback"; };
7 function archive_store:append(username, key, value, when, with)
8 local archive = store[username];
9 if not archive then
10 archive = { [0] = 0 };
11 store[username] = archive;
12 end
13 local index = (archive[0] or #archive)+1;
14 local item = { key = key, when = when, with = with, value = value };
15 if not key or archive[key] then
16 key = uuid();
17 item.key = key;
18 end
19 archive[index] = item;
20 archive[key] = index;
21 archive[0] = index;
22 return key;
23 end
25 function archive_store:find(username, query)
26 local archive = store[username] or {};
27 local start, stop, step = 1, archive[0] or #archive, 1;
28 local qstart, qend, qwith = -math.huge, math.huge;
29 local limit;
31 if query then
32 if query.reverse then
33 start, stop, step = stop, start, -1;
34 if query.before and archive[query.before] then
35 start = archive[query.before] - 1;
36 end
37 elseif query.after and archive[query.after] then
38 start = archive[query.after] + 1;
39 end
40 qwith = query.with;
41 limit = query.limit;
42 qstart = query.start or qstart;
43 qend = query["end"] or qend;
44 end
46 return function ()
47 if limit and limit <= 0 then return end
48 for i = start, stop, step do
49 local item = archive[i];
50 if (not qwith or qwith == item.with) and item.when >= qstart and item.when <= qend then
51 if limit then limit = limit - 1; end
52 start = i + step; -- Start on next item
53 return item.key, item.value, item.when, item.with;
54 end
55 end
56 end
57 end
59 function archive_store:delete(username, query)
60 if not query or next(query) == nil then
61 -- no specifics, delete everything
62 store[username] = nil;
63 return true;
64 end
65 local archive = store[username];
66 if not archive then return true; end -- no messages, nothing to delete
68 local qstart = query.start or -math.huge;
69 local qend = query["end"] or math.huge;
70 local qwith = query.with;
71 store[username] = nil;
72 for i = 1, #archive do
73 local item = archive[i];
74 local when, with = item.when, item.when;
75 -- Add things that don't match the query
76 if not ((not qwith or qwith == item.with) and item.when >= qstart and item.when <= qend) then
77 self:append(username, item.key, item.value, when, with);
78 end
79 end
80 return true;
81 end
83 return archive_store;