feat: миграция с булевых переменных на status для target_channel

This commit is contained in:
Artem Tsyrulnikov
2025-12-01 13:34:26 +03:00
parent f7754b707d
commit d117776e65
16 changed files with 56 additions and 67 deletions

View File

@@ -10,7 +10,7 @@ from src import domain
class Postgres(DatabaseBase):
async def get_user(self, *, user_id: uuid.UUID | None = None, telegram_id: int | None = None) -> domain.User | None:
async def get_user(self, user_id: uuid.UUID | None = None, telegram_id: int | None = None) -> domain.User | None:
if user_id:
return await self.session.get(domain.User, user_id)
@@ -85,7 +85,7 @@ class Postgres(DatabaseBase):
existing.title = channel.title
existing.username = channel.username
existing.user_id = channel.user_id
existing.is_active = channel.is_active
existing.status = channel.status
existing.deleted_at = None
existing.updated_at = datetime.datetime.now(datetime.UTC)

View File

@@ -10,11 +10,6 @@ log = logging.getLogger(__name__)
@telegram_callback_router.chat_join_request()
async def on_chat_join_request(event: ChatJoinRequest) -> None:
"""
Обрабатывает запросы на вступление в канал через приватную invite_link с одобрением.
Событие приходит когда пользователь отправляет запрос на вступление в канал.
"""
if event.chat.type not in {'channel', 'supergroup'}:
return

View File

@@ -10,12 +10,9 @@ if TYPE_CHECKING:
class Subscriber(domain.Base):
"""Пользователь Telegram, подписавшийся через invite links."""
telegram_id: Mapped[int] = mapped_column(BigInteger, unique=True, index=True)
username: Mapped[str | None]
first_name: Mapped[str | None]
last_name: Mapped[str | None]
# Relationships
subscriptions: Mapped[list['Subscription']] = relationship(back_populates='subscriber')

View File

@@ -25,22 +25,12 @@ class TargetChannel(domain.Base):
username: Mapped[str | None] = mapped_column(index=True)
status: Mapped[TargetChannelStatus] = mapped_column(default=TargetChannelStatus.ACTIVE)
# Relationship with user
user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey('user.id'), index=True)
user: Mapped['User'] = relationship(back_populates='target_channels')
# M2M relationship with external channels
external_channels: Mapped[list['ExternalChannel']] = relationship(
secondary='target_external_channel',
back_populates='target_channels',
)
creatives: Mapped[list['Creative']] = relationship(back_populates='target_channel')
@property
def is_active(self) -> bool:
return self.status == TargetChannelStatus.ACTIVE
@is_active.setter
def is_active(self, value: bool) -> None:
self.status = TargetChannelStatus.ACTIVE if value else TargetChannelStatus.INACTIVE

View File

@@ -2,6 +2,8 @@ import uuid
import pydantic
from src.domain.target_channel import TargetChannelStatus
class ChannelBotPermissions(pydantic.BaseModel):
is_admin: bool
@@ -28,6 +30,8 @@ class ConnectTargetChanOutput(pydantic.BaseModel):
telegram_id: int
title: str
username: str | None
status: TargetChannelStatus
# Deprecated: используйте status. Оставлено для обратной совместимости с фронтендом
is_active: bool

View File

@@ -57,7 +57,7 @@ async def connect_target_chan(self: 'Usecase', input: dto.ConnectTargetChanInput
title=input.title,
username=input.username,
user_id=user.id,
is_active=True,
status=domain.TargetChannelStatus.ACTIVE,
)
upserted_channel = await self.database.upsert_target_channel(channel)
@@ -73,5 +73,6 @@ async def connect_target_chan(self: 'Usecase', input: dto.ConnectTargetChanInput
telegram_id=upserted_channel.telegram_id,
title=upserted_channel.title,
username=upserted_channel.username,
is_active=upserted_channel.is_active,
status=upserted_channel.status,
is_active=upserted_channel.status == domain.TargetChannelStatus.ACTIVE,
)

View File

@@ -15,7 +15,7 @@ async def disconnect_target_chan(self: 'Usecase', input: dto.DisconnectTargetCha
if not channel:
raise domain.TargetChannelNotFound(input.channel_id)
channel.is_active = False
channel.status = domain.TargetChannelStatus.INACTIVE
await self.database.update_target_channel(channel)
log.info(f'Target channel {input.channel_id} deactivated')

View File

@@ -21,7 +21,7 @@ async def disconnect_target_chan_by_tg_id(self: 'Usecase', input: dto.Disconnect
log.warning(f'Target channel {input.telegram_id} not found')
raise domain.TargetChannelNotFound()
channel.is_active = False
channel.status = domain.TargetChannelStatus.INACTIVE
await self.database.update_target_channel(channel)
log.info(f'Target channel {input.telegram_id} deactivated')

View File

@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING
from src import dto
from src import domain, dto
if TYPE_CHECKING:
from .. import Usecase
@@ -17,7 +17,8 @@ async def get_user_target_chans(self: 'Usecase', input: dto.GetUserTargetChansIn
telegram_id=channel.telegram_id,
title=channel.title,
username=channel.username,
is_active=channel.is_active,
status=channel.status,
is_active=channel.status == domain.TargetChannelStatus.ACTIVE,
)
for channel in channels
]

View File

@@ -29,8 +29,8 @@ async def update_target_chan_permissions(self: 'Usecase', input: dto.UpdateTarge
raise domain.TargetChannelNotFound()
if not missing_permissions:
if not channel.is_active:
channel.is_active = True
if channel.status != domain.TargetChannelStatus.ACTIVE:
channel.status = domain.TargetChannelStatus.ACTIVE
await self.database.update_target_channel(channel)
await self.telegram_bot.send_message(
@@ -40,8 +40,8 @@ async def update_target_chan_permissions(self: 'Usecase', input: dto.UpdateTarge
log.info(f'Target channel {input.telegram_id} reactivated - all permissions granted')
return
if channel.is_active:
channel.is_active = False
if channel.status == domain.TargetChannelStatus.ACTIVE:
channel.status = domain.TargetChannelStatus.INACTIVE
await self.database.update_target_channel(channel)
missed_permissions = '\n'.join(f'{p}' for p in missing_permissions)