Show 'Link Discord' only when Discord linking is configured (#213)

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.
This commit is contained in:
Emi Griffith 2026-07-19 21:36:44 -07:00 committed by GitHub
parent 4520b9e4ba
commit c9abad3e13
2 changed files with 20 additions and 0 deletions

View file

@ -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():

View file

@ -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