API and web UI for posts, previews and post settings (phase 3, step 8)
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m0s
CI / test (push) Successful in 2m53s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s

/api/v1/federation gains:
  - GET /posts, DELETE /posts/{id}: manage your own posts from the UI.
  - GET /preview/weekly: what the weekly summary would say right now, for
    the week still in progress - never posted or stored. Backed by a new
    weekly.preview() and schedule.current_week_start().
  - GET /profiles/{username}/posts/{post_uuid} (public, unauthenticated):
    backs the UI's post page, which can't itself speak ActivityPub.

Settings gain timezone/post_weekly_summary/post_milestones, already
present in sharing.EDITABLE since phase 3 step 2 but never added to
SharingSettingsSchema, so the API was silently dropping them from
responses. timezone validates against the same is_valid_timezone() the
service layer uses, with a dedicated marshmallow validator.

Fixed a real gap found while wiring the post page: ids.post_page_url()
took a username but never put it in the URL, so post.html would have
had no way to know whose post to look up. It's now ?u=<username>&id=<uuid>.

Sharing page gets a "What to post" section (weekly summary and
milestones toggles, a timezone field with a browser-supplied IANA
datalist where Intl.supportedValuesOf exists, and a live preview).
New posts.html lists a user's posts with delete (mirroring followers.js's
confirm-before-destructive-action pattern); new post.html is the public
landing page for a post's `url`, reached by browsers redirected from the
raw ActivityPub endpoint.

Verified by hand against a local dev server and a real browser: sign in,
enable sharing, preview and publish, list and delete a post, and follow
the ActivityPub redirect through to the public post page and its 404
after deletion - fixed one bug found this way (the preview box was
collapsing the summary's line breaks).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
mattandClaude Sonnet 5 committed 2026-09-26 16:24:35 +01:00
1 parent d4f3607eef
commit 0acce9b948
16 files changed
+865 -8

No files matched your search

+105 -2
View File
@@ -5,17 +5,21 @@ from flask_smorest import Blueprint, abort
from scrobbler.api.decorators import authenticated
from scrobbler.extensions import db
from scrobbler.federation import activities, publishing, sharing
from scrobbler.federation import activities, ids, publishing, sharing
from scrobbler.federation import followers as follower_state
from scrobbler.federation import metrics as fed_metrics
from scrobbler.federation.models import FederationBlock, FederationFollower
from scrobbler.federation.models import FederationBlock, FederationFollower, FederationPost
from scrobbler.federation.publishing import weekly
from scrobbler.federation.schemas import (
BlockSchema,
FollowerListArgsSchema,
FollowerSchema,
PostSchema,
PublicPostSchema,
PublicProfileSchema,
SharingSettingsSchema,
SharingSettingsUpdateSchema,
WeeklyPreviewSchema,
)
from scrobbler.schemas import ErrorSchema
@@ -86,6 +90,73 @@ def update_settings(changes):
return _settings_body(g.user, settings)
@blp.route("/preview/weekly", methods=["GET"])
@authenticated(blp)
@blp.response(200, WeeklyPreviewSchema)
def preview_weekly():
"""Preview this week's summary
What it would say right now, for the week still in progress. Never posted or stored.
"""
result = weekly.preview(g.user.id)
if result is None: # pragma: no cover - g.user is always a real, current account
abort(404, code="not_found", message="No such user")
text, html = result
return {"text": text, "html": html}
def _post_body(post: FederationPost) -> dict:
return {
"id": post.id,
"kind": post.kind,
"text": post.content_text,
"visibility": post.visibility,
"created_at": post.created_at,
"deleted_at": post.deleted_at,
}
@blp.route("/posts", methods=["GET"])
@authenticated(blp)
@blp.response(200, PostSchema(many=True))
def list_posts():
"""Your posts
Newest first, including deleted ones (their text stays visible to you; deleted_at
shows when).
"""
posts = db.session.scalars(
db.select(FederationPost)
.filter_by(user_id=g.user.id)
.order_by(FederationPost.created_at.desc())
).all()
return [_post_body(p) for p in posts]
def _own_post(post_id: int) -> FederationPost:
post = db.session.get(FederationPost, post_id)
if post is None or post.user_id != g.user.id:
abort(404, code="not_found", message="No such post")
return post
@blp.route("/posts/<int:post_id>", methods=["DELETE"])
@authenticated(blp)
@blp.response(204)
@blp.alt_response(404, schema=ErrorSchema, description="No such post")
@blp.alt_response(409, schema=ErrorSchema, description="Already deleted")
def delete_post(post_id):
"""Delete a post
Sends a Delete to whoever received it.
"""
post = _own_post(post_id)
activity = publishing.delete_post(g.user.id, g.user.username, post)
if activity is None:
abort(409, code="already_deleted", message="That post is already deleted.")
db.session.commit()
@blp.route("/profiles/<string:username>", methods=["GET"])
@blp.response(200, PublicProfileSchema)
@blp.alt_response(404, schema=ErrorSchema, description="No one sharing with that name")
@@ -108,6 +179,38 @@ def public_profile(username):
}
@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")
def public_post(username, post_uuid):
"""A public or unlisted post
For the web UI's post page (the browser landing spot for a Note's `url`, since it
can't itself speak ActivityPub).
"""
found = sharing.shared_user(username)
if found is not None:
user, settings = found
post = publishing.find_by_note_id(user.id, ids.note_url(username, post_uuid))
else:
post = None
if (
post is None
or post.deleted_at is not None
or post.visibility not in publishing.LISTED_VISIBILITIES
):
fed_metrics.lookups_total.labels(kind="post", result="not_found").inc()
abort(404, code="not_found", message="No such post.")
fed_metrics.lookups_total.labels(kind="post", result="found").inc()
return {
"text": post.content_text,
"html": post.content_html,
"created_at": post.created_at,
"display_name": settings.display_name or user.username,
**{k: v for k, v in _identity(user.username).items() if k != "actor_url"},
}
# --- Followers ---------------------------------------------------------------------------