comparison mod_presence_cache/mod_presence_cache.lua @ 2175:cc0cf6748885

mod_presence_cache: Also cache the 'show' value
author Kim Alvefur <zash@zash.se>
date Wed, 11 May 2016 23:12:51 +0200
parents aa24d49c47ef
children b76b062e77db
comparison
equal deleted inserted replaced
2174:7be158b3376f 2175:cc0cf6748885
5 local datetime = require"util.datetime"; 5 local datetime = require"util.datetime";
6 local cache = require "util.cache"; 6 local cache = require "util.cache";
7 7
8 local cache_size = module:get_option_number("presence_cache_size", 100); 8 local cache_size = module:get_option_number("presence_cache_size", 100);
9 9
10 local bare_cache = {}; -- [username NUL bare_jid] = { [full_jid] = timestamp, ... } 10 local bare_cache = {}; -- [username NUL bare_jid] = { [full_jid] = { timestamp, ... } }
11 11
12 local function on_evict(cache_key) 12 local function on_evict(cache_key)
13 local bare_cache_key = cache_key:match("^%Z+%z[^/]+"); 13 local bare_cache_key = cache_key:match("^%Z+%z[^/]+");
14 local full_jid = cache_key:match("%z(.*)$"); 14 local full_jid = cache_key:match("%z(.*)$");
15 local jids = bare_cache[bare_cache_key]; 15 local jids = bare_cache[bare_cache_key];
49 presence_cache:set(cache_key, nil); 49 presence_cache:set(cache_key, nil);
50 on_evict(cache_key); 50 on_evict(cache_key);
51 return; 51 return;
52 end 52 end
53 53
54 local stamp = datetime.datetime(); 54 local presence_bits = {
55 stamp = datetime.datetime();
56 show = stanza:get_child_text("show");
57 };
55 if jids then 58 if jids then
56 jids[contact_full] = stamp; 59 jids[contact_full] = presence_bits;
57 else 60 else
58 jids = { [contact_full] = stamp }; 61 jids = { [contact_full] = presence_bits };
59 bare_cache[bare_cache_key] = jids; 62 bare_cache[bare_cache_key] = jids;
60 end 63 end
61 presence_cache:set(cache_key, true); 64 presence_cache:set(cache_key, true);
62 end 65 end
63 end 66 end
74 77
75 local bare_cache_key = username .. "\0" .. contact_bare; 78 local bare_cache_key = username .. "\0" .. contact_bare;
76 79
77 local cached = bare_cache[bare_cache_key]; 80 local cached = bare_cache[bare_cache_key];
78 if not cached then return end 81 if not cached then return end
79 for jid, stamp in pairs(cached) do 82 for jid, presence_bits in pairs(cached) do
80 local presence = st.presence({ to = origin.full_jid, from = jid }) 83 local presence = st.presence({ to = origin.full_jid, from = jid })
81 :tag("delay", { xmlns = "urn:xmpp:delay", from = module.host, stamp = stamp }):up(); 84 if presence_bits.show then
85 presence:tag("show"):text(presence_bits.show):up();
86 end
87 if presence_bits.stamp then
88 presence:tag("delay", { xmlns = "urn:xmpp:delay", from = module.host, stamp = presence_bits.stamp }):up();
89 end
82 origin.send(presence); 90 origin.send(presence);
83 end 91 end
84 end 92 end
85 93
86 module:hook("pre-presence/bare", answer_probe_from_cache, 10); 94 module:hook("pre-presence/bare", answer_probe_from_cache, 10);