Manage followers from the API and web UI (phase 2, step 6)
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 11s
CI / test (push) Successful in 2m4s
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 11s
CI / test (push) Successful in 2m4s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
- /api/v1/federation: list followers and requests, approve (Accept), decline or remove (Reject), block (Block, and future follows are refused), list and remove blocks; settings report follower counts - The actor now reflects the user's real follow-approval setting and the followers collection its accepted count - Turning sharing off sends every follower a Reject and forgets them; while those are being delivered the actor stays fetchable with just its key (no name or bio) so other servers can verify them, and WebFinger already says not found - Web UI: a Followers page (requests with Approve/Decline, followers with Remove/Block behind a confirm step, blocked accounts with Unblock; remote names shown as plain text only), follower counts on the Sharing page, and a confirmation before turning sharing off when that would remove followers 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
142dc13fea
commit
a90cd14f59
12 files changed
+1083
-18
No files matched your search
@@ -4,9 +4,15 @@ from flask import current_app, g
|
||||
from flask_smorest import Blueprint, abort
|
||||
|
||||
from scrobbler.api.decorators import authenticated
|
||||
from scrobbler.extensions import db
|
||||
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,
|
||||
FollowerListArgsSchema,
|
||||
FollowerSchema,
|
||||
PublicProfileSchema,
|
||||
SharingSettingsSchema,
|
||||
SharingSettingsUpdateSchema,
|
||||
@@ -33,10 +39,12 @@ def _identity(username: str) -> dict:
|
||||
|
||||
|
||||
def _settings_body(user, settings) -> dict:
|
||||
counts = follower_state.counts(user.id)
|
||||
return {
|
||||
**{field: getattr(settings, field) for field in sharing.EDITABLE},
|
||||
**_identity(user.username),
|
||||
"followers": 0,
|
||||
"followers": counts["accepted"],
|
||||
"pending_followers": counts["pending"],
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +68,8 @@ def update_settings(changes):
|
||||
"""Change your sharing settings
|
||||
|
||||
Turning sharing on makes you findable as your handle. Your handle can't change once
|
||||
people follow you. Visibility changes apply to new posts only.
|
||||
people follow you. Visibility changes apply to new posts only. Turning sharing off
|
||||
removes all your followers (each is told).
|
||||
"""
|
||||
try:
|
||||
settings, toggled = sharing.update(g.user.id, changes)
|
||||
@@ -68,6 +77,10 @@ def update_settings(changes):
|
||||
abort(422, code="invalid_settings", message=str(err))
|
||||
if toggled is not None:
|
||||
fed_metrics.sharing_changes_total.labels(change="enabled" if toggled else "disabled").inc()
|
||||
if toggled is False:
|
||||
# Tell every follower (a Reject each), then forget them
|
||||
follower_state.reject_all(g.user.id, g.user.username)
|
||||
db.session.commit()
|
||||
return _settings_body(g.user, settings)
|
||||
|
||||
|
||||
@@ -91,3 +104,131 @@ def public_profile(username):
|
||||
"bio": settings.bio,
|
||||
**{k: v for k, v in _identity(user.username).items() if k != "profile_url"},
|
||||
}
|
||||
|
||||
|
||||
# --- Followers ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _follower_body(follower: FederationFollower) -> dict:
|
||||
actor = follower.actor
|
||||
return {
|
||||
"id": follower.id,
|
||||
"state": follower.state,
|
||||
"handle": actor.handle,
|
||||
"display_name": actor.display_name,
|
||||
"actor_url": actor.uri,
|
||||
"server": actor.host,
|
||||
"since": follower.created_at,
|
||||
}
|
||||
|
||||
|
||||
def _own_follower(follower_id: int) -> FederationFollower:
|
||||
follower = db.session.get(FederationFollower, follower_id)
|
||||
if follower is None or follower.user_id != g.user.id:
|
||||
abort(404, code="not_found", message="No such follower")
|
||||
return follower
|
||||
|
||||
|
||||
@blp.route("/followers", methods=["GET"])
|
||||
@authenticated(blp)
|
||||
@blp.arguments(FollowerListArgsSchema, location="query")
|
||||
@blp.response(200, FollowerSchema(many=True))
|
||||
def list_followers(args):
|
||||
"""Your followers and follow requests
|
||||
|
||||
Newest first. Filter with `state=pending` (requests) or `state=accepted`.
|
||||
"""
|
||||
query = db.select(FederationFollower).filter_by(user_id=g.user.id)
|
||||
if args["state"]:
|
||||
query = query.filter_by(state=args["state"])
|
||||
followers = db.session.scalars(query.order_by(FederationFollower.created_at.desc())).all()
|
||||
return [_follower_body(f) for f in followers]
|
||||
|
||||
|
||||
@blp.route("/followers/<int:follower_id>/approve", methods=["POST"])
|
||||
@authenticated(blp)
|
||||
@blp.response(200, FollowerSchema)
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="No such follower")
|
||||
@blp.alt_response(409, schema=ErrorSchema, description="Not a pending request")
|
||||
def approve_follower(follower_id):
|
||||
"""Approve a follow request"""
|
||||
follower = _own_follower(follower_id)
|
||||
if follower.state != "pending":
|
||||
abort(409, code="not_pending", message="That follower isn't waiting for approval.")
|
||||
follower_state.approve(g.user.username, follower)
|
||||
db.session.commit()
|
||||
return _follower_body(follower)
|
||||
|
||||
|
||||
@blp.route("/followers/<int:follower_id>/decline", methods=["POST"])
|
||||
@authenticated(blp)
|
||||
@blp.response(204)
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="No such follower")
|
||||
def decline_follower(follower_id):
|
||||
"""Decline a follow request"""
|
||||
follower_state.remove(g.user.username, _own_follower(follower_id))
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@blp.route("/followers/<int:follower_id>/remove", methods=["POST"])
|
||||
@authenticated(blp)
|
||||
@blp.response(204)
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="No such follower")
|
||||
def remove_follower(follower_id):
|
||||
"""Remove a follower
|
||||
|
||||
They stop receiving your posts. They can follow you again; block them to prevent it.
|
||||
"""
|
||||
follower_state.remove(g.user.username, _own_follower(follower_id))
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@blp.route("/followers/<int:follower_id>/block", methods=["POST"])
|
||||
@authenticated(blp)
|
||||
@blp.response(204)
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="No such follower")
|
||||
def block_follower(follower_id):
|
||||
"""Block a follower
|
||||
|
||||
Removes them and refuses their future follows.
|
||||
"""
|
||||
follower_state.block(g.user.username, _own_follower(follower_id))
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@blp.route("/blocks", methods=["GET"])
|
||||
@authenticated(blp)
|
||||
@blp.response(200, BlockSchema(many=True))
|
||||
def list_blocks():
|
||||
"""Accounts you've blocked"""
|
||||
blocks = db.session.scalars(
|
||||
db.select(FederationBlock)
|
||||
.filter_by(user_id=g.user.id)
|
||||
.order_by(FederationBlock.created_at.desc())
|
||||
).all()
|
||||
return [
|
||||
{
|
||||
"id": b.id,
|
||||
"handle": b.actor.handle,
|
||||
"display_name": b.actor.display_name,
|
||||
"actor_url": b.actor.uri,
|
||||
"since": b.created_at,
|
||||
}
|
||||
for b in blocks
|
||||
]
|
||||
|
||||
|
||||
@blp.route("/blocks/<int:block_id>", methods=["DELETE"])
|
||||
@authenticated(blp)
|
||||
@blp.response(204)
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="No such block")
|
||||
def unblock(block_id):
|
||||
"""Unblock an account
|
||||
|
||||
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")
|
||||
db.session.delete(block)
|
||||
db.session.commit()
|
||||
@@ -23,8 +23,9 @@ class SharingSettingsSchema(Schema):
|
||||
handle = fields.String(dump_only=True, metadata={"example": "@alice@scrobble.example"})
|
||||
actor_url = fields.String(dump_only=True)
|
||||
profile_url = fields.String(dump_only=True)
|
||||
followers = fields.Integer(
|
||||
dump_only=True, metadata={"description": "0 until following arrives"}
|
||||
followers = fields.Integer(dump_only=True, metadata={"description": "Accepted followers"})
|
||||
pending_followers = fields.Integer(
|
||||
dump_only=True, metadata={"description": "Waiting for approval"}
|
||||
)
|
||||
|
||||
|
||||
@@ -38,3 +39,25 @@ class PublicProfileSchema(Schema):
|
||||
|
||||
class SharingSettingsUpdateSchema(SharingSettingsSchema):
|
||||
"""Any subset of the editable settings (read-only fields are rejected)."""
|
||||
|
||||
|
||||
class FollowerSchema(Schema):
|
||||
id = fields.Integer()
|
||||
state = fields.String(validate=validate.OneOf(["pending", "accepted"]))
|
||||
handle = fields.String(metadata={"example": "@bob@mastodon.example"})
|
||||
display_name = fields.String(allow_none=True)
|
||||
actor_url = fields.String()
|
||||
server = fields.String(metadata={"example": "mastodon.example"})
|
||||
since = fields.DateTime(metadata={"description": "When they followed (or asked to)"})
|
||||
|
||||
|
||||
class FollowerListArgsSchema(Schema):
|
||||
state = fields.String(load_default=None, validate=validate.OneOf(["pending", "accepted"]))
|
||||
|
||||
|
||||
class BlockSchema(Schema):
|
||||
id = fields.Integer()
|
||||
handle = fields.String()
|
||||
display_name = fields.String(allow_none=True)
|
||||
actor_url = fields.String()
|
||||
since = fields.DateTime()
|
||||
@@ -67,3 +67,24 @@ def sharing_count() -> int:
|
||||
return db.session.scalar(
|
||||
db.select(db.func.count()).select_from(FederationSettings).filter_by(enabled=True)
|
||||
)
|
||||
|
||||
|
||||
def farewell_user(username: str):
|
||||
"""A user who has stopped sharing but still has messages on their way out (the
|
||||
Rejects telling followers). Their actor stays fetchable, minimally, so other servers
|
||||
can verify those messages. None otherwise."""
|
||||
from scrobbler.federation.models import FederationActivity, FederationDelivery
|
||||
|
||||
user = accounts.find_user(username)
|
||||
if user is None:
|
||||
return None
|
||||
settings = db.session.get(FederationSettings, user.id)
|
||||
if settings is None or settings.enabled:
|
||||
return None
|
||||
pending = db.session.scalar(
|
||||
db.select(db.func.count())
|
||||
.select_from(FederationDelivery)
|
||||
.join(FederationActivity, FederationActivity.id == FederationDelivery.activity_id)
|
||||
.filter(FederationActivity.user_id == user.id, FederationDelivery.status == "pending")
|
||||
)
|
||||
return user if pending else None
|
||||
@@ -7,6 +7,7 @@ import json
|
||||
from flask import Blueprint, Response, current_app, redirect, request
|
||||
|
||||
from scrobbler.extensions import db
|
||||
from scrobbler.federation import followers as follower_state
|
||||
from scrobbler.federation import inbox, keys, sharing
|
||||
from scrobbler.federation import metrics as fed_metrics
|
||||
from scrobbler.federation.models import FederationActivity
|
||||
@@ -110,7 +111,23 @@ def actor(username: str):
|
||||
return redirect(_profile_page(username), code=302)
|
||||
found = sharing.shared_user(username)
|
||||
if found is None:
|
||||
return _not_found("actor")
|
||||
leaving = sharing.farewell_user(username)
|
||||
if leaving is None:
|
||||
return _not_found("actor")
|
||||
# Stopped sharing, but Rejects are still on their way: just enough for other
|
||||
# servers to verify them. No name, bio or other profile details.
|
||||
_found("actor")
|
||||
doc = vocab.person(
|
||||
_urls(leaving.username),
|
||||
username=leaving.username,
|
||||
name=leaving.username,
|
||||
summary=None,
|
||||
public_key_pem=keys.key_for(leaving.id).public_key_pem,
|
||||
manually_approves_followers=True,
|
||||
discoverable=False,
|
||||
indexable=False,
|
||||
)
|
||||
return _json(doc, cache=0)
|
||||
user, settings = found
|
||||
_found("actor")
|
||||
urls = _urls(user.username)
|
||||
@@ -120,9 +137,7 @@ def actor(username: str):
|
||||
name=settings.display_name or user.username,
|
||||
summary=settings.bio,
|
||||
public_key_pem=keys.key_for(user.id).public_key_pem,
|
||||
# Phase 1 can't accept follows yet: advertise approval so follows show as
|
||||
# requests rather than failing silently.
|
||||
manually_approves_followers=True,
|
||||
manually_approves_followers=settings.manually_approves_followers,
|
||||
discoverable=settings.discoverable,
|
||||
indexable=settings.indexable,
|
||||
published=settings.created_at.isoformat().replace("+00:00", "Z"),
|
||||
@@ -130,12 +145,12 @@ def actor(username: str):
|
||||
return _json(doc)
|
||||
|
||||
|
||||
def _collection(username: str, which: str, items: list | None):
|
||||
def _collection(username: str, which: str, items: list | None, total: int = 0):
|
||||
found = sharing.shared_user(username)
|
||||
if found is None:
|
||||
return _not_found("actor")
|
||||
urls = _urls(found[0].username)
|
||||
return _json(vocab.ordered_collection(getattr(urls, which), 0, items))
|
||||
return _json(vocab.ordered_collection(getattr(urls, which), total, items))
|
||||
|
||||
|
||||
@bp.get("/users/<username>/outbox")
|
||||
@@ -145,7 +160,9 @@ def outbox(username: str):
|
||||
|
||||
@bp.get("/users/<username>/followers")
|
||||
def followers(username: str):
|
||||
return _collection(username, "followers", None) # count only
|
||||
found = sharing.shared_user(username)
|
||||
total = follower_state.counts(found[0].id)["accepted"] if found else 0
|
||||
return _collection(username, "followers", None, total) # the count, not who
|
||||
|
||||
|
||||
@bp.get("/users/<username>/following")
|
||||
|
||||
Reference in new issue
Block a user