diff --git a/tg/controllers.py b/tg/controllers.py index 1cd5eec..4f7e709 100644 --- a/tg/controllers.py +++ b/tg/controllers.py @@ -13,7 +13,7 @@ from telegram.utils import AsyncResult from tg import config from tg.models import Model from tg.msg import MsgProxy -from tg.tdlib import ChatAction, ChatType, Tdlib, get_chat_type +from tg.tdlib import ChatAction, ChatType, Tdlib, UserType, get_chat_type from tg.utils import ( get_duration, get_mime, @@ -76,6 +76,35 @@ class Controller: self.tg = tg self.chat_size = 0.5 + @bind(msg_handler, ["u"]) + def user_info(self) -> None: + """Show user profile""" + msg = MsgProxy(self.model.current_msg) + user_id = msg.sender_id + users = self.model.users + name = users.get_user_label(user_id) + status = users.get_status(user_id) + user = users.get_user(user_id) + user_info = users.get_user_full_info(user_id) + user_type = None + try: + user_type = UserType[user["type"]["@type"]].value + except KeyError: + pass + info = { + name: status, + "Username": user.get("username", ""), + "UserId": user_id, + "Bio": user_info.get("bio", ""), + "Phone": user.get("phone_number", ""), + "Type": user_type, + } + with suspend(self.view) as s: + s.run_with_input( + config.VIEW_TEXT_CMD, + "\n".join(f"{k}: {v}" for k, v in info.items() if v), + ) + @bind(msg_handler, ["o"]) def open_url(self) -> None: msg = MsgProxy(self.model.current_msg) diff --git a/tg/models.py b/tg/models.py index c139d43..07dd268 100644 --- a/tg/models.py +++ b/tg/models.py @@ -608,12 +608,27 @@ class UserModel: return True return False + def get_user_full_info(self, user_id: int) -> Dict[str, Any]: + user = self.get_user(user_id) + if not user: + return user + if user.get("full_info"): + return user["full_info"] + + result = self.tg.get_user_full_info(user_id) + result.wait() + if result.error: + log.warning(f"get user full info error: {result.error_info}") + return {} + user["full_info"] = result.update + return result.update + def get_user(self, user_id: int) -> Dict[str, Any]: if user_id in self.not_found: return {} if user_id in self.users: return self.users[user_id] - result = self.tg.call_method("getUser", {"user_id": user_id}) + result = self.tg.get_user(user_id) result.wait() if result.error: log.warning(f"get user error: {result.error_info}") diff --git a/tg/tdlib.py b/tg/tdlib.py index d4713c9..9f07094 100644 --- a/tg/tdlib.py +++ b/tg/tdlib.py @@ -37,6 +37,13 @@ class UserStatus(Enum): userStatusLastMonth = "last month" +class UserType(Enum): + userTypeRegular = "" + userTypeDeleted = "deleted" + userTypeBot = "bot" + userTypeUnknown = "unknownn" + + class TextParseModeInput(Enum): textParseModeMarkdown = "markdown" textParseModeHTML = "html" @@ -85,18 +92,15 @@ class Tdlib(Telegram): limit: int = 0, synchronous: bool = False, ) -> None: - result = self.call_method( - "downloadFile", - params=dict( - file_id=file_id, - priority=priority, - offset=offset, - limit=limit, - synchronous=synchronous, - ), - block=False, - ) - result.wait() + data = { + "@type": "downloadFile", + "file_id": file_id, + "priority": priority, + "offset": offset, + "limit": limit, + "synchronous": synchronous, + } + return self._send_data(data) def reply_message( self, chat_id: int, reply_to_message_id: int, text: str @@ -366,6 +370,20 @@ class Tdlib(Telegram): } return self._send_data(data) + def get_user(self, user_id: int) -> AsyncResult: + data = { + "@type": "getUser", + "user_id": user_id, + } + return self._send_data(data) + + def get_user_full_info(self, user_id: int) -> AsyncResult: + data = { + "@type": "getUserFullInfo", + "user_id": user_id, + } + return self._send_data(data) + def get_chat_type(chat: Dict[str, Any]) -> Optional[ChatType]: try: