annotate mod_mam_smart_retention/mod_mam_smart_retention.lua @ 6562:5da6fb562df9 default tip

mod_unified_push: Fix push error handling (fixes #2000) Use the error object that send_iq() passes as an argument to it's reject callback instead of attempting and failing to do the parsing in the callback itself.
author kmq
date Mon, 06 Jul 2026 14:23:57 +0200
parents e920ce2f6472
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6349
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local um = require "prosody.core.usermanager";
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local async = require "prosody.util.async";
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local filters = require "prosody.util.filters";
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local archive = module:open_store("archive", "archive");
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local archive_fetches = module:open_store("archive_fetches", "keyval+");
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local strategy = module:get_option_enum("smart_retention_strategy", "one", "all");
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local min_retention = module:get_option_period("smart_retention_min", false);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local function get_newest_fetched(username, client_id)
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 archive_fetches:get_key(username, client_id or "");
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local function set_newest_fetched(username, client_id, msg_id)
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 archive_fetches:set_key(username, client_id or "", {
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 newest = msg_id;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 });
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 function handle_query_result(event)
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if not event.start_time then
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 module:log("debug", "Ignoring empty query results");
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 return; -- empty results
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local origin = event.origin;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if not origin.outgoing_stanza_queue then
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 -- No XEP-0198, so can't reliably determine successful fetches
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 module:log("warn", "Ignoring query on non-smacks stream");
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 return;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local newest_result = event.end_id;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local newest_fetched = get_newest_fetched(origin.username, origin.client_id);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if newest_result <= newest_fetched then
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 -- The client hasn't actually fetched anything newer than before
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 origin.outgoing_stanza_queue:add_checkpoint(function ()
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 origin.log("debug", "Recording successful delivery of messages up to %s", newest_result);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 set_newest_fetched(origin.username, origin.client_id, newest_result);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 function handle_sent(stanza, session)
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 local msg_id = stanza.name == "message" and stanza:get_meta("archive-id");
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 if not msg_id then return stanza; end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local queue = session.outgoing_stanza_queue;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 if not queue then return stanza; end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 queue:add_checkpoint(function ()
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 session.log("debug", "Recording successful delivery of message %s", msg_id);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 set_newest_fetched(session.username, session.client_id, msg_id);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 return stanza;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 module:hook("resource-bind", function (event)
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 event.session.log("debug", "Installing filter to track message delivery");
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 filters.add_filter(event.session, "stanzas/out", handle_sent);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 module:hook("archive-query", handle_query_result);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 module:daily("Clean up delivered messages", function ()
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 local n_users, n_messages = 0, 0;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 for username in assert(um.users(module.host)) do
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local clients = archive_fetches:get(username);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 if clients then
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local newest, oldest;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 for client_id, fetch_info in pairs(clients) do --luacheck: ignore 213/client_id
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local fetched = fetch_info.newest;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if not newest or fetched > newest then
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 newest = fetched;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if not oldest or fetched < oldest then
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 oldest = fetched;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 local expire_older_than;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 if strategy == "one" then
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 expire_older_than = newest;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 elseif strategy == "all" then
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 expire_older_than = oldest;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 module:log("debug", "Expiring messages prior to %s for %s", expire_older_than, username);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96
6359
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
97 local item, expire_before = archive:get(username, expire_older_than);
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
98 if not item and expire_before ~= "item-not-found" then
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
99 module:log("warn", "Error fetching item '%s' for '%s': %s", expire_older_than, username, expire_before);
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
100 else
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
101 local ok, err = archive:delete(username, {
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
102 ["end"] = expire_before;
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
103 ["before"] = min_retention ~= math.huge and (now() - min_retention) or nil;
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
104 });
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
105 if ok then
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
106 local n_deleted = tonumber(ok);
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
107 if n_deleted then
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
108 n_messages = n_messages + n_deleted;
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
109 end
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
110 else
e920ce2f6472 mod_mam_smart_retention: Improve error handling during cleanup
Matthew Wild <mwild1@gmail.com>
parents: 6349
diff changeset
111 module:log("warn", "Expiry failure: deleting messages before %s for %s: %s", expire_older_than, username, err);
6349
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 n_users = n_users + 1;
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 async.sleep(0.1);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 if n_users > 0 then
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 module:log("info", "Expired %d delivered messages for %d users", n_messages, n_users);
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 end
2d256ea0c157 mod_mam_smart_retention: New experimental module for smart retention in MAM archives
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end);