Interop with a real GoToSocial server, and the fixes it found (phase 2, step 8)
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 10s
CI / test (push) Successful in 2m16s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 10s
CI / test (push) Successful in 2m16s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
scripts/interop/run.sh brings up Scrobbler (API, worker, UI behind a throwaway-TLS Caddy) and GoToSocial 0.22.1 on a private .test network, creates a GoToSocial user, drives its sign-in/OAuth screens for a token, and checks: lookup by handle, follow accepted, unfollow, manual approval and approve, remove, block and unblock, and decline. Found and fixed along the way: - Keys whose id is a path serving the actor (GoToSocial's <actor>/main-key) weren't resolved; now all three publishing styles are handled - An Undo of an older Follow arriving after a newer Follow removed the new follower; an Undo now only removes the Follow it names (and Undo referencing a Follow by id is understood) - Rejects embedded the Follow, which some servers (GoToSocial) match by account pair, so a late Reject could cancel a newer request; Rejects now reference the Follow by id - Unblocking didn't tell the other server; it now sends Undo(Block) - Actors publish an (empty) featured collection, which Mastodon and GoToSocial fetch GoToSocial 0.22.1 ignores Reject for already-accepted follows (a TODO in its code, fixed on its main branch); the removal check on its side is skipped until a release has it (GTS_REJECT_ENDS_FOLLOWS=1 enables). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
1 parent
ec658a7751
commit
b93cfea5b3
16 files changed
+694
-17
No files matched your search
@@ -68,3 +68,22 @@ def block(user_id: int, username: str, actor: FederationRemoteActor) -> Federati
|
||||
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
|
||||
@@ -5,9 +5,9 @@ from flask_smorest import Blueprint, abort
|
||||
|
||||
from scrobbler.api.decorators import authenticated
|
||||
from scrobbler.extensions import db
|
||||
from scrobbler.federation import activities, sharing
|
||||
from scrobbler.federation import followers as follower_state
|
||||
from scrobbler.federation import metrics as fed_metrics
|
||||
from scrobbler.federation import sharing
|
||||
from scrobbler.federation.models import FederationBlock, FederationFollower
|
||||
from scrobbler.federation.schemas import (
|
||||
BlockSchema,
|
||||
@@ -225,10 +225,12 @@ def list_blocks():
|
||||
def unblock(block_id):
|
||||
"""Unblock an account
|
||||
|
||||
They can follow you again (they aren't re-added).
|
||||
Their server is told (an Undo of the Block). They can follow you again; they aren't
|
||||
re-added.
|
||||
"""
|
||||
block = db.session.get(FederationBlock, block_id)
|
||||
if block is None or block.user_id != g.user.id:
|
||||
abort(404, code="not_found", message="No such block")
|
||||
activities.undo_block(g.user.id, g.user.username, block.actor) # tell their server
|
||||
db.session.delete(block)
|
||||
db.session.commit()
|
||||
@@ -63,12 +63,29 @@ def follow_received(
|
||||
return "pending"
|
||||
|
||||
|
||||
def undo_follow(user_id: int, actor: FederationRemoteActor) -> bool:
|
||||
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 False
|
||||
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 True
|
||||
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:
|
||||
|
||||
@@ -294,12 +294,14 @@ def on_follow(doc: dict, actor: FederationRemoteActor) -> str:
|
||||
@handles("Undo")
|
||||
def on_undo(doc: dict, actor: FederationRemoteActor) -> str:
|
||||
inner = doc.get("object")
|
||||
if isinstance(inner, str): # the Follow referenced by id rather than embedded
|
||||
return followers.undo_follow_by_id(actor, inner)
|
||||
if not isinstance(inner, dict) or inner.get("type") != "Follow":
|
||||
return "undo_ignored"
|
||||
if object_id(inner.get("actor")) not in (None, actor.uri):
|
||||
raise Rejected("undo_other_actors_follow")
|
||||
user, _ = _local_user(inner.get("object"))
|
||||
return "unfollowed" if followers.undo_follow(user.id, actor) else "undo_not_following"
|
||||
return followers.undo_follow(user.id, actor, object_id(inner))
|
||||
|
||||
|
||||
@handles("Delete")
|
||||
|
||||
@@ -111,6 +111,13 @@ _OPERATIONS = {
|
||||
[_USERNAME],
|
||||
{"200": {"description": "OrderedCollection", "content": _AP}, "404": _NOT_FOUND},
|
||||
),
|
||||
"federation.featured": (
|
||||
"get",
|
||||
"Featured",
|
||||
"Pinned posts (always empty for now).",
|
||||
[_USERNAME],
|
||||
{"200": {"description": "OrderedCollection", "content": _AP}, "404": _NOT_FOUND},
|
||||
),
|
||||
"federation.instance_actor": (
|
||||
"get",
|
||||
"Instance actor",
|
||||
|
||||
@@ -16,6 +16,7 @@ ACTOR_CONTEXT = [
|
||||
"toot": "http://joinmastodon.org/ns#",
|
||||
"discoverable": "toot:discoverable",
|
||||
"indexable": "toot:indexable",
|
||||
"featured": {"@id": "toot:featured", "@type": "@id"},
|
||||
"schema": "http://schema.org#",
|
||||
"PropertyValue": "schema:PropertyValue",
|
||||
"value": "schema:value",
|
||||
@@ -30,6 +31,7 @@ class ActorUrls:
|
||||
outbox: str
|
||||
followers: str
|
||||
following: str
|
||||
featured: str
|
||||
shared_inbox: str
|
||||
key_id: str
|
||||
profile_page: str
|
||||
@@ -43,6 +45,7 @@ def actor_urls(base_url: str, username: str, profile_page: str) -> ActorUrls:
|
||||
outbox=f"{actor}/outbox",
|
||||
followers=f"{actor}/followers",
|
||||
following=f"{actor}/following",
|
||||
featured=f"{actor}/collections/featured",
|
||||
shared_inbox=f"{base_url}/inbox",
|
||||
key_id=f"{actor}#main-key",
|
||||
profile_page=profile_page,
|
||||
@@ -90,6 +93,7 @@ def person(
|
||||
"outbox": urls.outbox,
|
||||
"followers": urls.followers,
|
||||
"following": urls.following,
|
||||
"featured": urls.featured,
|
||||
"endpoints": {"sharedInbox": urls.shared_inbox},
|
||||
"publicKey": public_key(urls, public_key_pem),
|
||||
"manuallyApprovesFollowers": manually_approves_followers,
|
||||
@@ -164,14 +168,31 @@ def accept(activity_id: str, actor_id: str, follow: dict) -> dict:
|
||||
|
||||
|
||||
def reject(activity_id: str, actor_id: str, follow: dict) -> dict:
|
||||
"""Decline a follow request, or end an existing follow."""
|
||||
return _response("Reject", activity_id, actor_id, follow, follow["actor"])
|
||||
"""Decline a follow request, or end an existing follow.
|
||||
|
||||
The Follow is referenced by id rather than embedded: some servers match an embedded
|
||||
Follow by the pair of accounts, so a late Reject of an old Follow would cancel a
|
||||
newer request between the same accounts. An id matches exactly that Follow."""
|
||||
return _response("Reject", activity_id, actor_id, follow["id"], follow["actor"])
|
||||
|
||||
|
||||
def block(activity_id: str, actor_id: str, blocked_actor: str) -> dict:
|
||||
return _response("Block", activity_id, actor_id, blocked_actor, blocked_actor)
|
||||
|
||||
|
||||
def undo(activity_id: str, actor_id: str, undone: dict) -> dict:
|
||||
"""Withdraw one of our earlier activities (e.g. a Block when unblocking)."""
|
||||
to = undone.get("to") or []
|
||||
return {
|
||||
"@context": AS_CONTEXT,
|
||||
"id": activity_id,
|
||||
"type": "Undo",
|
||||
"actor": actor_id,
|
||||
"object": undone,
|
||||
"to": list(to),
|
||||
}
|
||||
|
||||
|
||||
def object_id(value) -> str | None:
|
||||
"""An object's id, whether it's given inline (a dict) or as a bare URI."""
|
||||
if isinstance(value, dict):
|
||||
|
||||
@@ -105,8 +105,10 @@ def get_actor(uri: str, refresh: bool = False) -> FederationRemoteActor:
|
||||
|
||||
|
||||
def actor_for_key(key_id: str, refresh: bool = False) -> FederationRemoteActor:
|
||||
"""The actor that owns `key_id`: usually `<actor>#main-key`, sometimes a separate
|
||||
key document naming its owner."""
|
||||
"""The actor that owns `key_id`. Servers publish keys three ways:
|
||||
* a fragment of the actor (`<actor>#main-key`, Mastodon);
|
||||
* a URL that serves the actor document itself (`<actor>/main-key`, GoToSocial);
|
||||
* a separate key document naming its `owner`."""
|
||||
cached = db.session.scalar(db.select(FederationRemoteActor).filter_by(public_key_id=key_id))
|
||||
if cached is not None and not refresh:
|
||||
# Any age will do for checking a signature (a failure refreshes once). This also
|
||||
@@ -116,12 +118,24 @@ def actor_for_key(key_id: str, refresh: bool = False) -> FederationRemoteActor:
|
||||
try:
|
||||
return _matching(get_actor(document_url, refresh=True), key_id)
|
||||
except ActorError:
|
||||
# Not an actor: a standalone key document pointing at its owner
|
||||
doc = outbound.signed_get_json(document_url)
|
||||
owner = doc.get("owner")
|
||||
if doc.get("id") != key_id or not isinstance(owner, str) or _host(owner) != _host(key_id):
|
||||
raise
|
||||
return _matching(get_actor(owner, refresh=True), key_id)
|
||||
pass # the key's URL isn't the actor's own URL: find the owner from the document
|
||||
doc = outbound.signed_get_json(document_url)
|
||||
if doc.get("id") == key_id and isinstance(doc.get("owner"), str):
|
||||
owner = doc["owner"] # a standalone key document
|
||||
elif isinstance(doc.get("id"), str) and _key_id_of(doc) == key_id:
|
||||
owner = doc["id"] # the actor document, served at the key's URL
|
||||
else:
|
||||
raise ActorError(f"can't find who owns key {key_id}")
|
||||
if _host(owner) != _host(key_id):
|
||||
raise ActorError(f"key {key_id} claims an owner on another host")
|
||||
return _matching(get_actor(owner, refresh=True), key_id)
|
||||
|
||||
|
||||
def _key_id_of(doc: dict) -> str | None:
|
||||
key = doc.get("publicKey")
|
||||
if isinstance(key, list):
|
||||
key = next((k for k in key if isinstance(k, dict)), None)
|
||||
return key.get("id") if isinstance(key, dict) else None
|
||||
|
||||
|
||||
def _matching(actor: FederationRemoteActor, key_id: str) -> FederationRemoteActor:
|
||||
|
||||
@@ -165,6 +165,11 @@ def followers(username: str):
|
||||
return _collection(username, "followers", None, total) # the count, not who
|
||||
|
||||
|
||||
@bp.get("/users/<username>/collections/featured")
|
||||
def featured(username: str):
|
||||
return _collection(username, "featured", []) # pinned posts: none (yet)
|
||||
|
||||
|
||||
@bp.get("/users/<username>/following")
|
||||
def following(username: str):
|
||||
return _collection(username, "following", [])
|
||||
|
||||
Reference in new issue
Block a user