From 4bd1d9e348918b2e55db47a1ac3489d035cf55e2 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 12 Nov 2026 19:57:08 +0100 Subject: 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. --- webmentions_ssg/models.py | 68 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) (limited to 'webmentions_ssg/models.py') diff --git a/webmentions_ssg/models.py b/webmentions_ssg/models.py index 31516d5..9442309 100644 --- a/webmentions_ssg/models.py +++ b/webmentions_ssg/models.py @@ -6,6 +6,7 @@ from __future__ import annotations +import json import uuid from datetime import UTC, datetime from enum import StrEnum @@ -28,8 +29,17 @@ from sqlalchemy import ( from sqlalchemy.orm import Mapped, mapped_column, relationship, validates from werkzeug.security import check_password_hash, generate_password_hash -from . import Base -from .url_digest import received_url_digest, sent_target_digest +from .extensions import Base + + +class ReceivedStatus(StrEnum): + """Persisted lifecycle state of an incoming Webmention.""" + + RECEIVED = "received" + VERIFYING = "verifying" + VERIFIED = "verified" + FAILED = "failed" + DELETED = "deleted" class SentWebmentionStatus(StrEnum): @@ -120,7 +130,9 @@ class ReceivedWebmention(Base): self.url_digest = received_url_digest(source, target) return value - status: Mapped[str] = mapped_column(Text(), nullable=False, default="received") + status: Mapped[str] = mapped_column( + Text(), nullable=False, default=ReceivedStatus.RECEIVED + ) # Counts stale-row requeues, not Huey's bounded in-task retries. A fresh # explicit submission resets this counter. recovery_attempts: Mapped[int] = mapped_column( @@ -154,6 +166,24 @@ class ReceivedWebmention(Base): """ return f" {self.target!r}>" + @staticmethod + def verification_reset_values( + *, at: datetime, recoveries: int = 0 + ) -> dict[str, object]: + """Return the columns to reset when verification is explicitly reopened.""" + return { + "status": ReceivedStatus.RECEIVED, + "failure_reason": None, + "recovery_attempts": recoveries, + "updated_at": at, + } + + def reset_for_verification(self) -> ReceivedWebmention: + """Reopen an existing row for manual verification and return it.""" + for key, value in self.verification_reset_values(at=datetime.now(UTC)).items(): + setattr(self, key, value) + return self + @property def verified(self) -> bool: """ @@ -161,7 +191,7 @@ class ReceivedWebmention(Base): :return: Whether the Webmention status is ``verified``. """ - return self.status == "verified" + return self.status == ReceivedStatus.VERIFIED @property def created_at(self) -> datetime: @@ -308,6 +338,23 @@ class SentWebmention(Base): """ return f"" + def reopen( + self, revision: int, active: bool, *, resend: bool = False + ) -> SentWebmention: + """Reopen delivery for a revision while preserving send history.""" + self.desired_revision = revision + self.active = active + self.attempt_count = 0 + self.next_retry_at = None + if resend: + self.processed_revision = None + self.status = None + self.failure_reason = None + self.endpoint = None + self.response_status = None + self.status_url = None + return self + @property def pending(self) -> bool: """ @@ -338,3 +385,16 @@ def uuid7_to_datetime(identifier: uuid.UUID) -> datetime: :return: Timestamp as a timezone-aware UTC datetime. """ return datetime.fromtimestamp(identifier.time / 1000, tz=UTC) + + +def received_url_digest(source: str, target: str) -> str: + """Fingerprint an unambiguous, ordered source/target pair.""" + encoded = json.dumps( + [source, target], ensure_ascii=False, separators=(",", ":") + ).encode("utf-8") + return sha256(encoded).hexdigest() + + +def sent_target_digest(target: str) -> str: + """Fingerprint an outgoing target (unique within each source ID).""" + return sha256(target.encode("utf-8")).hexdigest() -- cgit v1.3.1