From 8dbbd16cf9643cd4b366f42eb9974a865992abb0 Mon Sep 17 00:00:00 2001 From: shay Date: Mon, 4 May 2026 16:34:12 -0400 Subject: [PATCH] feat(api): auto-enroll users typing unknown usernames into workshop --- generator/src/api.py | 31 ++++++++++++++++++++++++------- generator/src/processor.py | 9 +++++++-- generator/tests/test_api.py | 23 ++++++++++++++++++----- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/generator/src/api.py b/generator/src/api.py index 905e913..a82a892 100644 --- a/generator/src/api.py +++ b/generator/src/api.py @@ -623,18 +623,35 @@ def get_widget_named(username: str, widget: str): def get_user_data(username: str): """Precomputed widget data for client-side SVG rendering. - Pure DB lookup — never touches the fetcher or runs compute on the hot - path. Enrollment is now OAuth-driven only. + Auto-enrolls visitors for users that haven't signed in themselves yet + (subject to ENROLLMENT_DAILY_CAP, same as the /?username=X shortcut). + Without this, typing someone else's username into the workshop's + search bar produced 'not_enrolled' forever — the user only got + enrolled if they personally went through OAuth, which makes the + public-preview UX dead-on-arrival. """ if not is_valid_username(username): return jsonify({"error": "invalid_username"}), 400 + username = username.lower() settings_row = db.get_settings(username) if settings_row is None: - # The user has never been enrolled. We don't know yet whether they - # exist on GitHub — only the fetcher can confirm that. Use a - # distinct status so the frontend doesn't surface a misleading - # "User not found" error for a user who simply hasn't signed in. - return jsonify({"status": "not_enrolled"}), 404 + # Daily cap is the abuse backstop: a botnet typing random names + # can't burn through the GitHub PAT budget past this number. + if db.enrollments_today() >= config.ENROLLMENT_DAILY_CAP: + return jsonify({"status": "rate_limited"}), 429 + defaults = { + "theme": "dark", + "enabled": config.ENABLED_WIDGETS, + "widget_order": config.WIDGET_ORDER, + } + # Same triple-converging kickoff as the OAuth callback: enqueue + # the build directly + ask the fetcher to start in the background + # + run process_one in the API's thread pool. Robust to any one + # of the three paths failing. + db.enroll(username, defaults) + _request_fetch_async(username) + _kickoff_prefetch_async(username) + return jsonify({"status": "building"}), 202 db.touch_last_requested(username) diff --git a/generator/src/processor.py b/generator/src/processor.py index d7d31a1..c5cb1a6 100644 --- a/generator/src/processor.py +++ b/generator/src/processor.py @@ -445,8 +445,13 @@ def compute_focus(github_data: dict, hidden_languages: list[str] = None) -> list focus_counts = defaultdict(float) - # Use top 30 most recently pushed repos - sorted_repos = sorted(repos, key=lambda r: r.get("pushed_at", ""), reverse=True) + # Use top 30 most recently pushed repos. `r.get("pushed_at", "")` is + # NOT enough — when GitHub returns the field as JSON null (archived / + # never-pushed repos, common in torvalds-sized accounts), .get returns + # None and the sort blows up with "'<' not supported between instances + # of 'str' and 'NoneType'". `or ""` coerces null-valued keys to the + # same empty-string fallback as missing keys. + sorted_repos = sorted(repos, key=lambda r: r.get("pushed_at") or "", reverse=True) active_repos = sorted_repos[:30] # Count languages in active repos (excluding hidden languages) diff --git a/generator/tests/test_api.py b/generator/tests/test_api.py index cc3d064..e76dced 100644 --- a/generator/tests/test_api.py +++ b/generator/tests/test_api.py @@ -292,12 +292,25 @@ def test_refresh_without_session_is_401(client): assert r.status_code == 401 -def test_data_unknown_user_returns_404(client): +def test_data_unknown_user_auto_enrolls_and_returns_building(client): + """Visitor auto-enroll: typing someone else's username into the workshop + search bar (which polls /data) must kick off the build pipeline, not + sit forever on a 404. Daily-cap protected by ENROLLMENT_DAILY_CAP.""" r = client.get("/api/testuser/data") - assert r.status_code == 404 - # Unknown-but-not-enrolled is distinct from confirmed-not-on-GitHub so - # the frontend can avoid a misleading "User not found" message. - assert r.get_json()["status"] == "not_enrolled" + assert r.status_code == 202 + assert r.get_json()["status"] == "building" + # The user is now in `users` with a build queued, just like an OAuth + # signup or the /?username=X shortcut would have done. + assert dbmod.get_settings("testuser") is not None + + +def test_data_unknown_user_when_cap_reached_returns_429(client, monkeypatch): + monkeypatch.setattr(cfg, "ENROLLMENT_DAILY_CAP", 0) + r = client.get("/api/testuser/data") + assert r.status_code == 429 + assert r.get_json()["status"] == "rate_limited" + # No enrollment should have happened — cap is the abuse backstop. + assert dbmod.get_settings("testuser") is None def test_enroll_endpoint_is_gone(client):