From da05614b0809fe7984d64ee81ebfc094b744e28e Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Mon, 4 May 2020 14:37:42 +0800 Subject: [PATCH] Sort chats when new message comes --- tg/controllers/__init__.py | 3 ++- tg/models/__init__.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tg/controllers/__init__.py b/tg/controllers/__init__.py index 85be9c8..cecdb33 100644 --- a/tg/controllers/__init__.py +++ b/tg/controllers/__init__.py @@ -130,7 +130,8 @@ class Controller: def refresh_chats(self): 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.model.current_chat, self.model.get_chats(limit=self.view.chats.h) diff --git a/tg/models/__init__.py b/tg/models/__init__.py index 766037b..57e7e71 100644 --- a/tg/models/__init__.py +++ b/tg/models/__init__.py @@ -102,7 +102,7 @@ class ChatModel: def fetch_chats(self, offset=0, limit=10): if offset + limit < len(self.chats): # 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) @@ -111,13 +111,13 @@ class ChatModel: limit=len(self.chats) + limit ) 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: chat = self.fetch_chat(chat_id) 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): if len(self.chats):