aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/views/sources.py
blob: b68d2fde0a99f75dab20e49de52e4d507462a32c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

"""Authenticated source document and sent Webmention views."""

import uuid
from http import HTTPStatus

import sqlalchemy as sa
from flask import abort, flash, redirect, render_template, 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 ..models.sources import SentWebmention, SourceDocument
from ..tasks.scanner import manual_scan_sources
from ..tasks.sender import send_webmention
from .blueprint import webmentions_bp


@webmentions_bp.route("/sources")
@login_required
def source_list() -> ResponseReturnValue:
    """Display sources with sent Webmentions.

    :returns: Rendered page containing the paginated source list.
    """
    sources = db.paginate(
        sa.select(SourceDocument)
        .options(selectinload(SourceDocument.sent_webmentions))
        .order_by(SourceDocument.last_seen_at.desc()),
        per_page=25,
    )
    return render_template(
        "sent.html",
        title="Sent Webmentions",
        sources=sources,
        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)

    manual_scan_sources()
    flash("Source rescan queued.", "success")

    return redirect(url_for("webmentions.source_list"))


@webmentions_bp.route("/sources/<uuid:source_uuid>")
@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.
    """
    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,
        action_form=ConfirmActionForm(),
    )


@webmentions_bp.post("/sources/<uuid:source_uuid>/<uuid:webmention_uuid>/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)

    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")

    return redirect(url_for("webmentions.source_detail", source_uuid=source_uuid))