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

check if it's a relay group in pickle instead of guessing #13

Merged
merged 1 commit into from
Aug 20, 2024
Merged
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
19 changes: 4 additions & 15 deletions src/team_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 14 additions & 6 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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(
Expand All @@ -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)
Expand Down