Spaces:
Running on Zero
Running on Zero
Commit ·
50058db
1
Parent(s): a14f7f7
english only, full manual, announce
Browse files- app/bot/handlers/admin.py +8 -3
- app/bot/handlers/gate.py +2 -4
- app/bot/middlewares/gate.py +3 -1
- app/db/migrate.py +1 -0
- app/services/onboarding.py +20 -0
- tests/test_logic.py +10 -0
app/bot/handlers/admin.py
CHANGED
|
@@ -244,10 +244,15 @@ async def growth_report(message: Message, session: AsyncSession) -> None:
|
|
| 244 |
"📈 <b>Growth</b>",
|
| 245 |
f"Total users: <b>{r['total']}</b> · groups using the bot: <b>{r['groups']}</b>",
|
| 246 |
f"New users per day (7 days): {days}",
|
| 247 |
-
"\n<b>Where they come from</b>
|
| 248 |
]
|
| 249 |
for source, joined, gate_ok, onboarded, active in r["by_source"]:
|
| 250 |
-
lines.append(f"• {source}: {joined} → {gate_ok} → {onboarded} → {active}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
lines.append("\nMake a new trackable link by putting a tag after ?start= , e.g. t.me/YourBot?start=dorm5")
|
| 252 |
await message.answer("\n".join(lines))
|
| 253 |
|
|
@@ -362,7 +367,7 @@ async def stats(message: Message, session: AsyncSession) -> None:
|
|
| 362 |
s = await stats_repo.overview(session, clock.now_et().date())
|
| 363 |
await message.answer(
|
| 364 |
"📊 <b>Stats</b>\n"
|
| 365 |
-
f"Users: <b>{s['users']}</b> (
|
| 366 |
f"Active today: <b>{s['active_today']}</b>\n"
|
| 367 |
f"Referrals: <b>{s['referrals']}</b>\n"
|
| 368 |
f"Student files waiting: <b>{await sub_repo.pending_total(session)}</b>\n"
|
|
|
|
| 244 |
"📈 <b>Growth</b>",
|
| 245 |
f"Total users: <b>{r['total']}</b> · groups using the bot: <b>{r['groups']}</b>",
|
| 246 |
f"New users per day (7 days): {days}",
|
| 247 |
+
"\n<b>Where they come from</b>\njoined → in the channel → finished setup → used the bot on 3+ days",
|
| 248 |
]
|
| 249 |
for source, joined, gate_ok, onboarded, active in r["by_source"]:
|
| 250 |
+
lines.append(f"• <b>{source}</b>: {joined} → {gate_ok} → {onboarded} → {active} ({round(100 * onboarded / joined)}% finished setup)")
|
| 251 |
+
lines.append(
|
| 252 |
+
"\n<b>Sources:</b> organic = found the bot by name or search; link = a shared course/file link; referral = a "
|
| 253 |
+
"friend's invite; poster / channel / group / any tag = links you made; before tracking = students from before "
|
| 254 |
+
"sources were recorded."
|
| 255 |
+
)
|
| 256 |
lines.append("\nMake a new trackable link by putting a tag after ?start= , e.g. t.me/YourBot?start=dorm5")
|
| 257 |
await message.answer("\n".join(lines))
|
| 258 |
|
|
|
|
| 367 |
s = await stats_repo.overview(session, clock.now_et().date())
|
| 368 |
await message.answer(
|
| 369 |
"📊 <b>Stats</b>\n"
|
| 370 |
+
f"Users: <b>{s['users']}</b> (verified members {s['gate_passed']}, finished setup {s['onboarded']})\n"
|
| 371 |
f"Active today: <b>{s['active_today']}</b>\n"
|
| 372 |
f"Referrals: <b>{s['referrals']}</b>\n"
|
| 373 |
f"Student files waiting: <b>{await sub_repo.pending_total(session)}</b>\n"
|
app/bot/handlers/gate.py
CHANGED
|
@@ -8,7 +8,7 @@ from app.core.aio import gather_safe
|
|
| 8 |
from app.core.i18n import t
|
| 9 |
from app.db.models import User
|
| 10 |
from app.services import gate
|
| 11 |
-
from app.services import
|
| 12 |
|
| 13 |
router = Router()
|
| 14 |
|
|
@@ -19,9 +19,7 @@ async def gate_check(call: CallbackQuery, user: User, session: AsyncSession) ->
|
|
| 19 |
if left:
|
| 20 |
await call.answer(t(user.lang, "gate_not_yet_list", list=", ".join(left)), show_alert=True)
|
| 21 |
return
|
| 22 |
-
|
| 23 |
-
user.gate_passed = True
|
| 24 |
-
await referral_service.reward_if_eligible(session, call.bot, user)
|
| 25 |
if call.message:
|
| 26 |
await gather_safe(call.answer(), call.message.delete(), next_step(call.message, user))
|
| 27 |
if user.year is not None:
|
|
|
|
| 8 |
from app.core.i18n import t
|
| 9 |
from app.db.models import User
|
| 10 |
from app.services import gate
|
| 11 |
+
from app.services import onboarding
|
| 12 |
|
| 13 |
router = Router()
|
| 14 |
|
|
|
|
| 19 |
if left:
|
| 20 |
await call.answer(t(user.lang, "gate_not_yet_list", list=", ".join(left)), show_alert=True)
|
| 21 |
return
|
| 22 |
+
await onboarding.mark_passed(session, call.bot, user)
|
|
|
|
|
|
|
| 23 |
if call.message:
|
| 24 |
await gather_safe(call.answer(), call.message.delete(), next_step(call.message, user))
|
| 25 |
if user.year is not None:
|
app/bot/middlewares/gate.py
CHANGED
|
@@ -6,7 +6,7 @@ from aiogram.types import CallbackQuery, Message, TelegramObject
|
|
| 6 |
from app.bot.keyboards.common import gate_kb
|
| 7 |
from app.config import get_settings
|
| 8 |
from app.core.i18n import t
|
| 9 |
-
from app.services import gate, growth
|
| 10 |
|
| 11 |
|
| 12 |
class GateMiddleware(BaseMiddleware):
|
|
@@ -21,6 +21,8 @@ class GateMiddleware(BaseMiddleware):
|
|
| 21 |
is_start = isinstance(event, Message) and (event.text or "").startswith("/start")
|
| 22 |
left = await gate.missing(data["bot"], tg.id, fresh=is_start) # /start always re-verifies
|
| 23 |
if not left:
|
|
|
|
|
|
|
| 24 |
return await handler(event, data)
|
| 25 |
|
| 26 |
if is_start: # keep the link they came with, open it right after they join
|
|
|
|
| 6 |
from app.bot.keyboards.common import gate_kb
|
| 7 |
from app.config import get_settings
|
| 8 |
from app.core.i18n import t
|
| 9 |
+
from app.services import gate, growth, onboarding
|
| 10 |
|
| 11 |
|
| 12 |
class GateMiddleware(BaseMiddleware):
|
|
|
|
| 21 |
is_start = isinstance(event, Message) and (event.text or "").startswith("/start")
|
| 22 |
left = await gate.missing(data["bot"], tg.id, fresh=is_start) # /start always re-verifies
|
| 23 |
if not left:
|
| 24 |
+
if not user.gate_passed:
|
| 25 |
+
await onboarding.mark_passed(data["session"], data["bot"], user)
|
| 26 |
return await handler(event, data)
|
| 27 |
|
| 28 |
if is_start: # keep the link they came with, open it right after they join
|
app/db/migrate.py
CHANGED
|
@@ -41,6 +41,7 @@ POSTGRES_UPGRADES = [
|
|
| 41 |
"ALTER TABLE users ADD COLUMN IF NOT EXISTS share_prompt_day VARCHAR(10)",
|
| 42 |
"ALTER TABLE users ADD COLUMN IF NOT EXISTS credit_name BOOLEAN NOT NULL DEFAULT FALSE",
|
| 43 |
"ALTER TABLE materials ADD COLUMN IF NOT EXISTS credit VARCHAR(40)",
|
|
|
|
| 44 |
"ALTER TABLE referrals ADD COLUMN IF NOT EXISTS qualified BOOLEAN NOT NULL DEFAULT FALSE",
|
| 45 |
]
|
| 46 |
|
|
|
|
| 41 |
"ALTER TABLE users ADD COLUMN IF NOT EXISTS share_prompt_day VARCHAR(10)",
|
| 42 |
"ALTER TABLE users ADD COLUMN IF NOT EXISTS credit_name BOOLEAN NOT NULL DEFAULT FALSE",
|
| 43 |
"ALTER TABLE materials ADD COLUMN IF NOT EXISTS credit VARCHAR(40)",
|
| 44 |
+
"UPDATE users SET gate_passed = TRUE WHERE gate_passed = FALSE AND year IS NOT NULL",
|
| 45 |
"ALTER TABLE referrals ADD COLUMN IF NOT EXISTS qualified BOOLEAN NOT NULL DEFAULT FALSE",
|
| 46 |
]
|
| 47 |
|
app/services/onboarding.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""The moment a student is first seen inside the required channels."""
|
| 2 |
+
from aiogram import Bot
|
| 3 |
+
from sqlalchemy.ext.asyncio import AsyncSession
|
| 4 |
+
|
| 5 |
+
from app.db.models import User
|
| 6 |
+
from app.services import referrals
|
| 7 |
+
from app.services import xp as xp_service
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
async def mark_passed(session: AsyncSession, bot: Bot, user: User) -> bool:
|
| 11 |
+
"""Called for EVERY student who is verified as a member, whether they tapped "I joined" or were
|
| 12 |
+
already a member and never saw the gate. Turns on XP/streaks and pays the inviter's join bonus.
|
| 13 |
+
(Before this existed only people who tapped the button were counted; everybody who was already in
|
| 14 |
+
the channel earned nothing, never became "active" and could not contribute or win.)"""
|
| 15 |
+
if user.gate_passed:
|
| 16 |
+
return False
|
| 17 |
+
user.gate_passed = True
|
| 18 |
+
await referrals.reward_if_eligible(session, bot, user)
|
| 19 |
+
await xp_service.on_activity(session, user, bot)
|
| 20 |
+
return True
|
tests/test_logic.py
CHANGED
|
@@ -500,6 +500,16 @@ def test_qr_png_really_scans():
|
|
| 500 |
assert text == link, (transparent, text)
|
| 501 |
|
| 502 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
if __name__ == "__main__":
|
| 504 |
for name, fn in list(globals().items()):
|
| 505 |
if name.startswith("test_"):
|
|
|
|
| 500 |
assert text == link, (transparent, text)
|
| 501 |
|
| 502 |
|
| 503 |
+
def test_members_who_never_saw_the_gate_still_count():
|
| 504 |
+
"""Regression: gate_passed used to be set only by the 'I joined' button, so students who were already
|
| 505 |
+
channel members got no XP, no streak and no referral credit."""
|
| 506 |
+
root = Path(__file__).resolve().parents[1]
|
| 507 |
+
middleware = (root / "app/bot/middlewares/gate.py").read_text(encoding="utf-8")
|
| 508 |
+
assert "onboarding.mark_passed" in middleware
|
| 509 |
+
assert "onboarding.mark_passed" in (root / "app/bot/handlers/gate.py").read_text(encoding="utf-8")
|
| 510 |
+
assert "SET gate_passed = TRUE" in (root / "app/db/migrate.py").read_text(encoding="utf-8")
|
| 511 |
+
|
| 512 |
+
|
| 513 |
if __name__ == "__main__":
|
| 514 |
for name, fn in list(globals().items()):
|
| 515 |
if name.startswith("test_"):
|