From f9186fe0c033f8113268c643a1372238a8f84db2 Mon Sep 17 00:00:00 2001 From: missytake Date: Mon, 19 Aug 2024 14:36:44 +0200 Subject: [PATCH] get relay group from pickle instead of guessing --- src/team_bot/bot.py | 19 ++++--------------- tests/test_bot.py | 20 ++++++++++++++------ 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/team_bot/bot.py b/src/team_bot/bot.py index 351fd87..4c79d5e 100644 --- a/src/team_bot/bot.py +++ b/src/team_bot/bot.py @@ -234,21 +234,10 @@ def forward_to_relay_group(self, message: deltachat.Message, started_by_crew=Fal def is_relay_group(self, chat: deltachat.Chat) -> bool: """Check whether a chat is a relay group.""" - if not chat.get_name().startswith( - "[%s] " % (self.account.get_config("addr").split("@")[0],) - ): - return False # all relay groups' names begin with a [tag] with the localpart of the team-bot's address - if ( - chat.get_messages()[0].get_sender_contact() - != self.account.get_self_contact() - ): - return False # all relay groups were started by the team-bot - if chat.is_protected(): - return False # relay groups don't need to be protected, so they are not - for crew_member in self.crew.get_contacts(): - if crew_member not in chat.get_contacts(): - return False # all crew members have to be in any relay group - return True + for mapping in self.kvstore.get("relays"): + if mapping[1] == chat.id: + return True + return False def get_outside_chat(self, relay_group_id: int) -> deltachat.Chat: """Get the corresponding outside chat for the ID of a relay group. diff --git a/tests/test_bot.py b/tests/test_bot.py index 27a684d..7f8621a 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -27,6 +27,12 @@ def test_not_relay_groups(relaycrew, outsider, lp): bot = relaycrew.bot user = relaycrew.user + def find_msg(ac, text): + for chat in ac.get_chats(): + for msg in chat.get_messages(): + if msg.text == text: + return msg + text = "outsider -> bot 1:1 chat" lp.sec(text) outsider_botcontact = outsider.create_contact(bot.get_config("addr")) @@ -38,6 +44,14 @@ def test_not_relay_groups(relaycrew, outsider, lp): assert bot_message_from_outsider.text == text assert not bot.relayplugin.is_relay_group(bot_outside_chat) + lp.sec("leave relay group with user") + relayed_msg = find_msg(user, text) + if not relayed_msg: + relayed_msg = user._evtracker.wait_next_incoming_message() + relayed_msg.chat.remove_contact(user.get_config("addr")) + leave_msg = bot._evtracker.wait_next_incoming_message() + assert bot.relayplugin.is_relay_group(leave_msg.chat) + text = "outsider -> bot group chat" lp.sec(text) outsider_bot_group = outsider.create_group_chat( @@ -58,12 +72,6 @@ def test_not_relay_groups(relaycrew, outsider, lp): # somehow the message doesn't trigger DC_EVENT_INCOMING_MSG # bot._evtracker.wait_next_incoming_message() - def find_msg(ac, text): - for chat in ac.get_chats(): - for msg in chat.get_messages(): - if msg.text == text: - return msg - bot_message_from_user = find_msg(bot, text) while not bot_message_from_user: bot_message_from_user = find_msg(bot, text)