Files
Scrobbler/src/scrobbler/federation/activities.py
T
mattandClaude Opus 5.5 f312c4b577
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
Inbox: intake and verified processing of follows (phase 2, step 4)
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
2026-09-25 21:29:55 +01:00

71 lines
2.3 KiB
Python

"""Activities we send: stored (so they have a stable id) and queued for delivery."""
import uuid
from scrobbler.extensions import db
from scrobbler.federation import ids
from scrobbler.federation.models import (
FederationActivity,
FederationDelivery,
FederationFollower,
FederationRemoteActor,
)
from scrobbler.federation.protocol import vocab
def _store(user_id: int, document_for, public: bool = False) -> FederationActivity:
"""Create an activity. `document_for(activity_url)` builds its JSON."""
activity_uuid = str(uuid.uuid4())
document = document_for(ids.activity_url(activity_uuid))
activity = FederationActivity(
id=activity_uuid,
user_id=user_id,
activity_type=document["type"],
document=document,
public=public,
)
db.session.add(activity)
return activity
def queue(activity: FederationActivity, inboxes: list[str]) -> list[FederationDelivery]:
deliveries = [
FederationDelivery(activity_id=activity.id, inbox=inbox)
for inbox in dict.fromkeys(inboxes) # one per distinct inbox
]
db.session.add_all(deliveries)
return deliveries
def accept_follow(username: str, follower: FederationFollower) -> FederationActivity:
activity = _store(
follower.user_id,
lambda url: vocab.accept(url, ids.actor_id(username), follower.follow_activity),
)
queue(activity, [follower.actor.inbox]) # replies go to their own inbox
return activity
def reject_follow(username: str, follower: FederationFollower) -> FederationActivity:
activity = _store(
follower.user_id,
lambda url: vocab.reject(url, ids.actor_id(username), follower.follow_activity),
)
queue(activity, [follower.actor.inbox])
return activity
def reject_follow_activity(
user_id: int, username: str, follow: dict, actor: FederationRemoteActor
) -> FederationActivity:
"""Reject a Follow that never became a follower (e.g. from a blocked account)."""
activity = _store(user_id, lambda url: vocab.reject(url, ids.actor_id(username), follow))
queue(activity, [actor.inbox])
return activity
def block(user_id: int, username: str, actor: FederationRemoteActor) -> FederationActivity:
activity = _store(user_id, lambda url: vocab.block(url, ids.actor_id(username), actor.uri))
queue(activity, [actor.inbox])
return activity