From c9abad3e130a2577b7d212ef33747006cd98bf7a Mon Sep 17 00:00:00 2001 From: Emi Griffith Date: Sun, 19 Jul 2026 21:36:44 -0700 Subject: [PATCH] Show 'Link Discord' only when Discord linking is configured (#213) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The account menu offered 'Link Discord' to every signed-in user even on a server with no Discord OAuth app configured, where it dead-ends (the start route 303-bounces to /alerts). Add GET /api/v2/discord/config reporting whether linking is enabled, and have the menu render the entry only when it is. On a server without Discord set up nothing surfaces; setting the OAuth env vars later makes the entry appear on its own — no code change needed to turn it on. --- discord_link.py | 9 +++++++++ tests/test_discord_link.py | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/discord_link.py b/discord_link.py index 236ce17..b48bee9 100644 --- a/discord_link.py +++ b/discord_link.py @@ -101,6 +101,15 @@ def _account_redirect(status: str) -> RedirectResponse: # --- routes ------------------------------------------------------------------ +@router.get("/discord/config") +async def discord_config(): + """Whether Discord account-linking is configured on this server. The account + menu uses it to show the "Link Discord" entry only when linking will actually + work — so an unconfigured server surfaces no dead-end Discord UI, and enabling + it later (setting the OAuth env vars) makes the entry appear on its own.""" + return {"enabled": enabled()} + + @router.get("/discord/link/start") async def link_start(request: Request, user=Depends(current_active_user)): if not enabled(): diff --git a/tests/test_discord_link.py b/tests/test_discord_link.py index 66efb8f..e75e599 100644 --- a/tests/test_discord_link.py +++ b/tests/test_discord_link.py @@ -46,6 +46,17 @@ def test_state_is_secret_dependent(monkeypatch): # --- routes ------------------------------------------------------------------ +def test_config_reports_enabled_state(monkeypatch): + c = TestClient(appmod.app) + monkeypatch.setattr(dl, "CLIENT_ID", "") + monkeypatch.setattr(dl, "CLIENT_SECRET", "") + r = c.get(f"{V2}/discord/config") # no auth required + assert r.status_code == 200 and r.json() == {"enabled": False} + monkeypatch.setattr(dl, "CLIENT_ID", "app123") + monkeypatch.setattr(dl, "CLIENT_SECRET", "secret456") + assert c.get(f"{V2}/discord/config").json() == {"enabled": True} + + def test_link_requires_auth(): c = TestClient(appmod.app) assert c.get(f"{V2}/discord/link/start", follow_redirects=False).status_code == 401