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

+146
View File
@@ -1,5 +1,12 @@
from datetime import UTC, datetime, timedelta
import pytest
from scrobbler.extensions import db
from scrobbler.federation import publishing, sharing
from scrobbler.federation.models import FederationPost
from scrobbler.models import Scrobble
@pytest.fixture
def fed_auth(fed_ctx, user):
@@ -17,11 +24,38 @@ def test_settings_start_private(fed_client, fed_auth):
False,
False,
)
assert (body["timezone"], body["post_weekly_summary"], body["post_milestones"]) == (
"UTC",
True,
True,
)
assert body["handle"] == "@alice@scrobble.test"
assert body["actor_url"] == "https://scrobble.test/users/alice"
assert body["profile_url"] == "http://localhost:8080/profile.html?u=alice"
def test_timezone_and_post_toggles_can_be_changed(fed_client, fed_auth):
response = fed_client.patch(
"/api/v1/federation/settings",
json={"timezone": "Europe/London", "post_weekly_summary": False},
headers=fed_auth,
)
assert response.status_code == 200
body = response.get_json()
assert (body["timezone"], body["post_weekly_summary"], body["post_milestones"]) == (
"Europe/London",
False,
True,
)
def test_an_unknown_timezone_is_rejected(fed_client, fed_auth):
response = fed_client.patch(
"/api/v1/federation/settings", json={"timezone": "Mars/Phobos"}, headers=fed_auth
)
assert response.status_code == 422
def test_enable_and_change_settings(fed_client, fed_auth, metric_delta):
enabled = metric_delta("scrobbler_federation_sharing_changes_total", change="enabled")
response = fed_client.patch(
@@ -80,3 +114,115 @@ def test_public_profile(fed_client, sharing_user, make_user):
def test_disabled_server_has_no_federation_api(client, auth):
assert client.get("/api/v1/federation/settings", headers=auth).status_code == 404
def test_public_post_serves_a_public_or_unlisted_note(fed_client, fed_ctx, sharing_user):
sharing.update(sharing_user.id, {"visibility": "unlisted"})
post = publishing.publish(sharing_user.id, "alice", "weekly", "a", text="t", html="<p>t</p>")
note_uuid = post.activity.document["object"]["id"].rsplit("/", 1)[-1]
body = fed_client.get(f"/api/v1/federation/profiles/alice/posts/{note_uuid}").get_json()
assert body == {
"text": "t",
"html": "<p>t</p>",
"created_at": body["created_at"],
"display_name": "Alice",
"handle": "@alice@scrobble.test",
"profile_url": "http://localhost:8080/profile.html?u=alice",
}
def test_public_post_404s_for_followers_only_deleted_or_unknown(fed_client, fed_ctx, sharing_user):
post = publishing.publish(sharing_user.id, "alice", "weekly", "a", text="t", html="<p>t</p>")
note_uuid = post.activity.document["object"]["id"].rsplit("/", 1)[-1]
# followers-only (the default)
assert fed_client.get(f"/api/v1/federation/profiles/alice/posts/{note_uuid}").status_code == 404
sharing.update(post.user_id, {"visibility": "public"})
publishing.delete_post(post.user_id, "alice", post)
db.session.commit()
assert fed_client.get(f"/api/v1/federation/profiles/alice/posts/{note_uuid}").status_code == 404
assert fed_client.get("/api/v1/federation/profiles/alice/posts/not-a-uuid").status_code == 404
assert fed_client.get("/api/v1/federation/profiles/nobody/posts/not-a-uuid").status_code == 404
# --- Posts and preview ------------------------------------------------------------------
def test_preview_weekly_reflects_the_week_in_progress(fed_client, fed_auth, fed_ctx, user):
now = datetime.now(UTC)
monday = now.date() - timedelta(days=now.weekday())
db.session.add(
Scrobble(
user_id=user.id,
artist="Radiohead",
track="Reckoner",
played_at=datetime(monday.year, monday.month, monday.day, 12, tzinfo=UTC),
)
)
db.session.commit()
response = fed_client.get("/api/v1/federation/preview/weekly", headers=fed_auth)
assert response.status_code == 200
body = response.get_json()
assert "1 play" in body["text"]
assert "<p>" in body["html"]
# Never stored or posted
assert db.session.scalar(db.select(db.func.count()).select_from(FederationPost)) == 0
def test_preview_weekly_needs_auth(fed_client):
assert fed_client.get("/api/v1/federation/preview/weekly").status_code == 401
def test_list_posts_newest_first_including_deleted(fed_client, fed_auth, fed_ctx, user):
sharing.update(user.id, {"enabled": True, "visibility": "public"})
first = publishing.publish(user.id, "alice", "weekly", "a", text="first", html="<p>1</p>")
second = publishing.publish(user.id, "alice", "weekly", "b", text="second", html="<p>2</p>")
publishing.delete_post(user.id, "alice", second)
db.session.commit()
body = fed_client.get("/api/v1/federation/posts", headers=fed_auth).get_json()
assert [p["id"] for p in body] == [second.id, first.id]
assert body[0]["text"] == "second"
assert body[0]["deleted_at"] is not None
assert body[1]["deleted_at"] is None
def test_list_posts_needs_auth(fed_client):
assert fed_client.get("/api/v1/federation/posts").status_code == 401
def test_delete_post(fed_client, fed_auth, fed_ctx, user):
sharing.update(user.id, {"enabled": True, "visibility": "public"})
post = publishing.publish(user.id, "alice", "weekly", "a", text="t", html="<p>t</p>")
db.session.commit()
response = fed_client.delete(f"/api/v1/federation/posts/{post.id}", headers=fed_auth)
assert response.status_code == 204
assert db.session.get(FederationPost, post.id).deleted_at is not None
def test_deleting_an_already_deleted_post_conflicts(fed_client, fed_auth, fed_ctx, user):
sharing.update(user.id, {"enabled": True, "visibility": "public"})
post = publishing.publish(user.id, "alice", "weekly", "a", text="t", html="<p>t</p>")
db.session.commit()
fed_client.delete(f"/api/v1/federation/posts/{post.id}", headers=fed_auth)
response = fed_client.delete(f"/api/v1/federation/posts/{post.id}", headers=fed_auth)
assert response.status_code == 409
def test_deleting_someone_elses_post_is_not_found(fed_client, fed_auth, fed_ctx, user, make_user):
other = make_user(username="bob")
sharing.update(other.id, {"enabled": True, "visibility": "public"})
post = publishing.publish(other.id, "bob", "weekly", "a", text="t", html="<p>t</p>")
db.session.commit()
response = fed_client.delete(f"/api/v1/federation/posts/{post.id}", headers=fed_auth)
assert response.status_code == 404
def test_deleting_an_unknown_post_is_not_found(fed_client, fed_auth):
assert fed_client.delete("/api/v1/federation/posts/999999", headers=fed_auth).status_code == 404
+1 -1
View File
@@ -170,7 +170,7 @@ def test_post_note_browser_is_redirected_to_the_ui_page(fed_client, fed_ctx, sha
note_uuid = post.activity.document["object"]["id"].rsplit("/", 1)[-1]
response = fed_client.get(f"/users/alice/posts/{note_uuid}", headers={"Accept": "text/html"})
assert response.status_code == 302
assert response.headers["Location"] == f"http://localhost:8080/post.html?id={note_uuid}"
assert response.headers["Location"] == f"http://localhost:8080/post.html?u=alice&id={note_uuid}"
def test_nodeinfo(fed_client, sharing_user):
+1
View File
@@ -77,6 +77,7 @@ def test_ui_routes_declare_bearer_auth(fed_spec):
("/api/v1/auth/login", "post"),
("/api/v1/auth/register", "post"),
("/api/v1/federation/profiles/{username}", "get"),
("/api/v1/federation/profiles/{username}/posts/{post_uuid}", "get"),
}
for path, operations in spec["paths"].items():
if not path.startswith("/api/v1/"):