"""Who is allowed into the admin site and who counts as a member — the private members list. private/members.json (gitignored, written by hand on the host) is a JSON object with two arrays: {"admins": ["did:plc:…", "…"], "members": ["did:plc:…"]} `admins` may open the admin site and use its writes. `members` (admins included) get the product's member surfaces — for now the fit/no-fit buttons under posts inside topics, the ground truth the judge will learn from. An entry is an atproto DID, or a handle — a handle is resolved lazily against the public directory the first time it is checked and the result cached, so the file can be edited by hand with handles and kept stable with DIDs. This file is the only allowlist in the product, and it is deliberately a file next to the store, not a table: it lists people, not posts, and editing it is an operator act on the host — exactly the same trust level as editing .env. """ import json import threading from pathlib import Path ROOT = Path(__file__).parent MEMBERS_FILE = ROOT.parent / "private" / "members.json" _load_lock = threading.Lock() # resolved handle -> did, so a handle entry is only ever resolved once _handle_cache: dict[str, str] = {} def _load() -> dict[str, list[str]]: """The raw `admins` and `members` entries (DIDs and handles, as written).""" try: data = json.loads(MEMBERS_FILE.read_text()) except (json.JSONDecodeError, OSError, FileNotFoundError): return {"admins": [], "members": []} out = {"admins": [], "members": []} for key in out: out[key] = [str(x).strip() for x in (data.get(key) or []) if str(x).strip()] return out def _resolve(entries: list[str]) -> set[str]: """Entries as resolved DIDs. Handles are resolved once and cached; a handle that cannot be resolved is dropped, not fatal — a directory hiccup must not lock a member out, and the next check re-tries.""" out: set[str] = set() for entry in entries: if entry.startswith("did:"): out.add(entry) continue with _load_lock: if entry in _handle_cache: out.add(_handle_cache[entry]) continue try: from shared.identity import handle_to_did did = handle_to_did(entry) except Exception: # noqa: BLE001 — an unresolvable entry is just absent continue if did: with _load_lock: _handle_cache[entry] = did out.add(did) return out def admin_dids() -> set[str]: return _resolve(_load()["admins"]) def member_dids() -> set[str]: lists = _load() return _resolve(lists["admins"] + lists["members"]) def is_admin(did: str) -> bool: """Whether this DID may use the admin site. The empty string (no session, or a request that did not come through the proxy) is never an admin.""" if not did: return False return did in admin_dids() def is_member(did: str) -> bool: """Whether this DID counts as a member — admins and members both. Members get the product's member surfaces (the fit/no-fit buttons).""" if not did: return False return did in member_dids()