Skip to content

Commit

Permalink
Refactor folders
Browse files Browse the repository at this point in the history
  • Loading branch information
KurimuzonAkuma committed Dec 1, 2023
1 parent 97aeee2 commit 511b7e3
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 143 deletions.
1 change: 0 additions & 1 deletion compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ def get_title_list(s: str) -> list:
toggle_forum_topics
delete_folder
export_folder_link
get_folder
get_folders
update_folder
""",
Expand Down
2 changes: 0 additions & 2 deletions pyrogram/methods/chats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from .get_chat_online_count import GetChatOnlineCount
from .get_dialogs import GetDialogs
from .get_dialogs_count import GetDialogsCount
from .get_folder import GetFolder
from .get_folders import GetFolders
from .get_forum_topics import GetForumTopics
from .get_forum_topics_by_id import GetForumTopicsByID
Expand Down Expand Up @@ -96,7 +95,6 @@ class Chats(
SetChatUsername,
SetChatPermissions,
GetDialogsCount,
GetFolder,
GetFolders,
GetForumTopics,
GetForumTopicsByID,
Expand Down
19 changes: 13 additions & 6 deletions pyrogram/methods/chats/export_folder_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,22 @@ async def export_folder_link(
if not folder:
return

peers = []

if folder.included_chats:
peers.extend(iter(folder.included_chats))

if folder.excluded_chats:
peers.extend(iter(folder.included_chats))

if folder.pinned_chats:
peers.extend(iter(folder.included_chats))

r = await self.invoke(
raw.functions.chatlists.ExportChatlistInvite(
chatlist=raw.types.InputChatlistDialogFilter(
filter_id=folder_id
),
chatlist=raw.types.InputChatlistDialogFilter(filter_id=folder_id),
title=folder.title,
peers=[await self.resolve_peer(i.id) for i in folder.pinned_peers] if folder.pinned_peers else []
+ [await self.resolve_peer(i.id) for i in folder.included_peers] if folder.included_peers else []
+ [await self.resolve_peer(i.id) for i in folder.excluded_peers] if folder.excluded_peers else [],
peers=[await self.resolve_peer(i.id) for i in peers],
)
)

Expand Down
45 changes: 0 additions & 45 deletions pyrogram/methods/chats/get_folder.py

This file was deleted.

43 changes: 32 additions & 11 deletions pyrogram/methods/chats/get_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +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 typing import AsyncGenerator, Optional
from typing import Union, List, Iterable

import pyrogram
from pyrogram import types, raw
Expand All @@ -25,21 +25,37 @@
class GetFolders:
async def get_folders(
self: "pyrogram.Client",
) -> AsyncGenerator["types.Folder", None]:
"""Get a user's folders with chats sequentially.
folder_ids: Union[int, Iterable[int]] = None,
) -> Union["types.Folder", List["types.Folder"]]:
"""Get one or more folders by using folder identifiers.
.. include:: /_includes/usable-by/users.rst
Parameters:
folder_ids (``int`` | Iterable of ``int``, *optional*):
Pass a single folder identifier or an iterable of folder ids (as integers) to get the content of the
folders themselves.
By default all folders are returned.
Returns:
``Generator``: A generator yielding :obj:`~pyrogram.types.Folder` objects.
:obj:`~pyrogram.types.Folder` | List of :obj:`~pyrogram.types.Folder`: In case *folder_ids* was not
a list, a single folder is returned, otherwise a list of folders is returned.
Example:
.. code-block:: python
# Iterate through all folders
async for folder in app.get_folders():
print(folder.title)
# Get one folder
await app.get_folders(12345)
# Get more than one folders (list of folders)
await app.get_folders([12345, 12346])
# Get all folders
await app.get_folders()
"""
is_iterable = hasattr(folder_ids, "__iter__")
ids = list(folder_ids) if is_iterable else [ids]

raw_folders = await self.invoke(raw.functions.messages.GetDialogFilters())
dialog_peers = []

