From ef90dc381322122ecfb9af039cf790de92807514 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 21 Nov 2026 14:49:28 +0100 Subject: feat(sender): split source and delivery history Add per-Webmention detail pages so delivery state, failure guidance, status URLs, and delivery history no longer crowd the source overview. Track source lifecycle events separately for discovery, revision, deletion, and restoration, and add a source-level action to resend only failed Webmentions. --- tests/tasks/test_scanner.py | 54 +++ tests/tasks/test_source_parser.py | 5 +- tests/tasks/test_verification.py | 18 +- tests/test_cli.py | 5 +- tests/test_migration_env.py | 7 +- tests/test_net.py | 3 +- tests/views/test_moderation.py | 5 +- tests/views/test_received.py | 3 +- tests/views/test_sent.py | 504 ++++++++++++++++++--- .../4e6fbc9d207a_source_document_events.py | 53 +++ webmentions_ssg/models/sources.py | 49 +- webmentions_ssg/tasks/scanner.py | 5 + webmentions_ssg/templates/moderation.html | 2 +- webmentions_ssg/templates/received.html | 8 +- webmentions_ssg/templates/sent.html | 37 +- webmentions_ssg/templates/sent_source.html | 227 +++------- .../templates/sent_webmention_detail.html | 176 +++++++ webmentions_ssg/views/__init__.py | 4 +- webmentions_ssg/views/sent.py | 97 +++- 19 files changed, 955 insertions(+), 307 deletions(-) create mode 100644 webmentions_ssg/migrations/versions/4e6fbc9d207a_source_document_events.py create mode 100644 webmentions_ssg/templates/sent_webmention_detail.html diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py index af5a64e..dae4628 100644 --- a/tests/tasks/test_scanner.py +++ b/tests/tasks/test_scanner.py @@ -1521,3 +1521,57 @@ def test_failed_scan_does_not_update_completion_time( state = db.session.get(ScanState, 1) assert state is not None assert state.last_completed_at == previous_time + + +def test_source_lifecycle_history_records_real_transitions_only( + app: Flask, scanner_module: ModuleType +) -> None: + """An unchanged rescan is not an event; changes are preserved per source.""" + from webmentions_ssg.models.sources import SourceDocumentEvent + + timestamp = datetime(2026, 11, 1, tzinfo=UTC) + original = source_parser.ScannedSource( + path="tracked/index.html", + url="https://site.example/tracked/", + content_hash="0" * 32, + targets=frozenset({"https://example.org/linked"}), + ) + changed = source_parser.ScannedSource( + path=original.path, + url=original.url, + content_hash="1" * 32, + targets=original.targets, + ) + + with app.app_context(): + source = scanner_module.build_source(original, timestamp) + db.session.add(source) + db.session.commit() + assert [(event.action, event.revision) for event in source.history_events] == [ + ("Discovered", 1) + ] + scanner_module.apply_scan_to_source( + source, original, timestamp + timedelta(days=1) + ) + assert len(source.history_events) == 1 + scanner_module.apply_scan_to_source( + source, changed, timestamp + timedelta(days=2) + ) + scanner_module.mark_missing_sources_deleted( + {source.path: source}, frozenset(), timestamp + timedelta(days=3) + ) + scanner_module.apply_scan_to_source( + source, original, timestamp + timedelta(days=4) + ) + db.session.commit() + persisted = db.session.scalars( + sa.select(SourceDocumentEvent) + .where(SourceDocumentEvent.source_uuid == source.uuid) + .order_by(SourceDocumentEvent.id) + ).all() + assert [(event.action, event.revision) for event in persisted] == [ + ("Discovered", 1), + ("Revised", 2), + ("Deleted", 3), + ("Restored", 4), + ] diff --git a/tests/tasks/test_source_parser.py b/tests/tasks/test_source_parser.py index 38a5dc1..8c9b74a 100644 --- a/tests/tasks/test_source_parser.py +++ b/tests/tasks/test_source_parser.py @@ -1035,10 +1035,7 @@ def test_outgoing_targets_skip_urls_with_unparseable_hostname( monkeypatch.setattr(source_parser_module, "normalize_hostname", invalid_hostname) assert list( source_parser_module.iter_outgoing_targets( - content, - {}, - base_url=SOURCE_URL, - source_url=SOURCE_URL, + content, {}, base_url=SOURCE_URL, source_url=SOURCE_URL ) ) == ["https://good.example/post"] diff --git a/tests/tasks/test_verification.py b/tests/tasks/test_verification.py index d9c2f77..a581cc1 100644 --- a/tests/tasks/test_verification.py +++ b/tests/tasks/test_verification.py @@ -849,9 +849,12 @@ def test_verification_claim_race_rolls_back_without_status_event( row = db.session.get(ReceivedWebmention, identifier) assert row is not None assert row.status == "received" - assert db.session.scalar( - sa.select(sa.func.count(ReceivedWebmentionStatusEvent.id)) - ) == 0 + assert ( + db.session.scalar( + sa.select(sa.func.count(ReceivedWebmentionStatusEvent.id)) + ) + == 0 + ) def test_stale_verification_claim_race_does_not_enqueue( @@ -870,7 +873,10 @@ def test_stale_verification_claim_race_does_not_enqueue( row = db.session.get(ReceivedWebmention, identifier) assert row is not None assert row.status == "verifying" - assert db.session.scalar( - sa.select(sa.func.count(ReceivedWebmentionStatusEvent.id)) - ) == 0 + assert ( + db.session.scalar( + sa.select(sa.func.count(ReceivedWebmentionStatusEvent.id)) + ) + == 0 + ) enqueue.assert_not_called() diff --git a/tests/test_cli.py b/tests/test_cli.py index 1507544..d8781ea 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -916,8 +916,9 @@ def test_all_migrations_can_downgrade_to_empty_schema(database_app: Flask) -> No assert tables <= {"alembic_version"} if "alembic_version" in tables: assert ( - connection.execute(sa.text("SELECT count(*) FROM alembic_version")) - .scalar_one() + connection.execute( + sa.text("SELECT count(*) FROM alembic_version") + ).scalar_one() == 0 ) diff --git a/tests/test_migration_env.py b/tests/test_migration_env.py index 07462dc..7ae09bb 100644 --- a/tests/test_migration_env.py +++ b/tests/test_migration_env.py @@ -40,12 +40,7 @@ def test_offline_migration_uses_legacy_engine_url_and_metadata( patcher.setattr(context, "run_migrations", run_migrations, raising=False) runpy.run_path( - str( - Path(__file__).parents[1] - / "webmentions_ssg" - / "migrations" - / "env.py" - ) + str(Path(__file__).parents[1] / "webmentions_ssg" / "migrations" / "env.py") ) config.set_main_option.assert_called_once_with( diff --git a/tests/test_net.py b/tests/test_net.py index ec62472..99410d0 100644 --- a/tests/test_net.py +++ b/tests/test_net.py @@ -6,11 +6,10 @@ import ipaddress import socket - -import dns.exception from http import HTTPStatus from unittest.mock import Mock, PropertyMock, patch +import dns.exception import httpcore import httpx import pytest diff --git a/tests/views/test_moderation.py b/tests/views/test_moderation.py index 92446c4..31f17bc 100644 --- a/tests/views/test_moderation.py +++ b/tests/views/test_moderation.py @@ -329,10 +329,7 @@ def test_moderation_add_recovers_from_duplicate_insert_race( @pytest.mark.parametrize("kind", ["invalid", "blocked"]) def test_moderation_delete_rejects_bad_kind_or_invalid_form( - app: Flask, - client: FlaskClient, - monkeypatch: pytest.MonkeyPatch, - kind: str, + app: Flask, client: FlaskClient, monkeypatch: pytest.MonkeyPatch, kind: str ) -> None: from webmentions_ssg.views import moderation diff --git a/tests/views/test_received.py b/tests/views/test_received.py index baea6c7..384f2af 100644 --- a/tests/views/test_received.py +++ b/tests/views/test_received.py @@ -1406,8 +1406,7 @@ def test_single_received_block_source_rejects_invalid_request( ) -> None: log_in(app, client) identifier = create_received_webmention( - app, - source="not-a-url" if failure == "invalid-source" else SOURCE_URL, + app, source="not-a-url" if failure == "invalid-source" else SOURCE_URL ) mock_admin_action_form(views_module, monkeypatch, valid=failure != "csrf") diff --git a/tests/views/test_sent.py b/tests/views/test_sent.py index 644fc6f..a9db480 100644 --- a/tests/views/test_sent.py +++ b/tests/views/test_sent.py @@ -50,7 +50,9 @@ def sent_module(app: Flask) -> ModuleType: return module -@pytest.mark.parametrize("path", ["/sent", f"/sent/{uuid.uuid7()}"]) +@pytest.mark.parametrize( + "path", ["/sent", f"/sent/{uuid.uuid7()}", f"/sent/{uuid.uuid7()}/{uuid.uuid7()}"] +) def test_sent_views_require_login(client: FlaskClient, path: str) -> None: response = client.get(path) @@ -74,7 +76,7 @@ def test_sent_lists_source_documents(app: Flask, client: FlaskClient) -> None: assert "1 source document" in html assert soup.select_one('form[action="/sent"]') is not None assert "blog/example/index.html" in html - assert "https://dennisfink.me/blog/example/" in html + assert "https://dennisfink.me/blog/example/" not in html assert f"/sent/{identifier}" in html @@ -92,16 +94,13 @@ def test_sent_source_documents_table_hides_secondary_columns_on_mobile( assert table is not None headers = table.select("thead th") cells = table.select("tbody tr:first-child td") - assert len(headers) == len(cells) == 8 - for index in (2, 4, 5, 6): # URL, revision, created, last seen - for cell in (headers[index], cells[index]): - assert {"d-none", "d-md-table-cell"} <= set( - cell.get_attribute_list("class") - ) - for index in (0, 1, 3, 7): # Status, path, counts, details stay visible + assert len(headers) == len(cells) == 5 + assert {"d-none", "d-md-table-cell"} <= set(headers[3].get_attribute_list("class")) + assert {"d-none", "d-md-table-cell"} <= set(cells[3].get_attribute_list("class")) + for index in (0, 1, 2, 4): # Status, path, counts, details stay visible assert "d-none" not in cells[index].get_attribute_list("class") assert "text-break" in cells[1].get_attribute_list("class") - details = cells[7].select_one('a.btn[href^="/sent/"]') + details = cells[4].select_one('a.btn[href^="/sent/"]') assert details is not None assert details.get_text(strip=True) == "Details" @@ -128,8 +127,16 @@ def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None: assert second_target in html assert html.index(first_target) < html.index(second_target) - assert "Webmention endpoint returned HTTP 400" in html - assert "https://example.com/webmention" in html + assert "Webmention endpoint returned HTTP 400" not in html + assert "https://example.com/webmention" not in html + assert ( + len( + BeautifulSoup(html, "html.parser").select( + 'a[aria-label^="Details for Webmention"]' + ) + ) + == 2 + ) def test_sent_webmentions_table_keeps_status_and_actions_on_mobile( @@ -146,15 +153,15 @@ def test_sent_webmentions_table_keeps_status_and_actions_on_mobile( assert table is not None headers = table.select("thead th") cells = table.select("tbody tr:first-child td") - assert len(headers) == len(cells) == 7 - for index in (2, 3, 4, 5): # Active, revisions, delivery, activity + assert len(headers) == len(cells) == 5 + for index in (2, 3): # Link and last attempt for cell in (headers[index], cells[index]): assert {"d-none", "d-md-table-cell"} <= set( cell.get_attribute_list("class") ) - for index in (0, 1, 6): # Target, status, resend action stay visible + for index in (0, 1, 4): # Status, target, details stay visible assert "d-none" not in cells[index].get_attribute_list("class") - assert cells[6].select_one("button[data-action]") is not None + assert cells[4].select_one('a[href^="/sent/"]') is not None def test_sent_source_returns_404_for_unknown_source( @@ -317,7 +324,9 @@ def test_resend_sent_webmention_reopens_current_revision( def test_admin_only_links_safe_existing_status_urls( app: Flask, client: FlaskClient, status_url: str, safe: bool ) -> None: - source_identifier, _ = create_outgoing(app, status_url=status_url) + source_identifier, webmention_identifier = create_outgoing( + app, status_url=status_url + ) with app.app_context(): user = User(username="admin", password="test") db.session.add(user) @@ -328,11 +337,24 @@ def test_admin_only_links_safe_existing_status_urls( session["_user_id"] = str(user_id) session["_fresh"] = True - response = client.get(f"/sent/{source_identifier}") + # Status URLs belong to individual Webmentions, not the source overview. + source_response = client.get(f"/sent/{source_identifier}") + assert source_response.status_code == HTTPStatus.OK + assert status_url not in source_response.get_data(as_text=True) + + response = client.get(f"/sent/{source_identifier}/{webmention_identifier}") assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert (f'href="{status_url}"' in html) is safe - assert status_url in html + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + label = soup.find("dt", string="Status URL") + assert label is not None + status_details = label.find_next_sibling("dd") + assert status_details is not None + assert status_details.get_text(strip=True) == status_url + link = status_details.find("a", href=True) + assert (link is not None) is safe + if safe: + assert isinstance(link, Tag) + assert _href(link) == status_url def make_source( @@ -464,9 +486,7 @@ def test_sent_filter_by_inclusive_last_seen_dates( assert "blog/new/index.html" not in html -@pytest.mark.parametrize( - "sort", ["path", "url", "webmentions", "revision", "created", "last_seen", "status"] -) +@pytest.mark.parametrize("sort", ["status", "path", "webmentions", "last_seen"]) def test_sent_sort_links_preserve_filters( app: Flask, client: FlaskClient, sort: str ) -> None: @@ -484,32 +504,44 @@ def test_sent_sort_links_preserve_filters( }, ) assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - soup = BeautifulSoup(html, "html.parser") + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") assert soup.select_one('th[aria-sort="ascending"]') is not None links = [ parse_qs(urlsplit(_href(a)).query) for a in soup.select("th a[href]") if urlsplit(_href(a)).path == "/sent" ] - assert len(links) == 7 + assert len(links) == 4 + assert {link["sort"][0] for link in links} == { + "status", "path", "webmentions", "last_seen" + } for link in links: assert link["status"] == ["failing"] assert link["q"] == ["alpha"] assert link["date_from"] == ["2026-11-01"] assert link["per_page"] == ["37"] - assert link["sort"][0] in ( - "path", - "url", - "webmentions", - "revision", - "created", - "last_seen", - "status", - ) assert any(link["sort"] == [sort] and link["order"] == ["desc"] for link in links) +@pytest.mark.parametrize("sort", ["url", "revision", "created"]) +def test_sent_hidden_sort_keys_remain_supported_without_hidden_columns( + app: Flask, client: FlaskClient, sort: str +) -> None: + log_in(app, client) + make_source(app, "alpha", statuses=(SentWebmentionStatus.FAILED,)) + response = client.get( + "/sent", query_string={"sort": sort, "order": "asc"} + ) + assert response.status_code == HTTPStatus.OK + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + assert soup.select_one("table tbody tr") is not None + # Sorting these fields is still accepted, but they are intentionally absent + # from the simplified overview table. + assert soup.select_one('th[aria-sort="ascending"]') is None + assert len(soup.select("table thead th")) == 5 + assert len(soup.select("table thead th a[href]")) == 4 + + def test_sent_sort_orders_by_count_and_path(app: Flask, client: FlaskClient) -> None: log_in(app, client) make_source(app, "z", statuses=(SentWebmentionStatus.SENT,)) @@ -638,7 +670,15 @@ def test_sent_show_failed_and_retrying_counts_and_retry_deadline( response = client.get(f"/sent/{identifier}") assert response.status_code == HTTPStatus.OK - assert "Retry scheduled after attempt 2:" in response.get_data(as_text=True) + with app.app_context(): + mention_id = db.session.scalar( + sa.select(SentWebmention.uuid).where( + SentWebmention.source_uuid == identifier + ) + ) + response = client.get(f"/sent/{identifier}/{mention_id}") + assert response.status_code == HTTPStatus.OK + assert "Next retry" in response.get_data(as_text=True) def test_resend_preserves_source_filter_context( @@ -723,8 +763,10 @@ def test_sent_list_and_source_use_confirmation_actions_and_detail_history( log_in(app, client) source_id, mention_id = create_resendable_sent_webmention(app) with app.app_context(): + source = db.session.get(SourceDocument, source_id) mention = db.session.get(SentWebmention, mention_id) - assert mention is not None + assert source is not None and mention is not None + source.record_event("Revised") mention.record_status_event("Delivery failed") db.session.commit() @@ -741,19 +783,43 @@ def test_sent_list_and_source_use_confirmation_actions_and_detail_history( ) assert detail.select_one("section.card #source-details-heading") is not None assert detail.select_one("section #sent-webmentions-heading") is not None - assert detail.select_one("section #status-history-heading") is not None - history = detail.select("#status-history-heading ~ ol li") - assert len(history) == 1 - assert "Delivery failed" in history[0].get_text() - assert "https://example.com/resend" in history[0].get_text() + assert detail.select_one("section #source-history-heading") is not None + source_history = detail.select("#source-history-heading ~ ol li") + assert len(source_history) == 1 + assert "Revised" in source_history[0].get_text() + assert "Delivery failed" not in detail.get_text() + mention_link = detail.select_one('a[aria-label^="Details for Webmention"]') + assert mention_link is not None + assert urlsplit(_href(mention_link)).path == f"/sent/{source_id}/{mention_id}" assert detail.select_one("section table tbody tr") is not None resend = detail.select_one('button[data-action*="resend-all"]') assert resend is not None assert resend.get_text(strip=True) == "Resend all" + resend_failed = detail.select_one('button[data-action*="resend-failed"]') + assert resend_failed is not None + assert resend_failed.get_text(strip=True) == "Resend all failed" + assert resend_failed.get("data-bs-target") == "#confirmation-modal" + assert resend_failed.get("data-confirm-label") == "Resend all failed" + action = resend_failed.get("data-action") + assert isinstance(action, str) + assert urlsplit(action).path == f"/sent/{source_id}/resend-failed" + + mention_detail = BeautifulSoup( + client.get(_href(mention_link)).get_data(as_text=True), "html.parser" + ) + assert mention_detail.select_one("#status-history-heading") is not None + assert "Delivery failed" in mention_detail.get_text() + assert "Revised" not in mention_detail.get_text() + assert mention_detail.select_one('button[data-action*="return_to=detail"]') @pytest.mark.parametrize( - "path", ["/sent/failed/resend", f"/sent/{uuid.uuid7()}/resend-all"] + "path", + [ + "/sent/failed/resend", + f"/sent/{uuid.uuid7()}/resend-all", + f"/sent/{uuid.uuid7()}/resend-failed", + ], ) def test_bulk_resend_requires_login(client: FlaskClient, path: str) -> None: response = client.post(path) @@ -823,6 +889,161 @@ def test_global_resend_all_failed_ignores_filters_and_preserves_other_statuses( assert send.call_count == (2 if valid else 0) +def test_source_resend_all_failed_requeues_only_this_sources_failures( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_id = make_source( + app, + "failed-only", + statuses=( + SentWebmentionStatus.SENT, + SentWebmentionStatus.FAILED, + SentWebmentionStatus.NO_ENDPOINT, + None, + SentWebmentionStatus.FAILED, + ), + ) + other_source = make_source( + app, "other-failures", statuses=(SentWebmentionStatus.FAILED,) + ) + with app.app_context(): + source_mentions = db.session.scalars( + sa.select(SentWebmention) + .where(SentWebmention.source_uuid == source_id) + .order_by(SentWebmention.target) + ).all() + # An already scheduled retry is still a failed Webmention and should + # be reopened; preserve all untouched delivery states exactly. + source_mentions[-1].attempt_count = 3 + source_mentions[-1].next_retry_at = datetime(2026, 11, 30, tzinfo=UTC) + db.session.commit() + failed_ids = { + mention.uuid + for mention in source_mentions + if mention.status == SentWebmentionStatus.FAILED + } + untouched_before = { + mention.uuid: ( + mention.status, + mention.processed_revision, + mention.attempt_count, + mention.next_retry_at, + ) + for mention in db.session.scalars(sa.select(SentWebmention)) + if mention.uuid not in failed_ids + } + assert len(failed_ids) == 2 + + mock_admin_action_form(sent_module, monkeypatch, valid=True) + send = Mock() + monkeypatch.setattr(sent_module, "send_webmention", send) + response = client.post( + f"/sent/{source_id}/resend-failed", + query_string={ + "status": "failing", + "q": "nothing-matches", + "source_page": 2, + "page": 3, + "history_page": 4, + }, + ) + assert response.status_code == HTTPStatus.FOUND + assert urlsplit(response.headers["Location"]).path == f"/sent/{source_id}" + assert parse_qs(urlsplit(response.headers["Location"]).query) == { + "status": ["failing"], + "q": ["nothing-matches"], + "source_page": ["2"], + "page": ["3"], + "history_page": ["4"], + } + assert {call.args[0] for call in send.call_args_list} == failed_ids + assert send.call_count == 2 + with app.app_context(): + mentions = db.session.scalars(sa.select(SentWebmention)).all() + for mention in mentions: + if mention.uuid in failed_ids: + assert mention.status is None + assert mention.processed_revision is None + assert mention.attempt_count == 0 + assert mention.next_retry_at is None + else: + assert ( + mention.status, + mention.processed_revision, + mention.attempt_count, + mention.next_retry_at, + ) == untouched_before[mention.uuid] + assert any(m.source_uuid == other_source for m in mentions) + events = db.session.scalars(sa.select(SentWebmentionStatusEvent)).all() + assert {event.webmention_uuid for event in events} == failed_ids + assert all( + event.action == "Manually queued for resend (bulk)" for event in events + ) + + +def test_source_resend_all_failed_with_no_failures_queues_nothing( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_id = make_source(app, "all-ok", statuses=(SentWebmentionStatus.SENT, None)) + mock_admin_action_form(sent_module, monkeypatch, valid=True) + send = Mock() + monkeypatch.setattr(sent_module, "send_webmention", send) + # Do not follow the redirect: ConfirmActionForm is mocked for the POST, + # and the destination GET needs a real iterable form to render. + response = client.post(f"/sent/{source_id}/resend-failed") + assert response.status_code == HTTPStatus.FOUND + assert urlsplit(response.headers["Location"]).path == f"/sent/{source_id}" + with client.session_transaction() as session: + assert ("success", "0 failed Webmentions queued for resend.") in session.get( + "_flashes", [] + ) + send.assert_not_called() + with app.app_context(): + count = db.session.scalar( + sa.select(sa.func.count(SentWebmentionStatusEvent.id)) + ) + assert count == 0 + + +def test_source_resend_all_failed_rejects_invalid_form_and_unknown_source( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_id = make_source( + app, "failed-protected", statuses=(SentWebmentionStatus.FAILED,) + ) + form = mock_admin_action_form(sent_module, monkeypatch, valid=False) + send = Mock() + monkeypatch.setattr(sent_module, "send_webmention", send) + assert client.post(f"/sent/{source_id}/resend-failed").status_code == ( + HTTPStatus.BAD_REQUEST + ) + form.validate_on_submit.assert_called_once_with() + mock_admin_action_form(sent_module, monkeypatch, valid=True) + assert client.post(f"/sent/{uuid.uuid7()}/resend-failed").status_code == ( + HTTPStatus.NOT_FOUND + ) + with app.app_context(): + mention = db.session.scalar(sa.select(SentWebmention)) + assert mention is not None and mention.status == SentWebmentionStatus.FAILED + count = db.session.scalar( + sa.select(sa.func.count(SentWebmentionStatusEvent.id)) + ) + assert count == 0 + send.assert_not_called() + + def test_source_resend_all_includes_successful_and_pending_but_only_this_source( app: Flask, client: FlaskClient, @@ -915,28 +1136,31 @@ def test_sent_source_history_is_paginated_independently_of_webmentions( log_in(app, client) source_id, mention_id = create_resendable_sent_webmention(app) with app.app_context(): + source = db.session.get(SourceDocument, source_id) mention = db.session.get(SentWebmention, mention_id) - assert mention is not None + assert source is not None and mention is not None for n in range(27): - mention.record_status_event(f"Historical delivery {n}") + source.record_event(f"Source revision {n}") + mention.record_status_event("Unrelated delivery") db.session.commit() first_page = client.get(f"/sent/{source_id}") assert first_page.status_code == HTTPStatus.OK soup = BeautifulSoup(first_page.get_data(as_text=True), "html.parser") - history = soup.select("#status-history-heading ~ ol li") + history = soup.select("#source-history-heading ~ ol li") assert len(history) == 25 - assert "Historical delivery 26" in history[0].get_text() - assert "Historical delivery 0" not in first_page.get_data(as_text=True) - links = [_href(link) for link in soup.select("#status-history-heading ~ div a")] + assert "Source revision 26" in history[0].get_text() + assert "Source revision 0" not in first_page.get_data(as_text=True) + assert "Unrelated delivery" not in first_page.get_data(as_text=True) + links = [_href(link) for link in soup.select("#source-history-heading ~ div a")] assert any("history_page=2" in link for link in links) second_page = client.get(f"/sent/{source_id}?history_page=2") assert second_page.status_code == HTTPStatus.OK soup = BeautifulSoup(second_page.get_data(as_text=True), "html.parser") - history = soup.select("#status-history-heading ~ ol li") + history = soup.select("#source-history-heading ~ ol li") assert len(history) == 2 - assert "Historical delivery 0" in history[-1].get_text() + assert "Source revision 0" in history[-1].get_text() assert len(soup.select("section table tbody tr")) == 1 @@ -947,8 +1171,7 @@ def test_sent_source_history_links_keep_webmentions_page( source_id, mention_id = create_resendable_sent_webmention(app) with app.app_context(): source = db.session.get(SourceDocument, source_id) - mention = db.session.get(SentWebmention, mention_id) - assert source is not None and mention is not None + assert source is not None source.sent_webmentions.extend( SentWebmention( target=f"https://example.org/extra/{number}", @@ -958,7 +1181,7 @@ def test_sent_source_history_links_keep_webmentions_page( for number in range(25) ) for number in range(27): - mention.record_status_event(f"History event {number}") + source.record_event(f"Source event {number}") db.session.commit() response = client.get(f"/sent/{source_id}?page=2&source_page=3") @@ -966,13 +1189,11 @@ def test_sent_source_history_links_keep_webmentions_page( soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") history_links = [ (_href(link), parse_qs(urlsplit(_href(link)).query)) - for link in soup.select("#status-history-heading ~ div a") + for link in soup.select("#source-history-heading ~ div a") if urlsplit(_href(link)).path == f"/sent/{source_id}" ] next_history = next( - href - for href, query in history_links - if query.get("history_page") == ["2"] + href for href, query in history_links if query.get("history_page") == ["2"] ) query = parse_qs(urlsplit(next_history).query) assert query["page"] == ["2"] @@ -981,7 +1202,7 @@ def test_sent_source_history_links_keep_webmentions_page( following = client.get(next_history) assert following.status_code == HTTPStatus.OK next_soup = BeautifulSoup(following.get_data(as_text=True), "html.parser") - assert len(next_soup.select("#status-history-heading ~ ol li")) == 2 + assert len(next_soup.select("#source-history-heading ~ ol li")) == 2 assert len(next_soup.select("section table tbody tr")) == 1 @@ -991,7 +1212,14 @@ def test_sent_source_explains_current_failed_delivery( log_in(app, client) identifier = create_sent_source(app) - response = client.get(f"/sent/{identifier}") + with app.app_context(): + mention_id = db.session.scalar( + sa.select(SentWebmention.uuid).where( + SentWebmention.source_uuid == identifier, + SentWebmention.status == SentWebmentionStatus.FAILED, + ) + ) + response = client.get(f"/sent/{identifier}/{mention_id}") assert response.status_code == HTTPStatus.OK soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") @@ -1001,8 +1229,7 @@ def test_sent_source_explains_current_failed_delivery( reason = soup.find("details") assert reason is not None assert "Webmention endpoint returned HTTP 400" in reason.get_text() - # Keep the existing target table and its resend action. - assert soup.select_one(".table-responsive table tbody tr button[data-action]") + assert soup.select_one('button[data-action*="resend"]') def test_sent_source_explains_historical_failures_and_escapes_raw_reason( @@ -1039,9 +1266,10 @@ def test_sent_source_explains_historical_failures_and_escapes_raw_reason( ), ] ) + mention_id = mention.uuid db.session.commit() - response = client.get(f"/sent/{identifier}") + response = client.get(f"/sent/{identifier}/{mention_id}") assert response.status_code == HTTPStatus.OK html = response.get_data(as_text=True) @@ -1056,7 +1284,7 @@ def test_sent_source_explains_historical_failures_and_escapes_raw_reason( assert "<script>" in html -def test_sent_source_explains_no_endpoint_without_suggesting_remote_edits( +def test_sent_webmention_explains_no_endpoint_without_suggesting_remote_edits( app: Flask, client: FlaskClient ) -> None: log_in(app, client) @@ -1068,18 +1296,150 @@ def test_sent_source_explains_no_endpoint_without_suggesting_remote_edits( assert mention is not None mention.status = SentWebmentionStatus.NO_ENDPOINT mention.failure_reason = "No Webmention endpoint discovered" + mention_id = mention.uuid db.session.commit() - response = client.get(f"/sent/{identifier}") + response = client.get(f"/sent/{identifier}/{mention_id}") assert response.status_code == HTTPStatus.OK soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") assert "does not advertise a usable Webmention endpoint" in soup.get_text() assert "No Webmention endpoint discovered" in soup.get_text() - # The unrelated failed mention may have guidance; the no-endpoint row must not. - no_endpoint_row = next( - row - for row in soup.select(".table-responsive table tbody tr") - if "No Webmention endpoint discovered" in row.get_text() + assert "Suggested next step:" not in soup.get_text() + + +def test_sent_webmention_detail_is_scoped_to_one_target( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + source_id = create_sent_source(app) + with app.app_context(): + mentions = db.session.scalars( + sa.select(SentWebmention) + .where(SentWebmention.source_uuid == source_id) + .order_by(SentWebmention.target) + ).all() + assert len(mentions) == 2 + first, second = mentions + first.record_status_event("First target delivery") + second.record_status_event("Other target delivery") + first_id = first.uuid + second_id = second.uuid + db.session.commit() + + response = client.get(f"/sent/{source_id}/{first_id}") + assert response.status_code == HTTPStatus.OK + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + history = soup.select("#status-history-heading ~ ol li") + assert len(history) == 1 + assert "First target delivery" in history[0].get_text() + assert "Other target delivery" not in soup.get_text() + assert f"/sent/{source_id}/{second_id}" not in response.get_data(as_text=True) + + +def test_sent_webmention_detail_rejects_other_sources_and_missing_targets( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + source_id, mention_id = create_resendable_sent_webmention(app) + other_id = create_sent_source(app) + assert ( + client.get(f"/sent/{other_id}/{mention_id}").status_code == HTTPStatus.NOT_FOUND ) - assert "Suggested next step:" not in no_endpoint_row.get_text() + assert ( + client.get(f"/sent/{source_id}/{uuid.uuid7()}").status_code + == HTTPStatus.NOT_FOUND + ) + + +def test_sent_webmention_history_is_paginated_and_preserves_source_page( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + source_id, mention_id = create_resendable_sent_webmention(app) + with app.app_context(): + mention = db.session.get(SentWebmention, mention_id) + assert mention is not None + for n in range(27): + mention.record_status_event(f"Delivery attempt {n}") + db.session.commit() + + response = client.get(f"/sent/{source_id}/{mention_id}?page=3&source_page=2") + assert response.status_code == HTTPStatus.OK + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + assert len(soup.select("#status-history-heading ~ ol li")) == 25 + assert "Delivery attempt 26" in soup.get_text() + links = [_href(a) for a in soup.select("#status-history-heading ~ div a")] + second_page = next(link for link in links if "history_page=2" in link) + query = parse_qs(urlsplit(second_page).query) + assert query["source_page"] == ["2"] + assert query["page"] == ["3"] + assert "Delivery attempt 0" not in soup.get_text() + + response = client.get(second_page) + assert response.status_code == HTTPStatus.OK + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + assert len(soup.select("#status-history-heading ~ ol li")) == 2 + assert "Delivery attempt 0" in soup.get_text() + back = soup.select_one('a[href*="/sent/"]') + assert back is not None + assert urlsplit(_href(back)).path == f"/sent/{source_id}" + assert parse_qs(urlsplit(_href(back)).query) == { + "source_page": ["2"], + "page": ["3"], + } + + +def test_resend_from_sent_webmention_detail_returns_to_detail( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_id, mention_id = create_resendable_sent_webmention(app) + mock_admin_action_form(sent_module, monkeypatch, valid=True) + send = Mock() + monkeypatch.setattr(sent_module, "send_webmention", send) + + response = client.post( + f"/sent/{source_id}/{mention_id}/resend", + query_string={ + "return_to": "detail", + "source_page": 2, + "page": 3, + "history_page": 4, + "q": "resend", + }, + ) + assert response.status_code == HTTPStatus.FOUND + assert urlsplit(response.headers["Location"]).path == ( + f"/sent/{source_id}/{mention_id}" + ) + assert parse_qs(urlsplit(response.headers["Location"]).query) == { + "source_page": ["2"], + "page": ["3"], + "history_page": ["4"], + "q": ["resend"], + } + send.assert_called_once_with(mention_id) + + +def test_source_history_does_not_include_other_source_lifecycle_events( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + source_id = create_sent_source(app) + other_id = make_source(app, "other-history", statuses=()) + with app.app_context(): + source = db.session.get(SourceDocument, source_id) + other = db.session.get(SourceDocument, other_id) + assert source is not None and other is not None + source.record_event("Source discovered") + other.record_event("Another source deleted") + db.session.commit() + + response = client.get(f"/sent/{source_id}") + assert response.status_code == HTTPStatus.OK + assert "Source discovered" in response.get_data(as_text=True) + assert "Another source deleted" not in response.get_data(as_text=True) diff --git a/webmentions_ssg/migrations/versions/4e6fbc9d207a_source_document_events.py b/webmentions_ssg/migrations/versions/4e6fbc9d207a_source_document_events.py new file mode 100644 index 0000000..eb162dd --- /dev/null +++ b/webmentions_ssg/migrations/versions/4e6fbc9d207a_source_document_events.py @@ -0,0 +1,53 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Record source-document lifecycle history separately from deliveries. + +Revision ID: 4e6fbc9d207a +Revises: c8fa49e1a203 +""" + +import sqlalchemy as sa +from alembic import op + +revision = "4e6fbc9d207a" +down_revision = "c8fa49e1a203" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.create_table( + "source_document_events", + sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), + sa.Column("source_uuid", sa.Uuid(), nullable=False), + sa.Column("action", sa.String(length=80), nullable=False), + sa.Column("revision", sa.Integer(), nullable=False), + sa.Column("occurred_at", sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint( + ["source_uuid"], ["source_documents.uuid"], ondelete="CASCADE" + ), + ) + op.create_index( + "ix_source_document_events_source_id", + "source_document_events", + ["source_uuid", "id"], + ) + # The old schema did not retain lifecycle transitions. Seed one honest + # baseline snapshot per existing source; do not fabricate earlier events. + op.execute( + sa.text( + """INSERT INTO source_document_events + (source_uuid, action, revision, occurred_at) + SELECT uuid, 'Existing source at upgrade', revision, CURRENT_TIMESTAMP + FROM source_documents""" + ) + ) + + +def downgrade() -> None: + op.drop_index( + "ix_source_document_events_source_id", table_name="source_document_events" + ) + op.drop_table("source_document_events") diff --git a/webmentions_ssg/models/sources.py b/webmentions_ssg/models/sources.py index 9fc4212..9f2c2fc 100644 --- a/webmentions_ssg/models/sources.py +++ b/webmentions_ssg/models/sources.py @@ -23,7 +23,13 @@ from sqlalchemy import ( Uuid, ) from sqlalchemy.ext.hybrid import hybrid_property -from sqlalchemy.orm import Mapped, mapped_column, relationship, validates +from sqlalchemy.orm import ( + Mapped, + mapped_column, + object_session, + relationship, + validates, +) from ..extensions import Base from .utils import target_digest, uuid7_to_datetime @@ -65,6 +71,23 @@ class SourceDocument(Base): sent_webmentions: Mapped[list[SentWebmention]] = relationship( back_populates="source", cascade="all, delete-orphan" ) + history_events: Mapped[list[SourceDocumentEvent]] = relationship( + back_populates="source", cascade="all, delete-orphan" + ) + + def record_event(self, action: str, *, at: datetime | None = None) -> None: + """Record a source lifecycle change without including delivery events.""" + event = SourceDocumentEvent( + action=action, + revision=self.revision if self.revision is not None else 1, + occurred_at=at or datetime.now(UTC), + ) + if session := object_session(self): + # Do not load the entire history just to append one new event. + event.source = self + session.add(event) + else: + self.history_events.append(event) def __repr__(self) -> str: """Represent the source by its public URL.""" @@ -301,3 +324,27 @@ class SentWebmentionStatusEvent(Base): "ix_sent_webmention_status_events_webmention_id", "webmention_uuid", "id" ), ) + + +class SourceDocumentEvent(Base): + """Record discovery, revision, removal, and restoration of a source page.""" + + __tablename__ = "source_document_events" + + id: Mapped[int] = mapped_column(Integer(), primary_key=True, autoincrement=True) + source_uuid: Mapped[UUID] = mapped_column( + Uuid(as_uuid=True), + ForeignKey("source_documents.uuid", ondelete="CASCADE"), + nullable=False, + ) + action: Mapped[str] = mapped_column(String(80), nullable=False) + revision: Mapped[int] = mapped_column(Integer(), nullable=False) + occurred_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), nullable=False + ) + + source: Mapped[SourceDocument] = relationship(back_populates="history_events") + + __table_args__ = ( + Index("ix_source_document_events_source_id", "source_uuid", "id"), + ) diff --git a/webmentions_ssg/tasks/scanner.py b/webmentions_ssg/tasks/scanner.py index 9058b9b..6d4045e 100644 --- a/webmentions_ssg/tasks/scanner.py +++ b/webmentions_ssg/tasks/scanner.py @@ -49,6 +49,8 @@ def build_source(scanned: ScannedSource, scan_time: datetime) -> SourceDocument: deleted_at=None, ) + source.record_event("Discovered", at=scan_time) + for target in scanned.targets: source.sent_webmentions.append( SentWebmention(target=target, linked=True, desired_revision=1) @@ -83,10 +85,12 @@ def apply_scan_to_source( if source.deleted_at is None and source.content_hash == scanned.content_hash: return source + restored = source.deleted_at is not None source.revision += 1 source.content_hash = scanned.content_hash source.revised_at = scan_time source.deleted_at = None + source.record_event("Restored" if restored else "Revised", at=scan_time) existing = {webmention.target: webmention for webmention in source.sent_webmentions} @@ -219,6 +223,7 @@ def mark_missing_sources_deleted( source.revision += 1 source.revised_at = scan_time source.deleted_at = scan_time + source.record_event("Deleted", at=scan_time) marked += 1 for webmention in source.sent_webmentions: diff --git a/webmentions_ssg/templates/moderation.html b/webmentions_ssg/templates/moderation.html index 3c1e549..2fd922b 100644 --- a/webmentions_ssg/templates/moderation.html +++ b/webmentions_ssg/templates/moderation.html @@ -54,7 +54,7 @@ SPDX-License-Identifier: BSD-3-Clause method="post"> + class="btn btn-sm btn-danger">{{ "Unblock" if kind == "blocked" else "Remove" }} diff --git a/webmentions_ssg/templates/received.html b/webmentions_ssg/templates/received.html index e7ec5a8..323475a 100644 --- a/webmentions_ssg/templates/received.html +++ b/webmentions_ssg/templates/received.html @@ -81,7 +81,7 @@ SPDX-License-Identifier: BSD-3-Clause
Clear + class="btn btn-secondary">Clear
0 selected
Clear + class="btn btn-secondary">Clear
-
+
-

Details

+

Source details

-
Source ID
-
- {{ source.uuid }} -
-
Content hash
-
- {{ source.content_hash }} -
Path
{{ source.path }} @@ -57,46 +45,53 @@ SPDX-License-Identifier: BSD-3-Clause
{{ source.url }}
+
Revision
+
+ {{ source.revision }} +
Created
+ title="{{ source.created_at | utc_timestamp }}">{{ source.created_at | relative_time }}
Last seen
+ title="{{ source.last_seen_at | utc_timestamp }}">{{ source.last_seen_at | relative_time }}
-
Revised
+
Last revised
+ title="{{ source.revised_at | utc_timestamp }}">{{ source.revised_at | relative_time }}
-
Deleted
-
- {% if source.deleted_at is none %} - Never - {% else %} + {% if source.deleted_at is not none %} +
Deleted
+
- {% endif %} + title="{{ source.deleted_at | utc_timestamp }}">{{ source.deleted_at | relative_time }} +
+ {% endif %} +
Source ID
+
+ {{ source.uuid }}
-
Revision
+
Content hash
- {{ source.revision }} + {{ source.content_hash }}
+ + Details {% endfor %} @@ -247,35 +157,18 @@ SPDX-License-Identifier: BSD-3-Clause

No Webmentions have been discovered for this source.

{% endif %} -
-

Status history

+
+

Source history

{% if history.items %} -
    +
      {% for event in history.items %} - {% set style = sent_status_presentation.get(event.status, {"badge_class": "text-bg-info", "label": "Pending"}) %} -
    1. -
      -
      - {{ style.label }} - {{ event.action }} - -
      - -
      - Revision {{ event.revision }} · Attempt {{ event.attempt_count }} - {% if event.endpoint_response_status is not none %} - · HTTP {{ event.endpoint_response_status }} - {% endif %} -
      - {% if event.failure_reason %} -
      {{ render_failure(event.failure_reason, "sent") }}
      - {% endif %} +
    2. +
      + {{ event.action }} + Revision {{ event.revision }} +
    3. {% endfor %} @@ -291,12 +184,12 @@ SPDX-License-Identifier: BSD-3-Clause source_page=source_page if source_page > 1 else none, webmentions_page=webmentions.page if webmentions.page > 1 else none ), - fragment='status-history-heading' + fragment='source-history-heading' ) }}
{% endif %} {% else %} -

No historical delivery events were recorded for this source. Current Webmention statuses appear above.

+

No source lifecycle events have been recorded yet. Delivery history is available on each Webmention's detail page.

{% endif %} {% include "includes/confirmation_modal.html" %} diff --git a/webmentions_ssg/templates/sent_webmention_detail.html b/webmentions_ssg/templates/sent_webmention_detail.html new file mode 100644 index 0000000..fc7d7a4 --- /dev/null +++ b/webmentions_ssg/templates/sent_webmention_detail.html @@ -0,0 +1,176 @@ + +{% extends "base.html" %} + +{% from "bootstrap5/pagination.html" import render_pagination %} +{% from "includes/failure_guidance.html" import render_failure %} +{% set sent_status_presentation = { + "sent": { + "badge_class": "text-bg-success", + "label": "Sent" + }, + "no_endpoint": { + "badge_class": "text-bg-warning", + "label": "Unsupported" + }, + "failed": { + "badge_class": "text-bg-danger", + "label": "Failed" + } +} %} + +{% block content %} +
+ ← Back to source Webmentions +
+
+

Sent Webmention

+ {% set style = sent_status_presentation.get(webmention.status, {"badge_class": "text-bg-info", "label": "Pending"}) %} + {{ style.label }} +
+
+
+

Delivery details

+
+
Source
+
+ {{ source.url }} +
+
Target
+
+ {{ webmention.target }} +
+
Link
+
+ {{ 'Linked' if webmention.linked else 'Removed' }} +
+
Created
+
+ +
+
Revisions
+
+ Desired {{ webmention.desired_revision }} · Processed {{ webmention.processed_revision if webmention.processed_revision is not none else '—' }} · Sent {{ webmention.sent_revision if webmention.sent_revision is not none else '—' }} +
+
Attempts
+
+ {{ webmention.attempt_count }} +
+ {% if webmention.next_retry_at is not none and webmention.awaiting_processing %} +
Next retry
+
+ +
+ {% endif %} +
Last attempted
+
+ {% if webmention.last_attempted_at is not none %} + +{% else %}—{% endif %} +
+
Last sent
+
+ {% if webmention.last_sent_at is not none %} + +{% else %}—{% endif %} +
+ {% if webmention.webmention_endpoint is not none %} +
Endpoint
+
+ {{ webmention.webmention_endpoint }} +
+ {% endif %} + {% if webmention.endpoint_response_status is not none %} +
Endpoint HTTP
+
+ {{ webmention.endpoint_response_status }} +
+ {% endif %} + {% if webmention.status_url is not none %} +
Status URL
+
+ {% if webmention.status_url | is_http_url %}{{ webmention.status_url }}{% else %}{{ webmention.status_url }}{% endif %} +
+ {% endif %} + {% if webmention.failure_reason %} +
Failure
+
+ {{ render_failure(webmention.failure_reason, "sent") }} +
+ {% endif %} +
Webmention ID
+
+ {{ webmention.uuid }} +
+
+
+
+
+ +
+
+

Delivery history

+ {% if history.items %} +
    + {% for event in history.items %} + {% set event_style = sent_status_presentation.get(event.status, {"badge_class": "text-bg-info", "label": "Pending"}) %} +
  1. +
    + {{ event_style.label }} + {{ event.action }} + +
    +
    + Revision {{ event.revision }} · Attempt {{ event.attempt_count }} + {% if event.endpoint_response_status is not none %}· HTTP {{ event.endpoint_response_status }}{% endif %} +
    + {% if event.failure_reason %} +
    {{ render_failure(event.failure_reason, "sent") }}
    + {% endif %} +
  2. + {% endfor %} +
+ {% if history.pages > 1 %} +
+ {{ render_pagination( + history, + endpoint='webmentions.sent_webmention_history_pagination', + args=dict( + filters, + source_uuid=source.uuid, + webmention_uuid=webmention.uuid, + source_page=source_page if source_page > 1 else none, + webmentions_page=webmentions_page if webmentions_page > 1 else none + ), + fragment='status-history-heading' + ) }} +
+ {% endif %} + {% else %} +

No delivery events have been recorded for this Webmention yet.

+ {% endif %} +
+ {% include "includes/confirmation_modal.html" %} +{% endblock %} + +{% block scripts %} + {{ super() }} + +{% endblock %} diff --git a/webmentions_ssg/views/__init__.py b/webmentions_ssg/views/__init__.py index 1701838..0f8efbc 100644 --- a/webmentions_ssg/views/__init__.py +++ b/webmentions_ssg/views/__init__.py @@ -4,7 +4,7 @@ """Collect and expose the routes for the blueprint.""" -from . import dashboard, endpoint, moderation, received, session, sent, user +from . import dashboard, endpoint, moderation, received, sent, session, user from .blueprint import webmentions_bp __all__ = [ @@ -12,8 +12,8 @@ __all__ = [ "endpoint", "moderation", "received", - "session", "sent", + "session", "user", "webmentions_bp", ] diff --git a/webmentions_ssg/views/sent.py b/webmentions_ssg/views/sent.py index 614d486..10ad34b 100644 --- a/webmentions_ssg/views/sent.py +++ b/webmentions_ssg/views/sent.py @@ -13,7 +13,6 @@ import sqlalchemy as sa from flask import abort, current_app, flash, redirect, render_template, request, url_for from flask.typing import ResponseReturnValue from flask_login import login_required -from sqlalchemy.orm import selectinload from ..extensions import db from ..failure_guidance import describe_sent_failure @@ -24,6 +23,7 @@ from ..models.sources import ( SentWebmentionStatus, SentWebmentionStatusEvent, SourceDocument, + SourceDocumentEvent, ) from ..tasks.scanner import manual_scan_sources from ..tasks.sender import send_webmention @@ -39,6 +39,11 @@ webmentions_bp.add_url_rule( endpoint="sent_source_history_pagination", build_only=True, ) +webmentions_bp.add_url_rule( + "/sent//", + endpoint="sent_webmention_history_pagination", + build_only=True, +) @webmentions_bp.url_defaults @@ -46,7 +51,10 @@ def sent_history_pagination_url_defaults( endpoint: str, values: dict[str, object] ) -> None: """Map the macro's page number to the history pagination parameter.""" - if endpoint != "webmentions.sent_source_history_pagination": + if endpoint not in ( + "webmentions.sent_source_history_pagination", + "webmentions.sent_webmention_history_pagination", + ): return values["history_page"] = values.pop("page") @@ -246,11 +254,9 @@ def sent_source_detail(source_uuid: uuid.UUID) -> ResponseReturnValue: ) history = db.paginate( - sa.select(SentWebmentionStatusEvent) - .join(SentWebmention) - .options(selectinload(SentWebmentionStatusEvent.webmention)) - .where(SentWebmention.source_uuid == source.uuid) - .order_by(SentWebmentionStatusEvent.id.desc()), + sa.select(SourceDocumentEvent) + .where(SourceDocumentEvent.source_uuid == source.uuid) + .order_by(SourceDocumentEvent.id.desc()), page=request.args.get("history_page", 1, type=int), per_page=25, error_out=False, @@ -268,6 +274,39 @@ def sent_source_detail(source_uuid: uuid.UUID) -> ResponseReturnValue: ) +@webmentions_bp.route("/sent//") +@login_required +def sent_webmention_detail( + source_uuid: uuid.UUID, webmention_uuid: uuid.UUID +) -> ResponseReturnValue: + """Show one target's delivery status and its own historical events.""" + filters = sent_filter_args(sent_filter_form()) + source = db.get_or_404(SourceDocument, source_uuid) + webmention = db.get_or_404(SentWebmention, webmention_uuid) + if webmention.source_uuid != source.uuid: + abort(HTTPStatus.NOT_FOUND) + + history = db.paginate( + sa.select(SentWebmentionStatusEvent) + .where(SentWebmentionStatusEvent.webmention_uuid == webmention.uuid) + .order_by(SentWebmentionStatusEvent.id.desc()), + page=request.args.get("history_page", 1, type=int), + per_page=25, + error_out=False, + ) + return render_template( + "sent_webmention_detail.html", + title="Sent Webmention", + source=source, + webmention=webmention, + history=history, + filters=filters, + source_page=request.args.get("source_page", 1, type=int), + webmentions_page=request.args.get("page", 1, type=int), + action_form=ConfirmActionForm(), + ) + + @webmentions_bp.post("/sent///resend") @login_required def resend_webmention( @@ -308,6 +347,18 @@ def resend_webmention( page = request.args.get("page", 1, type=int) source_page = request.args.get("source_page", 1, type=int) history_page = request.args.get("history_page", 1, type=int) + if request.args.get("return_to") == "detail": + return redirect( + url_for( + "webmentions.sent_webmention_detail", + source_uuid=source_uuid, + webmention_uuid=webmention_uuid, + page=page if page > 1 else None, + source_page=source_page if source_page > 1 else None, + history_page=history_page if history_page > 1 else None, + **filters, + ) + ) return redirect( url_for( "webmentions.sent_source_detail", @@ -325,8 +376,9 @@ def queue_sent_webmentions( ) -> list[uuid.UUID]: """Reopen sends in one transaction, then dispatch each task once. - A global retry selects only failed rows; a source-level retry can select - every delivery, regardless of status, list filters, or pagination. + By default, only failed rows are selected, optionally scoped to a source. + A source-level resend-all can explicitly include every status. Selection + does not depend on list filters or pagination. """ query = sa.select(SentWebmention) if failed_only: @@ -388,3 +440,30 @@ def resend_all_source_webmentions(source_uuid: uuid.UUID) -> ResponseReturnValue **filters, ) ) + + +@webmentions_bp.post("/sent//resend-failed") +@login_required +def resend_failed_source_webmentions(source_uuid: uuid.UUID) -> ResponseReturnValue: + """Resend only failed outgoing Webmentions for one source document.""" + if not ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + filters = sent_filter_args(sent_filter_form()) + db.get_or_404(SourceDocument, source_uuid) + identifiers = queue_sent_webmentions(source_uuid=source_uuid) + flash(f"{len(identifiers)} failed Webmentions queued for resend.", "success") + + page = request.args.get("page", 1, type=int) + history_page = request.args.get("history_page", 1, type=int) + source_page = request.args.get("source_page", 1, type=int) + return redirect( + url_for( + "webmentions.sent_source_detail", + source_uuid=source_uuid, + page=page if page > 1 else None, + history_page=history_page if history_page > 1 else None, + source_page=source_page if source_page > 1 else None, + **filters, + ) + ) -- cgit v1.3.1