1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-04 10:19:16 +02:00

add contact handling and tests, complete get_contacts() API with flags,query

This commit is contained in:
holger krekel 2018-09-13 23:44:20 +02:00
parent d24a1b5375
commit 9d22585cea
3 changed files with 35 additions and 8 deletions

View file

@ -12,7 +12,7 @@ deltah = joinpath(dirname(dirname(dirname(here))), "src", "deltachat.h")
def read_event_defines():
rex = re.compile(r'#define\s+(?:DC_EVENT_|DC_CONTACT_ID_|DC_CHAT)\S+\s+(\d+).*')
rex = re.compile(r'#define\s+(?:DC_EVENT_|DC_CONTACT_ID_|DC_GCL|DC_CHAT)\S+\s+([x\d]+).*')
return filter(rex.match, open(deltah))

View file

@ -129,8 +129,7 @@ class Chat(object):
def get_messages(self):
""" return list of messages in this chat. """
dc_array_t = lib.dc_get_chat_msgs(self.dc_context, self.id, 0, 0)
return map(lambda x: Message(self.dc_context, x),
iter_array_and_unref(dc_array_t))
return list(iter_array_and_unref(dc_array_t, lambda x: Message(self.dc_context, x)))
@attr.s
@ -198,8 +197,24 @@ class Account(object):
contact_id = capi.lib.dc_create_contact(self.dc_context, name, email)
return Contact(self.dc_context, contact_id)
def get_contacts(self, query=ffi.NULL, with_self=False, only_verified=False):
""" return list of :pyclass:`Contact` objects.
:query: if a string is specified, only return contacts whose name or e-mail matches query.
:only_verified: if true only return verified contacts.
:with_self: if true the self-contact is also returned.
"""
flags = 0
query = convert_to_bytes_utf8(query)
if only_verified:
flags |= lib.DC_GCL_VERIFIED_ONLY
if with_self:
flags |= lib.DC_GCL_ADD_SELF
dc_array_t = lib.dc_get_contacts(self.dc_context, flags, query)
return list(iter_array_and_unref(dc_array_t, lambda x: Contact(self.dc_context, x)))
def create_chat_by_contact(self, contact):
""" return a Chat object, created from the contact.
""" return a Chat object with the specified contact.
@param contact: chat_id (int) or contact object.
"""
@ -278,10 +293,10 @@ def convert_to_bytes_utf8(obj):
return obj
def iter_array_and_unref(dc_array_t):
def iter_array_and_unref(dc_array_t, constructor):
try:
for i in range(0, lib.dc_array_get_cnt(dc_array_t)):
yield lib.dc_array_get_id(dc_array_t, i)
yield constructor(lib.dc_array_get_id(dc_array_t, i))
finally:
lib.dc_array_unref(dc_array_t)

View file

@ -15,7 +15,7 @@ class TestOfflineAccount:
with pytest.raises(ValueError):
ac1.get_self_contact()
def test_contacts(self, acfactory):
def test_contact_attr(self, acfactory):
ac1 = acfactory.get_offline_account()
contact1 = ac1.create_contact(email="some1@hello.com", name="some1")
assert contact1.id
@ -24,6 +24,19 @@ class TestOfflineAccount:
assert not contact1.is_blocked
assert not contact1.is_verified
def test_contact_get_contacts(self, acfactory):
ac1 = acfactory.get_offline_account()
contact1 = ac1.create_contact(email="some1@hello.com", name="some1")
contacts = ac1.get_contacts()
assert len(contacts) == 1
assert contact1 in contacts
assert not ac1.get_contacts(query="some2")
assert ac1.get_contacts(query="some1")
assert not ac1.get_contacts(only_verified=True)
contacts = ac1.get_contacts(with_self=True)
assert len(contacts) == 2
def test_chat(self, acfactory):
ac1 = acfactory.get_offline_account()
contact1 = ac1.create_contact("some1@hello.com", name="some1")
@ -100,4 +113,3 @@ class TestOnlineAccount:
assert msg.text == "msg1"
messages = msg.chat.get_messages()
assert msg in messages, (msg, messages)