Inbox: intake and verified processing of follows (phase 2, step 4)
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 14s
CI / test (push) Successful in 1m57s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s

Intake (in the request, no network): size, content type and shape
checks, blocked domains refused (403), duplicates acknowledged once,
then stored and answered 202.

Processing (federation.inbox worker task):
- verify the HTTP signature with either scheme, as of when it arrived,
  using the cached key and refreshing the sender once on failure (key
  rotation); a deleted account's Delete still verifies from cache
- the signer must be the activity's actor and the activity id must be
  on the actor's host
- handlers: Follow (accept, or pending with manual approval; repeat
  follows re-accepted; blocked accounts rejected), Undo of a Follow
  (only by the same actor), Delete of an actor, Update of an actor;
  anything else recorded
- items being processed are marked, retried on transient failures,
  re-queued if a worker dies, and pruned after 30 days

Outgoing Accept/Reject/Block activities are stored with stable ids and
queued per inbox for the delivery task (next step). Public activities
are fetchable at /activities/<uuid>. Metrics for inbox activities and
signature checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
mattandClaude Opus 5.5 committed 2026-09-25 21:29:55 +01:00
1 parent c1a7dd0877
commit f312c4b577
15 files changed
+1147 -60

No files matched your search

+124
View File
@@ -0,0 +1,124 @@
"""Follower state changes, from incoming activities and from the user's own actions.
Every change that the other server needs to know about queues an activity (Accept,
Reject or Block); the delivery task sends it. Callers commit.
"""
from datetime import UTC, datetime
from scrobbler.extensions import db
from scrobbler.federation import activities
from scrobbler.federation.models import FederationBlock, FederationFollower, FederationRemoteActor
def _now() -> datetime:
return datetime.now(UTC)
def get(user_id: int, actor: FederationRemoteActor) -> FederationFollower | None:
return db.session.scalar(
db.select(FederationFollower).filter_by(user_id=user_id, remote_actor_id=actor.id)
)
def is_blocked(user_id: int, actor: FederationRemoteActor) -> bool:
return (
db.session.scalar(
db.select(FederationBlock).filter_by(user_id=user_id, remote_actor_id=actor.id)
)
is not None
)
def follow_received(
user_id: int, username: str, manual_approval: bool, actor: FederationRemoteActor, follow: dict
) -> str:
"""Handle a Follow. Returns what happened: accepted, pending, refollowed or blocked."""
if is_blocked(user_id, actor):
activities.reject_follow_activity(user_id, username, follow, actor)
return "blocked"
follower = get(user_id, actor)
if follower is not None:
# A repeat Follow (e.g. they lost our Accept): keep the newest activity and
# answer again if we'd already accepted.
follower.follow_activity_id, follower.follow_activity = follow["id"], follow
if follower.state == "accepted":
activities.accept_follow(username, follower)
return "refollowed"
return "pending"
follower = FederationFollower(
user_id=user_id,
remote_actor_id=actor.id,
actor=actor,
state="pending" if manual_approval else "accepted",
follow_activity_id=follow["id"],
follow_activity=follow,
accepted_at=None if manual_approval else _now(),
)
db.session.add(follower)
db.session.flush()
if not manual_approval:
activities.accept_follow(username, follower)
return "accepted"
return "pending"
def undo_follow(user_id: int, actor: FederationRemoteActor) -> bool:
follower = get(user_id, actor)
if follower is None:
return False
db.session.delete(follower)
return True
def actor_deleted(actor: FederationRemoteActor) -> int:
"""The remote account was deleted: it follows no one here any more."""
followers = db.session.scalars(
db.select(FederationFollower).filter_by(remote_actor_id=actor.id)
).all()
for follower in followers:
db.session.delete(follower)
actor.gone = True
return len(followers)
# --- The user's own actions --------------------------------------------------------------
def approve(username: str, follower: FederationFollower) -> None:
if follower.state != "pending":
return
follower.state, follower.accepted_at = "accepted", _now()
activities.accept_follow(username, follower)
def remove(username: str, follower: FederationFollower) -> None:
"""Decline a request or remove a follower: both are a Reject for the other server."""
activities.reject_follow(username, follower)
db.session.delete(follower)
def block(username: str, follower: FederationFollower) -> FederationBlock:
blocked = FederationBlock(user_id=follower.user_id, remote_actor_id=follower.remote_actor_id)
db.session.add(blocked)
activities.block(follower.user_id, username, follower.actor)
db.session.delete(follower)
return blocked
def reject_all(user_id: int, username: str) -> int:
"""Sharing switched off: tell every follower, then forget them."""
followers = db.session.scalars(db.select(FederationFollower).filter_by(user_id=user_id)).all()
for follower in followers:
remove(username, follower)
return len(followers)
def counts(user_id: int) -> dict[str, int]:
rows = db.session.execute(
db.select(FederationFollower.state, db.func.count())
.filter_by(user_id=user_id)
.group_by(FederationFollower.state)
).all()
found = dict(rows)
return {"accepted": found.get("accepted", 0), "pending": found.get("pending", 0)}