Skip to content

Commit

Permalink
Merge pull request #82 from sehaas/fix_new_groups
Browse files Browse the repository at this point in the history
Re-fetch group information
  • Loading branch information
Era-Dorta authored Jan 5, 2025
2 parents 6986a82 + 6f3ce04 commit 7b10bb7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions signalbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,13 @@ async def _produce(self, name: int) -> None:
except UnknownMessageFormatError:
continue

# Update groups if message is from an unknown group
if (
message.is_group()
and self._groups_by_internal_id.get(message.group) is None
):
await self._detect_groups()

await self._ask_commands_to_handle(message)

except ReceiveMessagesError as e:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import aiohttp
import asyncio
from unittest.mock import patch, AsyncMock
from signalbot import SignalBot, Command, SignalAPI
Expand All @@ -21,8 +22,9 @@ def setUp(self):


class TestProducer(BotTestCase):
@patch("aiohttp.ClientSession.get", new_callable=AsyncMock)
@patch("websockets.connect")
async def test_produce(self, mock):
async def test_produce(self, mock, get_group_mock):
# Two messages
message1 = '{"envelope":{"source":"+4901234567890","sourceNumber":"+4901234567890","sourceUuid":"asdf","sourceName":"name","sourceDevice":1,"timestamp":1633169000000,"syncMessage":{"sentMessage":{"timestamp":1633169000000,"message":"Message 1","expiresInSeconds":0,"viewOnce":false,"mentions":[],"attachments":[],"contacts":[],"groupInfo":{"groupId":"group_id1=","type":"DELIVER"},"destination":null,"destinationNumber":null,"destinationUuid":null}}}}' # noqa
message2 = '{"envelope":{"source":"+4901234567890","sourceNumber":"+4901234567890","sourceUuid":"asdf","sourceName":"name","sourceDevice":1,"timestamp":1633169000000,"syncMessage":{"sentMessage":{"timestamp":1633169000000,"message":"Message 2","expiresInSeconds":0,"viewOnce":false,"mentions":[],"attachments":[],"contacts":[],"groupInfo":{"groupId":"group_id1=","type":"DELIVER"},"destination":null,"destinationNumber":null,"destinationUuid":null}}}}' # noqa
Expand All @@ -31,6 +33,18 @@ async def test_produce(self, mock):
mock_iterator.__aiter__.return_value = messages
mock.return_value.__aenter__.return_value = mock_iterator

group_mock = AsyncMock()
group_mock.return_value = [
{
"name": "mocked group",
"id": self.group_id,
"internal_id": self.internal_id,
}
]
get_group_mock.return_value = AsyncMock(
spec=aiohttp.ClientResponse, status_code=200, json=group_mock
)

self.signal_bot._q = asyncio.Queue()
self.signal_bot._signal = SignalAPI(
TestProducer.signal_service, TestProducer.phone_number
Expand Down

0 comments on commit 7b10bb7

Please sign in to comment.