From f72f37959542eb6fc94ae6739e3ca9f9de4d96b6 Mon Sep 17 00:00:00 2001 From: Kurosawa <145038102+KurosawaAngel@users.noreply.github.com> Date: Thu, 7 Nov 2024 22:57:47 +0500 Subject: [PATCH 1/2] fix edit voice --- src/aiogram_dialog/api/entities/new_message.py | 2 ++ src/aiogram_dialog/api/entities/stack.py | 2 ++ src/aiogram_dialog/manager/manager.py | 6 +++++- src/aiogram_dialog/manager/message_manager.py | 11 +++++++++-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/aiogram_dialog/api/entities/new_message.py b/src/aiogram_dialog/api/entities/new_message.py index 062f794f..4ac6883a 100644 --- a/src/aiogram_dialog/api/entities/new_message.py +++ b/src/aiogram_dialog/api/entities/new_message.py @@ -2,6 +2,7 @@ from enum import Enum from typing import Optional, Union +from aiogram.enums import ContentType from aiogram.types import ( Chat, ForceReply, @@ -30,6 +31,7 @@ class OldMessage: text: Union[str, None, UnknownText] = None has_reply_keyboard: bool = False business_connection_id: Optional[str] = None + content_type: Optional[ContentType] = None @dataclass diff --git a/src/aiogram_dialog/api/entities/stack.py b/src/aiogram_dialog/api/entities/stack.py index 3256d018..27b3f01f 100644 --- a/src/aiogram_dialog/api/entities/stack.py +++ b/src/aiogram_dialog/api/entities/stack.py @@ -4,6 +4,7 @@ from dataclasses import dataclass, field from typing import Optional +from aiogram.enums import ContentType from aiogram.fsm.state import State from aiogram_dialog.api.exceptions import DialogStackOverflow @@ -47,6 +48,7 @@ class Stack: last_income_media_group_id: Optional[str] = field( compare=False, default=None, ) + content_type: Optional[ContentType] = field(compare=False, default=None) access_settings: Optional[AccessSettings] = None @property diff --git a/src/aiogram_dialog/manager/manager.py b/src/aiogram_dialog/manager/manager.py index 680ef54e..79cef84b 100644 --- a/src/aiogram_dialog/manager/manager.py +++ b/src/aiogram_dialog/manager/manager.py @@ -421,6 +421,7 @@ def _get_message_from_callback( chat=event_context.chat, message_id=current_message.message_id, business_connection_id=event_context.business_connection_id, + content_type=current_message.content_type, ) elif not stack or not stack.last_message_id: return None @@ -433,6 +434,7 @@ def _get_message_from_callback( chat=event_context.chat, message_id=stack.last_message_id, business_connection_id=event_context.business_connection_id, + content_type=stack.content_type, ) def _get_last_message(self) -> Optional[OldMessage]: @@ -457,14 +459,16 @@ def _get_last_message(self) -> Optional[OldMessage]: chat=event_context.chat, message_id=stack.last_message_id, business_connection_id=event_context.business_connection_id, + content_type=stack.content_type, ) - def _save_last_message(self, message: OldMessage): + def _save_last_message(self, message: OldMessage) -> None: stack = self.current_stack() stack.last_message_id = message.message_id stack.last_media_id = message.media_id stack.last_media_unique_id = message.media_uniq_id stack.last_reply_keyboard = message.has_reply_keyboard + stack.content_type = message.content_type def _calc_show_mode(self) -> ShowMode: # noqa: PLR0911 if self.show_mode is not ShowMode.AUTO: diff --git a/src/aiogram_dialog/manager/message_manager.py b/src/aiogram_dialog/manager/message_manager.py index 7f740a8b..128f730d 100644 --- a/src/aiogram_dialog/manager/message_manager.py +++ b/src/aiogram_dialog/manager/message_manager.py @@ -71,6 +71,7 @@ def _combine(sent_message: NewMessage, message_result: Message) -> OldMessage: media_uniq_id=(media_id.file_unique_id if media_id else None), media_id=(media_id.file_id if media_id else None), business_connection_id=message_result.business_connection_id, + content_type=message_result.content_type, ) @@ -116,6 +117,9 @@ def need_reply_keyboard(self, new_message: Optional[NewMessage]) -> bool: return False return isinstance(new_message.reply_markup, ReplyKeyboardMarkup) + def had_voice(self, old_message: OldMessage) -> bool: + return old_message.content_type == ContentType.VOICE + def _message_changed( self, new_message: NewMessage, old_message: OldMessage, ) -> bool: @@ -140,9 +144,12 @@ def _can_edit(self, new_message: NewMessage, # we cannot edit message if media removed if self.had_media(old_message) and not self.need_media(new_message): return False + # we cannot edit a message if there was voice + if self.had_voice(old_message): + return False return not ( - self.had_reply_keyboard(old_message) or - self.need_reply_keyboard(new_message) + self.had_reply_keyboard(old_message) + or self.need_reply_keyboard(new_message) ) async def show_message( From 5552ee2acf958df25b7e32f92f1ea177a40b2310 Mon Sep 17 00:00:00 2001 From: Kurosawa <145038102+KurosawaAngel@users.noreply.github.com> Date: Thu, 7 Nov 2024 23:42:53 +0500 Subject: [PATCH 2/2] add voice added check --- src/aiogram_dialog/manager/message_manager.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/aiogram_dialog/manager/message_manager.py b/src/aiogram_dialog/manager/message_manager.py index 128f730d..f5836157 100644 --- a/src/aiogram_dialog/manager/message_manager.py +++ b/src/aiogram_dialog/manager/message_manager.py @@ -120,6 +120,12 @@ def need_reply_keyboard(self, new_message: Optional[NewMessage]) -> bool: def had_voice(self, old_message: OldMessage) -> bool: return old_message.content_type == ContentType.VOICE + def need_voice(self, new_message: NewMessage) -> bool: + return ( + new_message.media is not None + and new_message.media.type == ContentType.VOICE + ) + def _message_changed( self, new_message: NewMessage, old_message: OldMessage, ) -> bool: @@ -145,7 +151,7 @@ def _can_edit(self, new_message: NewMessage, if self.had_media(old_message) and not self.need_media(new_message): return False # we cannot edit a message if there was voice - if self.had_voice(old_message): + if self.had_voice(old_message) or self.need_voice(new_message): return False return not ( self.had_reply_keyboard(old_message)