Skip to content

Commit 35f3d39

Browse files
committed
feat: backfill preauth requests for existing auto-enter users on startup (temporary)
1 parent b2c36db commit 35f3d39

3 files changed

Lines changed: 78 additions & 1 deletion

File tree

lucky_pot.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from luckypot import db
66
from luckypot.config import settings
77
import stackcoin
8-
from luckypot.game import on_request_accepted, on_request_denied
8+
from luckypot.game import on_request_accepted, on_request_denied, backfill_preauths_for_auto_enter_users
99
from luckypot import stk
1010
from luckypot.stk import get_client as get_stk_client
1111
from luckypot.discord.bot import (
@@ -87,6 +87,14 @@ async def handle_denied(event: stackcoin.RequestDeniedEvent):
8787
background_tasks.append(gateway_task)
8888
logger.info("StackCoin gateway started")
8989

90+
# TEMPORARY: backfill preauths for existing auto-enter users.
91+
# Remove once all users have been sent preauth requests.
92+
backfill_task = asyncio.create_task(
93+
backfill_preauths_for_auto_enter_users(),
94+
name="preauth-backfill",
95+
)
96+
backfill_task.add_done_callback(_task_done_callback)
97+
9098
draw_task = asyncio.create_task(
9199
run_daily_draw_loop(
92100
announce=announce,

luckypot/db.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ def get_auto_enter_users(conn, guild_id: str) -> list[str]:
320320
return [row["discord_id"] for row in cursor.fetchall()]
321321

322322

323+
def get_all_auto_enter_users(conn) -> list[dict]:
324+
"""Return all auto-enter users across all guilds."""
325+
cursor = conn.execute("SELECT discord_id, guild_id FROM auto_enter_users")
326+
return [{"discord_id": row["discord_id"], "guild_id": row["guild_id"]} for row in cursor.fetchall()]
327+
328+
323329
def get_auto_enter_status(conn, discord_id: str, guild_id: str) -> bool:
324330
"""Return True if the user is opted in to auto-enter for this guild."""
325331
cursor = conn.execute(

luckypot/game.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,69 @@ async def daily_pot_draw(
470470
conn.close()
471471

472472

473+
# ── TEMPORARY: preauth migration for existing auto-enter users ──────────
474+
# Remove this function and its call in lucky_pot.py once all existing
475+
# auto-enter users have been sent preauth requests.
476+
477+
478+
async def backfill_preauths_for_auto_enter_users() -> None:
479+
"""Request preauths for existing auto-enter users who don't have one yet.
480+
481+
TEMPORARY — safe to remove after rollout once preauths have been sent.
482+
"""
483+
conn = db.get_connection()
484+
try:
485+
all_users = db.get_all_auto_enter_users(conn)
486+
finally:
487+
conn.close()
488+
489+
if not all_users:
490+
logger.info("Preauth backfill: no auto-enter users found")
491+
return
492+
493+
# Deduplicate by discord_id (a user may be auto-entered in multiple guilds,
494+
# but preauths are per-user not per-guild)
495+
seen: set[str] = set()
496+
unique_users: list[str] = []
497+
for entry in all_users:
498+
if entry["discord_id"] not in seen:
499+
seen.add(entry["discord_id"])
500+
unique_users.append(entry["discord_id"])
501+
502+
logger.info(f"Preauth backfill: checking {len(unique_users)} auto-enter user(s)")
503+
requested = 0
504+
505+
for discord_id in unique_users:
506+
try:
507+
stk_user = await stk.get_user_by_discord_id(discord_id)
508+
if stk_user is None:
509+
continue
510+
511+
preauths = await stk.get_preauths(user_id=stk_user["id"])
512+
has_active_or_pending = any(
513+
p.get("status") in ("active", "pending") for p in preauths
514+
)
515+
if has_active_or_pending:
516+
continue
517+
518+
result = await stk.create_preauth(
519+
user_id=stk_user["id"],
520+
max_amount=10,
521+
window_hours=24,
522+
)
523+
if result:
524+
requested += 1
525+
logger.info(
526+
f"Preauth backfill: requested preauth for discord_id={discord_id} (user {stk_user['id']})"
527+
)
528+
except Exception:
529+
logger.exception(
530+
f"Preauth backfill: failed for discord_id={discord_id}"
531+
)
532+
533+
logger.info(f"Preauth backfill: done, requested {requested} new preauth(s)")
534+
535+
473536
async def on_request_accepted(
474537
event_data: RequestAcceptedData, announce: RawAnnounceFn = None
475538
):

0 commit comments

Comments
 (0)