feat(api): auto-enroll users typing unknown usernames into workshop

This commit is contained in:
shay
2026-05-04 16:34:12 -04:00
parent 6edf98334c
commit 8dbbd16cf9
3 changed files with 49 additions and 14 deletions
+24 -7
View File
@@ -623,18 +623,35 @@ def get_widget_named(username: str, widget: str):
def get_user_data(username: str): def get_user_data(username: str):
"""Precomputed widget data for client-side SVG rendering. """Precomputed widget data for client-side SVG rendering.
Pure DB lookup — never touches the fetcher or runs compute on the hot Auto-enrolls visitors for users that haven't signed in themselves yet
path. Enrollment is now OAuth-driven only. (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): if not is_valid_username(username):
return jsonify({"error": "invalid_username"}), 400 return jsonify({"error": "invalid_username"}), 400
username = username.lower()
settings_row = db.get_settings(username) settings_row = db.get_settings(username)
if settings_row is None: if settings_row is None:
# The user has never been enrolled. We don't know yet whether they # Daily cap is the abuse backstop: a botnet typing random names
# exist on GitHub — only the fetcher can confirm that. Use a # can't burn through the GitHub PAT budget past this number.
# distinct status so the frontend doesn't surface a misleading if db.enrollments_today() >= config.ENROLLMENT_DAILY_CAP:
# "User not found" error for a user who simply hasn't signed in. return jsonify({"status": "rate_limited"}), 429
return jsonify({"status": "not_enrolled"}), 404 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) db.touch_last_requested(username)
+7 -2
View File
@@ -445,8 +445,13 @@ def compute_focus(github_data: dict, hidden_languages: list[str] = None) -> list
focus_counts = defaultdict(float) focus_counts = defaultdict(float)
# Use top 30 most recently pushed repos # Use top 30 most recently pushed repos. `r.get("pushed_at", "")` is
sorted_repos = sorted(repos, key=lambda r: r.get("pushed_at", ""), reverse=True) # 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] active_repos = sorted_repos[:30]
# Count languages in active repos (excluding hidden languages) # Count languages in active repos (excluding hidden languages)
+18 -5
View File
@@ -292,12 +292,25 @@ def test_refresh_without_session_is_401(client):
assert r.status_code == 401 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") r = client.get("/api/testuser/data")
assert r.status_code == 404 assert r.status_code == 202
# Unknown-but-not-enrolled is distinct from confirmed-not-on-GitHub so assert r.get_json()["status"] == "building"
# the frontend can avoid a misleading "User not found" message. # The user is now in `users` with a build queued, just like an OAuth
assert r.get_json()["status"] == "not_enrolled" # 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): def test_enroll_endpoint_is_gone(client):