# SPDX-FileCopyrightText: 2026 Dennis Fink # # SPDX-License-Identifier: BSD-3-Clause """Authenticated source document and sent Webmention views.""" import uuid from datetime import UTC, date, datetime, time from http import HTTPStatus from typing import TypedDict, cast 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 ..forms.admin import ConfirmActionForm from ..forms.sources import ( SOURCE_STATUS_CHOICES, SourceFilterForm, default_source_page_size, ) from ..models.sources import SentWebmention, SentWebmentionStatus, SourceDocument from ..tasks.scanner import manual_scan_sources from ..tasks.sender import send_webmention from .blueprint import webmentions_bp class SourceFilterArgs(TypedDict, total=False): """Validated source-list query parameters; keys match GET form fields.""" status: str q: str date_from: str date_to: str sort: str order: str per_page: str def source_filter_form() -> SourceFilterForm: """Validate GET filters without requiring CSRF on the search form.""" form = SourceFilterForm(formdata=request.args, meta={"csrf": False}) if not form.validate(): abort(HTTPStatus.BAD_REQUEST) form.q.data = (form.q.data or "").strip() return form def source_filter_args(form: SourceFilterForm) -> SourceFilterArgs: """Serialize non-default values for pagination, sorting, and navigation.""" values: dict[str, str] = {} for name in SourceFilterArgs.__annotations__: field = form[name] value = field.data default = field.default() if callable(field.default) else field.default if value is None or value == "" or value == default: continue values[name] = value.isoformat() if isinstance(value, date) else str(value) return cast(SourceFilterArgs, values) @webmentions_bp.route("/sources") @login_required def source_list() -> ResponseReturnValue: """Display source documents with filters for outgoing delivery problems.""" form = source_filter_form() filters = source_filter_args(form) query = sa.select(SourceDocument).options( selectinload(SourceDocument.sent_webmentions) ) # EXISTS avoids duplicate source rows when several Webmentions match. sent = sa.select(SentWebmention.uuid).where( SentWebmention.source_uuid == SourceDocument.uuid ) if status := filters.get("status"): if status == "failing": query = query.where( sent.where( SentWebmention.status == SentWebmentionStatus.FAILED ).exists() ) elif status == "retrying": query = query.where( sent.where( SentWebmention.awaiting_processing, SentWebmention.attempt_count > 0, SentWebmention.next_retry_at.is_not(None), ).exists() ) else: # deleted query = query.where(SourceDocument.deleted_at.is_not(None)) if text := filters.get("q"): query = query.where( sa.or_( SourceDocument.path.icontains(text, autoescape=True), SourceDocument.url.icontains(text, autoescape=True), sent.where( SentWebmention.target.icontains(text, autoescape=True) ).exists(), ) ) if start := filters.get("date_from"): query = query.where( SourceDocument.last_seen_at >= datetime.combine(date.fromisoformat(start), time.min, UTC) ) if end := filters.get("date_to"): query = query.where( SourceDocument.last_seen_at <= datetime.combine(date.fromisoformat(end), time.max, UTC) ) count = ( sa.select(sa.func.count(SentWebmention.uuid)) .where(SentWebmention.source_uuid == SourceDocument.uuid) .scalar_subquery() ) sort_columns = { "path": SourceDocument.path, "url": SourceDocument.url, "webmentions": count, "revision": SourceDocument.revision, "created": SourceDocument.uuid, "last_seen": SourceDocument.last_seen_at, "status": sa.case((SourceDocument.deleted_at.is_(None), 0), else_=1), } sort_key = filters.get("sort", "last_seen") sort_order = filters.get("order", "desc") column = sort_columns[sort_key] query = query.order_by(column.asc() if sort_order == "asc" else column.desc()) if sort_key != "created": query = query.order_by(SourceDocument.uuid.desc()) page_size = int(filters.get("per_page", default_source_page_size())) sources = db.paginate( query, per_page=page_size, max_per_page=current_app.config["SOURCES_MAX_PAGE_SIZE"], ) sort_urls = {} for key in sort_columns: if key == sort_key: next_order = "asc" if sort_order == "desc" else "desc" else: next_order = "asc" if key in ("path", "url", "status") else "desc" sort_filters: SourceFilterArgs = {**filters, "sort": key, "order": next_order} sort_urls[key] = url_for("webmentions.source_list", **sort_filters) return render_template( "sent.html", title="Sent Webmentions", sources=sources, form=form, filters=filters, sort_key=sort_key, sort_order=sort_order, sort_urls=sort_urls, status_choices=SOURCE_STATUS_CHOICES, page_size=page_size, max_source_page_size=current_app.config["SOURCES_MAX_PAGE_SIZE"], action_form=ConfirmActionForm(), ) @webmentions_bp.post("/sources/rescan") @login_required def rescan_sources() -> ResponseReturnValue: """Queue a manual scan of sent Webmention sources. :returns: Redirect response to the sent Webmentions view. """ if not ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) filters = source_filter_args(source_filter_form()) manual_scan_sources() flash("Source rescan queued.", "success") page = request.args.get("page", 1, type=int) return redirect( url_for("webmentions.source_list", page=page if page > 1 else None, **filters) ) @webmentions_bp.route("/sources/") @login_required def source_detail(source_uuid: uuid.UUID) -> ResponseReturnValue: """Display the sent Webmentions associated with a source. :param source_uuid: UUID of the source to display. :returns: Rendered source details page. """ filters = source_filter_args(source_filter_form()) source_page = request.args.get("source_page", 1, type=int) source = db.get_or_404(SourceDocument, source_uuid) webmentions = db.paginate( sa.select(SentWebmention) .where(SentWebmention.source_uuid == source.uuid) .order_by(SentWebmention.target, SentWebmention.uuid), per_page=25, ) return render_template( "sent_source.html", title="Sent Webmentions", source=source, webmentions=webmentions, filters=filters, source_page=source_page, action_form=ConfirmActionForm(), ) @webmentions_bp.post("/sources///resend") @login_required def resend_webmention( source_uuid: uuid.UUID, webmention_uuid: uuid.UUID ) -> ResponseReturnValue: """Queue an outgoing Webmention to be sent again. The current desired source revision is reopened for processing so the sender task does not treat the Webmention as already completed. Historical successful-send fields are preserved, while result fields from the previous attempt are cleared. :param source_uuid: UUID of the source owning the Webmention. :param webmention_uuid: UUID of the sent Webmention to resend. :returns: Redirect response to the source details view. """ if not ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) filters = source_filter_args(source_filter_form()) webmention = db.get_or_404(SentWebmention, webmention_uuid) if webmention.source_uuid != source_uuid: abort(HTTPStatus.NOT_FOUND) # Requeue the existing desired revision instead of inventing a new source # revision. send_webmention() otherwise exits when this revision has already # been processed. webmention.clear_result_for_resend() db.session.commit() send_webmention(webmention_uuid) flash(f"Webmention {webmention.uuid} queued for resend.", "success") page = request.args.get("page", 1, type=int) source_page = request.args.get("source_page", 1, type=int) return redirect( url_for( "webmentions.source_detail", source_uuid=source_uuid, page=page if page > 1 else None, source_page=source_page if source_page > 1 else None, **filters, ) )