From 2883ce5c4c0ff2858f65a562a8c82f65d5ae0bc0 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Wed, 16 May 2018 14:10:24 +0300 Subject: [PATCH] messaging - db and saving fixes --- toxygen/app.py | 1 + toxygen/contacts/contact.py | 22 +++++++++++++------- toxygen/history/database.py | 6 +++--- toxygen/history/history.py | 17 +++++++++++----- toxygen/messenger/messages.py | 38 ++++++++++++++++++++++++----------- 5 files changed, 57 insertions(+), 27 deletions(-) diff --git a/toxygen/app.py b/toxygen/app.py index ef5e351..2d12876 100644 --- a/toxygen/app.py +++ b/toxygen/app.py @@ -309,6 +309,7 @@ class App: self._contacts_manager = ContactsManager(self._tox, self._settings, self._ms, self._profile_manager, self._contacts_provider, history, self._tox_dns, messages_items_factory) + history.set_contacts_manager(self._contacts_manager) self._messenger = Messenger(self._tox, self._plugin_loader, self._ms, self._contacts_manager, self._contacts_provider, messages_items_factory, profile) self._file_transfer_handler = FileTransfersHandler(self._tox, self._settings, self._contacts_provider) diff --git a/toxygen/contacts/contact.py b/toxygen/contacts/contact.py index 4c19e33..0718e5e 100644 --- a/toxygen/contacts/contact.py +++ b/toxygen/contacts/contact.py @@ -53,7 +53,7 @@ class Contact(basecontact.BaseContact): data.reverse() else: return - data = list(map(lambda tupl: TextMessage(*tupl), data)) + data = list(map(lambda p: self._get_text_message(p), data)) self._corr = data + self._corr self._history_loaded = True @@ -66,7 +66,7 @@ class Contact(basecontact.BaseContact): data = list(self._message_getter.get_all()) if data is not None and len(data): data.reverse() - data = list(map(lambda tupl: TextMessage(*tupl), data)) + data = list(map(lambda p: self._get_text_message(p), data)) self._corr = data + self._corr self._history_loaded = True @@ -76,7 +76,7 @@ class Contact(basecontact.BaseContact): :return: list of unsaved messages or [] """ messages = list(filter(lambda x: x.get_type() <= 1, self._corr)) - return list(map(lambda x: x.get_data(), messages[-self._unsaved_messages:])) if self._unsaved_messages else [] + return messages[-self._unsaved_messages:] if self._unsaved_messages else [] def get_corr(self): return self._corr[:] @@ -92,7 +92,7 @@ class Contact(basecontact.BaseContact): def get_last_message_text(self): messages = list(filter(lambda x: x.get_type() <= 1 and x.get_owner() != MESSAGE_AUTHOR['FRIEND'], self._corr)) if messages: - return messages[-1].get_data()[0] + return messages[-1].text else: return '' @@ -100,6 +100,13 @@ class Contact(basecontact.BaseContact): for message in self._corr: message.remove_widget() + @staticmethod + def _get_text_message(params): + (message, author_type, author_name, unix_time, message_type, unique_id) = params + author = MessageAuthor(author_name, author_type) + + return TextMessage(message, author, unix_time, message_type, unique_id) + # ----------------------------------------------------------------------------------------------------------------- # Unsent messages # ----------------------------------------------------------------------------------------------------------------- @@ -130,8 +137,9 @@ class Contact(basecontact.BaseContact): # ----------------------------------------------------------------------------------------------------------------- def delete_message(self, message_id): - elem = list(filter(lambda x: type(x) in (TextMessage, GroupChatMessage) and x.message_id == message_id, self._corr))[0] - tmp = list(filter(lambda x: x.get_type() <= 1, self._corr)) + elem = list(filter(lambda x: type(x) in (TextMessage, GroupChatMessage) and x.message_id == message_id, + self._corr))[0] + tmp = list(filter(lambda x: x.get_type() in (MESSAGE_TYPE['NORMAL'], MESSAGE_TYPE['ACTION']), self._corr)) if elem in tmp[-self._unsaved_messages:] and self._unsaved_messages: self._unsaved_messages -= 1 self._corr.remove(elem) @@ -143,7 +151,7 @@ class Contact(basecontact.BaseContact): Delete old messages (reduces RAM usage if messages saving is not enabled) """ def save_message(x): - if x.get_type() == 2 and (x.get_status() >= 2 or x.get_status() is None): + if x.get_type() == 2 and (x.get_status() >= 2 or x.get_status() is None): # FIXME MAGIC NUMBERS return True return x.get_owner() == MESSAGE_AUTHOR['NOT_SENT'] diff --git a/toxygen/history/database.py b/toxygen/history/database.py index 78c0bea..751c74b 100644 --- a/toxygen/history/database.py +++ b/toxygen/history/database.py @@ -67,7 +67,7 @@ class Database: ' id INTEGER PRIMARY KEY,' ' author_name TEXT,' ' message TEXT,' - ' author INTEGER,' + ' author_type INTEGER,' ' unix_time REAL,' ' message_type INTEGER' ')') @@ -95,7 +95,7 @@ class Database: try: cursor = db.cursor() cursor.executemany('INSERT INTO id' + tox_id + - '(message, author_name, author, unix_time, message_type) ' + + '(message, author_name, author_type, unix_time, message_type) ' + 'VALUES (?, ?, ?, ?, ?, ?);', messages_iter) db.commit() except: @@ -187,7 +187,7 @@ class Database: def _connect(self): self._db = connect(self._path, timeout=TIMEOUT) self._cursor = self._db.cursor() - self._cursor.execute('SELECT id, message, author, unix_time, message_type FROM id' + + self._cursor.execute('SELECT message, author_type, author_name, unix_time, message_type, id FROM id' + self._tox_id + ' ORDER BY unix_time DESC;') def _disconnect(self): diff --git a/toxygen/history/history.py b/toxygen/history/history.py index 234b640..21bad8c 100644 --- a/toxygen/history/history.py +++ b/toxygen/history/history.py @@ -10,10 +10,15 @@ class History: self._messages = main_screen.messages self._messages_items_factory = messages_items_factory self._is_loading = False + self._contacts_manager = None def __del__(self): del self._db + + def set_contacts_manager(self, contacts_manager): + self._contacts_manager = contacts_manager + # ----------------------------------------------------------------------------------------------------------------- # History support # ----------------------------------------------------------------------------------------------------------------- @@ -30,10 +35,9 @@ class History: else: messages = friend.get_unsent_messages_for_saving() self._db.delete_messages(friend.tox_id) + messages = map(lambda m: (m.text, m.author.name, m.author.type, m.time, m.type), messages) self._db.save_messages_to_db(friend.tox_id, messages) - unsent_messages = friend.get_unsent_messages() - # unsent_time = unsent_messages[0].get_data()[2] if len(unsent_messages) else time.time() + 1 - # self._db.update_messages(friend.tox_id, unsent_time) + self._db.save() def clear_history(self, friend, save_unsent=False): @@ -44,7 +48,11 @@ class History: self._db.delete_friend_from_db(friend.tox_id) def delete_message(self, message): - pass + contact = self._contacts_manager.get_curr_contact() + if message.type in (MESSAGE_TYPE['NORMAL'], MESSAGE_TYPE['ACTION']): + if message.is_saved(): + self._db.delete_message(contact.tox_id, message.id) + contact.delete_message(message.message_id) def load_history(self, friend): """ @@ -80,7 +88,6 @@ class History: return self._db.messages_getter(friend_public_key) def delete_history(self, friend): - self.clear_history(friend) self._db.delete_friend_from_db(friend.tox_id) def add_friend_to_db(self, tox_id): diff --git a/toxygen/messenger/messages.py b/toxygen/messenger/messages.py index dc70e8d..80376f4 100644 --- a/toxygen/messenger/messages.py +++ b/toxygen/messenger/messages.py @@ -22,11 +22,14 @@ class MessageAuthor: class Message: + MESSAGE_ID = 0 + def __init__(self, message_type, author, time): self._time = time self._type = message_type self._author = author self._widget = None + self._message_id = self._get_id() def get_type(self): return self._type @@ -43,6 +46,11 @@ class Message: time = property(get_time) + def get_message_id(self): + return self._message_id + + message_id = property(get_message_id) + def get_widget(self, *args): if self._widget is None: self._widget = self._create_widget(*args) @@ -60,23 +68,35 @@ class Message: def _create_widget(self, *args): pass + @staticmethod + def _get_id(): + Message.MESSAGE_ID += 1 + + return Message.MESSAGE_ID + class TextMessage(Message): """ Plain text or action message """ - def __init__(self, message, owner, time, message_type): + def __init__(self, message, owner, time, message_type, message_id=0): super().__init__(message_type, owner, time) self._message = message + self._id = message_id def get_text(self): return self._message text = property(get_text) - def get_data(self): - return self._message, self._owner, self._time, self._type + def get_id(self): + return self._id + + id = property(get_id) + + def is_saved(self): + return self._id > 0 def _create_widget(self, *args): return MessageItem(self, *args) @@ -100,9 +120,6 @@ class GroupChatMessage(TextMessage): super().__init__(id, message, owner, time, message_type) self._user_name = name - def get_data(self): - return self._message, self._owner, self._time, self._type, self._user_name - class TransferMessage(Message): """ @@ -131,18 +148,13 @@ class TransferMessage(Message): def set_status(self, value): self._status = value - def get_data(self): - return self._file_name, self._size, self._time, self._owner, self._friend_number, self._file_number, self._status - class UnsentFile(Message): + def __init__(self, id, path, data, time): super().__init__(id, MESSAGE_TYPE['FILE_TRANSFER'], 0, time) self._data, self._path = data, path - def get_data(self): - return self._path, self._data, self._time - def get_status(self): return None @@ -159,6 +171,8 @@ class InlineImage(Message): def get_data(self): return self._data + data = property(get_data) + class InfoMessage(TextMessage):