diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/tasks/test_notifications.py | 172 | ||||
| -rw-r--r-- | tests/tasks/test_receiver.py | 45 | ||||
| -rw-r--r-- | tests/test_views.py | 10 | ||||
| -rw-r--r-- | webmentions_ssg/config.py | 2 | ||||
| -rw-r--r-- | webmentions_ssg/migrations/versions/408fc2eca9ce_track_webmention_digest_notifications.py | 36 | ||||
| -rw-r--r-- | webmentions_ssg/models.py | 4 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/consumer.py | 5 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/notifications.py | 49 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/receiver.py | 16 | ||||
| -rw-r--r-- | webmentions_ssg/views.py | 6 |
10 files changed, 169 insertions, 176 deletions
diff --git a/tests/tasks/test_notifications.py b/tests/tasks/test_notifications.py index fa6ef79..f523d82 100644 --- a/tests/tasks/test_notifications.py +++ b/tests/tasks/test_notifications.py @@ -2,7 +2,9 @@ # # SPDX-License-Identifier: BSD-3-Clause + import uuid +from types import ModuleType from unittest.mock import patch import pytest @@ -12,143 +14,117 @@ from webmentions_ssg import DATABASE as db from webmentions_ssg.models import ReceivedWebmention -def test_sends_email_for_verified_webmention(app: Flask) -> None: - from webmentions_ssg.tasks.notifications import send_webmention_notification +@pytest.fixture +def notifications(app: Flask) -> ModuleType: + from webmentions_ssg.tasks import notifications + + return notifications - app.config["NOTIFICATION_EMAIL"] = "owner@example.com" - identifier = uuid.uuid7() +def create_mention(app: Flask, status: str = "verified") -> uuid.UUID: + identifier = uuid.uuid7() with app.app_context(): db.session.add( ReceivedWebmention( uuid=identifier, - source="https://source.example.com/post", + source=f"https://source.example/{identifier}", target="https://dennisfink.me/post", - status="verified", + status=status, ) ) db.session.commit() + return identifier - with patch("webmentions_ssg.tasks.notifications.mail.send") as send: - send_webmention_notification.call_local(identifier) - send.assert_called_once() - message = send.call_args.args[0] - assert message.recipients == ["owner@example.com"] - assert "https://source.example.com/post" in message.body - assert "https://dennisfink.me/post" in message.body +def test_digest_sends_one_email_for_multiple_verified_mentions( + notifications: ModuleType, app: Flask +) -> None: + app.config["NOTIFICATION_EMAIL"] = "owner@example.com" + first = create_mention(app) + second = create_mention(app) + unverified = create_mention(app, "failed") -@pytest.mark.parametrize("status", ["received", "verifying", "failed", "deleted"]) -def test_skips_unverified_webmentions(app: Flask, status: str) -> None: - from webmentions_ssg.tasks.notifications import send_webmention_notification + with app.app_context(): + with patch("webmentions_ssg.tasks.notifications.mail.send") as send: + notifications.send_webmention_digest() + notifications.send_webmention_digest() - app.config["NOTIFICATION_EMAIL"] = "owner@example.com" - identifier = uuid.uuid7() + send.assert_called_once() + message = send.call_args.args[0] - with app.app_context(): - db.session.add( - ReceivedWebmention( - uuid=identifier, - source="https://source.example.com/post", - target="https://dennisfink.me/post", - status=status, - ) - ) - db.session.commit() + assert message.recipients == ["owner@example.com"] + assert "2 new Webmentions" in message.body + assert "source.example" not in message.body - with patch("webmentions_ssg.tasks.notifications.mail.send") as send: - send_webmention_notification.call_local(identifier) + first_mention = db.session.get(ReceivedWebmention, first) + second_mention = db.session.get(ReceivedWebmention, second) + unverified_mention = db.session.get(ReceivedWebmention, unverified) - send.assert_not_called() + assert first_mention is not None + assert second_mention is not None + assert unverified_mention is not None + assert first_mention.notification_sent_at is not None + assert second_mention.notification_sent_at is not None + assert unverified_mention.notification_sent_at is None -def test_skips_when_notifications_disabled(app: Flask) -> None: - from webmentions_ssg.tasks.notifications import send_webmention_notification +def test_digest_skips_when_notifications_disabled( + notifications: ModuleType, app: Flask +) -> None: app.config["NOTIFICATION_EMAIL"] = None - identifier = uuid.uuid7() + identifier = create_mention(app) with app.app_context(): - db.session.add( - ReceivedWebmention( - uuid=identifier, - source="https://source.example.com/post", - target="https://dennisfink.me/post", - status="verified", - ) - ) - db.session.commit() - with patch("webmentions_ssg.tasks.notifications.mail.send") as send: - send_webmention_notification.call_local(identifier) + notifications.send_webmention_digest() send.assert_not_called() + mention = db.session.get(ReceivedWebmention, identifier) + assert mention is not None + assert mention.notification_sent_at is None -def test_skips_unknown_webmention(app: Flask) -> None: - from webmentions_ssg.tasks.notifications import send_webmention_notification +def test_digest_does_not_mark_mentions_when_email_fails( + notifications: ModuleType, app: Flask +) -> None: app.config["NOTIFICATION_EMAIL"] = "owner@example.com" + identifier = create_mention(app) with app.app_context(): - with patch("webmentions_ssg.tasks.notifications.mail.send") as send: - send_webmention_notification.call_local(uuid.uuid7()) + with ( + patch( + "webmentions_ssg.tasks.notifications.mail.send", + side_effect=RuntimeError("SMTP unavailable"), + ), + pytest.raises(RuntimeError, match="SMTP unavailable"), + ): + notifications.send_webmention_digest() - send.assert_not_called() + mention = db.session.get(ReceivedWebmention, identifier) + assert mention is not None + assert mention.notification_sent_at is None -@pytest.mark.parametrize( - ("send_notification", "recipient", "should_enqueue"), - [ - pytest.param(None, "owner@example.com", True, id="default-enabled"), - pytest.param(True, "owner@example.com", True, id="explicitly-enabled"), - pytest.param(False, "owner@example.com", False, id="explicitly-disabled"), - pytest.param(True, None, True, id="no-recipient-configured"), - ], -) -def test_queues_notification_after_verification( - app: Flask, - send_notification: bool | None, - recipient: str | None, - should_enqueue: bool, -) -> None: - from webmentions_ssg.tasks import receiver + with patch("webmentions_ssg.tasks.notifications.mail.send") as send: + notifications.send_webmention_digest() - app.config["NOTIFICATION_EMAIL"] = recipient - identifier = uuid.uuid7() + send.assert_called_once() + mention = db.session.get(ReceivedWebmention, identifier) - with app.app_context(): - db.session.add( - ReceivedWebmention( - uuid=identifier, - source="https://source.example.com/post", - target="https://dennisfink.me/post", - status="received", - ) - ) - db.session.commit() + assert mention is not None + assert mention.notification_sent_at is not None - with ( - patch( - "webmentions_ssg.tasks.receiver.source_mentions_target", return_value=True - ), - patch("webmentions_ssg.tasks.receiver.send_webmention_notification") as enqueue, - ): - if send_notification is None: - # Exercise the task's default argument. - receiver.verify_webmention.call_local(identifier) - else: - receiver.verify_webmention.call_local( - identifier, send_notification=send_notification - ) + +def test_digest_skips_when_no_new_mentions( + notifications: ModuleType, app: Flask +) -> None: + app.config["NOTIFICATION_EMAIL"] = "owner@example.com" with app.app_context(): - webmention = db.session.get(ReceivedWebmention, identifier) - assert webmention is not None - assert webmention.status == "verified" + with patch("webmentions_ssg.tasks.notifications.mail.send") as send: + notifications.send_webmention_digest() - if should_enqueue: - enqueue.assert_called_once_with(identifier) - else: - enqueue.assert_not_called() + send.assert_not_called() diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py index f4ab09f..1943b65 100644 --- a/tests/tasks/test_receiver.py +++ b/tests/tasks/test_receiver.py @@ -468,18 +468,10 @@ def test_verify_webmention_persists_final_state( ) case _: raise AssertionError(f"Unexpected outcome: {outcome}") - - app.config["NOTIFICATION_EMAIL"] = "owner@example.com" - - with patch.object(receiver, "send_webmention_notification") as enqueue: - receiver.verify_webmention.call_local(identifier) + receiver.verify_webmention.call_local(identifier) assert get_webmention_state(app, identifier) == (expected_status, expected_reason) - # The receiver always enqueues when requested; the notification task - # checks the persisted status before deciding whether to send an email. - enqueue.assert_called_once_with(identifier) - @patch("webmentions_ssg.tasks.receiver.source_mentions_target") @pytest.mark.parametrize("failure", ["temporary-http", "network"]) @@ -500,16 +492,10 @@ def test_verify_webmention_persists_retryable_failure_and_reraises( source_mentions_target.side_effect = exception - app.config["NOTIFICATION_EMAIL"] = "owner@example.com" - - with ( - patch.object(receiver, "send_webmention_notification") as enqueue, - pytest.raises(type(exception), match=str(exception)), - ): + with pytest.raises(type(exception), match=str(exception)): receiver.verify_webmention.call_local(identifier) assert get_webmention_state(app, identifier) == ("failed", str(exception)) - enqueue.assert_not_called() def test_verify_webmention_ignores_unknown_identifier( @@ -621,30 +607,3 @@ def test_source_mentions_target_falls_back_for_unknown_encoding( assert receiver.source_mentions_target(SOURCE_URL, TARGET_URL) fetch_source.assert_called_once_with(SOURCE_URL) - - -@patch("webmentions_ssg.tasks.receiver.source_mentions_target", return_value=True) -def test_verify_webmention_keeps_verified_state_when_notification_enqueue_fails( - source_mentions_target: Mock, - app: Flask, - receiver: ModuleType, - caplog: pytest.LogCaptureFixture, -) -> None: - identifier = create_webmention(app) - - with ( - patch.object( - receiver, - "send_webmention_notification", - side_effect=RuntimeError("Huey unavailable"), - ) as enqueue, - caplog.at_level(logging.ERROR), - ): - # Enqueueing fails, but verification must return without raising. - receiver.verify_webmention.call_local(identifier) - - source_mentions_target.assert_called_once_with(SOURCE_URL, TARGET_URL) - enqueue.assert_called_once_with(identifier) - assert get_webmention_state(app, identifier) == ("verified", None) - assert f"Could not enqueue webmention notification for {identifier}" in caplog.text - assert "RuntimeError: Huey unavailable" in caplog.text diff --git a/tests/test_views.py b/tests/test_views.py index 54098db..ed944c9 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -156,7 +156,7 @@ def test_endpoint_creates_webmention( identifier = webmention.uuid assert response.headers["Location"].endswith(str(identifier)) - verify_webmention.assert_called_once_with(identifier, send_notification=True) + verify_webmention.assert_called_once_with(identifier) def test_endpoint_is_idempotent( @@ -190,7 +190,7 @@ def test_endpoint_is_idempotent( identifier = webmention.uuid - verify_webmention.assert_called_once_with(identifier, send_notification=True) + verify_webmention.assert_called_once_with(identifier) def test_resending_resets_failure_state( @@ -229,7 +229,7 @@ def test_resending_resets_failure_state( assert webmention.status == "received" assert webmention.failure_reason is None - verify_webmention.assert_called_once_with(identifier, send_notification=False) + verify_webmention.assert_called_once_with(identifier) @pytest.mark.parametrize("path", ["/sent", f"/sent/{uuid.uuid7()}"]) @@ -604,7 +604,7 @@ def test_reverify_received_webmention( assert webmention.status == "received" assert webmention.failure_reason is None - verify_webmention.assert_called_once_with(identifier, send_notification=False) + verify_webmention.assert_called_once_with(identifier) def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None: @@ -931,4 +931,4 @@ def test_endpoint_reverifies_after_cooldown( assert stored.status == "received" assert stored.failure_reason is None - verify.assert_called_once_with(identifier, send_notification=False) + verify.assert_called_once_with(identifier) diff --git a/webmentions_ssg/config.py b/webmentions_ssg/config.py index aea201b..010e393 100644 --- a/webmentions_ssg/config.py +++ b/webmentions_ssg/config.py @@ -24,7 +24,9 @@ class DefaultConfig: SOURCE_DIRECTORY = None SOURCE_BASE_URL = None SCANNER_SCHEDULE = "0 * * * *" + NOTIFICATION_EMAIL = None + NOTIFICATION_SCHEDULE = "0 8 * * *" MAIL_SERVER = "localhost" MAIL_PORT = 25 diff --git a/webmentions_ssg/migrations/versions/408fc2eca9ce_track_webmention_digest_notifications.py b/webmentions_ssg/migrations/versions/408fc2eca9ce_track_webmention_digest_notifications.py new file mode 100644 index 0000000..905b2c2 --- /dev/null +++ b/webmentions_ssg/migrations/versions/408fc2eca9ce_track_webmention_digest_notifications.py @@ -0,0 +1,36 @@ +"""Track webmention digest notifications + +Revision ID: 408fc2eca9ce +Revises: be6e229d6a3d +Create Date: 2026-10-29 20:24:42.008723 + +""" + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "408fc2eca9ce" +down_revision = "be6e229d6a3d" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table("received_webmentions", schema=None) as batch_op: + batch_op.add_column( + sa.Column("notification_sent_at", sa.DateTime(timezone=True), nullable=True) + ) + op.execute( + "UPDATE received_webmentions SET notification_sent_at = CURRENT_TIMESTAMP" + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table("received_webmentions", schema=None) as batch_op: + batch_op.drop_column("notification_sent_at") + + # ### end Alembic commands ### diff --git a/webmentions_ssg/models.py b/webmentions_ssg/models.py index 688da67..11740a8 100644 --- a/webmentions_ssg/models.py +++ b/webmentions_ssg/models.py @@ -110,6 +110,10 @@ class ReceivedWebmention(Base): nullable=False, ) + notification_sent_at: Mapped[datetime | None] = mapped_column( + DateTime(timezone=True), nullable=True, default=None + ) + __table_args__ = ( UniqueConstraint("source", "target", name="uq_webmention_source_target"), ) diff --git a/webmentions_ssg/tasks/consumer.py b/webmentions_ssg/tasks/consumer.py index 4dd10c7..559ef5d 100644 --- a/webmentions_ssg/tasks/consumer.py +++ b/webmentions_ssg/tasks/consumer.py @@ -26,6 +26,11 @@ def create_consumer() -> Huey: schedule = crontab(*app.config["SCANNER_SCHEDULE"].split(), strict=True) HUEY.periodic_task(schedule)(scanner.scan_sources) + notification_schedule = crontab( + *app.config["NOTIFICATION_SCHEDULE"].split(), strict=True + ) + HUEY.periodic_task(notification_schedule)(notifications.send_webmention_digest) + return HUEY.huey diff --git a/webmentions_ssg/tasks/notifications.py b/webmentions_ssg/tasks/notifications.py index d2195b0..475fad1 100644 --- a/webmentions_ssg/tasks/notifications.py +++ b/webmentions_ssg/tasks/notifications.py @@ -2,10 +2,11 @@ # # SPDX-License-Identifier: BSD-3-Clause -"""Send email notifications for verified incoming Webmentions.""" +"""Send bounded digest notifications for verified incoming Webmentions.""" -import uuid +from datetime import UTC, datetime +import sqlalchemy as sa from flask import current_app from flask_mail import Message @@ -15,24 +16,50 @@ from .. import MAIL as mail from ..models import ReceivedWebmention -@huey.task(retries=2, retry_delay=60) -def send_webmention_notification(webmention_uuid: uuid.UUID) -> None: - """Send an email when a received webmention has been verified.""" +@huey.lock_task("send-webmention-digest") +def send_webmention_digest() -> None: + """Send one digest of verified, previously unreported Webmentions.""" + recipient = current_app.config.get("NOTIFICATION_EMAIL") if not recipient: return - webmention = db.session.get(ReceivedWebmention, webmention_uuid) - if webmention is None or webmention.status != "verified": + cutoff = datetime.now(UTC) + + conditions = ( + ReceivedWebmention.status == "verified", + ReceivedWebmention.notification_sent_at.is_(None), + ReceivedWebmention.updated_at <= cutoff, + ) + + count = ( + db.session.scalar( + sa.select(sa.func.count()) + .select_from(ReceivedWebmention) + .where(*conditions) + ) + or 0 + ) + + if count == 0: return message = Message( - subject="New verified webmention", + subject=f"Webmention digest: {count} new verified", recipients=[recipient], body=( - "A received webmention has been verified.\n\n" - f"Source: {webmention.source}\n" - f"Target: {webmention.target}\n" + f"{count} new Webmentions have been verified.\n\n" + "Visit your Webmentions administration page to review them.\n" ), ) + + # If SMTP fails, the records remain pending for the next digest. mail.send(message) + + db.session.execute( + sa.update(ReceivedWebmention) + .where(*conditions) + .values(notification_sent_at=cutoff) + .execution_options(synchronize_session="fetch") + ) + db.session.commit() diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py index 0b7743d..2bb7222 100644 --- a/webmentions_ssg/tasks/receiver.py +++ b/webmentions_ssg/tasks/receiver.py @@ -17,7 +17,6 @@ from .. import DATABASE as db from .. import HUEY as huey from ..models import ReceivedWebmention from ..url_security import AddressResolutionError, is_public_url -from .notifications import send_webmention_notification class VerificationError(Exception): @@ -217,9 +216,7 @@ def source_mentions_target(source_url: str, target_url: str) -> bool: @huey.task(retries=2, retry_delay=50) -def verify_webmention( - webmention_uuid: uuid.UUID, send_notification: bool = True -) -> None: +def verify_webmention(webmention_uuid: uuid.UUID) -> None: """ Verify a received webmention and store its verification status. @@ -227,8 +224,6 @@ def verify_webmention( retry the task. :param webmention_uuid: Identifier of the received webmention to verify. - :param send_notification: Whether to enqueue an email notification after - successful verification. :raises TemporaryFetchError: If fetching the source fails temporarily. :raises httpx.RequestError: If an HTTP request error occurs while fetching the source. @@ -270,12 +265,3 @@ def verify_webmention( webmention.failure_reason = "Source does not mention target" db.session.commit() - - if send_notification: - try: - send_webmention_notification(webmention_uuid) - except Exception: - # Notification delivery must not change the persisted verification status. - current_app.logger.exception( - "Could not enqueue webmention notification for %s", webmention_uuid - ) diff --git a/webmentions_ssg/views.py b/webmentions_ssg/views.py index bb1f38a..7a97480 100644 --- a/webmentions_ssg/views.py +++ b/webmentions_ssg/views.py @@ -170,7 +170,7 @@ def reverify_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: db.session.commit() - verify_webmention(identifier, send_notification=False) + verify_webmention(identifier) flash(f"Webmention {webmention.uuid} queued for reverification.", "success") @@ -327,7 +327,6 @@ def endpoint() -> ResponseReturnValue: ).scalar_one_or_none() should_verify = False - send_notification = False if webmention is None: identifier = uuid.uuid7() @@ -339,7 +338,6 @@ def endpoint() -> ResponseReturnValue: try: db.session.commit() should_verify = True - send_notification = True except sa_exc.IntegrityError: db.session.rollback() @@ -377,7 +375,7 @@ def endpoint() -> ResponseReturnValue: identifier = webmention.uuid if should_verify: - verify_webmention(identifier, send_notification=send_notification) + verify_webmention(identifier) status_url = url_for("root.status", identifier=str(identifier), _external=True) |
