nomadro/apps/api/services/hub_svc/trial_gate.py
Eric d971a1f3e6 Lock Hub to China-only cities and document the cn audience gate.
Hard-enable hubCnOnly, drop foreign cities from default seeds/map/coffeechat, and keep intl cities in separate INT L catalogs until globalization reopens.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-29 00:03:11 +08:00

77 lines
2.0 KiB
Python

"""Server-side social trial gates — unpaid guests cannot bypass client caps."""
from __future__ import annotations
from hub_svc.remote_flags import hub_trial_limits
def _paid_ok(raw: object) -> bool:
return str(raw or "").strip().lower() in {"1", "true", "yes", "paid"}
def _guest_is_full_member(guest_id: str) -> bool:
"""Paid join + verified liveness + roster avatar → unlimited."""
gid = (guest_id or "").strip()
if not gid:
return False
try:
from hub_svc.pb_data import (
C_JOIN_LEADS,
get_pb,
get_roster,
init_pb,
pb_quote,
)
init_pb()
roster = get_roster(gid) or {}
avatar = str(roster.get("avatar") or "").strip()
live = str(
roster.get("livenessStatus") or roster.get("liveness_status") or ""
).strip()
if not avatar or live != "verified":
return False
rows = get_pb().list_all(
C_JOIN_LEADS,
filter=f"guest_id={pb_quote(gid)}",
hard_limit=40,
)
for row in rows or []:
if _paid_ok(row.get("paid")):
return True
return False
except Exception:
return False
def count_guest_messages(guest_id: str, *, hard_limit: int = 50) -> int:
gid = (guest_id or "").strip()
if not gid:
return 0
try:
from hub_svc.pb_data import C_MESSAGES, get_pb, init_pb, pb_quote
init_pb()
rows = get_pb().list_all(
C_MESSAGES,
filter=f"guest_id={pb_quote(gid)}",
hard_limit=hard_limit,
)
return len(rows or [])
except Exception:
return 0
def assert_chat_trial_ok(guest_id: str) -> None:
"""Raise ValueError if unpaid guest already used chat trial quota."""
limits = hub_trial_limits()
if not limits.get("enabled", True):
return
if _guest_is_full_member(guest_id):
return
cap = int(limits.get("chatMessages") or 0)
if cap <= 0:
raise ValueError("trial exhausted: chat requires membership")
used = count_guest_messages(guest_id, hard_limit=cap + 5)
if used >= cap:
raise ValueError("trial exhausted: chat message limit reached")