aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/models.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-12 19:57:08 +0100
committerDennis Fink2026-11-12 19:57:08 +0100
commit4bd1d9e348918b2e55db47a1ac3489d035cf55e2 (patch)
treef7ed41a374a8a4136792039a262dc62aa8cf8c59 /webmentions_ssg/models.py
parent7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (diff)
downloadwebmentions-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 'webmentions_ssg/models.py')
-rw-r--r--webmentions_ssg/models.py68
1 files changed, 64 insertions, 4 deletions
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"<ReceivedWebmention {self.source!r} -> {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"<SentWebmention {self.target!r}>"
+ 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()