diff options
| author | Dennis Fink | 2026-11-12 19:57:08 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-12 19:57:08 +0100 |
| commit | 4bd1d9e348918b2e55db47a1ac3489d035cf55e2 (patch) | |
| tree | f7ed41a374a8a4136792039a262dc62aa8cf8c59 /webmentions_ssg/views/admin.py | |
| parent | 7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (diff) | |
| download | webmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.tar.gz webmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.zip | |
refactor(core): split modules by responsibility
Break up monolithic views, task helpers, networking, CLI, and
application extension setup so each module owns a focused concern.
Reorganize the test suite around the new boundaries while preserving
existing Webmention behavior and coverage.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/views/admin.py | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/webmentions_ssg/views/admin.py b/webmentions_ssg/views/admin.py new file mode 100644 index 0000000..46e3b18 --- /dev/null +++ b/webmentions_ssg/views/admin.py @@ -0,0 +1,217 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Authenticated incoming and outgoing Webmention management.""" + +import uuid + +import sqlalchemy as sa +from flask import abort, flash, redirect, render_template, request, url_for +from flask.typing import ResponseReturnValue +from flask_login import login_required +from sqlalchemy.orm import selectinload + +from .. import forms +from ..extensions import DATABASE as db +from ..models import ReceivedWebmention, SentWebmention, Source +from ..tasks.receiver import verify_webmention +from ..tasks.scanner import manual_scan_sources +from ..tasks.sender import send_webmention +from . import root_page + + +@root_page.route("/received") +@login_required +def received() -> ResponseReturnValue: + """ + Display received Webmentions. + + :return: Rendered page containing the paginated received Webmentions. + """ + webmentions = db.paginate( + sa.select(ReceivedWebmention).order_by(ReceivedWebmention.uuid.desc()), + per_page=25, + ) + + return render_template( + "received.html", + title="Received Webmentions", + webmentions=webmentions, + action_form=forms.AdminActionForm(), + ) + + +@root_page.post("/received/<uuid:identifier>/delete") +@login_required +def delete_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: + """ + Delete a received Webmention. + + :param identifier: Identifier of the Webmention to delete. + :return: Redirect response to the received Webmentions view. + """ + form = forms.AdminActionForm() + + if not form.validate_on_submit(): + return abort(400) + + webmention = db.session.get(ReceivedWebmention, identifier) + + if webmention is None: + return abort(404) + + db.session.delete(webmention) + db.session.commit() + + flash(f"Webmention {webmention.uuid} deleted.", "success") + + return redirect( + url_for("root.received", page=request.args.get("page", 1, type=int)) + ) + + +@root_page.post("/received/<uuid:identifier>/reverify") +@login_required +def reverify_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: + """ + Queue a received Webmention for reverification. + + The Webmention status is reset before a new verification task is queued. + + :param identifier: Identifier of the Webmention to reverify. + :return: Redirect response to the received Webmentions view. + """ + form = forms.AdminActionForm() + + if not form.validate_on_submit(): + return abort(400) + + webmention = db.session.get(ReceivedWebmention, identifier) + + if webmention is None: + return abort(404) + + webmention = webmention.reset_for_verification() + + db.session.commit() + + verify_webmention(identifier) + + flash(f"Webmention {webmention.uuid} queued for reverification.", "success") + + return redirect( + url_for("root.received", page=request.args.get("page", 1, type=int)) + ) + + +@root_page.route("/sent") +@login_required +def sent() -> ResponseReturnValue: + """ + Display sources with sent Webmentions. + + :return: Rendered page containing the paginated source list. + """ + sources = db.paginate( + sa.select(Source) + .options(selectinload(Source.sent_webmentions)) + .order_by(Source.last_seen_at.desc()), + per_page=25, + ) + return render_template( + "sent.html", + title="Sent Webmentions", + sources=sources, + action_form=forms.AdminActionForm(), + ) + + +@root_page.post("/sent/rescan") +@login_required +def rescan_sent_sources() -> ResponseReturnValue: + """ + Queue a manual scan of sent Webmention sources. + + :return: Redirect response to the sent Webmentions view. + """ + form = forms.AdminActionForm() + if form.validate_on_submit(): + manual_scan_sources() + flash("Source rescan queued.", "success") + return redirect(url_for("root.sent")) + + +@root_page.route("/sent/<uuid:source_identifier>") +@login_required +def sent_source(source_identifier: uuid.UUID) -> ResponseReturnValue: + """ + Display the sent Webmentions associated with a source. + + :param source_identifier: Identifier of the source to display. + :return: Rendered source details page. + """ + source = db.session.scalar( + sa.select(Source).where(Source.uuid == source_identifier) + ) + + if source is None: + return abort(404) + + webmentions = db.paginate( + sa.select(SentWebmention) + .where(SentWebmention.source_id == source.uuid) + .order_by(SentWebmention.target, SentWebmention.uuid), + per_page=25, + ) + + return render_template( + "sent_source.html", + title="Sent Webmentions", + source=source, + webmentions=webmentions, + action_form=forms.AdminActionForm(), + ) + + +@root_page.post("/sent/<uuid:source_identifier>/<uuid:webmention_identifier>/resend") +@login_required +def resend_sent_webmention( + source_identifier: uuid.UUID, webmention_identifier: 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_identifier: Identifier of the source owning the Webmention. + :param webmention_identifier: Identifier of the sent Webmention to resend. + :return: Redirect response to the source details view. + """ + form = forms.AdminActionForm() + + if not form.validate_on_submit(): + return abort(400) + + webmention = db.session.get(SentWebmention, webmention_identifier) + + if webmention is None or webmention.source_id != source_identifier: + return abort(404) + + # Reopen the existing desired revision instead of inventing a new source + # revision. send_webmention() otherwise exits when this revision has already + # been processed. + webmention = webmention.reopen( + webmention.desired_revision, webmention.active, resend=True + ) + + db.session.commit() + + send_webmention(webmention_identifier) + + flash(f"Webmention {webmention.uuid} queued for resend.", "success") + + return redirect(url_for("root.sent_source", source_identifier=source_identifier)) |
