Diff

mod_push2/mod_push2.lua @ 6190:aa240145aa22

mod_push2: support for disabling push notifications For example when logging out
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Wed, 05 Feb 2025 11:04:15 -0500 (4 months ago)
parent 6186:1cf563a94620
child 6212:051974b4c900
line wrap: on
line diff
--- a/mod_push2/mod_push2.lua	Tue Feb 04 23:40:33 2025 +0100
+++ b/mod_push2/mod_push2.lua	Wed Feb 05 11:04:15 2025 -0500
@@ -106,6 +106,28 @@
 end
 module:hook("iq-set/self/"..xmlns_push..":enable", push_enable)
 
+local function push_disable(event)
+	local origin, stanza = event.origin, event.stanza;
+	local enable = stanza.tags[1];
+	origin.log("debug", "Attempting to disable push notifications")
+	-- Tie registration to client, via client_id with sasl2 or else fallback to resource
+	local registration_id = origin.client_id or origin.resource
+	-- TODO: can we move to keyval+ on trunk?
+	local registrations = push2_registrations:get(origin.username) or {}
+	registrations[registration_id] = nil
+	if not push2_registrations:set(origin.username, registrations) then
+		origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
+	else
+		origin.push_registration_id = nil
+		origin.push_registration = nil
+		origin.first_hibernated_push = nil
+		origin.log("info", "Push notifications disabled for %s (%s)", tostring(stanza.attr.from), registration_id)
+		origin.send(st.reply(stanza))
+	end
+	return true
+end
+module:hook("iq-set/self/"..xmlns_push..":disable", push_disable)
+
 -- urgent stanzas should be delivered without delay
 local function is_voip(stanza)
 	if stanza.name == "message" then