refine contact API and introduce account.create_chat() helper

strike create_chat_by_contact in favor of contact.create_chat()
This commit is contained in:
holger krekel
2020-06-08 18:44:15 +02:00
parent 3a85b671a1
commit 7d5fba8416
8 changed files with 98 additions and 121 deletions

View File

@@ -214,27 +214,27 @@ class Account(object):
"""
return Contact(self, const.DC_CONTACT_ID_SELF)
def create_contact(self, email, name=None):
def create_contact(self, addr, name=None):
""" create a (new) Contact. If there already is a Contact
with that e-mail address, it is unblocked and its display
name is updated.
:param email: email-address (text type) or Contact (from other account).
:param addr: email-address (text type) or Contact (from other account).
:param name: display name for this contact (optional)
:returns: :class:`deltachat.contact.Contact` instance.
"""
if isinstance(email, Contact):
if isinstance(addr, Contact):
# might come from another account
name = email.name
addr = email.addr
name = addr.name
addr = addr.addr
else:
realname, addr = parseaddr(email)
if not name and realname:
name = realname
parse_name, addr = parseaddr(addr)
if not name and parse_name:
name = parse_name
name = as_dc_charpointer(name)
addr = as_dc_charpointer(addr)
contact_id = lib.dc_create_contact(self._dc_context, name, addr)
assert contact_id > const.DC_CHAT_ID_LAST_SPECIAL
assert contact_id > const.DC_CHAT_ID_LAST_SPECIAL, contact_id
return Contact(self, contact_id)
def delete_contact(self, contact):
@@ -256,6 +256,19 @@ class Account(object):
if contact_id:
return self.get_contact_by_id(contact_id)
def get_contact_by_id(self, contact_id):
""" return Contact instance or None.
:param contact_id: integer id of this contact.
:returns: None or :class:`deltachat.contact.Contact` instance.
"""
return Contact(self, contact_id)
def _port_contact(self, contact):
assert isinstance(contact, Contact)
if self != contact.account:
return self.create_contact(addr=contact.addr, name=contact.name)
return contact
def get_contacts(self, query=None, with_self=False, only_verified=False):
""" get a (filtered) list of contacts.
@@ -286,36 +299,19 @@ class Account(object):
yield from iter_array(dc_array, lambda x: Message.from_db(self, x))
def create_chat(self, obj):
""" Create a 1:1 chat with Account/Contact/e-mail addresses. """
""" Create a 1:1 chat with Account or e-mail addresse. """
if isinstance(obj, Account):
if not obj.is_configured():
raise ValueError("can only add addresses from a configured account")
other = obj.get_self_contact()
contact = self.create_contact(other.addr, other.name)
contact = self.create_contact(obj.get_self_contact().addr)
elif isinstance(obj, Contact):
contact = obj
contact = self._port_contact(obj)
elif isinstance(obj, str):
realname, addr = parseaddr(obj)
contact = self.create_contact(addr, realname)
name, addr = parseaddr(obj)
contact = self.create_contact(addr, name)
else:
raise TypeError("don't know how to create chat for %r" % (obj, ))
return contact.get_chat()
def create_chat_by_contact(self, contact):
""" create or get an existing 1:1 chat object for the specified contact or contact id.
:param contact: chat_id (int) or contact object.
:returns: a :class:`deltachat.chat.Chat` object.
"""
if hasattr(contact, "id"):
if contact.account != self:
raise ValueError("Contact belongs to a different Account")
contact_id = contact.id
else:
assert isinstance(contact, int)
contact_id = contact
chat_id = lib.dc_create_chat_by_contact_id(self._dc_context, contact_id)
return Chat(self, chat_id)
return contact.create_chat()
def create_chat_by_message(self, message):
""" create or get an existing chat object for the
@@ -376,13 +372,6 @@ class Account(object):
"""
return Message.from_db(self, msg_id)
def get_contact_by_id(self, contact_id):
""" return Contact instance or None.
:param contact_id: integer id of this contact.
:returns: None or :class:`deltachat.contact.Contact` instance.
"""
return Contact(self, contact_id)
def get_chat_by_id(self, chat_id):
""" return Chat instance.
:param chat_id: integer id of this chat.

View File

@@ -18,6 +18,8 @@ class Chat(object):
"""
def __init__(self, account, id):
from .account import Account
assert isinstance(account, Account), repr(account)
self.account = account
self.id = id
@@ -328,11 +330,6 @@ class Chat(object):
# ------ group management API ------------------------------
def _port_contact(self, contact):
if contact.account != self.account:
contact = self.account.create_contact(contact.addr, contact.display_name)
return contact
def add_contact(self, contact):
""" add a contact to this chat.
@@ -343,7 +340,7 @@ class Chat(object):
:raises ValueError: if contact could not be added
:returns: None
"""
contact = self._port_contact(contact)
contact = self.account._port_contact(contact)
ret = lib.dc_add_contact_to_chat(self.account._dc_context, self.id, contact.id)
if ret != 1:
raise ValueError("could not add contact {!r} to chat".format(contact))
@@ -356,7 +353,7 @@ class Chat(object):
:raises ValueError: if contact could not be removed
:returns: None
"""
contact = self._port_contact(contact)
contact = self.account._port_contact(contact)
ret = lib.dc_remove_contact_from_chat(self.account._dc_context, self.id, contact.id)
if ret != 1:
raise ValueError("could not remove contact {!r} from chat".format(contact))

View File

@@ -3,6 +3,8 @@
from . import props
from .cutil import from_dc_charpointer
from .capi import lib, ffi
from .chat import Chat
from . import const
class Contact(object):
@@ -11,6 +13,8 @@ class Contact(object):
You obtain instances of it through :class:`deltachat.account.Account`.
"""
def __init__(self, account, id):
from .account import Account
assert isinstance(account, Account), repr(account)
self.account = account
self.id = id
@@ -61,6 +65,16 @@ class Contact(object):
return None
return from_dc_charpointer(dc_res)
def get_chat(self):
"""return 1:1 chat for this contact. """
return self.account.create_chat_by_contact(self)
def create_chat(self):
""" create or get an existing 1:1 chat object for the specified contact or contact id.
:param contact: chat_id (int) or contact object.
:returns: a :class:`deltachat.chat.Chat` object.
"""
dc_context = self.account._dc_context
chat_id = lib.dc_create_chat_by_contact_id(dc_context, self.id)
assert chat_id > const.DC_CHAT_ID_LAST_SPECIAL, chat_id
return Chat(self.account, chat_id)
# deprecated name
get_chat = create_chat

View File

@@ -387,14 +387,9 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig, data):
imap.dump_account_info(logfile=logfile)
imap.dump_imap_structures(tmpdir, logfile=logfile)
def get_chats(self, ac1, ac2, both=True):
chat12 = ac1.create_chat_by_contact(
ac1.create_contact(email=ac2.get_config("addr")))
chat21 = None
if both:
chat21 = ac2.create_chat_by_contact(
ac2.create_contact(email=ac1.get_config("addr")))
return chat12, chat21
def get_accepted_chat(self, ac1, ac2):
ac2.create_chat(ac1)
return ac1.create_chat(ac2)
am = AccountMaker()
request.addfinalizer(am.finalize)