feat: creatives and external channels
This commit is contained in:
47
src/usecase/external_channel/create_external_channel.py
Normal file
47
src/usecase/external_channel/create_external_channel.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import logging
|
||||
import uuid
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from src import domain, dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def create_external_channel(
|
||||
self: 'Usecase', input: dto.CreateExternalChannelInput, user_id: uuid.UUID
|
||||
) -> dto.ExternalChannelOutput:
|
||||
async with self.database.transaction():
|
||||
for target_id in input.target_channel_ids:
|
||||
target_channel = await self.database.get_target_channel(user_id, channel_id=target_id)
|
||||
if not target_channel:
|
||||
log.warning('User %s attempted to add external channel to unavailable target %s', user_id, target_id)
|
||||
raise domain.TargetChannelNotFound(target_id)
|
||||
|
||||
channel = domain.ExternalChannel(
|
||||
telegram_id=input.telegram_id,
|
||||
title=input.title,
|
||||
username=input.username,
|
||||
description=input.description,
|
||||
subscribers_count=input.subscribers_count,
|
||||
user_id=user_id,
|
||||
)
|
||||
created_channel = await self.database.create_external_channel(channel)
|
||||
|
||||
if input.target_channel_ids:
|
||||
await self.database.add_external_channel_to_targets(created_channel.id, input.target_channel_ids)
|
||||
|
||||
log.info(
|
||||
'External channel %s created and linked to %s target channels', input.telegram_id, len(input.target_channel_ids)
|
||||
)
|
||||
|
||||
return dto.ExternalChannelOutput(
|
||||
id=created_channel.id,
|
||||
telegram_id=created_channel.telegram_id,
|
||||
title=created_channel.title,
|
||||
username=created_channel.username,
|
||||
description=created_channel.description,
|
||||
subscribers_count=created_channel.subscribers_count,
|
||||
)
|
||||
Reference in New Issue
Block a user