Sort chats when new message comes

This commit is contained in:
Paul Nameless 2020-05-04 14:37:42 +08:00
parent 3ee7a60948
commit da05614b08
2 changed files with 5 additions and 4 deletions

View file

@ -130,7 +130,8 @@ class Controller:
def refresh_chats(self): def refresh_chats(self):
with self.lock: with self.lock:
# using lock here, because model.get_chats is vulnerable to race conditions # using lock here, because refresh_chats is used from another
# thread by tdlib python wrapper
self.view.draw_chats( self.view.draw_chats(
self.model.current_chat, self.model.current_chat,
self.model.get_chats(limit=self.view.chats.h) self.model.get_chats(limit=self.view.chats.h)

View file

@ -102,7 +102,7 @@ class ChatModel:
def fetch_chats(self, offset=0, limit=10): def fetch_chats(self, offset=0, limit=10):
if offset + limit < len(self.chats): if offset + limit < len(self.chats):
# return data from cache # return data from cache
return self.chats[offset:limit] return sorted(self.chats, key=lambda it: it['last_message']['date'], reverse=True)[offset:limit]
previous_chats_num = len(self.chat_ids) previous_chats_num = len(self.chat_ids)
@ -111,13 +111,13 @@ class ChatModel:
limit=len(self.chats) + limit limit=len(self.chats) + limit
) )
if len(self.chat_ids) == previous_chats_num: if len(self.chat_ids) == previous_chats_num:
return self.chats[offset:limit] return sorted(self.chats, key=lambda it: it['last_message']['date'], reverse=True)[offset:limit]
for chat_id in self.chat_ids: for chat_id in self.chat_ids:
chat = self.fetch_chat(chat_id) chat = self.fetch_chat(chat_id)
self.chats.append(chat) self.chats.append(chat)
return self.chats[offset:limit] return sorted(self.chats, key=lambda it: it['last_message']['date'], reverse=True)[offset:limit]
def fetch_chat_ids(self, offset=0, limit=10): def fetch_chat_ids(self, offset=0, limit=10):
if len(self.chats): if len(self.chats):