offline messages part 1

This commit is contained in:
ingvar1995 2016-06-04 22:17:32 +03:00
parent 453b5e25be
commit 42617c8816
4 changed files with 38 additions and 4 deletions

View file

@ -110,6 +110,7 @@ def friend_status_message(tox, friend_num, status_message, size, user_data):
friend = profile.get_friend_by_number(friend_num) friend = profile.get_friend_by_number(friend_num)
invoke_in_main_thread(friend.set_status_message, status_message) invoke_in_main_thread(friend.set_status_message, status_message)
print 'User #{} has new status: {}'.format(friend_num, status_message) print 'User #{} has new status: {}'.format(friend_num, status_message)
profile.send_messages(friend_num)
if profile.get_active_number() == friend_num: if profile.get_active_number() == friend_num:
invoke_in_main_thread(profile.set_active) invoke_in_main_thread(profile.set_active)

View file

@ -10,7 +10,8 @@ PAGE_SIZE = 42
MESSAGE_OWNER = { MESSAGE_OWNER = {
'ME': 0, 'ME': 0,
'FRIEND': 1 'FRIEND': 1,
'NOT_SENT': 2
} }

View file

@ -31,6 +31,9 @@ class Message(object):
def get_owner(self): def get_owner(self):
return self._owner return self._owner
def mark_as_sent(self):
self._owner = 0
class TextMessage(Message): class TextMessage(Message):
""" """

View file

@ -205,6 +205,16 @@ class Friend(Contact):
else: else:
return -1 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): def clear_corr(self):
""" """
Clear messages list Clear messages list
@ -522,6 +532,21 @@ class Profile(Contact, Singleton):
# Private messages # 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): def split_and_send(self, number, message_type, message):
""" """
Message splitting Message splitting
@ -574,18 +599,22 @@ class Profile(Contact, Singleton):
if text.startswith('/plugin '): if text.startswith('/plugin '):
plugin_support.PluginLoader.get_instance().command(text[8:]) plugin_support.PluginLoader.get_instance().command(text[8:])
self._screen.messageEdit.clear() self._screen.messageEdit.clear()
elif self.is_active_online() and text: elif text:
if text.startswith('/me '): if text.startswith('/me '):
message_type = TOX_MESSAGE_TYPE['ACTION'] message_type = TOX_MESSAGE_TYPE['ACTION']
text = text[4:] text = text[4:]
else: else:
message_type = TOX_MESSAGE_TYPE['NORMAL'] message_type = TOX_MESSAGE_TYPE['NORMAL']
friend = self._friends[self._active_friend] friend = self._friends[self._active_friend]
if friend.status is not None:
self.split_and_send(friend.number, message_type, text.encode('utf-8')) 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.create_message_item(text, curr_time(), self._name, message_type)
self._screen.messageEdit.clear() self._screen.messageEdit.clear()
self._messages.scrollToBottom() 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 # History support