diff --git a/python/CHANGELOG b/python/CHANGELOG index 9dc014ab..5903a597 100644 --- a/python/CHANGELOG +++ b/python/CHANGELOG @@ -1,3 +1,9 @@ +0.7 +--- + +- add Chat.delete(), Chat.send_image() and Chat.send_file() + +- renamed Chat.send_text_message to Chat.sent_text 0.6 --- diff --git a/python/src/deltachat/__init__.py b/python/src/deltachat/__init__.py index 493ecf02..e2fad9bb 100644 --- a/python/src/deltachat/__init__.py +++ b/python/src/deltachat/__init__.py @@ -2,7 +2,7 @@ from deltachat import capi, const from deltachat.capi import ffi from deltachat.account import Account # noqa -__version__ = "0.6.0" +__version__ = "0.7.0" _DC_CALLBACK_MAP = {} diff --git a/python/src/deltachat/chatting.py b/python/src/deltachat/chatting.py index b6957bc4..a1911ec1 100644 --- a/python/src/deltachat/chatting.py +++ b/python/src/deltachat/chatting.py @@ -107,19 +107,34 @@ class Chat(object): # ------ chat messaging API ------------------------------ - def send_text_message(self, msg): + def send_text(self, text): """ send a text message and return the resulting Message instance. :param msg: unicode text :raises: ValueError if message can not be send/chat does not exist. :returns: the resulting :class:`deltachat.chatting.Message` instance """ - msg = as_dc_charpointer(msg) + msg = as_dc_charpointer(text) 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) + def send_file(self, path, mime_type="application/octet-stream"): + """ send a file and return the resulting Message instance. + + :param path: path to the file. + :param mime_type: the mime-type of this file, defaults to application/octet-stream. + :raises: ValueError if message can not be send/chat does not exist. + :returns: the resulting :class:`deltachat.chatting.Message` instance + """ + path = as_dc_charpointer(path) + mtype = as_dc_charpointer(mime_type) + 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) + def send_image(self, path): """ send an image message and return the resulting Message instance. diff --git a/python/tests/data/d.png b/python/tests/data/d.png new file mode 100644 index 00000000..bba6e0c6 Binary files /dev/null and b/python/tests/data/d.png differ diff --git a/python/tests/data/r.txt b/python/tests/data/r.txt new file mode 100644 index 00000000..3e23ae48 --- /dev/null +++ b/python/tests/data/r.txt @@ -0,0 +1,2 @@ + +hello diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 7091fdcf..4bd60af9 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -90,13 +90,13 @@ class TestOfflineAccount: chat.delete() ac1._evlogger.get_matching("DC_EVENT_MSGS_CHANGED") with pytest.raises(ValueError): - chat.send_text_message("msg1") + chat.send_text("msg1") def test_message(self, acfactory): ac1 = acfactory.get_configured_offline_account() contact1 = ac1.create_contact("some1@hello.com", name="some1") chat = ac1.create_chat_by_contact(contact1) - msg = chat.send_text_message("msg1") + msg = chat.send_text("msg1") assert msg assert msg.type.is_text() assert msg.type.name == "text" @@ -114,13 +114,14 @@ class TestOfflineAccount: assert not msg_state.is_out_delivered() assert not msg_state.is_out_mdn_received() - def test_message_image(self, acfactory, data): + def test_message_image(self, acfactory, data, lp): ac1 = acfactory.get_configured_offline_account() contact1 = ac1.create_contact("some1@hello.com", name="some1") chat = ac1.create_chat_by_contact(contact1) with pytest.raises(ValueError): chat.send_image(path="notexists") fn = data.get_path("d.png") + lp.sec("sending image") msg = chat.send_image(fn) assert msg.type.name == "image" assert msg @@ -128,12 +129,31 @@ class TestOfflineAccount: assert os.path.exists(msg.filename) assert msg.filemime == "image/png" + @pytest.mark.parametrize("typein,typeout", [ + (None, "application/octet-stream"), + ("text/plain", "text/plain"), + ("image/png", "image/png"), + ]) + def test_message_file(self, acfactory, data, lp, typein, typeout): + ac1 = acfactory.get_configured_offline_account() + contact1 = ac1.create_contact("some1@hello.com", name="some1") + chat = ac1.create_chat_by_contact(contact1) + lp.sec("sending file") + fn = data.get_path("r.txt") + msg = chat.send_file(fn, typein) + assert msg + assert msg.id > 0 + assert msg.type.name == "file" + assert msg.type.is_file() + assert os.path.exists(msg.filename) + assert msg.filemime == typeout + def test_chat_message_distinctions(self, acfactory): ac1 = acfactory.get_configured_offline_account() contact1 = ac1.create_contact("some1@hello.com", name="some1") chat = ac1.create_chat_by_contact(contact1) past1s = datetime.now() - timedelta(seconds=1) - msg = chat.send_text_message("msg1") + msg = chat.send_text("msg1") ts = msg.time_sent assert ts.strftime("Y") assert past1s < ts @@ -162,7 +182,7 @@ class TestOnlineAccount: wait_successful_IMAP_SMTP_connection(ac2) wait_configuration_progress(ac2, 1000) - msg_out = chat.send_text_message("message2") + msg_out = chat.send_text("message2") # wait for other account to receive ev = ac2._evlogger.get_matching("DC_EVENT_MSGS_CHANGED") @@ -195,7 +215,7 @@ class TestOnlineAccount: wait_configuration_progress(ac2, 1000) lp.sec("sending text message from ac1 to ac2") - msg_out = chat.send_text_message("message1") + msg_out = chat.send_text("message1") ev = ac1._evlogger.get_matching("DC_EVENT_MSG_DELIVERED") evt_name, data1, data2 = ev assert data1 == chat.id @@ -254,5 +274,6 @@ class TestOnlineAccount: ev = ac2._evlogger.get_matching("DC_EVENT_MSGS_CHANGED") assert ev[2] == msg_out.id msg_in = ac2.get_message_by_id(msg_out.id) + assert msg_in.type.is_image() assert os.path.exists(msg_in.filename) assert os.stat(msg_in.filename).st_size == os.stat(path).st_size diff --git a/python/tox.ini b/python/tox.ini index f2c8c474..52aac652 100644 --- a/python/tox.ini +++ b/python/tox.ini @@ -7,7 +7,6 @@ envlist = [testenv] commands = pytest -rsXx {posargs:tests} -usedevelop = True passenv = TRAVIS deps = pytest