Reduce Supabase disk IO

This commit is contained in:
2569718930@qq.com
2026-05-29 17:22:33 +08:00
parent 9e3a4e2f45
commit 2269aaefd7
63 changed files with 4637 additions and 410 deletions
+371 -18
View File
@@ -69,6 +69,14 @@ class SupabaseEntitlementService:
self._identity_cache_lock = threading.Lock()
self._sub_cache: Dict[str, Dict[str, object]] = {}
self._sub_cache_lock = threading.Lock()
self._latest_subscription_cache: Dict[str, Dict[str, object]] = {}
self._latest_subscription_cache_lock = threading.Lock()
self._active_subscription_bool_cache: Dict[str, Dict[str, object]] = {}
self._active_subscription_bool_cache_lock = threading.Lock()
self._active_subscriptions_cache: Dict[str, object] = {}
self._active_subscriptions_cache_lock = threading.Lock()
self._auth_users_cache: Dict[str, Dict[str, object]] = {}
self._auth_users_cache_lock = threading.Lock()
def invalidate_subscription_cache(self, user_id: str) -> None:
key = str(user_id or "").strip()
@@ -76,6 +84,12 @@ class SupabaseEntitlementService:
return
with self._sub_cache_lock:
self._sub_cache.pop(key, None)
with self._latest_subscription_cache_lock:
self._latest_subscription_cache.pop(key, None)
with self._active_subscription_bool_cache_lock:
self._active_subscription_bool_cache.pop(key, None)
with self._active_subscriptions_cache_lock:
self._active_subscriptions_cache.clear()
@property
def configured(self) -> bool:
@@ -90,6 +104,9 @@ class SupabaseEntitlementService:
def _entitlement_events_endpoint(self) -> str:
return f"{self.supabase_url}/rest/v1/entitlement_events"
def _profiles_endpoint(self) -> str:
return f"{self.supabase_url}/rest/v1/profiles"
def _request_headers_for_user(self, access_token: str) -> Dict[str, str]:
return {
"apikey": self.anon_key,
@@ -116,8 +133,7 @@ class SupabaseEntitlementService:
cached = self._identity_cache.get(access_token)
if cached and now_ts - float(cached.get("ts") or 0) < self.cache_ttl_sec:
identity = cached.get("identity")
if isinstance(identity, SupabaseIdentity):
return identity
return identity if isinstance(identity, SupabaseIdentity) else None
if not self.configured:
return None
@@ -129,10 +145,21 @@ class SupabaseEntitlementService:
timeout=self.timeout_sec,
)
if response.status_code != 200:
if response.status_code in {401, 403}:
with self._identity_cache_lock:
self._identity_cache[access_token] = {
"identity": None,
"ts": now_ts,
}
return None
data = response.json() if response.content else {}
user_id = str(data.get("id") or "").strip()
if not user_id:
with self._identity_cache_lock:
self._identity_cache[access_token] = {
"identity": None,
"ts": now_ts,
}
return None
# Extract points from user_metadata
@@ -176,17 +203,26 @@ class SupabaseEntitlementService:
if isinstance(row, dict):
return row
return None
with self._active_subscription_bool_cache_lock:
cached_bool = self._active_subscription_bool_cache.get(user_id)
if (
cached_bool
and now_ts - float(cached_bool.get("ts") or 0) < self.sub_cache_ttl_sec
and cached_bool.get("active") is False
):
return None
try:
now = datetime.now(timezone.utc)
now_iso = now.isoformat()
params = {
"select": "id,user_id,status,plan_code,starts_at,expires_at",
"select": "plan_code,source,starts_at,expires_at",
"user_id": f"eq.{user_id}",
"status": "eq.active",
"starts_at": f"lte.{now_iso}",
"expires_at": f"gt.{now_iso}",
"order": "expires_at.desc",
"limit": "20",
"limit": "1",
}
response = requests.get(
self._subscription_endpoint(),
@@ -205,13 +241,12 @@ class SupabaseEntitlementService:
else:
data = response.json() if response.content else []
rows = [item for item in data if isinstance(item, dict)] if isinstance(data, list) else []
row = self._pick_latest_current_subscription(rows, now=now)
row = rows[0] if rows else None
with self._sub_cache_lock:
self._sub_cache[user_id] = {
"active": bool(row),
"row": row,
"rows": rows,
"ts": now_ts,
}
return row
@@ -238,16 +273,12 @@ class SupabaseEntitlementService:
rows = cached.get("rows")
if isinstance(rows, list):
return [row for row in rows if isinstance(row, dict)]
row = cached.get("row")
if isinstance(row, dict):
return [row]
return []
try:
now = datetime.now(timezone.utc)
now_iso = now.isoformat()
params = {
"select": "id,user_id,status,plan_code,starts_at,expires_at,source,created_at,updated_at",
"select": "plan_code,source,starts_at,expires_at",
"user_id": f"eq.{user_id}",
"status": "eq.active",
"expires_at": f"gt.{now_iso}",
@@ -290,9 +321,15 @@ class SupabaseEntitlementService:
) -> Optional[Dict[str, object]]:
if not user_id or not self.service_role_key:
return None
now_ts = time.time()
with self._latest_subscription_cache_lock:
cached = self._latest_subscription_cache.get(user_id)
if cached and now_ts - float(cached.get("ts") or 0) < self.sub_cache_ttl_sec:
row = cached.get("row")
return row if isinstance(row, dict) else None
try:
params = {
"select": "id,user_id,status,plan_code,starts_at,expires_at,source,created_at,updated_at",
"select": "plan_code,starts_at,expires_at",
"user_id": f"eq.{user_id}",
"order": "created_at.desc",
"limit": "1",
@@ -312,7 +349,13 @@ class SupabaseEntitlementService:
return None
data = response.json() if response.content else []
row = data[0] if isinstance(data, list) and data else None
return row if isinstance(row, dict) else None
result = row if isinstance(row, dict) else None
with self._latest_subscription_cache_lock:
self._latest_subscription_cache[user_id] = {
"row": result,
"ts": now_ts,
}
return result
except Exception as exc:
logger.warning(f"supabase subscription history query error user_id={user_id}: {exc}")
return None
@@ -359,7 +402,68 @@ class SupabaseEntitlementService:
return None
def _query_active_subscription(self, user_id: str) -> bool:
return self._query_latest_active_subscription(user_id) is not None
if not user_id:
return False
if not self.service_role_key:
logger.warning("SUPABASE_SERVICE_ROLE_KEY is missing")
return False
now_ts = time.time()
with self._sub_cache_lock:
cached_detail = self._sub_cache.get(user_id)
if cached_detail and now_ts - float(cached_detail.get("ts") or 0) < self.sub_cache_ttl_sec:
rows = cached_detail.get("rows")
if isinstance(rows, list):
return self._pick_latest_current_subscription(
[row for row in rows if isinstance(row, dict)]
) is not None
if "row" in cached_detail:
return isinstance(cached_detail.get("row"), dict)
with self._active_subscription_bool_cache_lock:
cached = self._active_subscription_bool_cache.get(user_id)
if cached and now_ts - float(cached.get("ts") or 0) < self.sub_cache_ttl_sec:
return bool(cached.get("active"))
try:
now = datetime.now(timezone.utc)
now_iso = now.isoformat()
params = {
"select": "expires_at",
"user_id": f"eq.{user_id}",
"status": "eq.active",
"starts_at": f"lte.{now_iso}",
"expires_at": f"gt.{now_iso}",
"order": "expires_at.desc",
"limit": "1",
}
response = requests.get(
self._subscription_endpoint(),
headers=self._request_headers_for_service_role(),
params=params,
timeout=self.timeout_sec,
)
if response.status_code != 200:
logger.warning(
"supabase active subscription bool query failed user_id={} status={}",
user_id,
response.status_code,
)
active = False
else:
data = response.json() if response.content else []
rows = [row for row in data if isinstance(row, dict)] if isinstance(data, list) else []
active = bool(rows)
with self._active_subscription_bool_cache_lock:
self._active_subscription_bool_cache[user_id] = {
"active": bool(active),
"ts": now_ts,
}
return bool(active)
except Exception as exc:
logger.warning(f"supabase active subscription bool query error user_id={user_id}: {exc}")
return False
def get_latest_active_subscription(
self,
@@ -385,9 +489,14 @@ class SupabaseEntitlementService:
if respect_requirement and not self.require_subscription:
return {}
rows = self._query_active_subscription_rows(user_id, bypass_cache=bypass_cache)
return self._subscription_window_from_rows(rows)
def _subscription_window_from_rows(
self,
rows: List[Dict[str, object]],
) -> Dict[str, object]:
if not rows:
return {}
now = datetime.now(timezone.utc)
current = self._pick_latest_current_subscription(rows, now=now)
total_expiry: Optional[datetime] = None
@@ -422,6 +531,153 @@ class SupabaseEntitlementService:
"rows": rows,
}
def list_subscription_windows(
self,
user_ids: List[str],
bypass_cache: bool = False,
) -> Dict[str, Dict[str, object]]:
keys: List[str] = []
for item in user_ids or []:
key = str(item or "").strip().lower()
if key and key not in keys:
keys.append(key)
if not keys:
return {}
out: Dict[str, Dict[str, object]] = {}
if not bypass_cache:
missing: List[str] = []
now_ts = time.time()
with self._sub_cache_lock:
for key in keys:
cached = self._sub_cache.get(key)
if cached and now_ts - float(cached.get("ts") or 0) < self.sub_cache_ttl_sec:
rows = cached.get("rows")
if isinstance(rows, list):
out[key] = self._subscription_window_from_rows(
[row for row in rows if isinstance(row, dict)]
)
continue
missing.append(key)
keys = missing
if not keys:
return out
if not self.service_role_key:
logger.warning("SUPABASE_SERVICE_ROLE_KEY is missing")
return out
try:
now = datetime.now(timezone.utc)
now_iso = now.isoformat()
params = {
"select": "user_id,plan_code,source,starts_at,expires_at",
"user_id": f"in.({','.join(keys)})",
"status": "eq.active",
"expires_at": f"gt.{now_iso}",
"order": "user_id.asc,expires_at.desc",
"limit": str(max(1, min(len(keys) * 20, 1000))),
}
response = requests.get(
self._subscription_endpoint(),
headers=self._request_headers_for_service_role(),
params=params,
timeout=self.timeout_sec,
)
if response.status_code != 200:
logger.warning(
"supabase subscription window batch query failed users={} status={}",
len(keys),
response.status_code,
)
return out
data = response.json() if response.content else []
rows = [row for row in data if isinstance(row, dict)] if isinstance(data, list) else []
grouped: Dict[str, List[Dict[str, object]]] = {key: [] for key in keys}
for row in rows:
key = str(row.get("user_id") or "").strip().lower()
if key in grouped:
grouped[key].append(row)
now_ts = time.time()
with self._sub_cache_lock:
for key, user_rows in grouped.items():
current_row = self._pick_latest_current_subscription(user_rows, now=now)
self._sub_cache[key] = {
"active": bool(current_row),
"row": current_row,
"rows": user_rows,
"ts": now_ts,
}
out[key] = self._subscription_window_from_rows(user_rows)
return out
except Exception as exc:
logger.warning(f"supabase subscription window batch query error users={len(keys)}: {exc}")
return out
def list_active_subscription_windows(self, limit: int = 200) -> Dict[str, object]:
if not self.service_role_key:
logger.warning("SUPABASE_SERVICE_ROLE_KEY is missing")
return {"subscriptions": [], "windows": {}}
safe_limit = max(1, min(int(limit or 200), 1000))
try:
now = datetime.now(timezone.utc)
now_iso = now.isoformat()
params = {
"select": "user_id,plan_code,source,starts_at,expires_at",
"status": "eq.active",
"expires_at": f"gt.{now_iso}",
"order": "user_id.asc,expires_at.desc",
"limit": str(max(1, min(safe_limit * 20, 5000))),
}
response = requests.get(
self._subscription_endpoint(),
headers=self._request_headers_for_service_role(),
params=params,
timeout=self.timeout_sec,
)
if response.status_code != 200:
logger.warning(
"supabase active subscription window query failed status={}",
response.status_code,
)
return {"subscriptions": [], "windows": {}}
data = response.json() if response.content else []
rows = [row for row in data if isinstance(row, dict)] if isinstance(data, list) else []
grouped: Dict[str, List[Dict[str, object]]] = {}
for row in rows:
key = str(row.get("user_id") or "").strip().lower()
if key:
grouped.setdefault(key, []).append(row)
windows: Dict[str, Dict[str, object]] = {}
current_rows: List[Dict[str, object]] = []
now_ts = time.time()
with self._sub_cache_lock:
for key, user_rows in grouped.items():
current_row = self._pick_latest_current_subscription(user_rows, now=now)
self._sub_cache[key] = {
"active": bool(current_row),
"row": current_row,
"rows": user_rows,
"ts": now_ts,
}
windows[key] = self._subscription_window_from_rows(user_rows)
if isinstance(current_row, dict):
current_rows.append(current_row)
current_rows.sort(key=lambda row: str(row.get("expires_at") or ""))
current_rows = current_rows[:safe_limit]
with self._active_subscriptions_cache_lock:
self._active_subscriptions_cache[str(safe_limit)] = {
"rows": current_rows,
"ts": now_ts,
}
return {"subscriptions": current_rows, "windows": windows}
except Exception as exc:
logger.warning(f"supabase active subscription window query error: {exc}")
return {"subscriptions": [], "windows": {}}
def has_active_subscription(
self,
user_id: str,
@@ -435,12 +691,20 @@ class SupabaseEntitlementService:
if not self.service_role_key:
logger.warning("SUPABASE_SERVICE_ROLE_KEY is missing")
return []
safe_limit = max(1, min(int(limit or 200), 1000))
cache_key = str(safe_limit)
now_ts = time.time()
with self._active_subscriptions_cache_lock:
cached = self._active_subscriptions_cache.get(cache_key)
if isinstance(cached, dict) and now_ts - float(cached.get("ts") or 0) < self.sub_cache_ttl_sec:
rows = cached.get("rows")
if isinstance(rows, list):
return [row for row in rows if isinstance(row, dict)]
try:
now = datetime.now(timezone.utc)
safe_limit = max(1, min(int(limit or 200), 1000))
now_iso = now.isoformat()
params = {
"select": "id,user_id,status,plan_code,starts_at,expires_at",
"select": "user_id,plan_code,starts_at,expires_at",
"status": "eq.active",
"expires_at": f"gt.{now_iso}",
"order": "expires_at.asc",
@@ -461,11 +725,17 @@ class SupabaseEntitlementService:
data = response.json() if response.content else []
if not isinstance(data, list):
return []
return [
rows = [
row
for row in data
if isinstance(row, dict) and self._is_subscription_started(row, now=now)
]
with self._active_subscriptions_cache_lock:
self._active_subscriptions_cache[cache_key] = {
"rows": rows,
"ts": now_ts,
}
return rows
except Exception as exc:
logger.warning(f"supabase active subscriptions query error: {exc}")
return []
@@ -484,6 +754,29 @@ class SupabaseEntitlementService:
return {}
out: Dict[str, Dict[str, object]] = {}
now_ts = time.time()
missing_keys: List[str] = []
with self._auth_users_cache_lock:
for key in keys:
cached = self._auth_users_cache.get(key)
if cached and now_ts - float(cached.get("ts") or 0) < self.sub_cache_ttl_sec:
user = cached.get("user")
if isinstance(user, dict):
out[key] = dict(user)
continue
missing_keys.append(key)
keys = missing_keys
if not keys:
return out
profile_users = self._get_profile_users(keys)
if profile_users:
self._remember_auth_users(profile_users)
out.update(profile_users)
keys = [key for key in keys if key not in out]
if not keys:
return out
for user_id in keys:
try:
response = requests.get(
@@ -506,9 +799,69 @@ class SupabaseEntitlementService:
"email": str(payload.get("email") or "").strip(),
"created_at": payload.get("created_at"),
}
self._remember_auth_users({user_id: out[user_id]})
except Exception as exc:
logger.warning(f"supabase admin user query error user_id={user_id}: {exc}")
return out
def _remember_auth_users(self, users: Dict[str, Dict[str, object]]) -> None:
if not users:
return
now_ts = time.time()
with self._auth_users_cache_lock:
for raw_key, user in users.items():
key = str(raw_key or "").strip().lower()
if key and isinstance(user, dict):
self._auth_users_cache[key] = {
"user": dict(user),
"ts": now_ts,
}
if len(self._auth_users_cache) > 4096:
oldest_keys = sorted(
self._auth_users_cache,
key=lambda key: float(
self._auth_users_cache[key].get("ts") or 0.0
),
)
for key in oldest_keys[: len(self._auth_users_cache) - 4096]:
self._auth_users_cache.pop(key, None)
def _get_profile_users(self, user_ids: List[str]) -> Dict[str, Dict[str, object]]:
if not user_ids or not self.service_role_key:
return {}
try:
response = requests.get(
self._profiles_endpoint(),
headers=self._request_headers_for_service_role(),
params={
"select": "id,email,created_at",
"id": f"in.({','.join(user_ids)})",
"limit": str(max(1, min(len(user_ids), 1000))),
},
timeout=self.timeout_sec,
)
if response.status_code != 200:
logger.warning(
"supabase profile users batch query failed users={} status={}",
len(user_ids),
response.status_code,
)
return {}
data = response.json() if response.content else []
rows = [row for row in data if isinstance(row, dict)] if isinstance(data, list) else []
out: Dict[str, Dict[str, object]] = {}
for row in rows:
user_id = str(row.get("id") or "").strip().lower()
if not user_id:
continue
out[user_id] = {
"email": str(row.get("email") or "").strip(),
"created_at": row.get("created_at"),
}
return out
except Exception as exc:
logger.warning(f"supabase profile users batch query error users={len(user_ids)}: {exc}")
return {}
SUPABASE_ENTITLEMENT = SupabaseEntitlementService()
+1
View File
@@ -457,6 +457,7 @@ class BasicCommandHandler:
for row in rows:
if self._subscription_row_is_paid(row):
return True
return False
if hasattr(self.entitlement_service, "get_latest_active_subscription"):
row = self.entitlement_service.get_latest_active_subscription(
supabase_user_id,
+1 -1
View File
@@ -169,7 +169,7 @@ def _grant_bonus_subscription_days(
}
ins = requests.post(
f"{base}/rest/v1/subscriptions",
headers={**headers, "Prefer": "return=representation"},
headers={**headers, "Prefer": "return=minimal"},
json=create_payload,
timeout=timeout_sec,
)
+169 -6
View File
@@ -4,6 +4,7 @@ import hashlib
import json
import secrets
import threading
import time
from datetime import datetime, timedelta
from typing import Optional, Dict, Any, List, Set
@@ -14,6 +15,10 @@ from loguru import logger
class DBManager:
_init_lock = threading.Lock()
_initialized_paths: Set[str] = set()
_points_sync_lock = threading.Lock()
_points_sync_cache: Dict[str, Dict[str, Any]] = {}
_profile_sync_lock = threading.Lock()
_profile_sync_cache: Dict[str, Dict[str, Any]] = {}
def __init__(self, db_path: Optional[str] = None):
self.db_path = self._resolve_db_path(db_path)
@@ -67,7 +72,135 @@ class DBManager:
return ""
return f"{supabase_url}/auth/v1/admin/users"
def _sync_points_to_supabase_user_metadata(self, telegram_id: int) -> bool:
def _profile_sync_min_interval_sec(self) -> float:
raw = str(
os.getenv("POLYWEATHER_SUPABASE_PROFILE_SYNC_MIN_INTERVAL_SEC", "3600")
or ""
).strip()
try:
return max(0.0, float(raw))
except ValueError:
return 3600.0
def _profile_sync_cache_key(self, supabase_user_id: str) -> str:
endpoint = self._supabase_profiles_endpoint()
return f"{endpoint}:{str(supabase_user_id or '').strip().lower()}"
def _should_skip_profile_sync(
self,
*,
supabase_user_id: str,
telegram_id: Optional[int],
telegram_username: Optional[str],
force: bool = False,
) -> bool:
if force:
return False
min_interval = self._profile_sync_min_interval_sec()
if min_interval <= 0:
return False
payload_key = {
"telegram_id": int(telegram_id) if telegram_id is not None else None,
"telegram_username": str(telegram_username or "").strip() or None,
}
cache_key = self._profile_sync_cache_key(supabase_user_id)
now_ts = time.monotonic()
with self._profile_sync_lock:
cached = self._profile_sync_cache.get(cache_key)
if not isinstance(cached, dict):
return False
cached_payload = cached.get("payload")
cached_ts = float(cached.get("ts") or 0.0)
return cached_payload == payload_key and now_ts - cached_ts < min_interval
def _remember_profile_sync(
self,
*,
supabase_user_id: str,
telegram_id: Optional[int],
telegram_username: Optional[str],
) -> None:
cache_key = self._profile_sync_cache_key(supabase_user_id)
payload_key = {
"telegram_id": int(telegram_id) if telegram_id is not None else None,
"telegram_username": str(telegram_username or "").strip() or None,
}
with self._profile_sync_lock:
self._profile_sync_cache[cache_key] = {
"payload": payload_key,
"ts": time.monotonic(),
}
if len(self._profile_sync_cache) > 4096:
oldest_key = min(
self._profile_sync_cache,
key=lambda key: float(
self._profile_sync_cache[key].get("ts") or 0.0
),
)
self._profile_sync_cache.pop(oldest_key, None)
def _points_sync_cache_key(self, telegram_id: int) -> str:
return f"{os.path.abspath(self.db_path)}:{int(telegram_id)}"
def _points_sync_min_interval_sec(self) -> float:
raw = str(
os.getenv("POLYWEATHER_SUPABASE_POINTS_SYNC_MIN_INTERVAL_SEC", "60")
or ""
).strip()
try:
return max(0.0, float(raw))
except Exception:
return 60.0
def _should_skip_points_metadata_sync(
self,
*,
telegram_id: int,
points: int,
force: bool,
) -> bool:
if force:
return False
cache_key = self._points_sync_cache_key(telegram_id)
now_ts = time.monotonic()
min_interval = self._points_sync_min_interval_sec()
with self._points_sync_lock:
cached = self._points_sync_cache.get(cache_key)
if not cached:
return False
cached_points = int(cached.get("points") or 0)
cached_ts = float(cached.get("ts") or 0.0)
if cached_points == int(points):
return True
return min_interval > 0 and (now_ts - cached_ts) < min_interval
def _remember_points_metadata_sync(
self,
*,
telegram_id: int,
points: int,
) -> None:
cache_key = self._points_sync_cache_key(telegram_id)
with self._points_sync_lock:
self._points_sync_cache[cache_key] = {
"points": int(points),
"ts": time.monotonic(),
}
if len(self._points_sync_cache) > 4096:
oldest_key = min(
self._points_sync_cache,
key=lambda key: float(
self._points_sync_cache[key].get("ts") or 0.0
),
)
self._points_sync_cache.pop(oldest_key, None)
def _sync_points_to_supabase_user_metadata(
self,
telegram_id: int,
*,
force: bool = False,
) -> bool:
supabase_url = str(os.getenv("SUPABASE_URL") or "").strip().rstrip("/")
if not supabase_url:
return False
@@ -104,6 +237,13 @@ class DBManager:
if pts_row:
points = max(0, int(pts_row["points"] or 0))
if self._should_skip_points_metadata_sync(
telegram_id=int(telegram_id),
points=points,
force=force,
):
return False
try:
resp = requests.patch(
f"{endpoint}/{supabase_user_id}",
@@ -120,6 +260,10 @@ class DBManager:
(resp.text or "")[:200],
)
return False
self._remember_points_metadata_sync(
telegram_id=int(telegram_id),
points=points,
)
return True
except Exception as exc:
logger.warning(
@@ -136,12 +280,20 @@ class DBManager:
supabase_user_id: str,
telegram_id: Optional[int],
telegram_username: Optional[str],
force: bool = False,
) -> bool:
normalized_uid = str(supabase_user_id or "").strip().lower()
endpoint = self._supabase_profiles_endpoint()
headers = self._supabase_service_headers()
if not normalized_uid or not endpoint or not headers:
return False
if self._should_skip_profile_sync(
supabase_user_id=normalized_uid,
telegram_id=telegram_id,
telegram_username=telegram_username,
force=force,
):
return False
payload = {
"telegram_user_id": int(telegram_id) if telegram_id is not None else None,
@@ -164,6 +316,11 @@ class DBManager:
(response.text or "")[:240],
)
return False
self._remember_profile_sync(
supabase_user_id=normalized_uid,
telegram_id=telegram_id,
telegram_username=telegram_username,
)
return True
except Exception as exc:
logger.warning(
@@ -1274,7 +1431,7 @@ class DBManager:
(after, telegram_id),
)
conn.commit()
self._sync_points_to_supabase_user_metadata(telegram_id)
self._sync_points_to_supabase_user_metadata(telegram_id, force=True)
return {
"ok": True,
"telegram_id": telegram_id,
@@ -1326,7 +1483,7 @@ class DBManager:
(after, telegram_id),
)
conn.commit()
self._sync_points_to_supabase_user_metadata(telegram_id)
self._sync_points_to_supabase_user_metadata(telegram_id, force=True)
return {
"ok": True,
"telegram_id": telegram_id,
@@ -1471,6 +1628,7 @@ class DBManager:
supabase_user_id=normalized_uid,
telegram_id=int(telegram_id),
telegram_username=str((user_row["username"] if user_row else "") or "").strip(),
force=True,
)
return {"ok": True, "reason": "already_bound_same"}
@@ -1496,6 +1654,7 @@ class DBManager:
supabase_user_id=normalized_uid,
telegram_id=int(telegram_id),
telegram_username=str((user_row["username"] if user_row else "") or "").strip(),
force=True,
)
return {"ok": True, "reason": "bound"}
@@ -1551,6 +1710,7 @@ class DBManager:
supabase_user_id=user_id,
telegram_id=None,
telegram_username=None,
force=True,
)
return {"ok": True, "reason": "unbound", "previous_supabase_user_id": current_uid}
@@ -1940,7 +2100,7 @@ class DBManager:
(new_balance, telegram_id),
)
conn.commit()
self._sync_points_to_supabase_user_metadata(telegram_id)
self._sync_points_to_supabase_user_metadata(telegram_id, force=True)
return {"ok": True, "balance": new_balance, "spent": amount}
def spend_points_by_supabase_user_id(self, supabase_user_id: str, amount: int) -> Dict[str, Any]:
@@ -1978,7 +2138,7 @@ class DBManager:
(new_balance, telegram_id),
)
conn.commit()
self._sync_points_to_supabase_user_metadata(telegram_id)
self._sync_points_to_supabase_user_metadata(telegram_id, force=True)
return {"ok": True, "balance": new_balance, "spent": amount}
def set_premium(self, telegram_id: int, plan: str, months: int = 1):
@@ -2290,7 +2450,10 @@ class DBManager:
)
conn.commit()
if bonus > 0:
self._sync_points_to_supabase_user_metadata(int(telegram_id))
self._sync_points_to_supabase_user_metadata(
int(telegram_id),
force=True,
)
return True
def append_airport_obs(
+123 -123
View File
@@ -4,6 +4,7 @@ import json
import os
import secrets
import threading
import uuid
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from decimal import Decimal, InvalidOperation, ROUND_FLOOR
@@ -1307,7 +1308,7 @@ class PaymentContractCheckoutService:
"GET",
"user_wallets",
params={
"select": "chain_id,address,status,is_primary,verified_at",
"select": "chain_id,address,is_primary,verified_at",
"user_id": f"eq.{user_id}",
"chain_id": f"eq.{self.chain_id}",
"status": "eq.active",
@@ -1323,7 +1324,7 @@ class PaymentContractCheckoutService:
WalletBindingRecord(
chain_id=int(row.get("chain_id") or self.chain_id),
address=_normalize_address(row.get("address") or ""),
status=str(row.get("status") or "active"),
status="active",
is_primary=bool(row.get("is_primary")),
verified_at=row.get("verified_at"),
)
@@ -1338,7 +1339,7 @@ class PaymentContractCheckoutService:
"GET",
"user_wallets",
params={
"select": "id,user_id,address,chain_id,status,is_primary",
"select": "status",
"user_id": f"eq.{user_id}",
"chain_id": f"eq.{self.chain_id}",
"address": f"eq.{normalized}",
@@ -1381,7 +1382,7 @@ class PaymentContractCheckoutService:
"message": message,
"expires_at": _to_iso(expires),
},
prefer="return=representation",
prefer="return=minimal",
allowed_status=[201],
)
return {
@@ -1414,13 +1415,12 @@ class PaymentContractCheckoutService:
"GET",
"wallet_link_challenges",
params={
"select": "id,user_id,address,nonce,message,expires_at,consumed_at",
"select": "id,message,expires_at",
"user_id": f"eq.{user_id}",
"chain_id": f"eq.{self.chain_id}",
"address": f"eq.{normalized}",
"nonce": f"eq.{nonce_text}",
"consumed_at": "is.null",
"order": "created_at.desc",
"limit": "1",
},
allowed_status=[200],
@@ -1455,7 +1455,7 @@ class PaymentContractCheckoutService:
"GET",
"user_wallets",
params={
"select": "id,user_id,address,status,is_primary",
"select": "user_id,status",
"chain_id": f"eq.{self.chain_id}",
"address": f"eq.{normalized}",
"limit": "1",
@@ -1501,7 +1501,7 @@ class PaymentContractCheckoutService:
"verified_at": now_iso,
"updated_at": now_iso,
},
prefer="resolution=merge-duplicates,return=representation",
prefer="resolution=merge-duplicates,return=minimal",
allowed_status=[200, 201],
)
self._rest(
@@ -1509,7 +1509,7 @@ class PaymentContractCheckoutService:
"wallet_link_challenges",
params={"id": f"eq.{challenge.get('id')}"},
payload={"consumed_at": now_iso},
prefer="return=representation",
prefer="return=minimal",
allowed_status=[200],
)
return WalletBindingRecord(
@@ -1543,7 +1543,7 @@ class PaymentContractCheckoutService:
"is_primary": False,
"updated_at": now_iso,
},
prefer="return=representation",
prefer="return=minimal",
allowed_status=[200],
)
@@ -1591,7 +1591,7 @@ class PaymentContractCheckoutService:
"user_wallets",
params={"id": f"eq.{candidate_id}"},
payload={"is_primary": True, "updated_at": now_iso},
prefer="return=representation",
prefer="return=minimal",
allowed_status=[200],
)
new_primary = candidate_addr
@@ -1780,32 +1780,32 @@ class PaymentContractCheckoutService:
order_id_hex = "0x" + secrets.token_hex(32)
now = _now_utc()
expires_at = now + timedelta(seconds=self.intent_ttl_sec)
rows = self._rest(
intent_payload = {
"id": str(uuid.uuid4()),
"user_id": user_id,
"plan_code": plan["plan_code"],
"plan_id": plan["plan_id"],
"chain_id": selected_chain_id,
"token_address": selected_token.address,
"receiver_address": receiver_address,
"amount_units": str(amount_units),
"payment_mode": mode,
"allowed_wallet": target_wallet or None,
"order_id_hex": order_id_hex,
"status": "created",
"expires_at": _to_iso(expires_at),
"metadata": combined_metadata,
"created_at": _to_iso(now),
"updated_at": _to_iso(now),
}
self._rest(
"POST",
"payment_intents",
payload={
"user_id": user_id,
"plan_code": plan["plan_code"],
"plan_id": plan["plan_id"],
"chain_id": selected_chain_id,
"token_address": selected_token.address,
"receiver_address": receiver_address,
"amount_units": str(amount_units),
"payment_mode": mode,
"allowed_wallet": target_wallet or None,
"order_id_hex": order_id_hex,
"status": "created",
"expires_at": _to_iso(expires_at),
"metadata": combined_metadata,
"created_at": _to_iso(now),
"updated_at": _to_iso(now),
},
prefer="return=representation",
payload=intent_payload,
prefer="return=minimal",
allowed_status=[201],
)
if not isinstance(rows, list) or not rows:
raise PaymentCheckoutError(500, "failed to create payment intent")
intent = self._serialize_intent(rows[0])
intent = self._serialize_intent(intent_payload)
response = {
"intent": intent.__dict__,
"tx_payload": None if mode == "direct" else self._build_tx_payload(intent),
@@ -1885,7 +1885,7 @@ class PaymentContractCheckoutService:
"GET",
"payment_intents",
params={
"select": "id,user_id,tx_hash,status,updated_at,created_at,chain_id",
"select": "id,user_id,tx_hash,chain_id",
"status": "eq.submitted",
"tx_hash": "not.is.null",
"order": "updated_at.asc",
@@ -1911,9 +1911,6 @@ class PaymentContractCheckoutService:
"user_id": user_id,
"tx_hash": tx_hash,
"chain_id": int(row.get("chain_id") or self.chain_id),
"status": str(row.get("status") or ""),
"updated_at": row.get("updated_at"),
"created_at": row.get("created_at"),
}
)
return out
@@ -1937,8 +1934,7 @@ class PaymentContractCheckoutService:
"payment_intents",
params={
"select": (
"id,user_id,status,tx_hash,order_id_hex,plan_id,token_address,"
"amount_units,payment_mode,allowed_wallet,chain_id,created_at,updated_at"
"id,user_id,status,tx_hash,plan_id,token_address,amount_units"
),
"order_id_hex": f"eq.{normalized_order}",
"status": "in.(created,submitted,confirmed)",
@@ -1965,15 +1961,9 @@ class PaymentContractCheckoutService:
"user_id": user_id,
"status": status,
"tx_hash": str(row.get("tx_hash") or "").strip().lower(),
"order_id_hex": str(row.get("order_id_hex") or "").strip().lower(),
"plan_id": int(row.get("plan_id") or 0),
"chain_id": int(row.get("chain_id") or self.chain_id),
"token_address": _normalize_address(row.get("token_address")),
"amount_units": int(row.get("amount_units") or 0),
"payment_mode": str(row.get("payment_mode") or "").strip().lower(),
"allowed_wallet": _normalize_address(row.get("allowed_wallet")),
"created_at": row.get("created_at"),
"updated_at": row.get("updated_at"),
}
)
return out
@@ -1986,7 +1976,7 @@ class PaymentContractCheckoutService:
"GET",
"payment_transactions",
params={
"select": "intent_id,tx_hash,status",
"select": "intent_id",
"tx_hash": f"eq.{tx_hash_text}",
"limit": "5",
},
@@ -2018,7 +2008,7 @@ class PaymentContractCheckoutService:
return {}
now_iso = _to_iso(_now_utc())
try:
rows = self._rest(
self._rest(
"POST",
"payment_transactions",
params={"on_conflict": "tx_hash"},
@@ -2040,10 +2030,10 @@ class PaymentContractCheckoutService:
},
"updated_at": now_iso,
},
prefer="resolution=merge-duplicates,return=representation",
prefer="resolution=merge-duplicates,return=minimal",
allowed_status=[200, 201],
)
return rows[0] if isinstance(rows, list) and rows else {}
return {}
except Exception:
return {}
@@ -2060,6 +2050,13 @@ class PaymentContractCheckoutService:
"""
self._ensure_enabled()
intent = self.get_intent(user_id, intent_id)
return self._validate_loaded_intent_tx(intent, tx_hash)
def _validate_loaded_intent_tx(
self,
intent: PaymentIntentRecord,
tx_hash: str,
) -> Dict[str, Any]:
tx_hash_text = str(tx_hash or "").strip().lower()
if not (tx_hash_text.startswith("0x") and len(tx_hash_text) == 66):
return {
@@ -2266,7 +2263,7 @@ class PaymentContractCheckoutService:
"payment_intents",
params={"id": f"eq.{intent.intent_id}", "user_id": f"eq.{user_id}"},
payload={"status": "expired", "updated_at": _to_iso(now)},
prefer="return=representation",
prefer="return=minimal",
allowed_status=[200],
)
raise PaymentCheckoutError(409, "payment intent expired")
@@ -2283,7 +2280,7 @@ class PaymentContractCheckoutService:
self._require_user_wallet(user_id, from_addr)
try:
validation = self.validate_intent_tx(user_id, intent.intent_id, tx_hash_text)
validation = self._validate_loaded_intent_tx(intent, tx_hash_text)
except Exception as exc:
raise PaymentCheckoutError(
400,
@@ -2305,26 +2302,25 @@ class PaymentContractCheckoutService:
"tx_hash": tx_hash_text,
"updated_at": now_iso,
},
prefer="return=representation",
prefer="return=minimal",
allowed_status=[200],
)
tx_rows = self._rest(
tx_payload = {
"intent_id": intent.intent_id,
"chain_id": int(intent.chain_id),
"tx_hash": tx_hash_text,
"from_address": from_addr,
"to_address": intent.receiver_address,
"payment_method": "direct" if intent.payment_mode == "direct" else "wallet",
"status": "submitted",
"updated_at": now_iso,
}
self._rest(
"POST",
"payment_transactions",
params={"on_conflict": "tx_hash"},
payload={
"intent_id": intent.intent_id,
"chain_id": int(intent.chain_id),
"tx_hash": tx_hash_text,
"from_address": from_addr,
"to_address": intent.receiver_address,
"payment_method": "direct"
if intent.payment_mode == "direct"
else "wallet",
"status": "submitted",
"updated_at": now_iso,
},
prefer="resolution=merge-duplicates,return=representation",
payload=tx_payload,
prefer="resolution=merge-duplicates,return=minimal",
allowed_status=[200, 201],
)
return {
@@ -2332,9 +2328,7 @@ class PaymentContractCheckoutService:
"status": "submitted",
"tx_hash": tx_hash_text,
"from_address": from_addr,
"transaction": tx_rows[0]
if isinstance(tx_rows, list) and tx_rows
else None,
"transaction": tx_payload,
}
def _wait_receipt(self, tx_hash: str, chain_id: Optional[int] = None) -> Any:
@@ -2466,24 +2460,25 @@ class PaymentContractCheckoutService:
token_decimals = self._token_decimals_for(token_address, payment_chain_id)
amount_dec = _units_to_decimal(amount_units, token_decimals)
currency = self._token_symbol_for(token_address, payment_chain_id)
rows = self._rest(
payment_payload = {
"user_id": user_id,
"amount": str(amount_dec),
"currency": currency,
"chain": self._chain_label_for(payment_chain_id),
"tx_hash": tx_hash,
"status": "confirmed",
"raw_payload": payload,
"updated_at": _to_iso(_now_utc()),
}
self._rest(
"POST",
"payments",
params={"on_conflict": "tx_hash"},
payload={
"user_id": user_id,
"amount": str(amount_dec),
"currency": currency,
"chain": self._chain_label_for(payment_chain_id),
"tx_hash": tx_hash,
"status": "confirmed",
"raw_payload": payload,
"updated_at": _to_iso(_now_utc()),
},
prefer="resolution=merge-duplicates,return=representation",
payload=payment_payload,
prefer="resolution=merge-duplicates,return=minimal",
allowed_status=[200, 201],
)
return rows[0] if isinstance(rows, list) and rows else {}
return payment_payload
def _grant_subscription(
self,
@@ -2498,7 +2493,7 @@ class PaymentContractCheckoutService:
"GET",
"subscriptions",
params={
"select": "id,expires_at,status,plan_code,source,starts_at",
"select": "starts_at,expires_at",
"user_id": f"eq.{user_id}",
"status": "eq.active",
"order": "expires_at.desc",
@@ -2539,20 +2534,21 @@ class PaymentContractCheckoutService:
except Exception:
pass
expires = starts + timedelta(days=max(1, duration_days))
sub_rows = self._rest(
subscription_payload = {
"user_id": user_id,
"plan_code": plan_code,
"status": "active",
"starts_at": _to_iso(starts),
"expires_at": _to_iso(expires),
"source": "payment_contract",
"created_at": _to_iso(now),
"updated_at": _to_iso(now),
}
self._rest(
"POST",
"subscriptions",
payload={
"user_id": user_id,
"plan_code": plan_code,
"status": "active",
"starts_at": _to_iso(starts),
"expires_at": _to_iso(expires),
"source": "payment_contract",
"created_at": _to_iso(now),
"updated_at": _to_iso(now),
},
prefer="return=representation",
payload=subscription_payload,
prefer="return=minimal",
allowed_status=[201],
)
self._rest(
@@ -2566,11 +2562,11 @@ class PaymentContractCheckoutService:
"payload": {"tx_hash": tx_hash, **payload},
"created_at": _to_iso(now),
},
prefer="return=representation",
prefer="return=minimal",
allowed_status=[201],
)
SUPABASE_ENTITLEMENT.invalidate_subscription_cache(user_id)
return sub_rows[0] if isinstance(sub_rows, list) and sub_rows else {}
return subscription_payload
def _ensure_confirmed_subscription(
self,
@@ -2578,7 +2574,6 @@ class PaymentContractCheckoutService:
intent: PaymentIntentRecord,
tx_hash: str,
) -> Optional[Dict[str, Any]]:
SUPABASE_ENTITLEMENT.invalidate_subscription_cache(user_id)
latest_subscription = SUPABASE_ENTITLEMENT.get_latest_active_subscription(
user_id,
respect_requirement=False,
@@ -2696,7 +2691,7 @@ class PaymentContractCheckoutService:
"metadata": metadata,
"updated_at": now_iso,
},
prefer="return=representation",
prefer="return=minimal",
allowed_status=[200],
)
if tx_hash:
@@ -2713,7 +2708,7 @@ class PaymentContractCheckoutService:
"status": "failed",
"updated_at": now_iso,
},
prefer="resolution=merge-duplicates,return=representation",
prefer="resolution=merge-duplicates,return=minimal",
allowed_status=[200, 201],
)
self._db.append_payment_audit_event(
@@ -2931,6 +2926,7 @@ class PaymentContractCheckoutService:
"PATCH",
"payment_intents",
params={
"select": "id",
"id": f"eq.{intent.intent_id}",
"user_id": f"eq.{user_id}",
"status": "in.(created,submitted,failed)",
@@ -2972,24 +2968,25 @@ class PaymentContractCheckoutService:
raise PaymentCheckoutError(
409, f"intent status is {refreshed.status}, cannot confirm"
)
tx_rows = self._rest(
tx_payload = {
"intent_id": intent.intent_id,
"tx_hash": tx_hash_text,
"chain_id": int(intent.chain_id),
"from_address": tx_from,
"to_address": tx_to,
"block_number": block_number,
"payment_method": "direct" if is_direct else "wallet",
"status": "confirmed",
"raw_receipt": json.loads(Web3.to_json(receipt)),
"raw_tx": json.loads(Web3.to_json(tx)) if tx is not None else None,
"updated_at": now_iso,
}
self._rest(
"POST",
"payment_transactions",
params={"on_conflict": "tx_hash"},
payload={
"intent_id": intent.intent_id,
"tx_hash": tx_hash_text,
"chain_id": int(intent.chain_id),
"from_address": tx_from,
"to_address": tx_to,
"block_number": block_number,
"payment_method": "direct" if is_direct else "wallet",
"status": "confirmed",
"raw_receipt": json.loads(Web3.to_json(receipt)),
"raw_tx": json.loads(Web3.to_json(tx)) if tx is not None else None,
"updated_at": now_iso,
},
prefer="resolution=merge-duplicates,return=representation",
payload=tx_payload,
prefer="resolution=merge-duplicates,return=minimal",
allowed_status=[200, 201],
)
payload = {
@@ -3036,12 +3033,17 @@ class PaymentContractCheckoutService:
amount_usdc=intent.amount_usdc,
tx_hash=tx_hash_text,
)
refreshed = self.get_intent(user_id, intent.intent_id)
refreshed = PaymentIntentRecord(
**{
**intent.__dict__,
"status": "confirmed",
"tx_hash": tx_hash_text,
"metadata": confirmed_metadata,
}
)
return {
"intent": refreshed.__dict__,
"transaction": tx_rows[0]
if isinstance(tx_rows, list) and tx_rows
else None,
"transaction": tx_payload,
"payment": payment_row,
"subscription": subscription_row,
"points_redemption": points_result,
@@ -3091,11 +3093,10 @@ class PaymentContractCheckoutService:
repaired = self._ensure_confirm_side_effects(
user_id, intent, tx_hash_text
)
refreshed = self.get_intent(user_id, intent.intent_id)
return {
"ok": True,
"action": "reconciled_confirmed_intent",
"intent": refreshed.__dict__,
"intent": intent.__dict__,
"payment": repaired.get("payment"),
"subscription": repaired.get("subscription"),
}
@@ -3109,7 +3110,6 @@ class PaymentContractCheckoutService:
}
)
SUPABASE_ENTITLEMENT.invalidate_subscription_cache(user_id)
latest_subscription = SUPABASE_ENTITLEMENT.get_latest_active_subscription(
user_id,
respect_requirement=False,
@@ -3128,7 +3128,7 @@ class PaymentContractCheckoutService:
"GET",
"payment_intents",
params={
"select": "id,user_id,status,updated_at",
"select": "user_id",
"status": "in.(submitted,confirmed)",
"order": "updated_at.desc",
"limit": str(safe_limit),