Added check for referral user, small refactoring
With regular expressions we can be sure that the data is already valid
This commit is contained in:
+13
-23
@@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
from aiogram import Router, F, types, Bot
|
from aiogram import Router, F, types, Bot
|
||||||
from aiogram.utils.text_decorations import html_decoration as hd
|
from aiogram.utils.text_decorations import html_decoration as hd
|
||||||
from aiogram.filters import CommandStart, Command
|
from aiogram.filters import CommandStart, Command
|
||||||
@@ -112,13 +113,16 @@ async def send_main_menu(target_event: Union[types.Message,
|
|||||||
|
|
||||||
|
|
||||||
@router.message(CommandStart())
|
@router.message(CommandStart())
|
||||||
|
@router.message(CommandStart(magic=F.args.regexp(r"^ref_(\d+)$").as_("ref_match")))
|
||||||
|
@router.message(CommandStart(magic=F.args.regexp(r"^promo_(\w+)$").as_("promo_match")))
|
||||||
async def start_command_handler(message: types.Message,
|
async def start_command_handler(message: types.Message,
|
||||||
state: FSMContext,
|
state: FSMContext,
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
i18n_data: dict,
|
i18n_data: dict,
|
||||||
subscription_service: SubscriptionService,
|
subscription_service: SubscriptionService,
|
||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
command: Optional[CommandStart] = None):
|
ref_match: Optional[re.Match] = None,
|
||||||
|
promo_match: Optional[re.Match] = None):
|
||||||
await state.clear()
|
await state.clear()
|
||||||
current_lang = i18n_data.get("current_language", settings.DEFAULT_LANGUAGE)
|
current_lang = i18n_data.get("current_language", settings.DEFAULT_LANGUAGE)
|
||||||
i18n: Optional[JsonI18n] = i18n_data.get("i18n_instance")
|
i18n: Optional[JsonI18n] = i18n_data.get("i18n_instance")
|
||||||
@@ -130,28 +134,14 @@ async def start_command_handler(message: types.Message,
|
|||||||
|
|
||||||
referred_by_user_id: Optional[int] = None
|
referred_by_user_id: Optional[int] = None
|
||||||
promo_code_to_apply: Optional[str] = None
|
promo_code_to_apply: Optional[str] = None
|
||||||
|
|
||||||
if command and command.args:
|
if ref_match:
|
||||||
arg_payload = command.args
|
potential_referrer_id = int(ref_match.group(1))
|
||||||
if arg_payload.startswith("ref_"):
|
if await user_dal.get_user_by_id(session, potential_referrer_id):
|
||||||
try:
|
referred_by_user_id = potential_referrer_id
|
||||||
potential_referrer_id_str = arg_payload.split("_")[1]
|
elif promo_match:
|
||||||
if potential_referrer_id_str.isdigit():
|
promo_code_to_apply = promo_match.group(1)
|
||||||
potential_referrer_id = int(potential_referrer_id_str)
|
logging.info(f"User {user_id} started with promo code: {promo_code_to_apply}")
|
||||||
if potential_referrer_id != user_id:
|
|
||||||
referred_by_user_id = potential_referrer_id
|
|
||||||
except (IndexError, ValueError) as e:
|
|
||||||
logging.warning(
|
|
||||||
f"Could not parse referral from /start args '{arg_payload}': {e}"
|
|
||||||
)
|
|
||||||
elif arg_payload.startswith("promo_"):
|
|
||||||
try:
|
|
||||||
promo_code_to_apply = arg_payload.split("_")[1]
|
|
||||||
logging.info(f"User {user_id} started with promo code: {promo_code_to_apply}")
|
|
||||||
except (IndexError, ValueError) as e:
|
|
||||||
logging.warning(
|
|
||||||
f"Could not parse promo code from /start args '{arg_payload}': {e}"
|
|
||||||
)
|
|
||||||
|
|
||||||
db_user = await user_dal.get_user_by_id(session, user_id)
|
db_user = await user_dal.get_user_by_id(session, user_id)
|
||||||
if not db_user:
|
if not db_user:
|
||||||
|
|||||||
Reference in New Issue
Block a user