Mercurial > prosody-modules
annotate mod_mam/fallback_archive.lib.lua @ 4340:7cd3b7ec59e9
mod_http_oauth2: Rudimentary support for scopes (but not really)
We don't support limiting access, but this change will inform the
client what permissions the created token has (e.g. is the user an
admin or not).
There is some work in progress on real scope support.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 16 Jan 2021 19:47:22 +0000 |
parents | 03f6d9ed2903 |
children |
rev | line source |
---|---|
2023
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- luacheck: ignore 212/self |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local uuid = require "util.uuid".generate; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local store = module:shared("archive"); |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local archive_store = { _provided_by = "mam"; name = "fallback"; }; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 function archive_store:append(username, key, value, when, with) |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local archive = store[username]; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 if not archive then |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 archive = { [0] = 0 }; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 store[username] = archive; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local index = (archive[0] or #archive)+1; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local item = { key = key, when = when, with = with, value = value }; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 if not key or archive[key] then |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 key = uuid(); |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 item.key = key; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 archive[index] = item; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 archive[key] = index; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 archive[0] = index; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 return key; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 function archive_store:find(username, query) |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local archive = store[username] or {}; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 local start, stop, step = 1, archive[0] or #archive, 1; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 local qstart, qend, qwith = -math.huge, math.huge; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 local limit; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 if query then |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 if query.reverse then |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 start, stop, step = stop, start, -1; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 if query.before and archive[query.before] then |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 start = archive[query.before] - 1; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 elseif query.after and archive[query.after] then |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 start = archive[query.after] + 1; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 qwith = query.with; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 limit = query.limit; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 qstart = query.start or qstart; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 qend = query["end"] or qend; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 return function () |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 if limit and limit <= 0 then return end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 for i = start, stop, step do |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 local item = archive[i]; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 if (not qwith or qwith == item.with) and item.when >= qstart and item.when <= qend then |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 if limit then limit = limit - 1; end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 start = i + step; -- Start on next item |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 return item.key, item.value, item.when, item.with; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 function archive_store:delete(username, query) |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 if not query or next(query) == nil then |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 -- no specifics, delete everything |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 store[username] = nil; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 return true; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 local archive = store[username]; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 if not archive then return true; end -- no messages, nothing to delete |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 local qstart = query.start or -math.huge; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 local qend = query["end"] or math.huge; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 local qwith = query.with; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 store[username] = nil; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 for i = 1, #archive do |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 local item = archive[i]; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 local when, with = item.when, item.when; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 -- Add things that don't match the query |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 if not ((not qwith or qwith == item.with) and item.when >= qstart and item.when <= qend) then |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 self:append(username, item.key, item.value, when, with); |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 return true; |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 end |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 |
98b4794b72e4
mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 return archive_store; |