From a9e6b1df9cffaad2317d5eb281478495666295e5 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 7 Oct 2023 22:00:39 +0200 Subject: [PATCH] added command to set avatar. closes #1 --- src/teams_bot/bot.py | 53 +++---------------------------- src/teams_bot/commands.py | 66 ++++++++++++++++++++++++++++++++++++++- tests/test_bot.py | 38 ++-------------------- tests/test_commands.py | 36 +++++++++++++++++++++ 4 files changed, 107 insertions(+), 86 deletions(-) create mode 100644 tests/test_commands.py diff --git a/src/teams_bot/bot.py b/src/teams_bot/bot.py index 313e024..95021c2 100644 --- a/src/teams_bot/bot.py +++ b/src/teams_bot/bot.py @@ -5,7 +5,7 @@ import deltachat from deltachat import account_hookimpl from deltachat.capi import lib as dclib -from .commands import set_display_name, help_message +from .commands import help_message, set_display_name, set_avatar, get_crew_id class SetupPlugin: @@ -59,6 +59,9 @@ class RelayPlugin: self.reply(message.chat, help_message(), quote=message) if arguments[0] == "/set_name": self.reply(message.chat, set_display_name(self.account, arguments[1]), quote=message) + if arguments[0] == "/set_avatar": + result = set_avatar(self.account, message) + self.reply(message.chat, result, quote=message) else: logging.debug("Ignoring message, just the crew chatting") @@ -152,51 +155,3 @@ class RelayPlugin: if crew_member not in chat.get_contacts(): return False # all crew members have to be in any relay group return True - - -def get_crew_id(ac: deltachat.Account, setupplugin: SetupPlugin = None) -> int: - """Get the group ID of the crew group if it exists; warn old crews if they might still believe they are the crew. - - :param ac: the account object of the bot. - :param setupplugin: only if this function is run during `teams-bot init`. - :return: the chat ID of the crew group, if there is none, return 0. - """ - crew_id = 0 - for chat in reversed(ac.get_chats()): - if ( - chat.is_protected() - and chat.num_contacts() > 1 - and chat.get_name() == f"Team: {ac.get_config('addr')}" - ): - logging.debug( - "Chat with ID %s and title %s could be a crew", chat.id, chat.get_name() - ) - if crew_id > 0: - old_crew = ac.get_chat_by_id(crew_id) - old_crew.set_name(f"Old Team: {ac.get_config('addr')}") - new_crew = [contact.addr for contact in chat.get_contacts()] - new_crew_emails = " or ".join(new_crew) - quit_message = f"There is a new Group for the Team now; you can ask {new_crew_emails} to add you to it." - logging.debug( - "Sending quit message to old crew with ID %s: %s", - old_crew.id, - quit_message, - ) - old_crew.send_text(quit_message) - if setupplugin: - setupplugin.outgoing_messages += 1 - old_crew.remove_contact(ac.get_self_contact()) - crew_id = chat.id - else: - logging.debug( - "Chat with ID %s and title %s is not a crew.", chat.id, chat.get_name() - ) - if crew_id: - crew_members = [ - contact.addr for contact in ac.get_chat_by_id(crew_id).get_contacts() - ] - crew_emails = " or ".join(crew_members) - logging.debug("The current crew has ID %s and members %s", crew_id, crew_emails) - else: - logging.debug("Currently there is no crew") - return crew_id diff --git a/src/teams_bot/commands.py b/src/teams_bot/commands.py index 7eab2d5..dd4858c 100644 --- a/src/teams_bot/commands.py +++ b/src/teams_bot/commands.py @@ -1,5 +1,6 @@ -import deltachat +import logging +import deltachat def help_message() -> str: """Get the help message @@ -8,6 +9,7 @@ def help_message() -> str: """ help_text = """ Change the bot's name:\t/set_name +Change the bot's avatar:\t/set_avatar (attach image) Show this help text:\t\t/help """ return help_text @@ -20,3 +22,65 @@ def set_display_name(account: deltachat.Account, display_name: str) -> str: """ account.set_config("displayname", display_name) return "Display name changed to " + display_name + + +def set_avatar(account: deltachat.Account, message: deltachat.Message) -> str: + """Set the avatar of the bot. + + :return: a success/failure message + """ + if not message.is_image(): + return "Please attach an image so the avatar can be changed." + logging.debug("Found file with MIMEtype %s", message.filemime) + account.set_avatar(message.filename) + crew = account.get_chat_by_id(get_crew_id(account)) + crew.set_profile_image(message.filename) + return "Avatar changed to this image." + + +def get_crew_id(ac: deltachat.Account, setupplugin=None) -> int: + """Get the group ID of the crew group if it exists; warn old crews if they might still believe they are the crew. + + :param ac: the account object of the bot. + :param setupplugin: only if this function is run during `teams-bot init`. + :return: the chat ID of the crew group, if there is none, return 0. + """ + crew_id = 0 + for chat in reversed(ac.get_chats()): + if ( + chat.is_protected() + and chat.num_contacts() > 1 + and chat.get_name() == f"Team: {ac.get_config('addr')}" + ): + logging.debug( + "Chat with ID %s and title %s could be a crew", chat.id, chat.get_name() + ) + if crew_id > 0: + old_crew = ac.get_chat_by_id(crew_id) + old_crew.set_name(f"Old Team: {ac.get_config('addr')}") + new_crew = [contact.addr for contact in chat.get_contacts()] + new_crew_emails = " or ".join(new_crew) + quit_message = f"There is a new Group for the Team now; you can ask {new_crew_emails} to add you to it." + logging.debug( + "Sending quit message to old crew with ID %s: %s", + old_crew.id, + quit_message, + ) + old_crew.send_text(quit_message) + if setupplugin: + setupplugin.outgoing_messages += 1 + old_crew.remove_contact(ac.get_self_contact()) + crew_id = chat.id + else: + logging.debug( + "Chat with ID %s and title %s is not a crew.", chat.id, chat.get_name() + ) + if crew_id: + crew_members = [ + contact.addr for contact in ac.get_chat_by_id(crew_id).get_contacts() + ] + crew_emails = " or ".join(crew_members) + logging.debug("The current crew has ID %s and members %s", crew_id, crew_emails) + else: + logging.debug("Currently there is no crew") + return crew_id diff --git a/tests/test_bot.py b/tests/test_bot.py index 0e4f2a4..01f800c 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -1,39 +1,5 @@ -from teams_bot.bot import get_crew_id, RelayPlugin - - -def test_get_crew_id(crew): - """Test if crew is properly found in delta chat database.""" - assert crew.id == get_crew_id(crew.bot) - - -def test_disable_old_crew(crew, outsider): - """Test if crew is properly disabled if someone else creates a new crew on the command line.""" - old_crew_id = get_crew_id(crew.bot) - - # outsider fires up the command line and creates a new crew - new_crew = crew.bot.create_group_chat( - f"Team: {crew.bot.get_config('addr')}", verified=True - ) - assert new_crew.id != old_crew_id - qr = new_crew.get_join_qr() - - # prepare setupplugin for waiting on second group join - crew.bot.setupplugin.member_added.clear() - crew.bot.setupplugin.crew_id = new_crew.id - - # outsider joins new crew - outsider.qr_join_chat(qr) - crew.bot.setupplugin.member_added.wait(timeout=30) - assert len(new_crew.get_contacts()) == 2 - assert new_crew.get_name() == f"Team: {crew.bot.get_config('addr')}" - assert new_crew.is_protected() - assert new_crew.id == get_crew_id(crew.bot, crew.bot.setupplugin) - - # old user receives disable warning - crew.user.wait_next_incoming_message() - quit_message = crew.user.wait_next_incoming_message() - assert "There is a new Group for the Team now" in quit_message.text - assert outsider.get_config("addr") in quit_message.text +from teams_bot.bot import RelayPlugin +from teams_bot.commands import get_crew_id def test_is_relay_group(crew, outsider): diff --git a/tests/test_commands.py b/tests/test_commands.py new file mode 100644 index 0000000..d3f4aa8 --- /dev/null +++ b/tests/test_commands.py @@ -0,0 +1,36 @@ +from teams_bot.commands import get_crew_id + + +def test_get_crew_id(crew): + """Test if crew is properly found in delta chat database.""" + assert crew.id == get_crew_id(crew.bot) + + +def test_disable_old_crew(crew, outsider): + """Test if crew is properly disabled if someone else creates a new crew on the command line.""" + old_crew_id = get_crew_id(crew.bot) + + # outsider fires up the command line and creates a new crew + new_crew = crew.bot.create_group_chat( + f"Team: {crew.bot.get_config('addr')}", verified=True + ) + assert new_crew.id != old_crew_id + qr = new_crew.get_join_qr() + + # prepare setupplugin for waiting on second group join + crew.bot.setupplugin.member_added.clear() + crew.bot.setupplugin.crew_id = new_crew.id + + # outsider joins new crew + outsider.qr_join_chat(qr) + crew.bot.setupplugin.member_added.wait(timeout=30) + assert len(new_crew.get_contacts()) == 2 + assert new_crew.get_name() == f"Team: {crew.bot.get_config('addr')}" + assert new_crew.is_protected() + assert new_crew.id == get_crew_id(crew.bot, crew.bot.setupplugin) + + # old user receives disable warning + crew.user.wait_next_incoming_message() + quit_message = crew.user.wait_next_incoming_message() + assert "There is a new Group for the Team now" in quit_message.text + assert outsider.get_config("addr") in quit_message.text