mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(api): implement auto-enrollment for unknown users on first hit
This commit is contained in:
@@ -799,6 +799,20 @@ def _serve(username: str, widget_name: str) -> Response:
|
|||||||
"""
|
"""
|
||||||
settings_row = db.get_settings(username)
|
settings_row = db.get_settings(username)
|
||||||
if settings_row is None:
|
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)
|
return _placeholder_response("building", username)
|
||||||
|
|
||||||
db.touch_last_requested(username)
|
db.touch_last_requested(username)
|
||||||
|
|||||||
@@ -87,14 +87,15 @@ def test_root_without_username_serves_spa(client):
|
|||||||
assert r.mimetype != "image/svg+xml"
|
assert r.mimetype != "image/svg+xml"
|
||||||
|
|
||||||
|
|
||||||
def test_get_unknown_user_returns_building_placeholder_without_enrolling(client):
|
def test_get_unknown_user_auto_enrolls_and_returns_building(client):
|
||||||
"""The SVG endpoint is for README embeds — enrollment is frontend-driven
|
"""The SVG embed endpoint must kick off enrollment + prefetch on first
|
||||||
through /data or /api/enroll, so GET /api/<u> does not auto-enroll."""
|
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")
|
r = client.get("/api/alice")
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
assert r.headers["X-Widget-Status"] == "building"
|
assert r.headers["X-Widget-Status"] == "building"
|
||||||
assert "Building" in r.data.decode()
|
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):
|
def test_enrolled_but_not_generated_returns_building(client):
|
||||||
|
|||||||
Reference in New Issue
Block a user