Software /
code /
prosody
Annotate
plugins/mod_mam/fallback_archive.lib.lua @ 9247:26854d7a4947
mod_pubsub: Fix dataforms error handling
The :data method returns the table holding parsed values always. The
second return value is a table in case some fields had problems.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 01 Sep 2018 21:18:30 +0200 |
parent | 7854:68c5476960be |
rev | line source |
---|---|
7851
80ee0d9cd56f
mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents:
7836
diff
changeset
|
1 -- Prosody IM |
80ee0d9cd56f
mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents:
7836
diff
changeset
|
2 -- Copyright (C) 2008-2017 Matthew Wild |
80ee0d9cd56f
mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents:
7836
diff
changeset
|
3 -- Copyright (C) 2008-2017 Waqas Hussain |
80ee0d9cd56f
mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents:
7836
diff
changeset
|
4 -- Copyright (C) 2011-2017 Kim Alvefur |
80ee0d9cd56f
mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents:
7836
diff
changeset
|
5 -- |
80ee0d9cd56f
mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents:
7836
diff
changeset
|
6 -- This project is MIT/X11 licensed. Please see the |
80ee0d9cd56f
mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents:
7836
diff
changeset
|
7 -- COPYING file in the source package for more information. |
80ee0d9cd56f
mod_mam: Normalize copyright headers
Kim Alvefur <zash@zash.se>
parents:
7836
diff
changeset
|
8 -- |
7836
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 -- luacheck: ignore 212/self |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local uuid = require "util.uuid".generate; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local store = module:shared("archive"); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local archive_store = { _provided_by = "mam"; name = "fallback"; }; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 function archive_store:append(username, key, value, when, with) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local archive = store[username]; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 if not archive then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 archive = { [0] = 0 }; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 store[username] = archive; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local index = (archive[0] or #archive)+1; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local item = { key = key, when = when, with = with, value = value }; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 if not key or archive[key] then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 key = uuid(); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 item.key = key; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 archive[index] = item; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 archive[key] = index; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 archive[0] = index; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 return key; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 function archive_store:find(username, query) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 local archive = store[username] or {}; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 local start, stop, step = 1, archive[0] or #archive, 1; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 local qstart, qend, qwith = -math.huge, math.huge; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 local limit; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 if query then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 if query.reverse then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 start, stop, step = stop, start, -1; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 if query.before and archive[query.before] then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 start = archive[query.before] - 1; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 elseif query.after and archive[query.after] then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 start = archive[query.after] + 1; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 qwith = query.with; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 limit = query.limit; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 qstart = query.start or qstart; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 qend = query["end"] or qend; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 return function () |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 if limit and limit <= 0 then return end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 for i = start, stop, step do |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 local item = archive[i]; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 if (not qwith or qwith == item.with) and item.when >= qstart and item.when <= qend then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 if limit then limit = limit - 1; end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 start = i + step; -- Start on next item |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 return item.key, item.value, item.when, item.with; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 function archive_store:delete(username, query) |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 if not query or next(query) == nil then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 -- no specifics, delete everything |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 store[username] = nil; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 return true; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 local archive = store[username]; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 if not archive then return true; end -- no messages, nothing to delete |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 local qstart = query.start or -math.huge; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 local qend = query["end"] or math.huge; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 local qwith = query.with; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 store[username] = nil; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 for i = 1, #archive do |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 local item = archive[i]; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 local when, with = item.when, item.when; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 -- Add things that don't match the query |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 if not ((not qwith or qwith == item.with) and item.when >= qstart and item.when <= qend) then |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 self:append(username, item.key, item.value, when, with); |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 return true; |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 end |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 |
30fac9154fd4
mod_mam: Import from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 return archive_store; |