added command to set avatar. closes #1

This commit is contained in:
missytake 2023-10-07 22:00:39 +02:00
parent 37c758f388
commit a9e6b1df9c
4 changed files with 107 additions and 86 deletions

View file

@ -5,7 +5,7 @@ import deltachat
from deltachat import account_hookimpl from deltachat import account_hookimpl
from deltachat.capi import lib as dclib 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: class SetupPlugin:
@ -59,6 +59,9 @@ class RelayPlugin:
self.reply(message.chat, help_message(), quote=message) self.reply(message.chat, help_message(), quote=message)
if arguments[0] == "/set_name": if arguments[0] == "/set_name":
self.reply(message.chat, set_display_name(self.account, arguments[1]), quote=message) 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: else:
logging.debug("Ignoring message, just the crew chatting") logging.debug("Ignoring message, just the crew chatting")
@ -152,51 +155,3 @@ class RelayPlugin:
if crew_member not in chat.get_contacts(): if crew_member not in chat.get_contacts():
return False # all crew members have to be in any relay group return False # all crew members have to be in any relay group
return True 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

View file

@ -1,5 +1,6 @@
import deltachat import logging
import deltachat
def help_message() -> str: def help_message() -> str:
"""Get the help message """Get the help message
@ -8,6 +9,7 @@ def help_message() -> str:
""" """
help_text = """ help_text = """
Change the bot's name:\t/set_name <name> Change the bot's name:\t/set_name <name>
Change the bot's avatar:\t/set_avatar (attach image)
Show this help text:\t\t/help Show this help text:\t\t/help
""" """
return help_text return help_text
@ -20,3 +22,65 @@ def set_display_name(account: deltachat.Account, display_name: str) -> str:
""" """
account.set_config("displayname", display_name) account.set_config("displayname", display_name)
return "Display name changed to " + 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

View file

@ -1,39 +1,5 @@
from teams_bot.bot import get_crew_id, RelayPlugin from teams_bot.bot import RelayPlugin
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
def test_is_relay_group(crew, outsider): def test_is_relay_group(crew, outsider):

36
tests/test_commands.py Normal file
View file

@ -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