Skip to content

Commit

Permalink
ntfy alerts should include the thing you're moderating
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunsi committed Dec 19, 2024
1 parent 843f0f1 commit bf73085
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
20 changes: 8 additions & 12 deletions frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,49 +312,45 @@ def content_upload():
@login_required
def content_request_review(asset_id):
try:
asset = ib.get(f"asset/{asset_id}")
asset = get_asset(asset_id)
except Exception:
abort(404)

if asset["userdata"].get("user") != g.user:
if asset.user != g.user:
return error("Cannot review")

if "state" in asset["userdata"]: # not in new state?
if asset.state != State.REVIEW:
return error("Cannot review")

moderation_message = "{asset} uploaded by {user}. ".format(
user=g.user,
asset=asset["filetype"].capitalize(),
asset=asset.filetype.capitalize(),
)

if g.user_is_admin:
update_asset_userdata(asset, state=State.CONFIRMED, moderated_by=g.user)
app.logger.warn(
"auto-confirming {} because it was uploaded by admin {}".format(
asset["id"], g.user
asset.id, g.user
)
)
moderation_message += "It was automatically confirmed because user is an admin."
elif g.user_without_limits:
update_asset_userdata(asset, state=State.CONFIRMED, moderated_by=g.user)
app.logger.warn(
"auto-confirming {} because it was uploaded by no-limits user {}".format(
asset["id"], g.user
asset.id, g.user
)
)
moderation_message += (
"It was automatically confirmed because user is on the no-limits list."
)
else:
moderation_url = url_for("content_moderate", asset_id=asset_id, _external=True)
app.logger.info(
"moderation url for {} is {}".format(asset["id"], moderation_url)
)
update_asset_userdata(asset, state=State.REVIEW)
moderation_message += f"Check it at {moderation_url}"
moderation_message += f"Check it at {asset.moderate_url}"

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

return jsonify(ok=True)

Expand Down
18 changes: 14 additions & 4 deletions helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@ def to_dict(self, mod_data=False):
"user": self.user,
"filetype": self.filetype,
"thumb": self.thumb,
"url": url_for("static", filename=cached_asset_name(self)),
"url": self.url,
} | (
{
"moderate_url": url_for(
"content_moderate", asset_id=self.id, _external=True
),
"moderate_url": self.moderate_url,
"moderated_by": self.moderated_by,
"state": self.state,
"starts": self.starts,
Expand All @@ -90,6 +88,18 @@ def to_dict(self, mod_data=False):
else {}
)

@property
def external_url(self):
return url_for("static", filename=cached_asset_name(self), _external=True)

@property
def moderate_url(self):
return url_for("content_moderate", asset_id=self.id, _external=True)

@property
def url(self):
return url_for("static", filename=cached_asset_name(self))


def to_int(num):
return (
Expand Down
18 changes: 15 additions & 3 deletions ib_hosted.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from requests import Session

from conf import CONFIG
from helper import State


class IBHosted:
Expand Down Expand Up @@ -58,6 +59,17 @@ def get_scoped_api_key(statements, expire=60, uses=16):


def update_asset_userdata(asset, **kw):
userdata = asset["userdata"]
userdata.update(kw)
ib.post("asset/{}".format(asset["id"]), userdata=json_dumps(userdata))
for k, v in kw.items():
setattr(asset, k, v)
ib.post(
"asset/{}".format(asset["id"]),
userdata=json_dumps(
{
"user": asset.user,
"state": getattr(State, asset.state),
"starts": asset.starts,
"ends": asset.ends,
"moderated_by": asset.moderated_by,
}
),
)
11 changes: 8 additions & 3 deletions notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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 +32,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,9 +59,14 @@ 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"] = asset.moderate_url
headers["Attach"] = asset.external_url

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

0 comments on commit bf73085

Please sign in to comment.