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
];
10 archive
= { [0] = 0 };
11 store
[username
] = archive
;
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
19 archive
[index
] = item
;
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
;
33 start
, stop
, step
= stop
, start
, -1;
34 if query
.before
and archive
[query
.before
] then
35 start
= archive
[query
.before
] - 1;
37 elseif query
.after
and archive
[query
.after
] then
38 start
= archive
[query
.after
] + 1;
42 qstart
= query
.start
or qstart
;
43 qend
= query
["end"] or qend
;
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
;
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;
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
);