Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add multibot support to media #386

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/aiogram_dialog/api/protocols/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MediaIdStorageProtocol(Protocol):
@abstractmethod
async def get_media_id(
self,
bot_id: int,
path: Optional[str],
url: Optional[str],
type: ContentType,
Expand All @@ -19,6 +20,7 @@ async def get_media_id(
@abstractmethod
async def save_media_id(
self,
bot_id: int,
path: Optional[str],
url: Optional[str],
type: ContentType,
Expand Down
6 changes: 4 additions & 2 deletions src/aiogram_dialog/context/media_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ def __init__(self, maxsize=10240):

async def get_media_id(
self,
bot_id: int,
path: Optional[str],
url: Optional[str],
type: ContentType,
) -> Optional[MediaId]:
if not path and not url:
return None
return self.cache.get((path, url, type))
return self.cache.get((bot_id, path, url, type))

async def save_media_id(
self,
bot_id: int,
path: Optional[str],
url: Optional[str],
type: ContentType,
media_id: MediaId,
) -> None:
if not path and not url:
return None
self.cache[(path, url, type)] = media_id
self.cache[(bot_id, path, url, type)] = media_id
3 changes: 3 additions & 0 deletions src/aiogram_dialog/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ async def show(self, show_mode: Optional[ShowMode] = None) -> None:
self._save_last_message(sent_message)
if new_message.media:
await self.media_id_storage.save_media_id(
bot_id=bot.id,
path=new_message.media.path,
url=new_message.media.url,
type=new_message.media.type,
Expand All @@ -337,7 +338,9 @@ async def show(self, show_mode: Optional[ShowMode] = None) -> None:
async def _fix_cached_media_id(self, new_message: NewMessage):
if not new_message.media or new_message.media.file_id:
return
bot = self._data["bot"]
new_message.media.file_id = await self.media_id_storage.get_media_id(
bot_id=bot.id,
path=new_message.media.path,
url=new_message.media.url,
type=new_message.media.type,
Expand Down
Loading