diff --git a/tg/controllers.py b/tg/controllers.py index d4766fd..0d09300 100644 --- a/tg/controllers.py +++ b/tg/controllers.py @@ -533,6 +533,35 @@ class Controller: if self.model.set_current_chat_by_id(chat_id): self.render() + @bind(msg_handler, ["/"]) + def search_messages(self) -> None: + """Search contacts and set jumps to it if found""" + msg = self.view.status.get_input(prefix="/") + if not msg: + return self.present_info("Search discarded") + + chat = self.model.chats.chats[self.model.current_chat] + chat_id = chat["id"] + msg_id = chat["last_message"]["id"] + + rv = self.tg.search_messages(msg, chat_id, msg_id) + total_count = rv.update["total_count"] + if total_count: + return self.present_info(f"Found {total_count} messages") + else: + return self.present_info("Messages not found") + + + messages = rv.update["messages"] + if chat_id not in self.model.chats.chat_ids: + self.present_info("Chat not loaded") + return + + self.model.chats.found_chats = chat_ids + + if self.model.set_current_chat_by_id(chat_id): + self.render() + @bind(chat_handler, ["c"]) def view_contacts(self) -> None: contacts = self.model.users.get_contacts() diff --git a/tg/tdlib.py b/tg/tdlib.py index b5780d5..73b7a5a 100644 --- a/tg/tdlib.py +++ b/tg/tdlib.py @@ -113,6 +113,24 @@ class Tdlib(Telegram): return self._send_data(data) + def search_messages( + self, + target: str, + offset_chat_id: int, + offset_msg_id: int, + limit: int = 10, + ) -> AsyncResult: + data = { + "@type": "searchMessages", + "query": target, + "limit": limit, + "offset_date": 0, + "offset_chat_id": offset_chat_id, + "offset_message_id": offset_msg_id, + "chat_list": None + } + return self._send_data(data, block=True) + def search_contacts(self, target: str, limit: int = 10) -> AsyncResult: data = {"@type": "searchChats", "query": target, "limit": limit} return self._send_data(data, block=True)