Comparison

mod_cloud_notify/mod_cloud_notify.lua @ 2201:eb5555a3a535

mod_cloud_notify: Enable persistent storage of user notification settings
author Kim Alvefur <zash@zash.se>
date Mon, 06 Jun 2016 15:58:41 +0200
parent 2200:e9e38ae8037f
child 2246:a3e3dc9131e7
comparison
equal deleted inserted replaced
2200:e9e38ae8037f 2201:eb5555a3a535
13 -- configuration 13 -- configuration
14 local include_body = module:get_option("push_notification_with_body", false); 14 local include_body = module:get_option("push_notification_with_body", false);
15 local include_sender = module:get_option("push_notification_with_sender", false); 15 local include_sender = module:get_option("push_notification_with_sender", false);
16 16
17 -- For keeping state across reloads 17 -- For keeping state across reloads
18 local push_enabled = module:shared("push-enabled-users"); 18 local push_enabled = module:open_store();
19 -- TODO map store would be better here
19 20
20 -- http://xmpp.org/extensions/xep-0357.html#disco 21 -- http://xmpp.org/extensions/xep-0357.html#disco
21 module:hook("account-disco-info", function(event) 22 module:hook("account-disco-info", function(event)
22 (event.reply or event.stanza):tag("feature", {var=xmlns_push}):up(); 23 (event.reply or event.stanza):tag("feature", {var=xmlns_push}):up();
23 end); 24 end);
36 local publish_options = stanza.tags[1].tags[1]; 37 local publish_options = stanza.tags[1].tags[1];
37 if publish_options and ( publish_options.name ~= "x" or publish_options.attr.xmlns ~= "jabber:x:data" ) then 38 if publish_options and ( publish_options.name ~= "x" or publish_options.attr.xmlns ~= "jabber:x:data" ) then
38 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid publish options")); 39 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid publish options"));
39 return true; 40 return true;
40 end 41 end
41 local user_push_services = push_enabled[origin.username]; 42 local user_push_services = push_enabled:get(origin.username);
42 if not user_push_services then 43 if not user_push_services then
43 user_push_services = {}; 44 user_push_services = {};
44 push_enabled[origin.username] = user_push_services;
45 end 45 end
46 user_push_services[push_jid .. "<" .. (push_node or "")] = { 46 user_push_services[push_jid .. "<" .. (push_node or "")] = {
47 jid = push_jid; 47 jid = push_jid;
48 node = push_node; 48 node = push_node;
49 count = 0; 49 count = 0;
50 options = publish_options; 50 options = publish_options;
51 }; 51 };
52 origin.send(st.reply(stanza)); 52 local ok, err = push_enabled:set(origin.username, user_push_services);
53 if not ok then
54 origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
55 else
56 origin.send(st.reply(stanza));
57 end
53 return true; 58 return true;
54 end); 59 end);
55 60
56 -- http://xmpp.org/extensions/xep-0357.html#disabling 61 -- http://xmpp.org/extensions/xep-0357.html#disabling
57 module:hook("iq-set/self/"..xmlns_push..":disable", function (event) 62 module:hook("iq-set/self/"..xmlns_push..":disable", function (event)
60 local push_node = stanza.tags[1].attr.node; -- A 'node' attribute MAY be included 65 local push_node = stanza.tags[1].attr.node; -- A 'node' attribute MAY be included
61 if not push_jid then 66 if not push_jid then
62 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid")); 67 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid"));
63 return true; 68 return true;
64 end 69 end
65 local user_push_services = push_enabled[origin.username]; 70 local user_push_services = push_enabled:get(origin.username);
66 for key, push_info in pairs(user_push_services) do 71 for key, push_info in pairs(user_push_services) do
67 if push_info.jid == push_jid and (not push_node or push_info.node == push_node) then 72 if push_info.jid == push_jid and (not push_node or push_info.node == push_node) then
68 user_push_services[key] = nil; 73 user_push_services[key] = nil;
69 end 74 end
70 end 75 end
82 87
83 -- http://xmpp.org/extensions/xep-0357.html#publishing 88 -- http://xmpp.org/extensions/xep-0357.html#publishing
84 local function handle_notify_request(origin, stanza) 89 local function handle_notify_request(origin, stanza)
85 local to = stanza.attr.to; 90 local to = stanza.attr.to;
86 local node = to and jid.split(to) or origin.username; 91 local node = to and jid.split(to) or origin.username;
87 local user_push_services = push_enabled[node]; 92 local user_push_services = push_enabled:get(node);
88 if not user_push_services then return end 93 if not user_push_services then return end
89 94
90 for _, push_info in pairs(user_push_services) do 95 for _, push_info in pairs(user_push_services) do
91 push_info.count = push_info.count + 1; 96 push_info.count = push_info.count + 1;
92 local push_jid, push_node = push_info.jid, push_info.node; 97 local push_jid, push_node = push_info.jid, push_info.node;
161 module:hook("smacks-hibernation-start", hibernate_session); 166 module:hook("smacks-hibernation-start", hibernate_session);
162 module:hook("smacks-hibernation-end", restore_session); 167 module:hook("smacks-hibernation-end", restore_session);
163 168
164 169
165 module:hook("message/offline/broadcast", function(event) 170 module:hook("message/offline/broadcast", function(event)
166 local user_push_services = push_enabled[event.origin.username]; 171 local user_push_services = push_enabled:get(event.origin.username);
167 if not user_push_services then return end 172 if not user_push_services then return end
168 173
169 for _, push_info in pairs(user_push_services) do 174 for _, push_info in pairs(user_push_services) do
170 if push_info then 175 if push_info then
171 push_info.count = 0; 176 push_info.count = 0;