mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
remove MessageType, remove account.get_blob_dir which is duplicate of get_blobdir()
This commit is contained in:
@@ -11,8 +11,6 @@ high level API reference
|
|||||||
- :class:`deltachat.chatting.Contact`
|
- :class:`deltachat.chatting.Contact`
|
||||||
- :class:`deltachat.chatting.Chat`
|
- :class:`deltachat.chatting.Chat`
|
||||||
- :class:`deltachat.message.Message`
|
- :class:`deltachat.message.Message`
|
||||||
- :class:`deltachat.message.MessageType`
|
|
||||||
- :class:`deltachat.message.MessageState`
|
|
||||||
|
|
||||||
Account
|
Account
|
||||||
-------
|
-------
|
||||||
|
|||||||
@@ -69,10 +69,6 @@ class Account(object):
|
|||||||
d[key.lower()] = value
|
d[key.lower()] = value
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def get_blob_dir(self):
|
|
||||||
""" return blob directory for this account. """
|
|
||||||
return from_dc_charpointer(lib.dc_get_blobdir(self._dc_context))
|
|
||||||
|
|
||||||
def set_config(self, name, value):
|
def set_config(self, name, value):
|
||||||
""" set configuration values.
|
""" set configuration values.
|
||||||
|
|
||||||
|
|||||||
@@ -191,7 +191,7 @@ class Chat(object):
|
|||||||
|
|
||||||
:param path: path to the file.
|
:param path: path to the file.
|
||||||
:param mime_type: the mime-type of this file, defaults to auto-detection.
|
:param mime_type: the mime-type of this file, defaults to auto-detection.
|
||||||
:param view_type: passed to :meth:`MessageType.new`.
|
:param view_type: "text", "image", "gif", "audio", "video", "file"
|
||||||
:raises ValueError: if message can not be prepared/chat does not exist.
|
:raises ValueError: if message can not be prepared/chat does not exist.
|
||||||
:returns: the resulting :class:`Message` instance
|
:returns: the resulting :class:`Message` instance
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -40,8 +40,11 @@ class Message(object):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def new_empty(cls, account, view_type):
|
def new_empty(cls, account, view_type):
|
||||||
""" create a non-persistent message. """
|
""" create a non-persistent message.
|
||||||
view_type_code = MessageType.get_typecode(view_type)
|
|
||||||
|
:param: view_type is "text", "audio", "video", "file"
|
||||||
|
"""
|
||||||
|
view_type_code = get_viewtype_code_from_name(view_type)
|
||||||
return Message(account, ffi.gc(
|
return Message(account, ffi.gc(
|
||||||
lib.dc_msg_new(account._dc_context, view_type_code),
|
lib.dc_msg_new(account._dc_context, view_type_code),
|
||||||
lib.dc_msg_unref
|
lib.dc_msg_unref
|
||||||
@@ -68,7 +71,7 @@ class Message(object):
|
|||||||
mtype = ffi.NULL if mime_type is None else as_dc_charpointer(mime_type)
|
mtype = ffi.NULL if mime_type is None else as_dc_charpointer(mime_type)
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
raise ValueError("path does not exist: {!r}".format(path))
|
raise ValueError("path does not exist: {!r}".format(path))
|
||||||
blobdir = self.account.get_blob_dir()
|
blobdir = self.account.get_blobdir()
|
||||||
if not path.startswith(blobdir):
|
if not path.startswith(blobdir):
|
||||||
for i in range(50):
|
for i in range(50):
|
||||||
ext = "" if i == 0 else "-" + str(i)
|
ext = "" if i == 0 else "-" + str(i)
|
||||||
@@ -93,15 +96,6 @@ class Message(object):
|
|||||||
"""mime type of the file (if it exists)"""
|
"""mime type of the file (if it exists)"""
|
||||||
return from_dc_charpointer(lib.dc_msg_get_filemime(self._dc_msg))
|
return from_dc_charpointer(lib.dc_msg_get_filemime(self._dc_msg))
|
||||||
|
|
||||||
@props.with_doc
|
|
||||||
def view_type(self):
|
|
||||||
"""the view type of this message.
|
|
||||||
|
|
||||||
:returns: a :class:`deltachat.message.MessageType` instance.
|
|
||||||
"""
|
|
||||||
assert self.id > 0
|
|
||||||
return MessageType(lib.dc_msg_get_viewtype(self._dc_msg))
|
|
||||||
|
|
||||||
def is_setup_message(self):
|
def is_setup_message(self):
|
||||||
""" return True if this message is a setup message. """
|
""" return True if this message is a setup message. """
|
||||||
return lib.dc_msg_is_setupmessage(self._dc_msg)
|
return lib.dc_msg_is_setupmessage(self._dc_msg)
|
||||||
@@ -239,56 +233,55 @@ class Message(object):
|
|||||||
"""
|
"""
|
||||||
return self._msgstate == const.DC_STATE_OUT_MDN_RCVD
|
return self._msgstate == const.DC_STATE_OUT_MDN_RCVD
|
||||||
|
|
||||||
|
#
|
||||||
|
# Message type query methods
|
||||||
|
#
|
||||||
|
|
||||||
class MessageType(object):
|
@property
|
||||||
""" DeltaChat message type, with is_* methods. """
|
def _view_type(self):
|
||||||
_mapping = {
|
assert self.id > 0
|
||||||
const.DC_MSG_TEXT: 'text',
|
return lib.dc_msg_get_viewtype(self._dc_msg)
|
||||||
const.DC_MSG_IMAGE: 'image',
|
|
||||||
const.DC_MSG_GIF: 'gif',
|
|
||||||
const.DC_MSG_AUDIO: 'audio',
|
|
||||||
const.DC_MSG_VIDEO: 'video',
|
|
||||||
const.DC_MSG_FILE: 'file'
|
|
||||||
}
|
|
||||||
|
|
||||||
def __init__(self, _type):
|
|
||||||
self._type = _type
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
return self._type == getattr(other, "_type", None)
|
|
||||||
|
|
||||||
@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))
|
|
||||||
|
|
||||||
@props.with_doc
|
|
||||||
def name(self):
|
|
||||||
""" human readable type name. """
|
|
||||||
return self._mapping.get(self._type, "")
|
|
||||||
|
|
||||||
def is_text(self):
|
def is_text(self):
|
||||||
""" return True if it's a text message. """
|
""" return True if it's a text message. """
|
||||||
return self._type == const.DC_MSG_TEXT
|
return self._view_type == const.DC_MSG_TEXT
|
||||||
|
|
||||||
def is_image(self):
|
def is_image(self):
|
||||||
""" return True if it's an image message. """
|
""" return True if it's an image message. """
|
||||||
return self._type == const.DC_MSG_IMAGE
|
return self._view_type == const.DC_MSG_IMAGE
|
||||||
|
|
||||||
def is_gif(self):
|
def is_gif(self):
|
||||||
""" return True if it's a gif message. """
|
""" return True if it's a gif message. """
|
||||||
return self._type == const.DC_MSG_GIF
|
return self._view_type == const.DC_MSG_GIF
|
||||||
|
|
||||||
def is_audio(self):
|
def is_audio(self):
|
||||||
""" return True if it's an audio message. """
|
""" return True if it's an audio message. """
|
||||||
return self._type == const.DC_MSG_AUDIO
|
return self._view_type == const.DC_MSG_AUDIO
|
||||||
|
|
||||||
def is_video(self):
|
def is_video(self):
|
||||||
""" return True if it's a video message. """
|
""" return True if it's a video message. """
|
||||||
return self._type == const.DC_MSG_VIDEO
|
return self._view_type == const.DC_MSG_VIDEO
|
||||||
|
|
||||||
def is_file(self):
|
def is_file(self):
|
||||||
""" return True if it's a file message. """
|
""" return True if it's a file message. """
|
||||||
return self._type == const.DC_MSG_FILE
|
return self._view_type == const.DC_MSG_FILE
|
||||||
|
|
||||||
|
|
||||||
|
# some code for handling DC_MSG_* view types
|
||||||
|
|
||||||
|
_view_type_mapping = {
|
||||||
|
const.DC_MSG_TEXT: 'text',
|
||||||
|
const.DC_MSG_IMAGE: 'image',
|
||||||
|
const.DC_MSG_GIF: 'gif',
|
||||||
|
const.DC_MSG_AUDIO: 'audio',
|
||||||
|
const.DC_MSG_VIDEO: 'video',
|
||||||
|
const.DC_MSG_FILE: 'file'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_viewtype_code_from_name(view_type_name):
|
||||||
|
for code, value in _view_type_mapping.items():
|
||||||
|
if value == view_type_name:
|
||||||
|
return code
|
||||||
|
raise ValueError("message typecode not found for {!r}, "
|
||||||
|
"available {!r}".format(view_type_name, list(_view_type_mapping.values())))
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ class TestOfflineChat:
|
|||||||
assert msg == ac1.get_message_by_id(msg.id)
|
assert msg == ac1.get_message_by_id(msg.id)
|
||||||
|
|
||||||
def test_prepare_file(self, ac1, chat1):
|
def test_prepare_file(self, ac1, chat1):
|
||||||
blobdir = ac1.get_blob_dir()
|
blobdir = ac1.get_blobdir()
|
||||||
p = os.path.join(blobdir, "somedata.txt")
|
p = os.path.join(blobdir, "somedata.txt")
|
||||||
with open(p, "w") as f:
|
with open(p, "w") as f:
|
||||||
f.write("some data")
|
f.write("some data")
|
||||||
@@ -180,13 +180,13 @@ class TestOfflineChat:
|
|||||||
def test_message_send_text(self, chat1):
|
def test_message_send_text(self, chat1):
|
||||||
msg = chat1.send_text("msg1")
|
msg = chat1.send_text("msg1")
|
||||||
assert msg
|
assert msg
|
||||||
assert msg.view_type.is_text()
|
assert msg.is_text()
|
||||||
assert msg.view_type.name == "text"
|
assert not msg.is_audio()
|
||||||
assert not msg.view_type.is_audio()
|
assert not msg.is_video()
|
||||||
assert not msg.view_type.is_video()
|
assert not msg.is_gif()
|
||||||
assert not msg.view_type.is_gif()
|
assert not msg.is_file()
|
||||||
assert not msg.view_type.is_file()
|
assert not msg.is_image()
|
||||||
assert not msg.view_type.is_image()
|
|
||||||
assert not msg.is_in_fresh()
|
assert not msg.is_in_fresh()
|
||||||
assert not msg.is_in_noticed()
|
assert not msg.is_in_noticed()
|
||||||
assert not msg.is_in_seen()
|
assert not msg.is_in_seen()
|
||||||
@@ -206,7 +206,7 @@ class TestOfflineChat:
|
|||||||
fn = data.get_path("d.png")
|
fn = data.get_path("d.png")
|
||||||
lp.sec("sending image")
|
lp.sec("sending image")
|
||||||
msg = chat1.send_image(fn)
|
msg = chat1.send_image(fn)
|
||||||
assert msg.view_type.name == "image"
|
assert msg.is_image()
|
||||||
assert msg
|
assert msg
|
||||||
assert msg.id > 0
|
assert msg.id > 0
|
||||||
assert os.path.exists(msg.filename)
|
assert os.path.exists(msg.filename)
|
||||||
@@ -223,8 +223,7 @@ class TestOfflineChat:
|
|||||||
msg = chat1.send_file(fn, typein)
|
msg = chat1.send_file(fn, typein)
|
||||||
assert msg
|
assert msg
|
||||||
assert msg.id > 0
|
assert msg.id > 0
|
||||||
assert msg.view_type.name == "file"
|
assert msg.is_file()
|
||||||
assert msg.view_type.is_file()
|
|
||||||
assert os.path.exists(msg.filename)
|
assert os.path.exists(msg.filename)
|
||||||
assert msg.filename.endswith(msg.basename)
|
assert msg.filename.endswith(msg.basename)
|
||||||
assert msg.filemime == typeout
|
assert msg.filemime == typeout
|
||||||
@@ -470,7 +469,7 @@ class TestOnlineAccount:
|
|||||||
ev = ac2._evlogger.get_matching("DC_EVENT_MSGS_CHANGED")
|
ev = ac2._evlogger.get_matching("DC_EVENT_MSGS_CHANGED")
|
||||||
assert ev[2] == msg_out.id
|
assert ev[2] == msg_out.id
|
||||||
msg_in = ac2.get_message_by_id(msg_out.id)
|
msg_in = ac2.get_message_by_id(msg_out.id)
|
||||||
assert msg_in.view_type.is_image()
|
assert msg_in.is_image()
|
||||||
assert os.path.exists(msg_in.filename)
|
assert os.path.exists(msg_in.filename)
|
||||||
assert os.stat(msg_in.filename).st_size == os.stat(path).st_size
|
assert os.stat(msg_in.filename).st_size == os.stat(path).st_size
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user