pyrofork: Refactor folders

Signed-off-by: wulan17 <wulan17@komodos.id>
This commit is contained in:
wulan17 2025-07-10 19:21:36 +07:00
parent 4a109b3dc2
commit 413556a3f2
No known key found for this signature in database
GPG key ID: 737814D4B5FF0420
3 changed files with 28 additions and 7 deletions

View file

@ -81,7 +81,7 @@ class GetFolders:
users.update({i.id: i for i in r.users}) users.update({i.id: i for i in r.users})
chats.update({i.id: i for i in r.chats}) chats.update({i.id: i for i in r.chats})
folders = types.List(types.Folder._parse(self, folder, users, chats) for folder in raw_folders) folders = types.List([types.Folder._parse(self, folder, users, chats) for folder in raw_folders])
if not folders: if not folders:
return None return None

View file

@ -20,8 +20,7 @@
from typing import List, Union from typing import List, Union
import pyrogram import pyrogram
from pyrogram import raw from pyrogram import enums, raw, types, utils
from pyrogram import enums
class UpdateFolder: class UpdateFolder:
@ -29,6 +28,7 @@ class UpdateFolder:
self: "pyrogram.Client", self: "pyrogram.Client",
folder_id: int, folder_id: int,
title: str, title: str,
title_entities: List["types.MessageEntity"] = None,
included_chats: Union[Union[int, str], List[Union[int, str]]] = None, included_chats: Union[Union[int, str], List[Union[int, str]]] = None,
excluded_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, pinned_chats: Union[Union[int, str], List[Union[int, str]]] = None,
@ -41,7 +41,8 @@ class UpdateFolder:
exclude_read: bool = None, exclude_read: bool = None,
exclude_archived: bool = None, exclude_archived: bool = None,
color: "enums.FolderColor" = None, color: "enums.FolderColor" = None,
emoji: str = None emoji: str = None,
parse_mode: "pyrogram.enums.ParseMode" = pyrogram.enums.ParseMode.DEFAULT
) -> bool: ) -> bool:
"""Create or update a user's folder. """Create or update a user's folder.
@ -54,6 +55,9 @@ class UpdateFolder:
title (``str``): title (``str``):
Folder title. Folder title.
title_entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
Entities for the folder title.
included_chats (``int`` | ``str`` | List of ``int`` or ``str``, *optional*): included_chats (``int`` | ``str`` | List of ``int`` or ``str``, *optional*):
Users or chats that should added in the folder Users or chats that should added in the folder
You can pass an ID (int), username (str) or phone number (str). You can pass an ID (int), username (str) or phone number (str).
@ -98,6 +102,9 @@ class UpdateFolder:
Color type. Color type.
Pass :obj:`~pyrogram.enums.FolderColor` to set folder color. Pass :obj:`~pyrogram.enums.FolderColor` to set folder color.
parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*):
The parse mode to use for the title.
Returns: Returns:
``bool``: True, on success. ``bool``: True, on success.
@ -107,6 +114,8 @@ class UpdateFolder:
# Create or update folder # Create or update folder
app.update_folder(folder_id, title="New folder", included_chats="me") app.update_folder(folder_id, title="New folder", included_chats="me")
""" """
title_text, title_entities = (await utils.parse_text_entities(self, title, parse_mode, title_entities)).values()
if not isinstance(included_chats, list): if not isinstance(included_chats, list):
included_chats = [included_chats] if included_chats else [] included_chats = [included_chats] if included_chats else []
if not isinstance(excluded_chats, list): if not isinstance(excluded_chats, list):
@ -119,7 +128,10 @@ class UpdateFolder:
id=folder_id, id=folder_id,
filter=raw.types.DialogFilter( filter=raw.types.DialogFilter(
id=folder_id, id=folder_id,
title=title, title=raw.types.TextWithEntities(
text=title_text,
entities=title_entities or []
),
pinned_peers=[ pinned_peers=[
await self.resolve_peer(user_id) await self.resolve_peer(user_id)
for user_id in pinned_chats for user_id in pinned_chats
@ -146,4 +158,4 @@ class UpdateFolder:
) )
) )
return r return bool(r)

View file

@ -37,6 +37,9 @@ class Folder(Object):
title (``str``): title (``str``):
The folder title. The folder title.
title_entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
A list of entities in the folder title.
included_chats (List of :obj:`~pyrogram.types.Chat`, *optional*): included_chats (List of :obj:`~pyrogram.types.Chat`, *optional*):
A list of included chats in folder. A list of included chats in folder.
@ -83,6 +86,7 @@ class Folder(Object):
client: "pyrogram.Client" = None, client: "pyrogram.Client" = None,
id: int, id: int,
title: str, title: str,
title_entities: List["types.MessageEntity"] = None,
included_chats: List["types.Chat"] = None, included_chats: List["types.Chat"] = None,
excluded_chats: List["types.Chat"] = None, excluded_chats: List["types.Chat"] = None,
pinned_chats: List["types.Chat"] = None, pinned_chats: List["types.Chat"] = None,
@ -102,6 +106,7 @@ class Folder(Object):
self.id = id self.id = id
self.title = title self.title = title
self.title_entities = title_entities
self.included_chats = included_chats self.included_chats = included_chats
self.excluded_chats = excluded_chats self.excluded_chats = excluded_chats
self.pinned_chats = pinned_chats self.pinned_chats = pinned_chats
@ -122,6 +127,9 @@ class Folder(Object):
included_chats = [] included_chats = []
excluded_chats = [] excluded_chats = []
pinned_chats = [] pinned_chats = []
title = folder.title
title_text = title.text
title_entities = [types.MessageEntity._parse(client, entity) for entity in title.entities] if title.entities else None
for peer in folder.include_peers: for peer in folder.include_peers:
try: try:
@ -144,7 +152,8 @@ class Folder(Object):
return Folder( return Folder(
id=folder.id, id=folder.id,
title=folder.title, title=title_text,
title_entities=title_entities,
included_chats=types.List(included_chats) or None, included_chats=types.List(included_chats) or None,
excluded_chats=types.List(excluded_chats) or None, excluded_chats=types.List(excluded_chats) or None,
pinned_chats=types.List(pinned_chats) or None, pinned_chats=types.List(pinned_chats) or None,