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
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
93 lines
3.2 KiB
Python
93 lines
3.2 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.
|
|
|
|
Public: used both for our own narrow activity types below (Accept, Reject, Block,
|
|
Undo) and, from publishing/base.py, for Create/Delete of posts."""
|
|
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
|
|
|
|
|
|
def undo_block(
|
|
user_id: int, username: str, actor: FederationRemoteActor
|
|
) -> FederationActivity | None:
|
|
"""Tell them they're unblocked: Undo our most recent Block of them (if we sent one)."""
|
|
block_activity = db.session.scalars(
|
|
db.select(FederationActivity)
|
|
.filter_by(user_id=user_id, activity_type="Block")
|
|
.filter(FederationActivity.document["object"].as_string() == actor.uri)
|
|
.order_by(FederationActivity.created_at.desc())
|
|
).first()
|
|
if block_activity is None:
|
|
return None
|
|
activity = store(
|
|
user_id, lambda url: vocab.undo(url, ids.actor_id(username), block_activity.document)
|
|
)
|
|
queue(activity, [actor.inbox])
|
|
return activity
|