mirror of
https://github.com/paul-nameless/tg
synced 2025-02-16 18:48:24 +00:00
Sort chats once, when updateChatLastMessage received
This commit is contained in:
parent
180c147774
commit
8b3c7ab22d
1 changed files with 10 additions and 19 deletions
|
@ -93,21 +93,12 @@ class ChatModel:
|
||||||
def id_by_index(self, index):
|
def id_by_index(self, index):
|
||||||
if index >= len(self.chats):
|
if index >= len(self.chats):
|
||||||
return None
|
return None
|
||||||
|
return self.chats[index]["id"]
|
||||||
return sorted(
|
|
||||||
self.chats,
|
|
||||||
key=lambda it: it["last_message"]["date"],
|
|
||||||
reverse=True,
|
|
||||||
)[index]["id"]
|
|
||||||
|
|
||||||
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 sorted(
|
return self.chats[offset:limit]
|
||||||
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)
|
||||||
|
|
||||||
|
@ -115,19 +106,13 @@ class ChatModel:
|
||||||
offset=len(self.chats), limit=len(self.chats) + limit
|
offset=len(self.chats), limit=len(self.chats) + limit
|
||||||
)
|
)
|
||||||
if len(self.chat_ids) == previous_chats_num:
|
if len(self.chat_ids) == previous_chats_num:
|
||||||
return sorted(
|
return self.chats[offset:limit]
|
||||||
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 sorted(
|
return self.chats[offset:limit]
|
||||||
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):
|
||||||
|
@ -167,6 +152,12 @@ class ChatModel:
|
||||||
if c["id"] != chat_id:
|
if c["id"] != chat_id:
|
||||||
continue
|
continue
|
||||||
self.chats[i]["last_message"] = message
|
self.chats[i]["last_message"] = message
|
||||||
|
self.chats = sorted(
|
||||||
|
self.chats,
|
||||||
|
key=lambda it: it["last_message"]["date"],
|
||||||
|
reverse=True,
|
||||||
|
)
|
||||||
|
|
||||||
log.info("Updated last message")
|
log.info("Updated last message")
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Reference in a new issue