Finish implementing notification rules
parent
f93d37d1c0
commit
4dd8224a77
@ -0,0 +1,38 @@
|
||||
import requests
|
||||
|
||||
from core.util import logs
|
||||
|
||||
NTFY_URL = "https://ntfy.sh"
|
||||
|
||||
log = logs.get_logger(__name__)
|
||||
|
||||
|
||||
# Actual function to send a message to a topic
|
||||
def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None):
|
||||
if url is None:
|
||||
url = NTFY_URL
|
||||
headers = {"Title": "Fisk"}
|
||||
if title:
|
||||
headers["Title"] = title
|
||||
if priority:
|
||||
headers["Priority"] = priority
|
||||
if tags:
|
||||
headers["Tags"] = tags
|
||||
requests.post(
|
||||
f"{url}/{topic}",
|
||||
data=msg,
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
# Sendmsg helper to send a message to a user's notification settings
|
||||
def sendmsg(user, *args, **kwargs):
|
||||
notification_settings = user.get_notification_settings()
|
||||
|
||||
if notification_settings.ntfy_topic is None:
|
||||
# No topic set, so don't send
|
||||
return
|
||||
else:
|
||||
topic = notification_settings.ntfy_topic
|
||||
|
||||
raw_sendmsg(*args, **kwargs, url=notification_settings.ntfy_url, topic=topic)
|
@ -0,0 +1,22 @@
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from redis import StrictRedis
|
||||
from core.util import logs
|
||||
from core.lib.rules import process_rules
|
||||
import msgpack
|
||||
|
||||
log = logs.get_logger("processing")
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
r = StrictRedis(unix_socket_path="/var/run/socks/redis.sock", db=0)
|
||||
p = r.pubsub()
|
||||
p.psubscribe("messages")
|
||||
for message in p.listen():
|
||||
if message:
|
||||
if message["channel"] == b"messages":
|
||||
data = message["data"]
|
||||
try:
|
||||
unpacked = msgpack.unpackb(data, raw=False)
|
||||
except TypeError:
|
||||
continue
|
||||
process_rules(unpacked)
|
@ -1,2 +1,5 @@
|
||||
unixsocket /var/run/socks/redis.sock
|
||||
unixsocketperm 777
|
||||
unixsocketperm 777
|
||||
|
||||
# For Monolith PubSub
|
||||
port 6379
|
Loading…
Reference in New Issue