Delete acknowledge text on upload

This commit is contained in:
Taras Terletskyi 2023-09-24 17:16:50 +03:00
parent 412ef95c74
commit 2383ee6b3f
5 changed files with 29 additions and 7 deletions

View file

@ -39,16 +39,23 @@ class TelegramCallback:
self._log.debug('No urls to download, skipping message') self._log.debug('No urls to download, skipping message')
return return
urls = self._url_parser.parse_urls(urls=urls, message=message, user=user) acknowledge_message = await self._send_acknowledge_message(
await self._url_service.process_urls(urls) message=message, url_count=len(urls)
await self._send_acknowledge_message(message=message, url_count=len(urls)) )
context = {
'message': message,
'user': user,
'acknowledge_message': acknowledge_message,
}
url_objects = self._url_parser.parse_urls(urls=urls, context=context)
await self._url_service.process_urls(url_objects)
async def _send_acknowledge_message( async def _send_acknowledge_message(
self, self,
message: Message, message: Message,
url_count: int, url_count: int,
) -> None: ) -> Message:
await message.reply( return await message.reply(
text=self._format_acknowledge_text(url_count), text=self._format_acknowledge_text(url_count),
parse_mode=ParseMode.HTML, parse_mode=ParseMode.HTML,
reply_to_message_id=message.id, reply_to_message_id=message.id,

View file

@ -35,11 +35,17 @@ class SuccessDownloadHandler(AbstractDownloadHandler):
self._cleanup() self._cleanup()
async def _handle(self) -> None: async def _handle(self) -> None:
coro_tasks = [] coro_tasks = [self._delete_acknowledge_message()]
for media_object in self._body.media.get_media_objects(): for media_object in self._body.media.get_media_objects():
coro_tasks.append(self._handle_media_object(media_object)) coro_tasks.append(self._handle_media_object(media_object))
await asyncio.gather(*coro_tasks) await asyncio.gather(*coro_tasks)
async def _delete_acknowledge_message(self) -> None:
await self._bot.delete_messages(
chat_id=self._body.from_chat_id,
message_ids=[self._body.context.acknowledge_message_id],
)
async def _publish_error_message(self, err: Exception) -> None: async def _publish_error_message(self, err: Exception) -> None:
err_payload = ErrorDownloadGeneralPayload( err_payload = ErrorDownloadGeneralPayload(
task_id=self._body.task_id, task_id=self._body.task_id,

View file

@ -24,6 +24,7 @@ class UrlService:
payload = InbMediaPayload( payload = InbMediaPayload(
url=url.url, url=url.url,
message_id=url.message_id, message_id=url.message_id,
acknowledge_message_id=url.acknowledge_message_id,
from_user_id=url.from_user_id, from_user_id=url.from_user_id,
from_chat_id=url.from_chat_id, from_chat_id=url.from_chat_id,
from_chat_type=url.from_chat_type, from_chat_type=url.from_chat_type,
@ -42,7 +43,12 @@ class UrlParser:
self._log = logging.getLogger(self.__class__.__name__) self._log = logging.getLogger(self.__class__.__name__)
@staticmethod @staticmethod
def parse_urls(urls: list[str], message: Message, user: UserSchema) -> list[URL]: def parse_urls(
urls: list[str], context: dict[str, Message | UserSchema]
) -> list[URL]:
message: Message = context['message']
user: UserSchema = context['user']
acknowledge_message: Message = context['acknowledge_message']
return [ return [
URL( URL(
url=url, url=url,
@ -50,6 +56,7 @@ class UrlParser:
from_chat_type=TelegramChatType(message.chat.type.value), from_chat_type=TelegramChatType(message.chat.type.value),
from_user_id=message.from_user.id, from_user_id=message.from_user.id,
message_id=message.id, message_id=message.id,
acknowledge_message_id=acknowledge_message.id,
save_to_storage=user.save_to_storage, save_to_storage=user.save_to_storage,
download_media_type=user.download_media_type, download_media_type=user.download_media_type,
) )

View file

@ -24,6 +24,7 @@ class InbMediaPayload(RealBaseModel):
from_chat_type: TelegramChatType | None from_chat_type: TelegramChatType | None
from_user_id: StrictInt | None from_user_id: StrictInt | None
message_id: StrictInt | None message_id: StrictInt | None
acknowledge_message_id: StrictInt | None
url: StrictStr url: StrictStr
source: TaskSource source: TaskSource
save_to_storage: StrictBool save_to_storage: StrictBool

View file

@ -10,5 +10,6 @@ class URL(RealBaseModel):
from_chat_type: TelegramChatType from_chat_type: TelegramChatType
from_user_id: StrictInt from_user_id: StrictInt
message_id: StrictInt message_id: StrictInt
acknowledge_message_id: StrictInt
save_to_storage: StrictBool save_to_storage: StrictBool
download_media_type: DownMediaType download_media_type: DownMediaType