From 194bd82b8cc83a3672fcbb0c7cce9b41d4a81342 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sat, 8 Sep 2018 18:38:53 +0200 Subject: [PATCH] introduce message interface --- python/src/deltachat/account.py | 24 ++++++++++++++++++++++-- python/tests/test_account.py | 13 +++++++++---- python/tox.ini | 2 +- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 231b2e93..736b67a3 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -112,6 +112,17 @@ class Chat: return capi.lib.dc_send_text_msg(self.dc_context, self.id, msg) +class Message: + def __init__(self, dc_context, msg_id): + self.dc_context = dc_context + self.id = msg_id + self.dc_msg = capi.lib.dc_get_msg(self.dc_context, msg_id) + + @property + def text(self): + return ffi_unicode(capi.lib.dc_msg_get_text(self.dc_msg)) + + class Account: def __init__(self, db_path, _logid=None): self.dc_context = ctx = capi.lib.dc_context_new( @@ -145,10 +156,19 @@ class Account: return Contact(self.dc_context, contact_id) def create_chat_by_contact(self, contact): - chat_id = capi.lib.dc_create_chat_by_contact_id(self.dc_context, contact.id) - assert chat_id >= capi.lib.DC_CHAT_ID_LAST_SPECIAL, chat_id + """ return a Chat object, created from the contact. + + @param contact: chat_id (int) or contact object. + """ + contact_id = getattr(contact, "id", contact) + assert isinstance(contact_id, int) + chat_id = capi.lib.dc_create_chat_by_contact_id( + self.dc_context, contact_id) return Chat(self.dc_context, chat_id) + def get_message(self, msg_id): + return Message(self.dc_context, msg_id) + def start(self): deltachat.set_context_callback(self.dc_context, self._process_event) capi.lib.dc_configure(self.dc_context) diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 58c89708..e6442535 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -22,7 +22,10 @@ class TestOfflineAccount: ac1 = acfactory.get_offline_account() contact1 = ac1.create_contact("some1@hello.com", name="some1") chat = ac1.create_chat_by_contact(contact1) - assert chat.id + assert chat.id >= lib.DC_CHAT_ID_LAST_SPECIAL, chat.id + + chat2 = ac1.create_chat_by_contact(contact1.id) + assert chat2.id == chat.id class TestOnlineAccount: @@ -62,11 +65,13 @@ class TestOnlineAccount: self.wait_successful_IMAP_SMTP_connection(ac2) self.wait_configuration_progress(ac1, 1000) self.wait_configuration_progress(ac2, 1000) - msgnum = chat.send_text_message("msg1") + msg_id = chat.send_text_message("msg1") ev = ac1._evlogger.get_matching("DC_EVENT_MSG_DELIVERED") evt_name, data1, data2 = ev assert data1 == chat.id - assert data2 == msgnum + assert data2 == msg_id ev = ac2._evlogger.get_matching("DC_EVENT_MSGS_CHANGED") assert ev[1] == chat.id - assert ev[2] == msgnum + assert ev[2] == msg_id + msg = ac2.get_message(msg_id) + assert msg.text == "msg1" diff --git a/python/tox.ini b/python/tox.ini index 896a8716..fad05120 100644 --- a/python/tox.ini +++ b/python/tox.ini @@ -3,9 +3,9 @@ minversion = 2.0 distshare = {homedir}/.tox/distshare # make sure to update environment list in travis.yml and appveyor.yml envlist = - lint py27 py35 + lint [testenv] commands = pytest {posargs:tests}