Files
Scrobbler/src/scrobbler/federation/followers.py
T
mattandClaude Sonnet 5 b16ba116c4
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m1s
CI / test (push) Successful in 2m26s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
Publishing core: post, fan out, delete (phase 3, step 4)
federation/publishing/base.py:
- publish(user_id, username, kind, key, text=, html=): idempotent —
  refuses (returns None, doesn't post) if the user has stopped sharing
  or a post with that key already exists, including when two worker
  passes race (caught as a duplicate-key error at commit, not raised).
  Builds Note + Create addressed from the user's current visibility,
  stores both, and fans out one delivery per distinct accepted
  follower's inbox (shared inbox preferred).
- delete_post(user_id, username, post): sends Delete(Tombstone) to the
  same followers and tombstones the row; a no-op if already deleted.
  Addressed by the post's own visibility at the time it was posted,
  not the user's current setting.
- delete_all_for(user_id, username): deletes every live post, for
  turning sharing off.

Wired into PATCH /settings: turning sharing off now deletes every live
post *before* rejecting followers (reject_all removes the followers
that accepted_inboxes() needs). The phase 2 farewell-actor mechanism
already covers these deliveries with no changes, since it just checks
for any pending delivery on the user's activities.

Small supporting changes: activities.store() made public (used from
publishing now, not just activities.py's own narrow helpers);
followers.accepted_inboxes() for fan-out; ids.py gains
followers_url/note_url/post_page_url; sharing.settings_for()'s unsaved
default object now sets every field a caller might read, not just the
ones the old callers happened to need.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-26 11:51:06 +01:00

151 lines
5.4 KiB
Python

"""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, follow_id: str | None) -> str:
"""Handle an Undo of a Follow. It only removes the follower if it undoes their current
Follow: an Undo of an older one can arrive after a newer Follow (deliveries race)."""
follower = get(user_id, actor)
if follower is None:
return "undo_not_following"
if follow_id is not None and follow_id != follower.follow_activity_id:
return "undo_stale"
db.session.delete(follower)
return "unfollowed"
def undo_follow_by_id(actor: FederationRemoteActor, follow_id: str) -> str:
"""An Undo naming the Follow only by id: remove whoever follows with that Follow."""
follower = db.session.scalar(
db.select(FederationFollower).filter_by(
remote_actor_id=actor.id, follow_activity_id=follow_id
)
)
if follower is None:
return "undo_not_following"
db.session.delete(follower)
return "unfollowed"
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)}
def accepted_inboxes(user_id: int) -> list[str]:
"""Delivery inbox (their shared one, or their own) of every accepted follower, for
fanning out a post. Pending followers are excluded: they haven't been let in yet."""
accepted = db.session.scalars(
db.select(FederationFollower).filter_by(user_id=user_id, state="accepted")
).all()
return [follower.actor.delivery_inbox for follower in accepted]