From 42617c881660f10060a039cf3ece960c487bea41 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sat, 4 Jun 2016 22:17:32 +0300 Subject: [PATCH] offline messages part 1 --- src/callbacks.py | 1 + src/history.py | 3 ++- src/messages.py | 3 +++ src/profile.py | 35 ++++++++++++++++++++++++++++++++--- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index 92e5ddb..f267dad 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -110,6 +110,7 @@ def friend_status_message(tox, friend_num, status_message, size, user_data): friend = profile.get_friend_by_number(friend_num) invoke_in_main_thread(friend.set_status_message, status_message) print 'User #{} has new status: {}'.format(friend_num, status_message) + profile.send_messages(friend_num) if profile.get_active_number() == friend_num: invoke_in_main_thread(profile.set_active) diff --git a/src/history.py b/src/history.py index dad0506..abcb58b 100644 --- a/src/history.py +++ b/src/history.py @@ -10,7 +10,8 @@ PAGE_SIZE = 42 MESSAGE_OWNER = { 'ME': 0, - 'FRIEND': 1 + 'FRIEND': 1, + 'NOT_SENT': 2 } diff --git a/src/messages.py b/src/messages.py index 2d45ce9..fa769a8 100644 --- a/src/messages.py +++ b/src/messages.py @@ -31,6 +31,9 @@ class Message(object): def get_owner(self): return self._owner + def mark_as_sent(self): + self._owner = 0 + class TextMessage(Message): """ diff --git a/src/profile.py b/src/profile.py index 7adce11..5b31439 100644 --- a/src/profile.py +++ b/src/profile.py @@ -205,6 +205,16 @@ class Friend(Contact): else: return -1 + def not_sent_messages(self): + messages = filter(lambda x: x.get_owner() == 2, self._corr) + return messages + + def mark_as_sent(self, mark_all=True): + for message in filter(lambda x: x.get_owner() == 2, self._corr): + message.mark_as_sent() + if not mark_all: + break + def clear_corr(self): """ Clear messages list @@ -522,6 +532,21 @@ class Profile(Contact, Singleton): # Private messages # ----------------------------------------------------------------------------------------------------------------- + def send_messages(self, friend_number): + """ + Send 'offline' messages to friend + """ + friend = self.get_friend_by_number(friend_number) + friend.load_corr() + messages = friend.not_sent_messages() + try: + for message in messages: + self.split_and_send(friend_number, message.get_data()[-1], message.get_data()[0].encode('utf-8')) + except: + pass + else: + friend.mark_as_sent() + def split_and_send(self, number, message_type, message): """ Message splitting @@ -574,18 +599,22 @@ class Profile(Contact, Singleton): if text.startswith('/plugin '): plugin_support.PluginLoader.get_instance().command(text[8:]) self._screen.messageEdit.clear() - elif self.is_active_online() and text: + elif text: if text.startswith('/me '): message_type = TOX_MESSAGE_TYPE['ACTION'] text = text[4:] else: message_type = TOX_MESSAGE_TYPE['NORMAL'] friend = self._friends[self._active_friend] - self.split_and_send(friend.number, message_type, text.encode('utf-8')) + if friend.status is not None: + self.split_and_send(friend.number, message_type, text.encode('utf-8')) + owner = MESSAGE_OWNER['ME'] + else: + owner = MESSAGE_OWNER['NOT_SENT'] self.create_message_item(text, curr_time(), self._name, message_type) self._screen.messageEdit.clear() self._messages.scrollToBottom() - friend.append_message(TextMessage(text, MESSAGE_OWNER['ME'], time.time(), message_type)) + friend.append_message(TextMessage(text, owner, time.time(), message_type)) # ----------------------------------------------------------------------------------------------------------------- # History support