lots of changes leave me alone its better now
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
"""
|
||||
notifications.py - Multi-channel notification routing
|
||||
|
||||
Supported channels: Discord webhook, ntfy
|
||||
Supported channels: Discord webhook, ntfy, Web Push
|
||||
"""
|
||||
|
||||
import os
|
||||
import core.postgres as postgres
|
||||
import uuid
|
||||
import requests
|
||||
import time
|
||||
import json
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _sendToEnabledChannels(notif_settings, message):
|
||||
def _sendToEnabledChannels(notif_settings, message, user_uuid=None):
|
||||
"""Send message to all enabled channels. Returns True if at least one succeeded."""
|
||||
sent = False
|
||||
|
||||
@@ -22,6 +27,10 @@ def _sendToEnabledChannels(notif_settings, message):
|
||||
if ntfy.send(notif_settings["ntfy_topic"], message):
|
||||
sent = True
|
||||
|
||||
if notif_settings.get("web_push_enabled") and user_uuid:
|
||||
if web_push.send_to_user(user_uuid, message):
|
||||
sent = True
|
||||
|
||||
return sent
|
||||
|
||||
|
||||
@@ -39,6 +48,7 @@ def setNotificationSettings(userUUID, data_dict):
|
||||
"discord_enabled",
|
||||
"ntfy_topic",
|
||||
"ntfy_enabled",
|
||||
"web_push_enabled",
|
||||
]
|
||||
updates = {k: v for k, v in data_dict.items() if k in allowed}
|
||||
if not updates:
|
||||
@@ -72,3 +82,56 @@ class ntfy:
|
||||
return response.status_code == 200
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
class web_push:
|
||||
@staticmethod
|
||||
def send_to_user(user_uuid, message):
|
||||
"""Send web push notification to all subscriptions for a user."""
|
||||
try:
|
||||
from pywebpush import webpush, WebPushException
|
||||
|
||||
vapid_private_key = os.environ.get("VAPID_PRIVATE_KEY")
|
||||
vapid_claims_email = os.environ.get("VAPID_CLAIMS_EMAIL", "mailto:admin@synculous.app")
|
||||
if not vapid_private_key:
|
||||
logger.warning("VAPID_PRIVATE_KEY not set, skipping web push")
|
||||
return False
|
||||
|
||||
subscriptions = postgres.select("push_subscriptions", where={"user_uuid": user_uuid})
|
||||
if not subscriptions:
|
||||
return False
|
||||
|
||||
payload = json.dumps({"title": "Synculous", "body": message})
|
||||
sent_any = False
|
||||
|
||||
for sub in subscriptions:
|
||||
try:
|
||||
webpush(
|
||||
subscription_info={
|
||||
"endpoint": sub["endpoint"],
|
||||
"keys": {
|
||||
"p256dh": sub["p256dh"],
|
||||
"auth": sub["auth"],
|
||||
},
|
||||
},
|
||||
data=payload,
|
||||
vapid_private_key=vapid_private_key,
|
||||
vapid_claims={"sub": vapid_claims_email},
|
||||
)
|
||||
sent_any = True
|
||||
except WebPushException as e:
|
||||
# 410 Gone or 404 means subscription expired - clean up
|
||||
if hasattr(e, "response") and e.response and e.response.status_code in (404, 410):
|
||||
postgres.delete("push_subscriptions", {"id": sub["id"]})
|
||||
else:
|
||||
logger.error(f"Web push failed for subscription {sub['id']}: {e}")
|
||||
except Exception as e:
|
||||
logger.error(f"Web push error: {e}")
|
||||
|
||||
return sent_any
|
||||
except ImportError:
|
||||
logger.warning("pywebpush not installed, skipping web push")
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f"Web push send_to_user error: {e}")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user