From 0aed6355c3e524614c4899fe24a5b9502489d4cf Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sun, 9 Sep 2018 12:56:14 +0200 Subject: [PATCH] make chats objects grow equality operators, probably we rather want to move to "attr" which handles all of this. --- python/src/deltachat/account.py | 14 ++++++++++++++ python/tests/test_account.py | 5 ++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 736b67a3..8b314f7b 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -104,6 +104,15 @@ class Chat: self.dc_context = dc_context self.id = chat_id + def __eq__(self, other_chat): + return ( + self.dc_context == getattr(other_chat, "dc_context", None) and + self.id == getattr(other_chat, "id", None) + ) + + def __ne__(self, other_chat): + return not self == other_chat + def send_text_message(self, msg): """ return ID of the message in this chat. 'msg' should be unicode""" @@ -122,6 +131,11 @@ class Message: def text(self): return ffi_unicode(capi.lib.dc_msg_get_text(self.dc_msg)) + @property + def chat(self): + chat_id = capi.lib.dc_msg_get_chat_id(self.dc_msg) + return Chat(self.dc_context, chat_id) + class Account: def __init__(self, db_path, _logid=None): diff --git a/python/tests/test_account.py b/python/tests/test_account.py index e6442535..76353489 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -26,6 +26,8 @@ class TestOfflineAccount: chat2 = ac1.create_chat_by_contact(contact1.id) assert chat2.id == chat.id + assert chat == chat2 + assert not (chat != chat2) class TestOnlineAccount: @@ -71,7 +73,8 @@ class TestOnlineAccount: assert data1 == chat.id assert data2 == msg_id ev = ac2._evlogger.get_matching("DC_EVENT_MSGS_CHANGED") - assert ev[1] == chat.id assert ev[2] == msg_id msg = ac2.get_message(msg_id) assert msg.text == "msg1" + # note that ev[1] aka data1 contains a bogus channel id + # probably should just not get passed from the core