aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/tasks/notifications.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-22 11:11:23 +0100
committerDennis Fink2026-11-22 11:11:23 +0100
commit779d10051556242ba1e70b70f6c6e8f1158e861d (patch)
tree284d8228491f0a7ee9422a2100149adebad5aaa7 /webmentions_ssg/tasks/notifications.py
parentef90dc381322122ecfb9af039cf790de92807514 (diff)
downloadwebmentions-ssg-779d10051556242ba1e70b70f6c6e8f1158e861d.tar.gz
webmentions-ssg-779d10051556242ba1e70b70f6c6e8f1158e861d.zip
feat(receiver)!: enrich digest notification emailsHEADmain
Render plain-text digests with source, target, per-Webmention detail links, and a filtered dashboard link while preserving singular and plural wording. Freeze the notified rows before sending so mentions arriving during delivery are left for the next digest. BREAKING CHANGE: enabling `DIGEST_EMAIL` now requires `SERVER_NAME` so background workers can generate absolute administration URLs.
Diffstat (limited to 'webmentions_ssg/tasks/notifications.py')
-rw-r--r--webmentions_ssg/tasks/notifications.py43
1 files changed, 25 insertions, 18 deletions
diff --git a/webmentions_ssg/tasks/notifications.py b/webmentions_ssg/tasks/notifications.py
index 6a0e6b9..9871349 100644
--- a/webmentions_ssg/tasks/notifications.py
+++ b/webmentions_ssg/tasks/notifications.py
@@ -7,7 +7,7 @@
from datetime import UTC, datetime
import sqlalchemy as sa
-from flask import current_app
+from flask import current_app, render_template
from flask_mail import Message
from ..extensions import db, huey, mail
@@ -17,8 +17,8 @@ from ..models.received import ReceivedWebmention, ReceivedWebmentionStatus
def email_verified_digest() -> None:
"""Send one digest of verified, previously unreported Webmentions.
- Acquire the digest lock before notifying recipients about previously unreported
- verified mentions.
+ Acquire the digest lock before notifying recipients about previously
+ unreported verified mentions.
"""
# Resolve the lock against the current Huey backend, not the import-time one.
with huey.lock_task("send-webmention-digest"):
@@ -28,8 +28,8 @@ def email_verified_digest() -> None:
def _send_digest_locked() -> None:
"""Do the work while the current backend's digest lock is held.
- Collect verified mentions, send the configured message, and persist notification
- timestamps.
+ Collect verified mentions, send the configured message, and persist
+ notification timestamps.
"""
recipient = current_app.config.get("DIGEST_EMAIL")
if not recipient:
@@ -43,24 +43,28 @@ def _send_digest_locked() -> None:
ReceivedWebmention.updated_at <= cutoff,
)
- count = (
- db.session.scalar(
- sa.select(sa.func.count())
- .select_from(ReceivedWebmention)
- .where(*conditions)
+ # Freeze the list before sending. Only rows represented in this email may
+ # be marked as notified, even if new verified mentions arrive meanwhile.
+ mentions = db.session.execute(
+ sa.select(
+ ReceivedWebmention.uuid,
+ ReceivedWebmention.source,
+ ReceivedWebmention.target,
)
- or 0
- )
+ .where(*conditions)
+ .order_by(ReceivedWebmention.updated_at, ReceivedWebmention.uuid)
+ ).all()
- if count == 0:
+ if not mentions:
return
+ count = len(mentions)
+ subject = f"{count} new verified webmention{'s' if count != 1 else ''} on your site"
message = Message(
- subject=f"Webmention digest: {count} new verified",
+ subject=subject,
recipients=[recipient],
- body=(
- f"{count} new Webmentions have been verified.\n\n"
- "Visit your Webmentions administration page to review them.\n"
+ body=render_template(
+ "emails/verified_digest.txt", count=count, mentions=mentions
),
)
@@ -68,7 +72,10 @@ def _send_digest_locked() -> None:
db.session.execute(
sa.update(ReceivedWebmention)
- .where(*conditions)
+ .where(
+ ReceivedWebmention.uuid.in_([mention.uuid for mention in mentions]),
+ *conditions,
+ )
.values(notification_sent_at=cutoff)
.execution_options(synchronize_session="fetch")
)