подписки сразу отслеживаются

This commit is contained in:
Artem Tsyrulnikov
2026-01-28 11:42:09 +03:00
parent 2d35588a85
commit 0b25566ffb
9 changed files with 205 additions and 47 deletions

View File

@@ -17,13 +17,9 @@ async def handle_subscription(
first_name: str | None = None,
last_name: str | None = None,
) -> None:
placement_post = await self.database.get_placement_post_by_invite_link(invite_link)
if not placement_post:
log.warning('PlacementPost not found for invite_link: %s', invite_link)
return
if not placement_post.placement:
log.error('PlacementPost %s has no placement', placement_post.id)
placement = await self.database.get_placement_by_invite_link(invite_link)
if not placement:
log.warning('Placement not found for invite_link: %s', invite_link)
return
subscriber = await self.database.get_telegram_user(telegram_id=user_telegram_id)
@@ -42,27 +38,27 @@ async def handle_subscription(
await self.database.update_telegram_user(subscriber)
active_subscription = await self.database.get_active_subscription_by_subscriber_and_project(
subscriber.id, placement_post.placement.project_id
subscriber.id, placement.project_id
)
if active_subscription:
# Пользователь уже подписан на канал через другую публикацию
# Пользователь уже подписан на канал через другой placement
# Это не должно случиться (Telegram не даст подписаться дважды),
# но если случилось - логируем и игнорируем
log.warning(
'User %s (telegram_id: %s) already has active subscription to channel %s via placement_post %s, '
'ignoring new subscription attempt via placement_post %s',
'User %s (telegram_id: %s) already has active subscription to channel %s via placement %s, '
'ignoring new subscription attempt via placement %s',
subscriber.id,
user_telegram_id,
placement_post.placement.project_id,
active_subscription.placement_post_id,
placement_post.id,
placement.project_id,
active_subscription.placement_id,
placement.id,
)
return
# Проверяем, была ли раньше подписка через ЭТУ публикацию (для реактивации)
existing_sub = await self.database.get_subscription_by_subscriber_and_placement_post(
subscriber.id, placement_post.id
# Проверяем, была ли раньше подписка через ЭТОТ placement (для реактивации)
existing_sub = await self.database.get_subscription_by_subscriber_and_placement(
subscriber.id, placement.id
)
if existing_sub and existing_sub.status == domain.SubscriptionStatus.UNSUBSCRIBED:
@@ -71,24 +67,24 @@ async def handle_subscription(
await self.database.update_subscription(existing_sub)
log.info(
'Subscription reactivated: subscriber %s (telegram_id: %s) resubscribed via same placement_post %s',
'Subscription reactivated: subscriber %s (telegram_id: %s) resubscribed via same placement %s',
subscriber.id,
user_telegram_id,
placement_post.id,
placement.id,
)
return
subscription = domain.Subscription(
placement_post_id=placement_post.id,
placement_id=placement.id,
telegram_user_id=subscriber.id,
invite_link=invite_link,
)
await self.database.create_subscription(subscription)
log.info(
'Subscription created: subscriber %s (telegram_id: %s) subscribed via placement_post %s (invite_link: %s)',
'Subscription created: subscriber %s (telegram_id: %s) subscribed via placement %s (invite_link: %s)',
subscriber.id,
user_telegram_id,
placement_post.id,
placement.id,
invite_link,
)