Let users share their profile image with the fediverse (SCR-1)
CI / validate-monitoring (push) Successful in 9s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m44s
CI / test (push) Failing after 6m30s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 6s
CI / validate-monitoring (push) Successful in 9s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m44s
CI / test (push) Failing after 6m30s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 6s
A share_avatar switch (off by default) in the sharing settings publishes the image as the actor's icon and on the public profile page, via a public avatar endpoint. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9243fe4d3f
commit
016fc707d1
12 files changed
+286
-3
No files matched your search
@@ -34,6 +34,15 @@ def now_playing_field(user, settings) -> list[tuple[str, str]]:
|
||||
return [(NOW_PLAYING_FIELD, text)] if text else []
|
||||
|
||||
|
||||
def avatar_icon(urls: vocab.ActorUrls, user, settings) -> tuple[str, str] | None:
|
||||
"""The actor's picture: only if they've chosen to share one. The URL carries the
|
||||
image's age so other servers, which cache by URL, fetch a changed picture."""
|
||||
if not settings.share_avatar or not user.has_avatar:
|
||||
return None
|
||||
version = int(user.avatar_updated_at.timestamp()) if user.avatar_updated_at else 0
|
||||
return f"{urls.avatar}?v={version}", user.avatar_content_type or "image/jpeg"
|
||||
|
||||
|
||||
def build_document(urls: vocab.ActorUrls, user, settings) -> dict:
|
||||
return vocab.person(
|
||||
urls,
|
||||
@@ -46,4 +55,5 @@ def build_document(urls: vocab.ActorUrls, user, settings) -> dict:
|
||||
indexable=settings.indexable,
|
||||
published=settings.created_at.isoformat().replace("+00:00", "Z"),
|
||||
fields=now_playing_field(user, settings),
|
||||
icon=avatar_icon(urls, user, settings),
|
||||
)
|
||||
@@ -178,10 +178,28 @@ def public_profile(username):
|
||||
"now_playing": actors.now_playing_text(user)
|
||||
if settings.now_playing_mode != "off"
|
||||
else None,
|
||||
"has_avatar": settings.share_avatar and user.has_avatar,
|
||||
**{k: v for k, v in _identity(user.username).items() if k != "profile_url"},
|
||||
}
|
||||
|
||||
|
||||
@blp.route("/profiles/<string:username>/avatar", methods=["GET"])
|
||||
@blp.response(200, content_type="image/jpeg", description="The profile image (JPEG, PNG or WebP)")
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="No image shared under that name")
|
||||
def public_avatar(username):
|
||||
"""A public profile image
|
||||
|
||||
For the web UI's profile page. Only served when the user shares and has chosen to
|
||||
share their image.
|
||||
"""
|
||||
found = sharing.shared_avatar(username)
|
||||
if found is None:
|
||||
fed_metrics.lookups_total.labels(kind="avatar", result="not_found").inc()
|
||||
abort(404, code="not_found", message="No shared profile image under that name.")
|
||||
fed_metrics.lookups_total.labels(kind="avatar", result="found").inc()
|
||||
return sharing.avatar_response(*found)
|
||||
|
||||
|
||||
@blp.route("/profiles/<string:username>/posts/<string:post_uuid>", methods=["GET"])
|
||||
@blp.response(200, PublicPostSchema)
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="Not found, not public, or deleted")
|
||||
|
||||
@@ -4,7 +4,7 @@ from prometheus_client import Counter, Gauge, Histogram
|
||||
|
||||
lookups_total = Counter(
|
||||
"scrobbler_federation_lookups_total",
|
||||
"Fediverse lookups answered: WebFinger, actor, NodeInfo, profile",
|
||||
"Fediverse lookups answered: WebFinger, actor, NodeInfo, profile, avatar",
|
||||
["kind", "result"],
|
||||
)
|
||||
sharing_changes_total = Counter(
|
||||
|
||||
@@ -41,6 +41,9 @@ class FederationSettings(db.Model):
|
||||
indexable: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
display_name: Mapped[str | None] = mapped_column(String(100))
|
||||
bio: Mapped[str | None] = mapped_column(Text)
|
||||
# Offer the user's profile image to other servers (as the actor's icon) and on the
|
||||
# public profile page. Off until they say so.
|
||||
share_avatar: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
# An IANA zone name (validated with zoneinfo), used to decide when a Monday starts.
|
||||
timezone: Mapped[str] = mapped_column(String(64), default="UTC")
|
||||
post_weekly_summary: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||
|
||||
@@ -35,6 +35,7 @@ class ActorUrls:
|
||||
shared_inbox: str
|
||||
key_id: str
|
||||
profile_page: str
|
||||
avatar: str
|
||||
|
||||
|
||||
def actor_urls(base_url: str, username: str, profile_page: str) -> ActorUrls:
|
||||
@@ -49,6 +50,7 @@ def actor_urls(base_url: str, username: str, profile_page: str) -> ActorUrls:
|
||||
shared_inbox=f"{base_url}/inbox",
|
||||
key_id=f"{actor}#main-key",
|
||||
profile_page=profile_page,
|
||||
avatar=f"{actor}/avatar",
|
||||
)
|
||||
|
||||
|
||||
@@ -79,8 +81,10 @@ def person(
|
||||
indexable: bool,
|
||||
published: str | None = None,
|
||||
fields: list[tuple[str, str]] = (),
|
||||
icon: tuple[str, str] | None = None,
|
||||
) -> dict:
|
||||
"""A user's actor document. `fields` are (name, plain-text value) profile fields."""
|
||||
"""A user's actor document. `fields` are (name, plain-text value) profile fields;
|
||||
`icon` is the profile picture's (url, media type)."""
|
||||
doc = {
|
||||
"@context": ACTOR_CONTEXT,
|
||||
"id": urls.id,
|
||||
@@ -106,6 +110,8 @@ def person(
|
||||
}
|
||||
if published:
|
||||
doc["published"] = published
|
||||
if icon:
|
||||
doc["icon"] = {"type": "Image", "url": icon[0], "mediaType": icon[1]}
|
||||
return doc
|
||||
|
||||
|
||||
|
||||
@@ -55,6 +55,14 @@ class SharingSettingsSchema(Schema):
|
||||
)
|
||||
},
|
||||
)
|
||||
share_avatar = fields.Boolean(
|
||||
metadata={
|
||||
"description": (
|
||||
"Show your profile image on your public profile and send it to other "
|
||||
"servers as your picture. Off by default; has no effect if you have no image."
|
||||
)
|
||||
}
|
||||
)
|
||||
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)
|
||||
@@ -71,6 +79,9 @@ class PublicProfileSchema(Schema):
|
||||
now_playing = fields.String(
|
||||
allow_none=True, metadata={"description": '"Track by Artist", or null'}
|
||||
)
|
||||
has_avatar = fields.Boolean(
|
||||
metadata={"description": "Whether there's a profile image at `/profiles/{username}/avatar`"}
|
||||
)
|
||||
handle = fields.String(metadata={"example": "@alice@scrobble.example"})
|
||||
actor_url = fields.String()
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
"""Per-user sharing settings: whether a user federates, and how."""
|
||||
|
||||
from flask import Response, request
|
||||
|
||||
from scrobbler.extensions import db
|
||||
from scrobbler.federation import keys
|
||||
from scrobbler.federation.models import NOW_PLAYING_MODES, VISIBILITIES, FederationSettings
|
||||
from scrobbler.federation.protocol.schedule import is_valid_timezone
|
||||
from scrobbler.services import accounts
|
||||
from scrobbler.services import accounts, avatars
|
||||
|
||||
EDITABLE = (
|
||||
"enabled",
|
||||
@@ -18,6 +20,7 @@ EDITABLE = (
|
||||
"post_weekly_summary",
|
||||
"post_milestones",
|
||||
"now_playing_mode",
|
||||
"share_avatar",
|
||||
)
|
||||
|
||||
|
||||
@@ -38,6 +41,7 @@ def settings_for(user_id: int) -> FederationSettings:
|
||||
post_weekly_summary=True,
|
||||
post_milestones=True,
|
||||
now_playing_mode="off",
|
||||
share_avatar=False,
|
||||
)
|
||||
return settings
|
||||
|
||||
@@ -81,6 +85,27 @@ def shared_user(username: str):
|
||||
return user, settings
|
||||
|
||||
|
||||
def shared_avatar(username: str):
|
||||
"""(user, image bytes) for a sharing user who has chosen to share their profile image
|
||||
and has one, else None - indistinguishable from 'no such user', like shared_user."""
|
||||
found = shared_user(username)
|
||||
if found is None:
|
||||
return None
|
||||
user, settings = found
|
||||
if not settings.share_avatar:
|
||||
return None
|
||||
data = avatars.read(user)
|
||||
return None if data is None else (user, data)
|
||||
|
||||
|
||||
def avatar_response(user, data: bytes):
|
||||
"""The public image response: cacheable by anyone, but briefly, as it can change."""
|
||||
response = Response(data, mimetype=user.avatar_content_type or "image/jpeg")
|
||||
response.headers["Cache-Control"] = "public, max-age=300"
|
||||
response.add_etag()
|
||||
return response.make_conditional(request)
|
||||
|
||||
|
||||
def sharing_count() -> int:
|
||||
return db.session.scalar(
|
||||
db.select(db.func.count()).select_from(FederationSettings).filter_by(enabled=True)
|
||||
|
||||
@@ -134,6 +134,15 @@ def actor(username: str):
|
||||
return _json(doc)
|
||||
|
||||
|
||||
@bp.get("/users/<username>/avatar")
|
||||
def actor_avatar(username: str):
|
||||
found = sharing.shared_avatar(username)
|
||||
if found is None:
|
||||
return _not_found("avatar")
|
||||
_found("avatar")
|
||||
return sharing.avatar_response(*found)
|
||||
|
||||
|
||||
def _collection(username: str, which: str, items: list | None, total: int = 0):
|
||||
found = sharing.shared_user(username)
|
||||
if found is None:
|
||||
|
||||
Reference in new issue
Block a user