refine handling of accepted contacts in example

This commit is contained in:
holger krekel
2020-03-06 19:11:21 +01:00
parent a1d5120e58
commit d4e1c1b109
6 changed files with 36 additions and 6 deletions

View File

@@ -11,10 +11,11 @@ class EchoPlugin:
if message.text.strip() == "/quit": if message.text.strip() == "/quit":
message.account.shutdown() message.account.shutdown()
else: else:
ch = message.get_sender_chat() # unconditionally accept the chat
message.accept_sender_contact()
addr = message.get_sender_contact().addr addr = message.get_sender_contact().addr
text = message.text text = message.text
ch.send_text("echoing from {}:\n{}".format(addr, text)) message.chat.send_text("echoing from {}:\n{}".format(addr, text))
@deltachat.hookspec.account_hookimpl @deltachat.hookspec.account_hookimpl
def process_message_delivered(self, message): def process_message_delivered(self, message):

View File

@@ -22,7 +22,6 @@ def test_echo_quit_plugin(acfactory):
def run_bot(): def run_bot():
print("*"*20 + " starting bot") print("*"*20 + " starting bot")
print("*"*20 + " bot_ac.dbpath", bot_ac.db_path)
echo_and_quit.main([ echo_and_quit.main([
"echo", "echo",
"--show-ffi", "--show-ffi",
@@ -40,6 +39,7 @@ def test_echo_quit_plugin(acfactory):
ch1.send_text("hello") ch1.send_text("hello")
reply = ac1._evtracker.wait_next_incoming_message() reply = ac1._evtracker.wait_next_incoming_message()
assert "hello" in reply.text assert "hello" in reply.text
assert reply.chat == ch1
ch1.send_text("/quit") ch1.send_text("/quit")
t.join() t.join()

View File

@@ -283,6 +283,9 @@ class Account(object):
""" create or get an existing chat object for the """ create or get an existing chat object for the
the specified message. the specified message.
If this message is in the deaddrop chat then
the sender will become an accepted contact.
:param message: messsage id or message instance. :param message: messsage id or message instance.
:returns: a :class:`deltachat.chat.Chat` object. :returns: a :class:`deltachat.chat.Chat` object.
""" """

View File

@@ -128,3 +128,10 @@ class FFIEventTracker:
""" wait for and return next incoming message. """ """ wait for and return next incoming message. """
ev = self.get_matching("DC_EVENT_INCOMING_MSG") ev = self.get_matching("DC_EVENT_INCOMING_MSG")
return self.account.get_message_by_id(ev.data2) return self.account.get_message_by_id(ev.data2)
def wait_next_messages_changed(self):
""" wait for and return next message-changed message or None
if the event contains no msgid"""
ev = self.get_matching("DC_EVENT_MSGS_CHANGED")
if ev.data2 > 0:
return self.account.get_message_by_id(ev.data2)

View File

@@ -50,6 +50,16 @@ class Message(object):
lib.dc_msg_unref lib.dc_msg_unref
)) ))
def accept_sender_contact(self):
""" ensure that the sender is an accepted contact
and that the message has a non-deaddrop chat object.
"""
self.account.create_chat_by_message(self)
self._dc_msg = ffi.gc(
lib.dc_get_msg(self._dc_context, self.id),
lib.dc_msg_unref
)
@props.with_doc @props.with_doc
def text(self): def text(self):
"""unicode text of this messages (might be empty if not a text message). """ """unicode text of this messages (might be empty if not a text message). """

View File

@@ -276,7 +276,7 @@ class TestOfflineChat:
def test_delete_and_send_fails(self, ac1, chat1): def test_delete_and_send_fails(self, ac1, chat1):
chat1.delete() chat1.delete()
ac1._evtracker.get_matching("DC_EVENT_MSGS_CHANGED") ac1._evtracker.wait_next_messages_changed()
with pytest.raises(ValueError): with pytest.raises(ValueError):
chat1.send_text("msg1") chat1.send_text("msg1")
@@ -625,8 +625,8 @@ class TestOnlineAccount:
ev = ac1._evtracker.get_matching("DC_EVENT_DELETED_BLOB_FILE") ev = ac1._evtracker.get_matching("DC_EVENT_DELETED_BLOB_FILE")
# Second client receives only second message, but not the first # Second client receives only second message, but not the first
ev = ac1_clone._evtracker.get_matching("DC_EVENT_MSGS_CHANGED") ev_msg = ac1_clone._evtracker.wait_next_messages_changed()
assert ac1_clone.get_message_by_id(ev.data2).text == msg_out.text assert ev_msg.text == msg_out.text
def test_send_file_twice_unicode_filename_mangling(self, tmpdir, acfactory, lp): def test_send_file_twice_unicode_filename_mangling(self, tmpdir, acfactory, lp):
ac1, ac2 = acfactory.get_two_online_accounts() ac1, ac2 = acfactory.get_two_online_accounts()
@@ -1377,6 +1377,15 @@ class TestOnlineAccount:
assert chat1b.get_profile_image() is None assert chat1b.get_profile_image() is None
assert chat.get_profile_image() is None assert chat.get_profile_image() is None
def test_accept_sender_contact(self, acfactory, lp):
ac1, ac2 = acfactory.get_two_online_accounts()
ch = ac1.create_chat_by_contact(ac1.create_contact(ac2.get_config("addr")))
ch.send_text("hello")
msg = ac2._evtracker.wait_next_messages_changed()
assert msg.chat.is_deaddrop()
msg.accept_sender_contact()
assert not msg.chat.is_deaddrop()
def test_send_receive_locations(self, acfactory, lp): def test_send_receive_locations(self, acfactory, lp):
now = datetime.utcnow() now = datetime.utcnow()
ac1, ac2 = acfactory.get_two_online_accounts() ac1, ac2 = acfactory.get_two_online_accounts()