get relay group from pickle instead of guessing

This commit is contained in:
missytake 2024-08-19 14:36:44 +02:00
parent 18e5a67aa7
commit f9186fe0c0
2 changed files with 18 additions and 21 deletions

View file

@ -234,21 +234,10 @@ class RelayPlugin:
def is_relay_group(self, chat: deltachat.Chat) -> bool: def is_relay_group(self, chat: deltachat.Chat) -> bool:
"""Check whether a chat is a relay group.""" """Check whether a chat is a relay group."""
if not chat.get_name().startswith( for mapping in self.kvstore.get("relays"):
"[%s] " % (self.account.get_config("addr").split("@")[0],) if mapping[1] == chat.id:
): return True
return False # all relay groups' names begin with a [tag] with the localpart of the team-bot's address return False
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
def get_outside_chat(self, relay_group_id: int) -> deltachat.Chat: def get_outside_chat(self, relay_group_id: int) -> deltachat.Chat:
"""Get the corresponding outside chat for the ID of a relay group. """Get the corresponding outside chat for the ID of a relay group.

View file

@ -27,6 +27,12 @@ def test_not_relay_groups(relaycrew, outsider, lp):
bot = relaycrew.bot bot = relaycrew.bot
user = relaycrew.user 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" text = "outsider -> bot 1:1 chat"
lp.sec(text) lp.sec(text)
outsider_botcontact = outsider.create_contact(bot.get_config("addr")) 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 bot_message_from_outsider.text == text
assert not bot.relayplugin.is_relay_group(bot_outside_chat) 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" text = "outsider -> bot group chat"
lp.sec(text) lp.sec(text)
outsider_bot_group = outsider.create_group_chat( 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 # somehow the message doesn't trigger DC_EVENT_INCOMING_MSG
# bot._evtracker.wait_next_incoming_message() # 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) bot_message_from_user = find_msg(bot, text)
while not bot_message_from_user: while not bot_message_from_user:
bot_message_from_user = find_msg(bot, text) bot_message_from_user = find_msg(bot, text)