Expand All @@ -49,24 +65,29 @@ async def get_folders(

peers = folder.pinned_peers + folder.include_peers + getattr(folder, "exclude_peers", [])
input_peers = [raw.types.InputDialogPeer(peer=peer) for peer in peers] + [raw.types.InputDialogPeerFolder(folder_id=folder.id)]

dialog_peers.extend(input_peers)

r = await self.invoke(raw.functions.messages.GetPeerDialogs(peers=dialog_peers))

users = {i.id: i for i in r.users}
chats = {i.id: i for i in r.chats}
peers = {**users, **chats}

folders = []

for folder in raw_folders:
if not isinstance(folder, (raw.types.DialogFilter, raw.types.DialogFilterChatlist)):
continue

folders.append(types.Folder._parse(self, folder, peers))
folders.append(types.Folder._parse(self, folder, users, chats))

if not folders:
return

for folder in folders:
yield folder
if is_iterable:
return [folder for folder in folders if folder.id in ids] or None

if not folder_ids:
return folders

return folders[0]
73 changes: 60 additions & 13 deletions pyrogram/methods/chats/update_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class UpdateFolder:
async def update_folder(
self: "pyrogram.Client",
folder_id: int,
title: str,
pinned_peers: Union[Union[int, str], List[Union[int, str]]],
included_peers: Union[Union[int, str], List[Union[int, str]]],
excluded_peers: Union[Union[int, str], List[Union[int, str]]],
included_chats: Union[Union[int, str], List[Union[int, str]]] = None,
excluded_chats: Union[Union[int, str], List[Union[int, str]]] = None,
pinned_chats: Union[Union[int, str], List[Union[int, str]]] = None,
title: str = None,
contacts: bool = None,
non_contacts: bool = None,
groups: bool = None,
broadcasts: bool = None,
channels: bool = None,
bots: bool = None,
exclude_muted: bool = None,
exclude_read: bool = None,
Expand All @@ -44,21 +44,68 @@ async def update_folder(
.. include:: /_includes/usable-by/users.rst
Parameters:
folder_id (``int``):
Unique folder identifier.
included_chats (``int`` | ``str`` | List of ``int`` or ``str``, *optional*):
Users or chats that should added in the folder
You can pass an ID (int), username (str) or phone number (str).
Multiple users can be added by passing a list of IDs, usernames or phone numbers.
excluded_chats (``int`` | ``str`` | List of ``int`` or ``str``, *optional*):
Users or chats that should excluded from the folder
You can pass an ID (int), username (str) or phone number (str).
Multiple users can be added by passing a list of IDs, usernames or phone numbers.
pinned_chats (``int`` | ``str`` | List of ``int`` or ``str``, *optional*):
Users or chats that should pinned in the folder
You can pass an ID (int), username (str) or phone number (str).
Multiple users can be added by passing a list of IDs, usernames or phone numbers.
title (``str``, *optional*):
A folder title was changed to this value.
contacts (``bool``, *optional*):
Pass True if folder should contain contacts.
non_contacts (``bool``, *optional*):
Pass True if folder should contain non contacts.
groups (``bool``, *optional*):
Pass True if folder should contain groups.
channels (``bool``, *optional*):
Pass True if folder should contain channels.
bots (``bool``, *optional*):
Pass True if folder should contain bots.
exclude_muted (``bool``, *optional*):
Pass True if folder should exclude muted users.
exclude_archived (``bool``, *optional*):
Pass True if folder should exclude archived users.
emoji (``str``, *optional*):
Folder emoji.
Pass None to leave the folder icon as default.
Returns:
``bool``: True, on success.
Example:
.. code-block:: python
# Delete folder
app.delete_folder(folder_id)
app.update_folder(folder_id, title="New folder", included_chats=["me"])
"""
if not isinstance(pinned_peers, list):
pinned_peers = [pinned_peers]
if not isinstance(included_peers, list):
included_peers = [included_peers]
if not isinstance(excluded_peers, list):
excluded_peers = [excluded_peers]
if not isinstance(included_chats, list):
included_peers = [included_chats]
if not isinstance(excluded_chats, list):
excluded_peers = [excluded_chats]
if not isinstance(pinned_chats, list):
pinned_peers = [pinned_chats]

r = await self.invoke(
raw.functions.messages.UpdateDialogFilter(
Expand All @@ -81,7 +128,7 @@ async def update_folder(
contacts=contacts,
non_contacts=non_contacts,
groups=groups,
broadcasts=broadcasts,
broadcasts=channels,
bots=bots,
exclude_muted=exclude_muted,
exclude_read=exclude_read,
Expand Down
Loading

0 comments on commit 511b7e3

Please sign in to comment.