diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 5e39cfa4..aa628894 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -169,12 +169,16 @@ class Account(object): lib.dc_chatlist_unref ) + assert dc_chatlist != ffi.NULL chatlist = [] for i in range(0, lib.dc_chatlist_get_cnt(dc_chatlist)): chat_id = lib.dc_chatlist_get_chat_id(dc_chatlist, i) chatlist.append(Chat(self._dc_context, chat_id)) return chatlist + def get_deaddrop_chat(self): + return Chat(self._dc_context, lib.DC_CHAT_ID_DEADDROP) + def get_message_by_id(self, msg_id): """ return Message instance. """ return Message(self._dc_context, msg_id) @@ -201,6 +205,15 @@ class Account(object): msg_ids = [msg.id for msg in messages] lib.dc_forward_msgs(self._dc_context, msg_ids, len(msg_ids), chat.id) + def delete_messages(self, messages): + """ delete messages (local and remote). + + :param messages: list of :class:`Message` object. + :returns: None + """ + msg_ids = [msg.id for msg in messages] + lib.dc_delete_msgs(self._dc_context, msg_ids, len(msg_ids)) + def start(self): """ configure this account object, start receiving events, start IMAP/SMTP threads. """ @@ -318,6 +331,9 @@ class EventLogger: return ev def _log_event(self, evt_name, data1, data2): + # don't show events that are anyway empty impls now + if evt_name in ("DC_EVENT_GET_STRING", "DC_EVENT_IS_OFFLINE"): + return if self._debug: t = threading.currentThread() tname = getattr(t, "name", t) diff --git a/python/src/deltachat/chatting.py b/python/src/deltachat/chatting.py index c2e0be20..8f009a64 100644 --- a/python/src/deltachat/chatting.py +++ b/python/src/deltachat/chatting.py @@ -2,7 +2,7 @@ from .cutil import as_dc_charpointer, from_dc_charpointer, iter_array from .capi import lib, ffi -from .types import cached_property, property_with_doc +from .types import property_with_doc import attr from attr import validators as v @@ -16,7 +16,7 @@ class Contact(object): _dc_context = attr.ib(validator=v.instance_of(ffi.CData)) id = attr.ib(validator=v.instance_of(int)) - @cached_property + @property def _dc_contact(self): return ffi.gc( lib.dc_get_contact(self._dc_context, self.id), @@ -51,7 +51,7 @@ class Chat(object): _dc_context = attr.ib(validator=v.instance_of(ffi.CData)) id = attr.ib(validator=v.instance_of(int)) - @cached_property + @property def _dc_chat(self): return ffi.gc( lib.dc_get_chat(self._dc_context, self.id), @@ -71,6 +71,15 @@ class Chat(object): """ return not lib.dc_chat_is_unpromoted(self._dc_chat) + def get_name(self): + """ return name of this chat. """ + return from_dc_charpointer(lib.dc_chat_get_name(self._dc_chat)) + + def set_name(self, name): + """ set name of this chat. """ + return lib.dc_set_chat_name(self._dc_context, self.id, name) + + # ------ chat messaging API ------------------------------ def send_text_message(self, msg): @@ -129,7 +138,7 @@ class Chat(object): :returns: None """ dc_array = ffi.gc( - lib.dc_get_contacts(self._dc_context, 0, ffi.NULL), + lib.dc_get_contacts(self._dc_context, lib.DC_GCL_ADD_SELF, ffi.NULL), lib.dc_array_unref ) return list(iter_array( @@ -147,19 +156,13 @@ class Message(object): _dc_context = attr.ib(validator=v.instance_of(ffi.CData)) id = attr.ib(validator=v.instance_of(int)) - @cached_property + @property def _dc_msg(self): return ffi.gc( lib.dc_get_msg(self._dc_context, self.id), lib.dc_msg_unref ) - def _refresh(self): - try: - del self._dc_msg - except KeyError: - pass - def get_state(self): """ get the message in/out state. @@ -190,7 +193,6 @@ class MessageState(object): @property def _msgstate(self): - self.message._refresh() return lib.dc_msg_get_state(self.message._dc_msg) def is_in_fresh(self): diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 9ae20e4b..78321e51 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -51,6 +51,7 @@ class TestOfflineAccount: chat2 = ac1.create_chat_by_contact(contact1.id) assert chat2.id == chat.id + assert chat2.get_name() == chat.get_name() assert chat == chat2 assert not (chat != chat2) @@ -60,16 +61,19 @@ class TestOfflineAccount: else: pytest.fail("could not find chat") - def test_group_chat(self, acfactory): + def test_group_chat_creation(self, acfactory): ac1 = acfactory.get_offline_account() contact1 = ac1.create_contact("some1@hello.com", name="some1") contact2 = ac1.create_contact("some2@hello.com", name="some2") - chat = ac1.create_group_chat(name="chat name") + chat = ac1.create_group_chat(name="title1") chat.add_contact(contact1) chat.add_contact(contact2) + assert chat.get_name() == "title1" assert contact1 in chat.get_contacts() assert contact2 in chat.get_contacts() assert not chat.is_promoted() + chat.set_name("title2") + assert chat.get_name() == "title2" def test_message(self, acfactory): ac1 = acfactory.get_offline_account() @@ -145,11 +149,14 @@ class TestOnlineAccount: chat2 = msg_in.chat assert msg_in in chat2.get_messages() assert chat2.is_deaddrop() + assert chat2 == ac2.get_deaddrop_chat() chat3 = ac2.create_group_chat("newgroup") assert not chat3.is_promoted() ac2.forward_messages([msg_in], chat3) assert chat3.is_promoted() - assert chat3.get_messages() + messages = chat3.get_messages() + ac2.delete_messages(messages) + assert not chat3.get_messages() def test_send_and_receive_message(self, acfactory): ac1 = acfactory.get_live_account()