nomadro/apps/api/services/social_svc/trial_gate.py
Eric 9b7ffb24dc Ship social-first admin console, soft trials, and safer join gates.
Rebuild admin.nomadro.com around community ops, add configurable free peeks instead of nav locks, humanize user copy, and harden UGC/liveness admin APIs with backup/self-test scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-04 23:47:04 +08:00

77 lines
2.0 KiB
Python

"""Server-side social trial gates — unpaid guests cannot bypass client caps."""
from __future__ import annotations
from social_svc.remote_flags import social_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 social_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 social_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 = social_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")