Mercurial > prosody-hg
comparison plugins/mod_tombstones.lua @ 14211:a8ce6f45ba35
mod_tombstones: Factor out tombstone management to helper functions
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 05 Jun 2026 16:24:39 +0100 |
| parents | 50324f66ca2a |
| children | 55a7143a58ec |
comparison
equal
deleted
inserted
replaced
| 14210:41fa97090264 | 14211:a8ce6f45ba35 |
|---|---|
| 5 local jid_node = require"prosody.util.jid".node; | 5 local jid_node = require"prosody.util.jid".node; |
| 6 local st = require "prosody.util.stanza"; | 6 local st = require "prosody.util.stanza"; |
| 7 | 7 |
| 8 -- Using a map store as key-value store so that removal of all user data | 8 -- Using a map store as key-value store so that removal of all user data |
| 9 -- does not also remove the tombstone, which would defeat the point | 9 -- does not also remove the tombstone, which would defeat the point |
| 10 local graveyard = module:open_store(nil, "map"); | 10 local graveyard = module:open_store(nil, "keyval+"); |
| 11 local graveyard_cache = require "prosody.util.cache".new(module:get_option_integer("tombstone_cache_size", 1024, 1)); | 11 local graveyard_cache = require "prosody.util.cache".new(module:get_option_integer("tombstone_cache_size", 1024, 1)); |
| 12 | 12 |
| 13 local ttl = module:get_option_period("user_tombstone_expiry", nil); | 13 local ttl = module:get_option_period("user_tombstone_expiry", nil); |
| 14 -- Keep tombstones forever by default | 14 -- Keep tombstones forever by default |
| 15 -- | 15 -- |
| 19 | 19 |
| 20 -- TODO If the user left a JID they moved to, return a gone+redirect error | 20 -- TODO If the user left a JID they moved to, return a gone+redirect error |
| 21 -- TODO Attempt to deregister from MUCs based on bookmarks | 21 -- TODO Attempt to deregister from MUCs based on bookmarks |
| 22 -- TODO Unsubscribe from pubsub services if a notification is received | 22 -- TODO Unsubscribe from pubsub services if a notification is received |
| 23 | 23 |
| 24 local function set_tombstone(username) | |
| 25 local time = os.time(); | |
| 26 local ok, err = graveyard:set_key(nil, username, time); | |
| 27 if ok then | |
| 28 graveyard_cache:set(username, time); | |
| 29 end | |
| 30 return ok, err; | |
| 31 end | |
| 32 | |
| 33 local function clear_tombstone(username) | |
| 34 local ok, err = graveyard:set_key(nil, username, nil); | |
| 35 if ok then | |
| 36 graveyard_cache:set(username, false); | |
| 37 end | |
| 38 return ok, err; | |
| 39 end | |
| 40 | |
| 24 module:hook_global("user-deleted", function(event) | 41 module:hook_global("user-deleted", function(event) |
| 25 if event.host == module.host then | 42 if event.host == module.host then |
| 26 local ok, err = graveyard:set(nil, event.username, os.time()); | 43 local ok, err = set_tombstone(event.username); |
| 27 if not ok then module:log("error", "Could store tombstone for %s: %s", event.username, err); end | 44 if not ok then module:log("error", "Could store tombstone for %s: %s", event.username, err); end |
| 28 end | 45 end |
| 29 end); | 46 end); |
| 30 | 47 |
| 31 -- Public API | 48 -- Public API |
| 38 -- We cached that there is no tombstone for this user | 55 -- We cached that there is no tombstone for this user |
| 39 return false; | 56 return false; |
| 40 elseif cached_result then | 57 elseif cached_result then |
| 41 tombstone = cached_result; | 58 tombstone = cached_result; |
| 42 else | 59 else |
| 43 local stored_result, err = graveyard:get(nil, username); | 60 local stored_result, err = graveyard:get_key(nil, username); |
| 44 if not stored_result and not err then | 61 if not stored_result and not err then |
| 45 -- Cache that there is no tombstone for this user | 62 -- Cache that there is no tombstone for this user |
| 46 graveyard_cache:set(username, false); | 63 graveyard_cache:set(username, false); |
| 47 return false; | 64 return false; |
| 48 elseif err then | 65 elseif err then |
| 54 end | 71 end |
| 55 | 72 |
| 56 -- Check expiry | 73 -- Check expiry |
| 57 if ttl and tombstone + ttl < os.time() then | 74 if ttl and tombstone + ttl < os.time() then |
| 58 module:log("debug", "Tombstone for %s created at %s has expired", username, datetime.datetime(tombstone)); | 75 module:log("debug", "Tombstone for %s created at %s has expired", username, datetime.datetime(tombstone)); |
| 59 graveyard:set(nil, username, nil); | 76 graveyard:set_key(nil, username, nil); |
| 60 graveyard_cache:set(username, nil); -- clear cache entry (if any) | 77 graveyard_cache:set(username, nil); -- clear cache entry (if any) |
| 61 return nil; | 78 return nil; |
| 62 end | 79 end |
| 63 | 80 |
| 64 -- Cache for the future | 81 -- Cache for the future |
