From 72ddd33adf76aae7e06c6299c83cd0c63f9fb2ba Mon Sep 17 00:00:00 2001 From: adbenitez Date: Tue, 6 Apr 2021 17:50:34 -0400 Subject: [PATCH 1/4] avoid for loop --- python/src/deltachat/message.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/python/src/deltachat/message.py b/python/src/deltachat/message.py index 07906ff32..a3bd6a22e 100644 --- a/python/src/deltachat/message.py +++ b/python/src/deltachat/message.py @@ -359,21 +359,21 @@ class Message(object): # 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' + 'text': const.DC_MSG_TEXT, + 'image': const.DC_MSG_IMAGE, + 'gif': const.DC_MSG_GIF, + 'audio': const.DC_MSG_AUDIO, + 'video': const.DC_MSG_VIDEO, + 'file': const.DC_MSG_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 + code = _view_type_mapping.get(view_type_name) + if code is not None: + return code raise ValueError("message typecode not found for {!r}, " - "available {!r}".format(view_type_name, list(_view_type_mapping.values()))) + "available {!r}".format(view_type_name, list(_view_type_mapping.keys()))) # From 258856c23a2ced7bd3e14fc079c8333628f604c2 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Tue, 6 Apr 2021 17:55:27 -0400 Subject: [PATCH 2/4] add sticker type --- python/src/deltachat/message.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/src/deltachat/message.py b/python/src/deltachat/message.py index a3bd6a22e..ad91ce97c 100644 --- a/python/src/deltachat/message.py +++ b/python/src/deltachat/message.py @@ -365,6 +365,7 @@ _view_type_mapping = { 'audio': const.DC_MSG_AUDIO, 'video': const.DC_MSG_VIDEO, 'file': const.DC_MSG_FILE, + 'sticker': const.DC_MSG_STICKER, } From b9beaee7d427e1235802f53fa8487b7e3aaf54cd Mon Sep 17 00:00:00 2001 From: adbenitez Date: Tue, 6 Apr 2021 18:04:33 -0400 Subject: [PATCH 3/4] update `Message.new_empty()` to allow `view_type="sticker"` and also allow using message type directly (ex. `const.DC_MSG_STICKER`) so if new message types are added, they can be used direcly without needing the python API to be updated. --- python/src/deltachat/message.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/src/deltachat/message.py b/python/src/deltachat/message.py index ad91ce97c..4587f0dd4 100644 --- a/python/src/deltachat/message.py +++ b/python/src/deltachat/message.py @@ -46,9 +46,13 @@ class Message(object): def new_empty(cls, account, view_type): """ create a non-persistent message. - :param: view_type is "text", "audio", "video", "file" + :param: view_type is the message type code or one of the strings: + "text", "audio", "video", "file", "sticker" """ - view_type_code = get_viewtype_code_from_name(view_type) + if isinstance(view_type, int): + view_type_code = view_type + else: + view_type_code = get_viewtype_code_from_name(view_type) return Message(account, ffi.gc( lib.dc_msg_new(account._dc_context, view_type_code), lib.dc_msg_unref From d1237c9f8d3b76f326dde2884c12b087fcefd4aa Mon Sep 17 00:00:00 2001 From: adbenitez Date: Sat, 10 Apr 2021 02:02:10 -0400 Subject: [PATCH 4/4] add Message.is_sticker() --- python/src/deltachat/message.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/src/deltachat/message.py b/python/src/deltachat/message.py index 4587f0dd4..c3b340051 100644 --- a/python/src/deltachat/message.py +++ b/python/src/deltachat/message.py @@ -343,6 +343,10 @@ class Message(object): """ return True if it's a gif message. """ return self._view_type == const.DC_MSG_GIF + def is_sticker(self): + """ return True if it's a sticker message. """ + return self._view_type == const.DC_MSG_STICKER + def is_audio(self): """ return True if it's an audio message. """ return self._view_type == const.DC_MSG_AUDIO