Skip to content

Commit

Permalink
send more metadata to ntfy clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunsi committed Dec 24, 2024
1 parent 7193a5a commit 04163e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
get_user_assets,
is_within_timeframe,
login_required,
parse_asset,
)
from util.redis import REDIS
from util.sso import DEFAULT_SSO_PROVIDER, SSO_CONFIG
Expand Down Expand Up @@ -457,7 +458,7 @@ def content_request_review(asset_id):
moderation_message += f"Check it at {moderation_url}"

n = Notifier()
n.message(moderation_message)
n.message(moderation_message, asset=parse_asset(asset))

return jsonify(ok=True)

Expand Down
18 changes: 15 additions & 3 deletions notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from logging import getLogger

import paho.mqtt.client as mqtt
from flask import url_for
from requests import post

from conf import CONFIG
from util import cached_asset_name

LOG = getLogger("Notifier")

Expand All @@ -22,7 +24,7 @@ def __init__(self):
self.config["MQTT_USERNAME"], self.config["MQTT_PASSWORD"]
)

def message(self, message, level="INFO", component=None):
def message(self, message, level="INFO", component=None, asset=None):
LOG.debug(f"{message=} {level=} {component=}")
if self.mqtt:
try:
Expand All @@ -32,7 +34,7 @@ def message(self, message, level="INFO", component=None):

for ntfy_url in self.config.get("NTFY", set()):
try:
self._ntfy_message(ntfy_url, message)
self._ntfy_message(ntfy_url, message, asset)
except Exception:
LOG.exception(f"ntfy url {ntfy_url} failed sending")

Expand All @@ -59,12 +61,22 @@ def _mqtt_message(self, message, level, component_suffix):

LOG.info("sent mqtt message")

def _ntfy_message(self, ntfy_url, message):
def _ntfy_message(self, ntfy_url, message, asset):
LOG.info(f"sending alert to {ntfy_url} with message {message!r}")

headers = {}
if asset is not None:
headers["Click"] = url_for(
"content_moderate", asset_id=asset.id, _external=True
)
headers["Attach"] = url_for(
"static", filename=cached_asset_name(asset), _external=True
)

r = post(
ntfy_url,
data=str(message).encode("utf-8"),
headers=headers,
)
r.raise_for_status()

Expand Down

0 comments on commit 04163e9

Please sign in to comment.