From 438e9d2f0e2eef80bd9d69a3760955de7fb624a7 Mon Sep 17 00:00:00 2001 From: KurimuzonAkuma Date: Sun, 4 Aug 2024 15:31:31 +0300 Subject: [PATCH] Add search_contacts, set_account_ttl and get_account_ttl method --- compiler/docs/compiler.py | 7 ++ compiler/docs/template/methods.rst | 13 ++++ pyrogram/methods/__init__.py | 2 + pyrogram/methods/account/__init__.py | 27 +++++++ pyrogram/methods/account/get_account_ttl.py | 44 +++++++++++ pyrogram/methods/account/set_account_ttl.py | 51 +++++++++++++ pyrogram/methods/contacts/__init__.py | 4 +- pyrogram/methods/contacts/search_contacts.py | 59 +++++++++++++++ pyrogram/types/user_and_chats/__init__.py | 2 + .../types/user_and_chats/found_contacts.py | 75 +++++++++++++++++++ 10 files changed, 283 insertions(+), 1 deletion(-) create mode 100644 pyrogram/methods/account/__init__.py create mode 100644 pyrogram/methods/account/get_account_ttl.py create mode 100644 pyrogram/methods/account/set_account_ttl.py create mode 100644 pyrogram/methods/contacts/search_contacts.py create mode 100644 pyrogram/types/user_and_chats/found_contacts.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 483989df..5448c674 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -314,6 +314,7 @@ def pyrogram_api(): import_contacts get_contacts get_contacts_count + search_contacts """, payments=""" Payments @@ -406,6 +407,11 @@ def pyrogram_api(): apply_boost get_boosts get_boosts_status + """, + account=""" + Account + get_account_ttl + set_account_ttl """ ) @@ -475,6 +481,7 @@ def pyrogram_api(): Folder GroupCallMember ChatColor + FoundContacts """, messages_media=""" Messages & Media diff --git a/compiler/docs/template/methods.rst b/compiler/docs/template/methods.rst index 4fd34e1e..0c136a06 100644 --- a/compiler/docs/template/methods.rst +++ b/compiler/docs/template/methods.rst @@ -216,6 +216,19 @@ Payments {payments} +Account +------- + +.. autosummary:: + :nosignatures: + + {account} + +.. toctree:: + :hidden: + + {account} + Advanced -------- diff --git a/pyrogram/methods/__init__.py b/pyrogram/methods/__init__.py index bcf4ebe0..b91bf8f2 100644 --- a/pyrogram/methods/__init__.py +++ b/pyrogram/methods/__init__.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from .account import Account from .advanced import Advanced from .auth import Auth from .business import Business @@ -35,6 +36,7 @@ from .utilities import Utilities class Methods( + Account, Advanced, Auth, Business, diff --git a/pyrogram/methods/account/__init__.py b/pyrogram/methods/account/__init__.py new file mode 100644 index 00000000..e9065823 --- /dev/null +++ b/pyrogram/methods/account/__init__.py @@ -0,0 +1,27 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from .get_account_ttl import GetAccountTTL +from .set_account_ttl import SetAccountTTL + + +class Account( + GetAccountTTL, + SetAccountTTL +): + pass diff --git a/pyrogram/methods/account/get_account_ttl.py b/pyrogram/methods/account/get_account_ttl.py new file mode 100644 index 00000000..b468c5da --- /dev/null +++ b/pyrogram/methods/account/get_account_ttl.py @@ -0,0 +1,44 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import pyrogram +from pyrogram import raw + + +class GetAccountTTL: + async def get_account_ttl( + self: "pyrogram.Client", + ): + """Get days to live of account. + + .. include:: /_includes/usable-by/users.rst + + Returns: + ``int``: Time to live in days of the current account. + + Example: + .. code-block:: python + + # Get ttl in days + await app.get_account_ttl() + """ + r = await self.invoke( + raw.functions.account.GetAccountTTL() + ) + + return r.days diff --git a/pyrogram/methods/account/set_account_ttl.py b/pyrogram/methods/account/set_account_ttl.py new file mode 100644 index 00000000..45cc37ee --- /dev/null +++ b/pyrogram/methods/account/set_account_ttl.py @@ -0,0 +1,51 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import pyrogram +from pyrogram import raw + + +class SetAccountTTL: + async def set_account_ttl( + self: "pyrogram.Client", + days: int + ): + """Set days to live of account. + + .. include:: /_includes/usable-by/users.rst + + Parameters: + days (``int``): + Time to live in days. + + Returns: + ``bool``: On success, True is returned. + + Example: + .. code-block:: python + + # Set ttl in days + await app.set_account_ttl(365) + """ + r = await self.invoke( + raw.functions.account.SetAccountTTL( + ttl=raw.types.AccountDaysTTL(days=days) + ) + ) + + return r diff --git a/pyrogram/methods/contacts/__init__.py b/pyrogram/methods/contacts/__init__.py index 5849ce43..cf069b2a 100644 --- a/pyrogram/methods/contacts/__init__.py +++ b/pyrogram/methods/contacts/__init__.py @@ -21,6 +21,7 @@ from .delete_contacts import DeleteContacts from .get_contacts import GetContacts from .get_contacts_count import GetContactsCount from .import_contacts import ImportContacts +from .search_contacts import SearchContacts class Contacts( @@ -28,6 +29,7 @@ class Contacts( DeleteContacts, ImportContacts, GetContactsCount, - AddContact + AddContact, + SearchContacts ): pass diff --git a/pyrogram/methods/contacts/search_contacts.py b/pyrogram/methods/contacts/search_contacts.py new file mode 100644 index 00000000..5e9591a2 --- /dev/null +++ b/pyrogram/methods/contacts/search_contacts.py @@ -0,0 +1,59 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import pyrogram +from pyrogram import raw +from pyrogram import types + + +class SearchContacts: + async def search_contacts( + self: "pyrogram.Client", + query: str, + limit: int = 0 + ): + """Returns users or channels found by name substring and auxiliary data. + + .. include:: /_includes/usable-by/users.rst + + Parameters: + query (``str``): + Target substring. + + limit (``int``, *optional*): + Maximum number of users to be returned. + + Returns: + :obj:`~pyrogram.types.FoundContacts`: On success, a list of chats is returned. + + Example: + .. code-block:: python + + await app.search_contacts("pyrogram") + """ + total = limit or (1 << 31) - 1 + limit = min(100, total) + + r = await self.invoke( + raw.functions.contacts.Search( + q=query, + limit=limit + ) + ) + + return types.FoundContacts._parse(self, r) diff --git a/pyrogram/types/user_and_chats/__init__.py b/pyrogram/types/user_and_chats/__init__.py index 7ebea855..944bb8d5 100644 --- a/pyrogram/types/user_and_chats/__init__.py +++ b/pyrogram/types/user_and_chats/__init__.py @@ -40,6 +40,7 @@ from .chat_reactions import ChatReactions from .dialog import Dialog from .emoji_status import EmojiStatus from .folder import Folder +from .found_contacts import FoundContacts from .group_call_member import GroupCallMember from .invite_link_importer import InviteLinkImporter from .restriction import Restriction @@ -82,6 +83,7 @@ __all__ = [ "ChatJoiner", "EmojiStatus", "Folder", + "FoundContacts", "GroupCallMember", "ChatReactions" ] diff --git a/pyrogram/types/user_and_chats/found_contacts.py b/pyrogram/types/user_and_chats/found_contacts.py new file mode 100644 index 00000000..e5342ff3 --- /dev/null +++ b/pyrogram/types/user_and_chats/found_contacts.py @@ -0,0 +1,75 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from typing import Optional + +import pyrogram +from pyrogram import raw +from pyrogram import utils +from pyrogram import types +from ..object import Object + + +class FoundContacts(Object): + """Chats found by name substring and auxiliary data. + + Parameters: + my_results (List of :obj:`~pyrogram.types.Chat`, *optional*): + Personalized results. + + global_results (List of :obj:`~pyrogram.types.Chat`, *optional*): + List of found chats in global search. + """ + + def __init__( + self, + *, + client: "pyrogram.Client" = None, + my_results: Optional["types.Chat"] = None, + global_results: Optional["types.Chat"] = None + ): + super().__init__(client) + + self.my_results = my_results + self.global_results = global_results + + @staticmethod + def _parse(client, found: "raw.types.contacts.Found") -> "FoundContacts": + users = {u.id: u for u in found.users} + chats = {c.id: c for c in found.chats} + + my_results = [] + global_results = [] + + for result in found.my_results: + peer_id = utils.get_raw_peer_id(result) + peer = users.get(peer_id) or chats.get(peer_id) + + my_results.append(types.Chat._parse_chat(client, peer)) + + for result in found.results: + peer_id = utils.get_raw_peer_id(result) + peer = users.get(peer_id) or chats.get(peer_id) + + global_results.append(types.Chat._parse_chat(client, peer)) + + return FoundContacts( + my_results=types.List(my_results) or None, + global_results=types.List(global_results) or None, + client=client + )