messaging - db and saving fixes
This commit is contained in:
parent
eef02a1173
commit
2883ce5c4c
5 changed files with 57 additions and 27 deletions
|
@ -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)
|
||||
|
|
|
@ -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']
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
||||
|
|
Loading…
Reference in a new issue