bot: forward sending errors to relay group

This commit is contained in:
missytake 2023-12-08 13:58:15 +01:00
parent 6493fc1952
commit c8bc7f12c9
2 changed files with 33 additions and 5 deletions

View file

@ -1,4 +1,5 @@
import logging import logging
import time
from threading import Event from threading import Event
import pickledb import pickledb
@ -44,6 +45,25 @@ class RelayPlugin:
if not kvstore.get("relays"): if not kvstore.get("relays"):
kvstore.set("relays", list()) kvstore.set("relays", list())
@account_hookimpl
def ac_outgoing_message(self, message: deltachat.Message):
while not message.is_out_delivered():
time.sleep(0.1)
if message.is_out_failed():
break
begin = int(time.time())
while not message.is_out_failed():
time.sleep(0.1)
if int(time.time()) < begin + 10:
break # it probably just worked.
else:
error = message.get_message_info()
logging.warning(
"Outgoing message failed. Forwarding error to relay group: %s", error
)
relay_group = self.get_relay_group(message.chat.id)
relay_group.send_text(f"Sending Message failed:\n\n{error}")
@account_hookimpl @account_hookimpl
def ac_incoming_message(self, message: deltachat.Message): def ac_incoming_message(self, message: deltachat.Message):
"""This method is called on every incoming message and decides what to do with it.""" """This method is called on every incoming message and decides what to do with it."""

View file

@ -6,6 +6,9 @@ import pytest
from deltachat.capi import lib as dclib from deltachat.capi import lib as dclib
TIMEOUT = 20
@pytest.mark.timeout(60) @pytest.mark.timeout(60)
def test_not_relay_groups(relaycrew, outsider): def test_not_relay_groups(relaycrew, outsider):
bot = relaycrew.bot bot = relaycrew.bot
@ -218,7 +221,6 @@ def test_change_avatar(relaycrew):
assert botcontact.get_profile_image() assert botcontact.get_profile_image()
@pytest.mark.xfail(reason="Forwarding errors is not implemented yet")
def test_forward_sending_errors_to_relay_group(relaycrew): def test_forward_sending_errors_to_relay_group(relaycrew):
usercrew = relaycrew.user.get_chats()[-1] usercrew = relaycrew.user.get_chats()[-1]
usercrew.send_text("/start_chat alice@example.org This_Message_will_fail test") usercrew.send_text("/start_chat alice@example.org This_Message_will_fail test")
@ -229,16 +231,22 @@ def test_forward_sending_errors_to_relay_group(relaycrew):
outgoing_message = out_chat.get_messages()[-1] outgoing_message = out_chat.get_messages()[-1]
print(outgoing_message) print(outgoing_message)
begin = int(time.time()) begin = int(time.time())
while not outgoing_message.is_out_failed() and int(time.time()) < begin + 10: while not outgoing_message.is_out_failed() and int(time.time()) < begin + TIMEOUT:
time.sleep(0.1) time.sleep(0.1)
assert outgoing_message.is_out_failed() assert outgoing_message.is_out_failed()
while len(out_chat.get_messages()) < 3 and int(time.time()) < begin + 10:
while len(relaycrew.user.get_chats()) < 2 and int(time.time()) < begin + TIMEOUT:
time.sleep(0.1)
relay_group = relaycrew.user.get_chats()[-2]
while len(relay_group.get_messages()) < 3 and int(time.time()) < begin + TIMEOUT:
print(relay_group.get_messages()[-1].text)
time.sleep(0.1) time.sleep(0.1)
assert ( assert (
"Recipient address rejected: Domain example.org does not accept mail" "Recipient address rejected: Domain example.org does not accept mail"
not in out_chat.get_messages()[-1].text not in relay_group.get_messages()[-1].text
) )
assert ( assert (
"Invalid unencrypted mail to <alice@example.org>" "Invalid unencrypted mail to <alice@example.org>"
in out_chat.get_messages()[-1].text in relay_group.get_messages()[-1].text
) )