diff --git a/python/CHANGELOG b/python/CHANGELOG index 7c41368c..edeb0a00 100644 --- a/python/CHANGELOG +++ b/python/CHANGELOG @@ -8,6 +8,10 @@ as it's pretty clear Chat.send_X is going to send a message to the chat) +- new Account.create_message() to create new messages + that are not in the database (yet) + + 0.6 --- diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 96afe770..3f3ae83a 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -206,7 +206,7 @@ class Account(object): def get_message_by_id(self, msg_id): """ return Message instance. """ - return Message.from_id(self._dc_context, msg_id) + return Message.from_db(self._dc_context, msg_id) def mark_seen_messages(self, messages): """ mark the given set of messages as seen. diff --git a/python/src/deltachat/chatting.py b/python/src/deltachat/chatting.py index 6900d42f..8a85730f 100644 --- a/python/src/deltachat/chatting.py +++ b/python/src/deltachat/chatting.py @@ -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.from_id(self._dc_context, msg_id) + return Message.from_db(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.from_id(self._dc_context, msg_id) + return Message.from_db(self._dc_context, msg_id) def send_image(self, path): """ send an image message and return the resulting Message instance. @@ -144,11 +144,10 @@ class Chat(object): """ if not os.path.exists(path): raise ValueError("path does not exist: {!r}".format(path)) - path = as_dc_charpointer(path) - 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.from_id(self._dc_context, msg_id) + msg = Message.new(self._dc_context, "image") + msg.set_file(path) + msg_id = lib.dc_send_msg(self._dc_context, self.id, msg._dc_msg) + return Message.from_db(self._dc_context, msg_id) def get_messages(self): """ return list of messages in this chat. @@ -159,7 +158,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.from_id(self._dc_context, x))) + return list(iter_array(dc_array, lambda x: Message.from_db(self._dc_context, x))) def count_fresh_messages(self): """ return number of fresh messages in this chat. diff --git a/python/src/deltachat/message.py b/python/src/deltachat/message.py index 1631f3ce..1ddcc310 100644 --- a/python/src/deltachat/message.py +++ b/python/src/deltachat/message.py @@ -1,5 +1,6 @@ """ chatting related objects: Contact, Chat, Message. """ +import os from .cutil import from_dc_charpointer, as_dc_charpointer from .capi import lib, ffi from .types import property_with_doc @@ -29,7 +30,7 @@ class Message(object): return self._dc_msg_volatile @classmethod - def from_id(cls, _dc_context, id): + def from_db(cls, _dc_context, id): assert id > 0 return cls(_dc_context, id) @@ -37,17 +38,14 @@ class Message(object): def new(cls, dc_context, view_type): """ create a non-persistent method. """ msg = cls(dc_context, 0) + view_type_code = MessageType.get_typecode(view_type) 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)) + lib.dc_msg_set_type(msg._dc_msg, view_type_code) 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. @@ -71,6 +69,9 @@ class Message(object): def set_file(self, path, mime_type=None): """set file for this message. """ + mtype = ffi.NULL if mime_type is None else mime_type + assert os.path.exists(path) + lib.dc_msg_set_file(self._dc_msg, as_dc_charpointer(path), mtype) @property_with_doc def basename(self): diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 55efbf56..936e9a11 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -99,7 +99,7 @@ class TestOfflineAccount: assert message._dc_msg is message._dc_msg message.set_text("hello") assert message.text == "hello" - assert not message.is_persistent() + assert message.id == 0 def test_message(self, acfactory): ac1 = acfactory.get_configured_offline_account()