"""Read portal remote-config flags for social_svc (avatar face gate, etc.).""" from __future__ import annotations import json import os import time from pathlib import Path from typing import Any _CACHE: dict[str, Any] = {"ts": 0.0, "flags": {}, "copy": {}, "raw": {}} _TTL_SEC = 2.0 DEFAULT_SOCIAL_TRIAL = { "enabled": True, "chatMessages": 1, "dateViews": 5, "coffeeBookings": 0, "mapOpens": 3, } def _candidate_paths() -> list[Path]: env = (os.getenv("NOMADR_REMOTE_CONFIG") or os.getenv("REMOTE_CONFIG_PATH") or "").strip() out: list[Path] = [] if env: out.append(Path(env)) # .com primary first, then .cn mirror out.append(Path("/var/www/nomadro.com/data/remote-config.json")) out.append(Path("/var/www/nomadro.cn/data/remote-config.json")) here = Path(__file__).resolve() # apps/api/services/social_svc → repo root for up in (4, 5, 3): try: root = here.parents[up] except IndexError: continue out.append(root / "data" / "remote-config.json") seen: set[str] = set() uniq: list[Path] = [] for p in out: k = str(p) if k not in seen: seen.add(k) uniq.append(p) return uniq def _load_remote() -> dict[str, Any]: now = time.time() if now - float(_CACHE.get("ts") or 0) < _TTL_SEC: return { "flags": dict(_CACHE.get("flags") or {}), "copy": dict(_CACHE.get("copy") or {}), } for path in _candidate_paths(): try: if not path.is_file(): continue raw = json.loads(path.read_text(encoding="utf-8")) if not isinstance(raw, dict): continue flags = raw.get("flags") if isinstance(raw.get("flags"), dict) else {} copy = raw.get("copy") if isinstance(raw.get("copy"), dict) else {} # Fallback: copy.socialFlags.features.avatarFaceCheck if "socialAvatarFaceCheck" not in flags: sf = copy.get("socialFlags") if isinstance(copy.get("socialFlags"), dict) else {} feat = sf.get("features") if isinstance(sf.get("features"), dict) else {} if "avatarFaceCheck" in feat: flags = {**flags, "socialAvatarFaceCheck": bool(feat.get("avatarFaceCheck"))} _CACHE["ts"] = now _CACHE["flags"] = flags _CACHE["copy"] = copy _CACHE["raw"] = raw return {"flags": dict(flags), "copy": dict(copy)} except Exception: continue _CACHE["ts"] = now _CACHE["flags"] = {} _CACHE["copy"] = {} return {"flags": {}, "copy": {}} def _load_flags() -> dict[str, Any]: return _load_remote()["flags"] def social_avatar_face_check_enabled() -> bool | None: """Return True/False from remote-config, or None if unset (caller uses env).""" flags = _load_flags() if "socialAvatarFaceCheck" not in flags: return None return bool(flags.get("socialAvatarFaceCheck")) def social_trial_limits() -> dict[str, Any]: """Chat/date/coffee/map trial caps for unpaid guests.""" packed = _load_remote() flags = packed["flags"] copy = packed["copy"] raw = copy.get("socialTrial") if isinstance(copy.get("socialTrial"), dict) else {} out = dict(DEFAULT_SOCIAL_TRIAL) if "enabled" in raw: out["enabled"] = bool(raw["enabled"]) if "socialTrialEnabled" in flags: out["enabled"] = bool(flags.get("socialTrialEnabled")) for k in ("chatMessages", "dateViews", "coffeeBookings", "mapOpens"): if k in raw and raw[k] is not None: try: out[k] = max(0, int(raw[k])) except (TypeError, ValueError): pass return out