Skip to content

Commit

Permalink
Add search_contacts, set_account_ttl and get_account_ttl method
Browse files Browse the repository at this point in the history
  • Loading branch information
KurimuzonAkuma committed Aug 4, 2024
1 parent 151bbd7 commit 4dfdb81
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 1 deletion.
7 changes: 7 additions & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def get_title_list(s: str) -> list:
import_contacts
get_contacts
get_contacts_count
search_contacts
""",
payments="""
Payments
Expand Down Expand Up @@ -406,6 +407,11 @@ def get_title_list(s: str) -> list:
apply_boost
get_boosts
get_boosts_status
""",
account="""
Account
get_account_ttl
set_account_ttl
"""
)

Expand Down Expand Up @@ -475,6 +481,7 @@ def get_title_list(s: str) -> list:
Folder
GroupCallMember
ChatColor
FoundContacts
""",
messages_media="""
Messages & Media
Expand Down
13 changes: 13 additions & 0 deletions compiler/docs/template/methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ Payments

{payments}

Account
-------

.. autosummary::
:nosignatures:

{account}

.. toctree::
:hidden:

{account}

Advanced
--------

Expand Down
2 changes: 2 additions & 0 deletions pyrogram/methods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from .account import Account
from .advanced import Advanced
from .auth import Auth
from .business import Business
Expand All @@ -35,6 +36,7 @@


class Methods(
Account,
Advanced,
Auth,
Business,
Expand Down
27 changes: 27 additions & 0 deletions pyrogram/methods/account/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.

from .get_account_ttl import GetAccountTTL
from .set_account_ttl import SetAccountTTL


class Account(
GetAccountTTL,
SetAccountTTL
):
pass
44 changes: 44 additions & 0 deletions pyrogram/methods/account/get_account_ttl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.

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
51 changes: 51 additions & 0 deletions pyrogram/methods/account/set_account_ttl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.

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
4 changes: 3 additions & 1 deletion pyrogram/methods/contacts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
from .get_contacts import GetContacts
from .get_contacts_count import GetContactsCount
from .import_contacts import ImportContacts
from .search_contacts import SearchContacts


class Contacts(
GetContacts,
DeleteContacts,
ImportContacts,
GetContactsCount,
AddContact
AddContact,
SearchContacts
):
pass
59 changes: 59 additions & 0 deletions pyrogram/methods/contacts/search_contacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.

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)
2 changes: 2 additions & 0 deletions pyrogram/types/user_and_chats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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
Expand Down Expand Up @@ -82,6 +83,7 @@
"ChatJoiner",
"EmojiStatus",
"Folder",
"FoundContacts",
"GroupCallMember",
"ChatReactions"
]
75 changes: 75 additions & 0 deletions pyrogram/types/user_and_chats/found_contacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.

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.
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,
results: Optional["types.Chat"] = None
):
super().__init__(client)

self.my_results = my_results
self.results = 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 = []
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)

results.append(types.Chat._parse_chat(client, peer))

return FoundContacts(
my_results=types.List(my_results) or None,
results=types.List(results) or None,
client=client
)

0 comments on commit 4dfdb81

Please sign in to comment.