mirror of
https://github.com/deltachat/deltachat-core.git
synced 2025-10-04 18:29:19 +02:00
- add delete_messages() and chat.get/set_name()
- forget about caching dc_chat and dc_msg and dc_chat struct references for now
This commit is contained in:
parent
cfb05bdf82
commit
5914af77c2
3 changed files with 40 additions and 15 deletions
|
@ -169,12 +169,16 @@ class Account(object):
|
||||||
lib.dc_chatlist_unref
|
lib.dc_chatlist_unref
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assert dc_chatlist != ffi.NULL
|
||||||
chatlist = []
|
chatlist = []
|
||||||
for i in range(0, lib.dc_chatlist_get_cnt(dc_chatlist)):
|
for i in range(0, lib.dc_chatlist_get_cnt(dc_chatlist)):
|
||||||
chat_id = lib.dc_chatlist_get_chat_id(dc_chatlist, i)
|
chat_id = lib.dc_chatlist_get_chat_id(dc_chatlist, i)
|
||||||
chatlist.append(Chat(self._dc_context, chat_id))
|
chatlist.append(Chat(self._dc_context, chat_id))
|
||||||
return chatlist
|
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):
|
def get_message_by_id(self, msg_id):
|
||||||
""" return Message instance. """
|
""" return Message instance. """
|
||||||
return Message(self._dc_context, msg_id)
|
return Message(self._dc_context, msg_id)
|
||||||
|
@ -201,6 +205,15 @@ class Account(object):
|
||||||
msg_ids = [msg.id for msg in messages]
|
msg_ids = [msg.id for msg in messages]
|
||||||
lib.dc_forward_msgs(self._dc_context, msg_ids, len(msg_ids), chat.id)
|
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):
|
def start(self):
|
||||||
""" configure this account object, start receiving events,
|
""" configure this account object, start receiving events,
|
||||||
start IMAP/SMTP threads. """
|
start IMAP/SMTP threads. """
|
||||||
|
@ -318,6 +331,9 @@ class EventLogger:
|
||||||
return ev
|
return ev
|
||||||
|
|
||||||
def _log_event(self, evt_name, data1, data2):
|
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:
|
if self._debug:
|
||||||
t = threading.currentThread()
|
t = threading.currentThread()
|
||||||
tname = getattr(t, "name", t)
|
tname = getattr(t, "name", t)
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
from .cutil import as_dc_charpointer, from_dc_charpointer, iter_array
|
from .cutil import as_dc_charpointer, from_dc_charpointer, iter_array
|
||||||
from .capi import lib, ffi
|
from .capi import lib, ffi
|
||||||
from .types import cached_property, property_with_doc
|
from .types import property_with_doc
|
||||||
import attr
|
import attr
|
||||||
from attr import validators as v
|
from attr import validators as v
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ class Contact(object):
|
||||||
_dc_context = attr.ib(validator=v.instance_of(ffi.CData))
|
_dc_context = attr.ib(validator=v.instance_of(ffi.CData))
|
||||||
id = attr.ib(validator=v.instance_of(int))
|
id = attr.ib(validator=v.instance_of(int))
|
||||||
|
|
||||||
@cached_property
|
@property
|
||||||
def _dc_contact(self):
|
def _dc_contact(self):
|
||||||
return ffi.gc(
|
return ffi.gc(
|
||||||
lib.dc_get_contact(self._dc_context, self.id),
|
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))
|
_dc_context = attr.ib(validator=v.instance_of(ffi.CData))
|
||||||
id = attr.ib(validator=v.instance_of(int))
|
id = attr.ib(validator=v.instance_of(int))
|
||||||
|
|
||||||
@cached_property
|
@property
|
||||||
def _dc_chat(self):
|
def _dc_chat(self):
|
||||||
return ffi.gc(
|
return ffi.gc(
|
||||||
lib.dc_get_chat(self._dc_context, self.id),
|
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)
|
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 ------------------------------
|
# ------ chat messaging API ------------------------------
|
||||||
|
|
||||||
def send_text_message(self, msg):
|
def send_text_message(self, msg):
|
||||||
|
@ -129,7 +138,7 @@ class Chat(object):
|
||||||
:returns: None
|
:returns: None
|
||||||
"""
|
"""
|
||||||
dc_array = ffi.gc(
|
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
|
lib.dc_array_unref
|
||||||
)
|
)
|
||||||
return list(iter_array(
|
return list(iter_array(
|
||||||
|
@ -147,19 +156,13 @@ class Message(object):
|
||||||
_dc_context = attr.ib(validator=v.instance_of(ffi.CData))
|
_dc_context = attr.ib(validator=v.instance_of(ffi.CData))
|
||||||
id = attr.ib(validator=v.instance_of(int))
|
id = attr.ib(validator=v.instance_of(int))
|
||||||
|
|
||||||
@cached_property
|
@property
|
||||||
def _dc_msg(self):
|
def _dc_msg(self):
|
||||||
return ffi.gc(
|
return ffi.gc(
|
||||||
lib.dc_get_msg(self._dc_context, self.id),
|
lib.dc_get_msg(self._dc_context, self.id),
|
||||||
lib.dc_msg_unref
|
lib.dc_msg_unref
|
||||||
)
|
)
|
||||||
|
|
||||||
def _refresh(self):
|
|
||||||
try:
|
|
||||||
del self._dc_msg
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def get_state(self):
|
def get_state(self):
|
||||||
""" get the message in/out state.
|
""" get the message in/out state.
|
||||||
|
|
||||||
|
@ -190,7 +193,6 @@ class MessageState(object):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _msgstate(self):
|
def _msgstate(self):
|
||||||
self.message._refresh()
|
|
||||||
return lib.dc_msg_get_state(self.message._dc_msg)
|
return lib.dc_msg_get_state(self.message._dc_msg)
|
||||||
|
|
||||||
def is_in_fresh(self):
|
def is_in_fresh(self):
|
||||||
|
|
|
@ -51,6 +51,7 @@ class TestOfflineAccount:
|
||||||
|
|
||||||
chat2 = ac1.create_chat_by_contact(contact1.id)
|
chat2 = ac1.create_chat_by_contact(contact1.id)
|
||||||
assert chat2.id == chat.id
|
assert chat2.id == chat.id
|
||||||
|
assert chat2.get_name() == chat.get_name()
|
||||||
assert chat == chat2
|
assert chat == chat2
|
||||||
assert not (chat != chat2)
|
assert not (chat != chat2)
|
||||||
|
|
||||||
|
@ -60,16 +61,19 @@ class TestOfflineAccount:
|
||||||
else:
|
else:
|
||||||
pytest.fail("could not find chat")
|
pytest.fail("could not find chat")
|
||||||
|
|
||||||
def test_group_chat(self, acfactory):
|
def test_group_chat_creation(self, acfactory):
|
||||||
ac1 = acfactory.get_offline_account()
|
ac1 = acfactory.get_offline_account()
|
||||||
contact1 = ac1.create_contact("some1@hello.com", name="some1")
|
contact1 = ac1.create_contact("some1@hello.com", name="some1")
|
||||||
contact2 = ac1.create_contact("some2@hello.com", name="some2")
|
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(contact1)
|
||||||
chat.add_contact(contact2)
|
chat.add_contact(contact2)
|
||||||
|
assert chat.get_name() == "title1"
|
||||||
assert contact1 in chat.get_contacts()
|
assert contact1 in chat.get_contacts()
|
||||||
assert contact2 in chat.get_contacts()
|
assert contact2 in chat.get_contacts()
|
||||||
assert not chat.is_promoted()
|
assert not chat.is_promoted()
|
||||||
|
chat.set_name("title2")
|
||||||
|
assert chat.get_name() == "title2"
|
||||||
|
|
||||||
def test_message(self, acfactory):
|
def test_message(self, acfactory):
|
||||||
ac1 = acfactory.get_offline_account()
|
ac1 = acfactory.get_offline_account()
|
||||||
|
@ -145,11 +149,14 @@ class TestOnlineAccount:
|
||||||
chat2 = msg_in.chat
|
chat2 = msg_in.chat
|
||||||
assert msg_in in chat2.get_messages()
|
assert msg_in in chat2.get_messages()
|
||||||
assert chat2.is_deaddrop()
|
assert chat2.is_deaddrop()
|
||||||
|
assert chat2 == ac2.get_deaddrop_chat()
|
||||||
chat3 = ac2.create_group_chat("newgroup")
|
chat3 = ac2.create_group_chat("newgroup")
|
||||||
assert not chat3.is_promoted()
|
assert not chat3.is_promoted()
|
||||||
ac2.forward_messages([msg_in], chat3)
|
ac2.forward_messages([msg_in], chat3)
|
||||||
assert chat3.is_promoted()
|
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):
|
def test_send_and_receive_message(self, acfactory):
|
||||||
ac1 = acfactory.get_live_account()
|
ac1 = acfactory.get_live_account()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue