Skip to content

Commit

Permalink
Merge pull request #441 from KurosawaAngel/develop
Browse files Browse the repository at this point in the history
Edit voice media
  • Loading branch information
Tishka17 authored Nov 10, 2024
2 parents 32170e1 + 5552ee2 commit f0e4c61
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/aiogram_dialog/api/entities/new_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from enum import Enum
from typing import Optional, Union

from aiogram.enums import ContentType
from aiogram.types import (
Chat,
ForceReply,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/aiogram_dialog/api/entities/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/aiogram_dialog/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]:
Expand All @@ -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:
Expand Down
17 changes: 15 additions & 2 deletions src/aiogram_dialog/manager/message_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down Expand Up @@ -116,6 +117,15 @@ 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 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:
Expand All @@ -140,9 +150,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) or self.need_voice(new_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(
Expand Down

0 comments on commit f0e4c61

Please sign in to comment.