Comparison

plugins/mod_mam/mod_mam.lua @ 10734:136c41a3d03c

mod_mam: Factor out "should we store this" into a function Meant to improve readability and ease further improvements to this algorithm.
author Kim Alvefur <zash@zash.se>
date Tue, 21 Apr 2020 00:53:21 +0200
parent 10683:2f0b7f1d5e75
child 10735:f2838ffcc499
comparison
equal deleted inserted replaced
10733:89e0f5cb60a1 10734:136c41a3d03c
261 end); 261 end);
262 end 262 end
263 return stanza; 263 return stanza;
264 end 264 end
265 265
266 local function should_store(stanza) --> boolean, reason: string
267 local orig_type = stanza.attr.type or "normal";
268 -- We store chat messages or normal messages that have a body
269 if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then
270 return false, "type";
271 end
272
273 -- or if hints suggest we shouldn't
274 if not stanza:get_child("store", "urn:xmpp:hints") then -- No hint telling us we should store
275 if stanza:get_child("no-permanent-store", "urn:xmpp:hints")
276 or stanza:get_child("no-store", "urn:xmpp:hints") then -- Hint telling us we should NOT store
277 return false, "hint";
278 end
279 end
280
281 return true, "default";
282 end
283
266 -- Handle messages 284 -- Handle messages
267 local function message_handler(event, c2s) 285 local function message_handler(event, c2s)
268 local origin, stanza = event.origin, event.stanza; 286 local origin, stanza = event.origin, event.stanza;
269 local log = c2s and origin.log or module._log; 287 local log = c2s and origin.log or module._log;
270 local orig_type = stanza.attr.type or "normal";
271 local orig_from = stanza.attr.from; 288 local orig_from = stanza.attr.from;
272 local orig_to = stanza.attr.to or orig_from; 289 local orig_to = stanza.attr.to or orig_from;
273 -- Stanza without 'to' are treated as if it was to their own bare jid 290 -- Stanza without 'to' are treated as if it was to their own bare jid
274 291
275 -- Whos storage do we put it in? 292 -- Whos storage do we put it in?
278 local with = jid_bare(c2s and orig_to or orig_from); 295 local with = jid_bare(c2s and orig_to or orig_from);
279 296
280 -- Filter out <stanza-id> that claim to be from us 297 -- Filter out <stanza-id> that claim to be from us
281 event.stanza = strip_stanza_id(stanza, store_user); 298 event.stanza = strip_stanza_id(stanza, store_user);
282 299
283 -- We store chat messages or normal messages that have a body 300 local should, why = should_store(stanza);
284 if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then 301 if not should then
285 log("debug", "Not archiving stanza: %s (type)", stanza:top_tag()); 302 log("debug", "Not archiving stanza: %s (%s)", stanza:top_tag(), why);
286 return; 303 return;
287 end
288
289 -- or if hints suggest we shouldn't
290 if not stanza:get_child("store", "urn:xmpp:hints") then -- No hint telling us we should store
291 if stanza:get_child("no-permanent-store", "urn:xmpp:hints")
292 or stanza:get_child("no-store", "urn:xmpp:hints") then -- Hint telling us we should NOT store
293 log("debug", "Not archiving stanza: %s (hint)", stanza:top_tag());
294 return;
295 end
296 end 304 end
297 305
298 local clone_for_storage; 306 local clone_for_storage;
299 if not strip_tags:empty() then 307 if not strip_tags:empty() then
300 clone_for_storage = st.clone(stanza); 308 clone_for_storage = st.clone(stanza);