From 3472ae798dd33a29f2cc08645a59367e57bd3220 Mon Sep 17 00:00:00 2001 From: kirill Date: Tue, 21 Jan 2025 22:55:24 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D1=87=D1=82=D0=BE=20=D1=82=D0=BE=20=D1=83?= =?UTF-8?q?=D0=BD=D1=8B=D0=BB=D0=BE=20=D0=BA=D0=B0=D0=BA=20=D1=82=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.py | 19 ++++++++++++++++--- widgets/dialog.py | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/app.py b/app/app.py index 8a806b8..a3c1f09 100644 --- a/app/app.py +++ b/app/app.py @@ -6,6 +6,7 @@ from widgets.chat import Chat from widgets.dialog import Dialog from telegram.client import TelegramClientWrapper from tokens import api_id, api_hash +from time import sleep class TelegramTUI(App): CSS_PATH = "../tcss/style.tcss" @@ -16,17 +17,28 @@ class TelegramTUI(App): async def on_mount(self) -> None: self.chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container") - self.limit = 25 for i in range(self.limit): chat = Chat(id=f"chat-{i + 1}") self.chat_container.mount(chat) + #self.mount_chats(limit=25) await self.telegram_client.connect() + await self.update_chat_list() + # TODO: скоро сюда переедет маунт чатов из функции on_mount - def mount_chats(self): - pass + # P.S. сделано, но неудачно + def mount_chats(self, limit: int): + self.limit = limit + chats_amount = len(self.chat_container.query(Chat)) + if limit > chats_amount: + for i in range(limit - chats_amount): + chat = Chat(id=f"chat-{i + 1 + (limit - chats_amount)}") + self.chat_container.mount(chat) + elif not (limit == chats_amount): + for i in range(chats_amount - limit): + self.chat_container.query(Chat).last().remove() async def update_chat_list(self): dialogs = await self.telegram_client.get_dialogs(limit=self.limit) @@ -42,6 +54,7 @@ class TelegramTUI(App): with Horizontal(id="main_container"): with Horizontal(id="chats"): yield VerticalScroll(Static(id="chat_container")) + #TODO: сделать кнопку чтобы прогрузить больше чатов, это оптимизация yield Dialog() diff --git a/widgets/dialog.py b/widgets/dialog.py index 6d006c3..3c5cbde 100644 --- a/widgets/dialog.py +++ b/widgets/dialog.py @@ -15,6 +15,7 @@ class Dialog(Widget): yield Message(message="ДАТОУШЩАРШЩУРЩША!!!!", is_me=False) # должно быть примерно is_me = message.from_id == client.get_peer_id("me") # но я могу ошибаться, я это фиш если что + #TODO: сделать кнопку чтобы прогрузить больше сообщений, но при этом чтобы при перезаходе в чат оставались прогруженными только 10 сообщений, а остальные декомпоузились with Horizontal(id="input_place"): yield Input(placeholder="Сообщение", id="msg_input") From 5be6da7eeb13ec9a3fbe8c24b595bcfb881b3ad6 Mon Sep 17 00:00:00 2001 From: kirill Date: Fri, 24 Jan 2025 21:15:39 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BD=D0=B8=D1=87=D0=B5=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=82=D0=BE=D0=BB=D0=BA=D0=BE=D0=BC=20=D0=BD=D0=B5=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BC=D0=B5=D0=BD=D1=8F=D0=BB,=20=D0=BD=D0=BE=20=D0=B8?= =?UTF-8?q?=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB=20=D1=84=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D1=8E=20get=5Fdialogs,=20=D0=BA=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0=D1=8F=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89=D0=B0=D0=B5=D1=82=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D0=BD=D1=8B=D0=B9=20=D0=B4=D0=B8=D0=B0=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.py | 13 +++++++------ telegram/client.py | 17 +++++++++-------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/app.py b/app/app.py index a3c1f09..6b15054 100644 --- a/app/app.py +++ b/app/app.py @@ -1,23 +1,23 @@ -from telethon import TelegramClient, events +#from telethon import TelegramClient, events from textual.app import App, ComposeResult -from textual.containers import Horizontal, VerticalScroll, Vertical, Container -from textual.widgets import Placeholder, Label, Static, Input, Button +from textual.containers import Horizontal, VerticalScroll +from textual.widgets import Static, Footer from widgets.chat import Chat from widgets.dialog import Dialog from telegram.client import TelegramClientWrapper from tokens import api_id, api_hash -from time import sleep class TelegramTUI(App): CSS_PATH = "../tcss/style.tcss" def __init__(self): super().__init__() - self.telegram_client = TelegramClientWrapper(api_id, api_hash, self.update_chat_list) + async def on_mount(self) -> None: + self.telegram_client = TelegramClientWrapper(api_id, api_hash, self.update_chat_list) self.chat_container = self.query_one("#main_container").query_one("#chats").query_one("#chat_container") - self.limit = 25 + self.limit = 100 for i in range(self.limit): chat = Chat(id=f"chat-{i + 1}") self.chat_container.mount(chat) @@ -51,6 +51,7 @@ class TelegramTUI(App): #self.notify("Новое сообщение") #колхоз дебаг def compose(self) -> ComposeResult: + yield Footer() with Horizontal(id="main_container"): with Horizontal(id="chats"): yield VerticalScroll(Static(id="chat_container")) diff --git a/telegram/client.py b/telegram/client.py index 4a3ab25..03b5fbe 100644 --- a/telegram/client.py +++ b/telegram/client.py @@ -6,8 +6,6 @@ class TelegramClientWrapper: self.client = TelegramClient('user', api_id, api_hash) self.client.on(events.NewMessage())(self.local_message_handler) - #ни то ни то не работает, костя спаси - async def local_message_handler(self, event): await self.message_handler() @@ -17,21 +15,24 @@ class TelegramClientWrapper: async def disconnect(self): await self.client.disconnect() - async def get_dialogs(self, limit=10): + async def get_dialogs(self, limit=None): + await self.client.get_dialogs(limit=limit) dialogs_list = [] async for dialog in self.client.iter_dialogs(limit=limit): dialogs_list.append(dialog) - return [self._map_dialog(d) for d in dialogs_list] + #return [self._map_dialog(d) for d in dialogs_list] + return dialogs_list - def _map_dialog(self, dialog): +#ого: + """def _map_dialog(self, dialog): return DialogInfo( id=dialog.id, name=utils.get_display_name(dialog.entity), message=dialog.message - ) + )""" -class DialogInfo: +"""class DialogInfo: def __init__(self, id, name, message): self.id = id self.name = name - self.message = message + self.message = message"""