From f04829eca00989ac29886ee836e4e1241badf261 Mon Sep 17 00:00:00 2001 From: navjot Date: Sat, 2 May 2020 14:38:30 +0530 Subject: [PATCH] created chat table structure and added messagetypes --- src/class_sqlThread.py | 16 ++++++++++++++++ src/messagetypes/__init__.py | 2 +- src/messagetypes/chatmsg.py | 34 ++++++++++++++++++++++++++++++++++ src/tests/test_chatmsg.py | 23 +++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/messagetypes/chatmsg.py create mode 100644 src/tests/test_chatmsg.py diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py index 6bfdcb25..9355391a 100644 --- a/src/class_sqlThread.py +++ b/src/class_sqlThread.py @@ -392,6 +392,22 @@ class sqlThread(threading.Thread): ' and removing the hash field.') self.cur.execute('''update settings set value=10 WHERE key='version';''') + # Add a new table: chat and chatdata for storing chating conversation + item = '''SELECT value FROM settings WHERE key='version';''' + parameters = '' + self.cur.execute(item, parameters) + currentVersion = int(self.cur.fetchall()[0][0]) + if currentVersion == 10: + self.cur.execute('''DROP TABLE chat''') + self.cur.execute( + '''CREATE TABLE chat''' + ''' (msgid blob, senderaddress text, receiveraddress text, message text,''' + ''' senttime text, receivedtime text, image blob, audio blob, reference blob,''' + ''' UNIQUE(msgid) ON CONFLICT REPLACE)''') + item = '''update settings set value=? WHERE key='version';''' + parameters = (11,) + self.cur.execute(item, parameters) + # Are you hoping to add a new option to the keys.dat file of existing # Bitmessage users or modify the SQLite database? Add it right # above this line! diff --git a/src/messagetypes/__init__.py b/src/messagetypes/__init__.py index 54d32794..417fd169 100644 --- a/src/messagetypes/__init__.py +++ b/src/messagetypes/__init__.py @@ -20,7 +20,7 @@ class MsgBase(object): # pylint: disable=too-few-public-methods def constructObject(data): """Constructing an object""" - whitelist = ["message"] + whitelist = ["message", "chatmsg"] if data[""] not in whitelist: return None try: diff --git a/src/messagetypes/chatmsg.py b/src/messagetypes/chatmsg.py new file mode 100644 index 00000000..41109b6d --- /dev/null +++ b/src/messagetypes/chatmsg.py @@ -0,0 +1,34 @@ +import logging + +from messagetypes import MsgBase +# pylint: disable=attribute-defined-outside-init + +logger = logging.getLogger('default') + + +class Chatmsg(MsgBase): + """Encapsulate a chatmsg""" + # pylint: disable=attribute-defined-outside-init + + def decode(self, data): + """Decode a message""" + # UTF-8 and variable type validator + if isinstance(data["message"], str): + # Unicode is depreciated + self.message = data["message"] + else: + # Unicode is depreciated + self.message = str(data["message"]) + + def encode(self, data): + """Encode a message""" + super(Chatmsg, self).__init__() + try: + self.data["message"] = data["message"] + except KeyError as e: + logger.error("Missing key %s", e) + return self.data + + def process(self): + """Process a message""" + logger.debug("Message: %i bytes", len(self.message)) diff --git a/src/tests/test_chatmsg.py b/src/tests/test_chatmsg.py new file mode 100644 index 00000000..87459a22 --- /dev/null +++ b/src/tests/test_chatmsg.py @@ -0,0 +1,23 @@ +""" +Test for chatmsg group +""" +import unittest +from messagetypes.chatmsg import Chatmsg + + +class TestCharMessage(unittest.TestCase): + """ + Test case for chat message group + """ + def test_decode(self): + """Test various types of decode method""" + chat_obj = Chatmsg() + import messagetypes + result = messagetypes.constructObject({'': 'chatmsg', 'message': 'hello world'}) + self.assertTrue(isinstance(result.message, str)) + + + def test_encode(self): + chat_obj = Chatmsg() + result = chat_obj.encode({'message':'hello world'}) + self.assertTrue(True if result['message'] else False)