Skip to content

Commit

Permalink
Finish renaming Telegram groups to chats
Browse files Browse the repository at this point in the history
  • Loading branch information
Saluev committed Dec 26, 2023
1 parent 091f5d1 commit e8683af
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions suppgram/bridges/mongodb_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ async def get_newer_messages_of_kind(

async def _convert_to_messages(self, docs: List[Document]) -> List[TelegramMessage]:
chat_ids = [doc["_id"]["c"] for doc in docs]
groups = {chat.telegram_chat_id: chat for chat in await self.find_chats_by_ids(chat_ids)}
return [self._convert_to_message(doc, groups) for doc in docs]
chats = {chat.telegram_chat_id: chat for chat in await self.find_chats_by_ids(chat_ids)}
return [self._convert_to_message(doc, chats) for doc in docs]

def _compose_message_id(self, telegram_chat_id: int, telegram_message_id: int) -> Any:
return {"c": telegram_chat_id, "m": telegram_message_id}
Expand Down
14 changes: 7 additions & 7 deletions suppgram/frontends/telegram/manager_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,22 @@ async def _send_placeholder_message(
)

async def _send_or_edit_new_conversation_notifications(self, conversation: Conversation):
# For this conversation, there are some notifications in some groups.
# For this conversation, there are some notifications in some chats.
#
# In those groups where newer messages with non-NEW conversation
# In those chats where newer messages with non-NEW conversation
# notifications are present, we should delete the old notification and
# create a new one to avoid losing the notification in the chat history.
#
# In other groups (where the notification is still on top or at least
# In other chats (where the notification is still on top or at least
# among other NEW notifications), we should update the existing notification.
messages = await self._storage.get_messages(
TelegramMessageKind.NEW_CONVERSATION_NOTIFICATION,
conversation_id=conversation.id,
)
groups = await self._storage.get_chats_by_role(
chats = await self._storage.get_chats_by_role(
TelegramChatRole.NEW_CONVERSATION_NOTIFICATIONS
)
group_ids = {group.telegram_chat_id for group in groups}
group_ids = {group.telegram_chat_id for group in chats}
newer_messages = await self._storage.get_newer_messages_of_kind(messages)
newer_message_conversation_ids = {message.conversation_id for message in newer_messages}
conversations = await self._backend.get_conversations(list(newer_message_conversation_ids))
Expand All @@ -243,7 +243,7 @@ async def _send_or_edit_new_conversation_notifications(self, conversation: Conve
messages_to_update.append(message)
group_ids.remove(message.chat.telegram_chat_id)

groups_to_send_to = [group for group in groups if group.telegram_chat_id in group_ids]
chats_to_send_to = [group for group in chats if group.telegram_chat_id in group_ids]

all_tags = await self._backend.get_all_tags()

Expand All @@ -257,7 +257,7 @@ async def _send_or_edit_new_conversation_notifications(self, conversation: Conve
),
flat_gather(
self._send_new_conversation_notification(group, conversation, all_tags)
for group in groups_to_send_to
for group in chats_to_send_to
),
flat_gather(
self._update_new_conversation_notification(message, conversation, all_tags)
Expand Down
2 changes: 1 addition & 1 deletion suppgram/frontends/telegram/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def customer_identification(self) -> CustomerIdentification:
class TelegramStorage(abc.ABC):
"""Persistent storage for data specific to Telegram frontend.
Currently, two entities are stored: Telegram groups and messages within groups.
Currently, two entities are stored: Telegram chats and messages within chats.
We need these data to track group roles and edit messages sent by bots if needed."""

async def initialize(self):
Expand Down
10 changes: 4 additions & 6 deletions tests/frontends/telegram/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ async def test_add_non_existing_group_roles(self):

@pytest.mark.asyncio
async def test_add_group_roles(self, group: TelegramChat):
await self.telegram_storage.add_chat_roles(
group.telegram_chat_id, TelegramChatRole.AGENTS
)
await self.telegram_storage.add_chat_roles(group.telegram_chat_id, TelegramChatRole.AGENTS)
group = await self.telegram_storage.get_chat(group.telegram_chat_id)
assert group.roles == {TelegramChatRole.AGENTS}

Expand All @@ -72,13 +70,13 @@ async def test_add_group_roles(self, group: TelegramChat):
}

@pytest.mark.asyncio
async def test_get_groups_by_role(self):
async def test_get_chats_by_role(self):
g1 = await self.telegram_storage.create_or_update_chat(self.generate_telegram_id())
g2 = await self.telegram_storage.create_or_update_chat(self.generate_telegram_id())
await self.telegram_storage.add_chat_roles(g2.telegram_chat_id, TelegramChatRole.AGENTS)

groups = await self.telegram_storage.get_chats_by_role(TelegramChatRole.AGENTS)
group_ids = {g.telegram_chat_id for g in groups}
chats = await self.telegram_storage.get_chats_by_role(TelegramChatRole.AGENTS)
group_ids = {g.telegram_chat_id for g in chats}
assert g1.telegram_chat_id not in group_ids
assert g2.telegram_chat_id in group_ids
# Not checking for equality because of side effects of other tests.
Expand Down

0 comments on commit e8683af

Please sign in to comment.