feat(posts): in-context anchored feed with bidirectional infinite scroll

Provenance "View post" deep-links to /posts?post_id=X, which now opens the
feed centered on that post with infinite load in BOTH directions.

Backend: PostFeedService.scroll gains a direction (older|newer); new
around(post_id) returns a window of newer + the post + older with a cursor
for each end. /api/posts accepts ?around= and ?direction=. + API tests.

Frontend: posts store gains loadAround/loadOlder/loadNewer (older appends,
newer prepends) with per-end cursors; PostsView's anchored mode scrolls to
the post, observes top + bottom sentinels, and preserves scroll position on
upward prepend so the page doesn't jump. Normal feed mode unchanged.

Closes the remaining half of the post-navigation work.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-28 21:12:18 -04:00
parent 35fe420701
commit c95b760294
5 changed files with 382 additions and 54 deletions
+67 -1
View File
@@ -4,7 +4,7 @@ Validates list shape, cursor handling, filter validation, and detail
endpoint. Service-level fixtures are exercised by test_post_feed_service
— here we focus on the HTTP surface (validation, status codes, dict shape).
"""
from datetime import UTC, datetime
from datetime import UTC, datetime, timedelta
import pytest
@@ -107,6 +107,72 @@ async def test_detail_404_for_unknown(client):
assert body["error"] == "not_found"
@pytest.fixture
async def post_timeline(db):
"""Five posts on distinct dates: posts[0] oldest … posts[4] newest."""
artist = Artist(name="tl-api", slug="tl-api")
db.add(artist)
await db.flush()
source = Source(
artist_id=artist.id, platform="patreon",
url="https://p/tl-api", enabled=True,
)
db.add(source)
await db.flush()
base = datetime(2026, 1, 1, tzinfo=UTC)
posts = []
for i in range(5):
p = Post(
source_id=source.id, external_post_id=f"TL{i}",
post_title=f"post {i}", post_date=base + timedelta(days=i),
)
db.add(p)
posts.append(p)
await db.commit()
return artist, source, posts
@pytest.mark.asyncio
async def test_around_returns_window_with_anchor(client, post_timeline):
_, _, posts = post_timeline
anchor = posts[2]
resp = await client.get(f"/api/posts?around={anchor.id}&limit=1")
assert resp.status_code == 200
body = await resp.get_json()
assert set(body.keys()) == {"items", "cursor_older", "cursor_newer", "anchor_id"}
assert body["anchor_id"] == anchor.id
# limit=1: one newer + anchor + one older, in feed (desc) order.
assert [it["id"] for it in body["items"]] == [posts[3].id, posts[2].id, posts[1].id]
assert body["cursor_older"] is not None # posts[0] still older
assert body["cursor_newer"] is not None # posts[4] still newer
@pytest.mark.asyncio
async def test_around_404_for_unknown(client):
resp = await client.get("/api/posts?around=999999")
assert resp.status_code == 404
assert (await resp.get_json())["error"] == "not_found"
@pytest.mark.asyncio
async def test_direction_newer_walks_forward(client, post_timeline):
_, _, posts = post_timeline
around = await client.get(f"/api/posts?around={posts[1].id}&limit=1")
cursor_newer = (await around.get_json())["cursor_newer"]
assert cursor_newer is not None
resp = await client.get(f"/api/posts?cursor={cursor_newer}&direction=newer&limit=5")
assert resp.status_code == 200
# Newer than the window's newest (posts[2]) → posts[3], posts[4] in desc order.
assert [it["id"] for it in (await resp.get_json())["items"]] == [posts[4].id, posts[3].id]
@pytest.mark.asyncio
async def test_rejects_bad_direction(client):
resp = await client.get("/api/posts?direction=sideways")
assert resp.status_code == 400
assert (await resp.get_json())["error"] == "invalid_direction"
@pytest.mark.asyncio
async def test_detail_returns_uncapped_thumbnails(client, db):
"""Feed query caps thumbnails at 6 for previews; detail endpoint