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

move towards message creation API, refactor constructors for messages

This commit is contained in:
holger krekel 2018-10-07 15:07:14 +02:00
parent e45acb17a3
commit aaf37821c4
4 changed files with 55 additions and 10 deletions

View file

@ -104,12 +104,13 @@ class Account(object):
return Contact(self._dc_context, const.DC_CONTACT_ID_SELF)
def create_message(self, view_type):
""" create a new message (not yet persistent in database).
""" create a new non persistent message.
:param view_type: a string specifying "text", "video",
"image", "audio" or "file".
:returns: :class:`deltachat.chatting.Message` instance.
"""
return Message.new(self._dc_context, view_type)
def create_contact(self, email, name=None):
""" create a (new) Contact. If there already is a Contact
@ -205,8 +206,7 @@ class Account(object):
def get_message_by_id(self, msg_id):
""" return Message instance. """
assert msg_id > 0
return Message(self._dc_context, msg_id)
return Message.from_id(self._dc_context, msg_id)
def mark_seen_messages(self, messages):
""" mark the given set of messages as seen.

View file

@ -118,7 +118,7 @@ class Chat(object):
msg_id = lib.dc_send_text_msg(self._dc_context, self.id, msg)
if msg_id == 0:
raise ValueError("message could not be send, does chat exist?")
return Message(self._dc_context, msg_id)
return Message.from_id(self._dc_context, msg_id)
def send_file(self, path, mime_type="application/octet-stream"):
""" send a file and return the resulting Message instance.
@ -133,7 +133,7 @@ class Chat(object):
msg_id = lib.dc_send_file_msg(self._dc_context, self.id, path, mtype)
if msg_id == 0:
raise ValueError("message could not be send, does chat exist?")
return Message(self._dc_context, msg_id)
return Message.from_id(self._dc_context, msg_id)
def send_image(self, path):
""" send an image message and return the resulting Message instance.
@ -148,7 +148,7 @@ class Chat(object):
msg_id = lib.dc_send_image_msg(self._dc_context, self.id, path, ffi.NULL, 0, 0)
if msg_id == 0:
raise ValueError("chat does not exist")
return Message(self._dc_context, msg_id)
return Message.from_id(self._dc_context, msg_id)
def get_messages(self):
""" return list of messages in this chat.
@ -159,7 +159,7 @@ class Chat(object):
lib.dc_get_chat_msgs(self._dc_context, self.id, 0, 0),
lib.dc_array_unref
)
return list(iter_array(dc_array, lambda x: Message(self._dc_context, x)))
return list(iter_array(dc_array, lambda x: Message.from_id(self._dc_context, x)))
def count_fresh_messages(self):
""" return number of fresh messages in this chat.

View file

@ -1,6 +1,6 @@
""" chatting related objects: Contact, Chat, Message. """
from .cutil import from_dc_charpointer
from .cutil import from_dc_charpointer, as_dc_charpointer
from .capi import lib, ffi
from .types import property_with_doc
from . import const
@ -21,10 +21,32 @@ class Message(object):
@property
def _dc_msg(self):
return ffi.gc(
lib.dc_get_msg(self._dc_context, self.id),
if self.id > 0:
return ffi.gc(
lib.dc_get_msg(self._dc_context, self.id),
lib.dc_msg_unref
)
return self._dc_msg_volatile
@classmethod
def from_id(cls, _dc_context, id):
assert id > 0
return cls(_dc_context, id)
@classmethod
def new(cls, dc_context, view_type):
""" create a non-persistent method. """
msg = cls(dc_context, 0)
msg._dc_msg_volatile = ffi.gc(
lib.dc_msg_new(dc_context),
lib.dc_msg_unref
)
lib.dc_msg_set_type(msg._dc_msg, MessageType.get_typecode(view_type))
return msg
def is_persistent(self):
""" return True if the message is persistent in the database. """
return self.id > 0
def get_state(self):
""" get the message in/out state.
@ -38,11 +60,18 @@ class Message(object):
"""unicode text of this messages (might be empty if not a text message). """
return from_dc_charpointer(lib.dc_msg_get_text(self._dc_msg))
def set_text(self, text):
"""set text of this message. """
return lib.dc_msg_set_text(self._dc_msg, as_dc_charpointer(text))
@property_with_doc
def filename(self):
"""filename if there was an attachment, otherwise empty string. """
return from_dc_charpointer(lib.dc_msg_get_file(self._dc_msg))
def set_file(self, path, mime_type=None):
"""set file for this message. """
@property_with_doc
def basename(self):
"""basename of the attachment if it exists, otherwise empty string. """
@ -103,6 +132,13 @@ class MessageType(object):
const.DC_MSG_FILE: 'file'
}
@classmethod
def get_typecode(cls, view_type):
for code, value in cls._mapping.items():
if value == view_type:
return code
raise ValueError("message typecode not found for {!r}".format(view_type))
@property_with_doc
def name(self):
""" human readable type name. """

View file

@ -92,6 +92,15 @@ class TestOfflineAccount:
with pytest.raises(ValueError):
chat.send_text("msg1")
def test_create_message(self, acfactory):
ac1 = acfactory.get_configured_offline_account()
message = ac1.create_message("text")
assert message.id == 0
assert message._dc_msg is message._dc_msg
message.set_text("hello")
assert message.text == "hello"
assert not message.is_persistent()
def test_message(self, acfactory):
ac1 = acfactory.get_configured_offline_account()
contact1 = ac1.create_contact("some1@hello.com", name="some1")