remove acfactory.get_chat() in favour of account.create_chat(account2) directly working.

This commit is contained in:
holger krekel
2020-06-08 17:19:21 +02:00
parent 1083cab972
commit 3a85b671a1
5 changed files with 74 additions and 21 deletions

View File

@@ -216,19 +216,24 @@ class Account(object):
def create_contact(self, email, name=None):
""" create a (new) Contact. If there already is a Contact
with that e-mail address, it is unblocked and its name is
updated.
with that e-mail address, it is unblocked and its display
name is updated.
:param email: email-address (text type)
:param email: email-address (text type) or Contact (from other account).
:param name: display name for this contact (optional)
:returns: :class:`deltachat.contact.Contact` instance.
"""
realname, addr = parseaddr(email)
if name:
realname = name
realname = as_dc_charpointer(realname)
if isinstance(email, Contact):
# might come from another account
name = email.name
addr = email.addr
else:
realname, addr = parseaddr(email)
if not name and realname:
name = realname
name = as_dc_charpointer(name)
addr = as_dc_charpointer(addr)
contact_id = lib.dc_create_contact(self._dc_context, realname, addr)
contact_id = lib.dc_create_contact(self._dc_context, name, addr)
assert contact_id > const.DC_CHAT_ID_LAST_SPECIAL
return Contact(self, contact_id)
@@ -280,6 +285,22 @@ 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. """
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)
elif isinstance(obj, Contact):
contact = obj
elif isinstance(obj, str):
realname, addr = parseaddr(obj)
contact = self.create_contact(addr, realname)
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.

View File

@@ -328,16 +328,26 @@ 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.
If the contact is from another account create a new
contact and add it to the group.
:params: contact object.
:raises ValueError: if contact could not be added
:returns: None
"""
contact = self._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))
return contact
def remove_contact(self, contact):
""" remove a contact from this chat.
@@ -346,6 +356,7 @@ class Chat(object):
:raises ValueError: if contact could not be removed
:returns: None
"""
contact = self._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

@@ -36,10 +36,13 @@ class Contact(object):
return from_dc_charpointer(lib.dc_contact_get_addr(self._dc_contact))
@props.with_doc
def display_name(self):
def name(self):
""" display name for this contact. """
return from_dc_charpointer(lib.dc_contact_get_display_name(self._dc_contact))
# deprecated alias
display_name = name
def is_blocked(self):
""" Return True if the contact is blocked. """
return lib.dc_contact_is_blocked(self._dc_contact)

View File

@@ -387,10 +387,6 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig, data):
imap.dump_account_info(logfile=logfile)
imap.dump_imap_structures(tmpdir, logfile=logfile)
def get_chat(self, ac1, ac2):
chat12, chat21 = self.get_chats(ac1, ac2, both=False)
return chat12
def get_chats(self, ac1, ac2, both=True):
chat12 = ac1.create_chat_by_contact(
ac1.create_contact(email=ac2.get_config("addr")))