feat(api): implement auto-enrollment for unknown users on first hit

This commit is contained in:
shay
2026-05-26 15:10:20 -07:00
parent 151f330d31
commit 369d42bb5f
2 changed files with 19 additions and 4 deletions
+14
View File
@@ -799,6 +799,20 @@ def _serve(username: str, widget_name: str) -> Response:
"""
settings_row = db.get_settings(username)
if settings_row is None:
# Auto-enroll on first hit so embed URLs for never-seen users
# actually trigger a build, instead of returning a "building"
# placeholder that nothing ever resolves. Same daily-cap +
# triple-kickoff pattern as /api/<u>/data and /api/top-langs.
if db.enrollments_today() >= config.ENROLLMENT_DAILY_CAP:
return _placeholder_response("rate_limited", username)
defaults = {
"theme": "dark",
"enabled": config.ENABLED_WIDGETS,
"widget_order": config.WIDGET_ORDER,
}
db.enroll(username, defaults)
_request_fetch_async(username)
_kickoff_prefetch_async(username)
return _placeholder_response("building", username)
db.touch_last_requested(username)
+5 -4
View File
@@ -87,14 +87,15 @@ def test_root_without_username_serves_spa(client):
assert r.mimetype != "image/svg+xml"
def test_get_unknown_user_returns_building_placeholder_without_enrolling(client):
"""The SVG endpoint is for README embeds — enrollment is frontend-driven
through /data or /api/enroll, so GET /api/<u> does not auto-enroll."""
def test_get_unknown_user_auto_enrolls_and_returns_building(client):
"""The SVG embed endpoint must kick off enrollment + prefetch on first
hit — otherwise a README embed for a never-seen username would return
'building' forever with nothing ever advancing the build."""
r = client.get("/api/alice")
assert r.status_code == 200
assert r.headers["X-Widget-Status"] == "building"
assert "Building" in r.data.decode()
assert dbmod.get_settings("alice") is None
assert dbmod.get_settings("alice") is not None
def test_enrolled_but_not_generated_returns_building(client):