Comparison

plugins/mod_mam/mod_mam.lua @ 9886:710a116341cd

mod_mam: Trim archive when quota has been exceeded
author Kim Alvefur <zash@zash.se>
date Tue, 07 Nov 2017 18:58:52 +0100
parent 9882:18f025b3987d
child 9894:8747ccf0008c
comparison
equal deleted inserted replaced
9885:64e16d1e91f6 9886:710a116341cd
37 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50); 37 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50);
38 local strip_tags = module:get_option_set("dont_archive_namespaces", { "http://jabber.org/protocol/chatstates" }); 38 local strip_tags = module:get_option_set("dont_archive_namespaces", { "http://jabber.org/protocol/chatstates" });
39 39
40 local archive_store = module:get_option_string("archive_store", "archive"); 40 local archive_store = module:get_option_string("archive_store", "archive");
41 local archive = module:open_store(archive_store, "archive"); 41 local archive = module:open_store(archive_store, "archive");
42
43 local cleanup_after = module:get_option_string("archive_expires_after", "1w");
44 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60);
45 local archive_item_limit = module:get_option_number("storage_archive_item_limit", archive.caps and archive.caps.quota or 1000);
42 46
43 if not archive.find then 47 if not archive.find then
44 error("mod_"..(archive._provided_by or archive.name and "storage_"..archive.name).." does not support archiving\n" 48 error("mod_"..(archive._provided_by or archive.name and "storage_"..archive.name).." does not support archiving\n"
45 .."See https://prosody.im/doc/storage and https://prosody.im/doc/archiving for more information"); 49 .."See https://prosody.im/doc/storage and https://prosody.im/doc/archiving for more information");
46 end 50 end
293 -- Check with the users preferences 297 -- Check with the users preferences
294 if shall_store(store_user, with) then 298 if shall_store(store_user, with) then
295 log("debug", "Archiving stanza: %s", stanza:top_tag()); 299 log("debug", "Archiving stanza: %s", stanza:top_tag());
296 300
297 -- And stash it 301 -- And stash it
298 local ok = archive:append(store_user, nil, clone_for_storage, time_now(), with); 302 local time = time_now();
303 local ok, err = archive:append(store_user, nil, clone_for_storage, time, with);
304 if not ok and err == "quota-limit" then
305 if archive.caps and archive.caps.truncate then
306 module:log("debug", "User '%s' over quota, trimming archive", store_user);
307 local truncated = archive:delete(store_user, {
308 truncate = archive_item_limit - 1;
309 ["end"] = type(cleanup_after) == "number" and (os.time() - cleanup_after) or nil;
310 });
311 if truncated then
312 ok, err = archive:append(store_user, nil, clone_for_storage, time, with);
313 end
314 end
315 end
299 if ok then 316 if ok then
300 local clone_for_other_handlers = st.clone(stanza); 317 local clone_for_other_handlers = st.clone(stanza);
301 local id = ok; 318 local id = ok;
302 clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up(); 319 clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up();
303 event.stanza = clone_for_other_handlers; 320 event.stanza = clone_for_other_handlers;
319 end 336 end
320 337
321 module:hook("pre-message/bare", strip_stanza_id_after_other_events, -1); 338 module:hook("pre-message/bare", strip_stanza_id_after_other_events, -1);
322 module:hook("pre-message/full", strip_stanza_id_after_other_events, -1); 339 module:hook("pre-message/full", strip_stanza_id_after_other_events, -1);
323 340
324 local cleanup_after = module:get_option_string("archive_expires_after", "1w");
325 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60);
326 if cleanup_after ~= "never" then 341 if cleanup_after ~= "never" then
327 local cleanup_storage = module:open_store("archive_cleanup"); 342 local cleanup_storage = module:open_store("archive_cleanup");
328 local cleanup_map = module:open_store("archive_cleanup", "map"); 343 local cleanup_map = module:open_store("archive_cleanup", "map");
329 344
330 local day = 86400; 345 local day = 86400;