diff --git a/docs/source/api/methods.rst b/docs/source/api/methods.rst index 760c332b..2d3d8f23 100644 --- a/docs/source/api/methods.rst +++ b/docs/source/api/methods.rst @@ -111,10 +111,11 @@ Users - :meth:`~Client.get_me` - :meth:`~Client.get_users` - - :meth:`~Client.get_user_photos` - - :meth:`~Client.get_user_photos_count` - - :meth:`~Client.set_photo` - - :meth:`~Client.delete_photos` + - :meth:`~Client.get_profile_photos` + - :meth:`~Client.get_profile_photos_count` + - :meth:`~Client.iter_profile_photos` + - :meth:`~Client.set_profile_photo` + - :meth:`~Client.delete_profile_photos` - :meth:`~Client.update_username` - :meth:`~Client.get_user_dc` @@ -232,10 +233,11 @@ Details .. Users .. automethod:: Client.get_me() .. automethod:: Client.get_users() -.. automethod:: Client.get_user_photos() -.. automethod:: Client.get_user_photos_count() -.. automethod:: Client.set_photo() -.. automethod:: Client.delete_photos() +.. automethod:: Client.get_profile_photos() +.. automethod:: Client.get_profile_photos_count() +.. automethod:: Client.iter_profile_photos() +.. automethod:: Client.set_profile_photo() +.. automethod:: Client.delete_profile_photos() .. automethod:: Client.update_username() .. automethod:: Client.get_user_dc() diff --git a/pyrogram/client/ext/base_client.py b/pyrogram/client/ext/base_client.py index 3397c8d3..b193ceee 100644 --- a/pyrogram/client/ext/base_client.py +++ b/pyrogram/client/ext/base_client.py @@ -159,3 +159,6 @@ class BaseClient: def guess_extension(self, *args, **kwargs): pass + + def get_profile_photos(self, *args, **kwargs): + pass diff --git a/pyrogram/client/methods/users/__init__.py b/pyrogram/client/methods/users/__init__.py index bbc2df23..20b50ce9 100644 --- a/pyrogram/client/methods/users/__init__.py +++ b/pyrogram/client/methods/users/__init__.py @@ -16,24 +16,26 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from .delete_photos import DeletePhotos +from .delete_profile_photos import DeleteProfilePhotos from .get_me import GetMe +from .get_profile_photos import GetProfilePhotos +from .get_profile_photos_count import GetProfilePhotosCount from .get_user_dc import GetUserDC -from .get_user_photos import GetUserPhotos -from .get_user_photos_count import GetUserPhotosCount from .get_users import GetUsers -from .set_photo import SetPhoto +from .iter_profile_photos import IterProfilePhotos +from .set_profile_photo import SetProfilePhoto from .update_username import UpdateUsername class Users( - GetUserPhotos, - SetPhoto, - DeletePhotos, + GetProfilePhotos, + SetProfilePhoto, + DeleteProfilePhotos, GetUsers, GetMe, UpdateUsername, - GetUserPhotosCount, - GetUserDC + GetProfilePhotosCount, + GetUserDC, + IterProfilePhotos ): pass diff --git a/pyrogram/client/methods/users/delete_photos.py b/pyrogram/client/methods/users/delete_profile_photos.py similarity index 96% rename from pyrogram/client/methods/users/delete_photos.py rename to pyrogram/client/methods/users/delete_profile_photos.py index 30b3b533..1b46382c 100644 --- a/pyrogram/client/methods/users/delete_photos.py +++ b/pyrogram/client/methods/users/delete_profile_photos.py @@ -24,8 +24,8 @@ from pyrogram.api import functions, types from ...ext import BaseClient -class DeletePhotos(BaseClient): - def delete_photos( +class DeleteProfilePhotos(BaseClient): + def delete_profile_photos( self, id: Union[str, List[str]] ) -> bool: diff --git a/pyrogram/client/methods/users/get_user_photos.py b/pyrogram/client/methods/users/get_profile_photos.py similarity index 55% rename from pyrogram/client/methods/users/get_user_photos.py rename to pyrogram/client/methods/users/get_profile_photos.py index e134a502..d45bd5b6 100644 --- a/pyrogram/client/methods/users/get_user_photos.py +++ b/pyrogram/client/methods/users/get_profile_photos.py @@ -19,21 +19,21 @@ from typing import Union import pyrogram -from pyrogram.api import functions +from pyrogram.api import functions, types from ...ext import BaseClient -class GetUserPhotos(BaseClient): - def get_user_photos( +class GetProfilePhotos(BaseClient): + def get_profile_photos( self, - user_id: Union[int, str], + chat_id: Union[int, str], offset: int = 0, limit: int = 100 ) -> "pyrogram.Photos": - """Get a list of profile pictures for a user. + """Get a list of profile pictures for a user or a chat. Parameters: - user_id (``int`` | ``str``): + chat_id (``int`` | ``str``): Unique identifier (int) or username (str) of the target chat. For your personal cloud (Saved Messages) you can simply use "me" or "self". For a contact that exists in your Telegram address book you can use his phone number (str). @@ -52,14 +52,41 @@ class GetUserPhotos(BaseClient): Raises: RPCError: In case of a Telegram RPC error. """ - return pyrogram.Photos._parse( - self, - self.send( - functions.photos.GetUserPhotos( - user_id=self.resolve_peer(user_id), - offset=offset, - max_id=0, - limit=limit + peer_id = self.resolve_peer(chat_id) + + if isinstance(peer_id, types.InputPeerUser): + return pyrogram.Photos._parse( + self, + self.send( + functions.photos.GetUserPhotos( + user_id=peer_id, + offset=offset, + max_id=0, + limit=limit + ) ) ) - ) + else: + new_chat_photos = pyrogram.Messages._parse( + self, + self.send( + functions.messages.Search( + peer=peer_id, + q="", + filter=types.InputMessagesFilterChatPhotos(), + min_date=0, + max_date=0, + offset_id=0, + add_offset=offset, + limit=limit, + max_id=0, + min_id=0, + hash=0 + ) + ) + ) + + return pyrogram.Photos( + total_count=new_chat_photos.total_count, + photos=[m.new_chat_photo for m in new_chat_photos.messages][:limit] + ) diff --git a/pyrogram/client/methods/users/get_user_photos_count.py b/pyrogram/client/methods/users/get_profile_photos_count.py similarity index 72% rename from pyrogram/client/methods/users/get_user_photos_count.py rename to pyrogram/client/methods/users/get_profile_photos_count.py index 3bbaf55b..a65e0ada 100644 --- a/pyrogram/client/methods/users/get_user_photos_count.py +++ b/pyrogram/client/methods/users/get_profile_photos_count.py @@ -18,16 +18,15 @@ from typing import Union -from pyrogram.api import functions, types from ...ext import BaseClient -class GetUserPhotosCount(BaseClient): - def get_user_photos_count(self, user_id: Union[int, str]) -> int: +class GetProfilePhotosCount(BaseClient): + def get_profile_photos_count(self, chat_id: Union[int, str]) -> int: """Get the total count of profile pictures for a user. Parameters: - user_id (``int`` | ``str``): + chat_id (``int`` | ``str``): Unique identifier (int) or username (str) of the target chat. For your personal cloud (Saved Messages) you can simply use "me" or "self". For a contact that exists in your Telegram address book you can use his phone number (str). @@ -39,16 +38,4 @@ class GetUserPhotosCount(BaseClient): RPCError: In case of a Telegram RPC error. """ - r = self.send( - functions.photos.GetUserPhotos( - user_id=self.resolve_peer(user_id), - offset=0, - max_id=0, - limit=1 - ) - ) - - if isinstance(r, types.photos.Photos): - return len(r.photos) - else: - return r.count + return self.get_profile_photos(chat_id, limit=1).total_count diff --git a/pyrogram/client/methods/users/set_photo.py b/pyrogram/client/methods/users/set_profile_photo.py similarity index 96% rename from pyrogram/client/methods/users/set_photo.py rename to pyrogram/client/methods/users/set_profile_photo.py index cd7f955d..a713fd34 100644 --- a/pyrogram/client/methods/users/set_photo.py +++ b/pyrogram/client/methods/users/set_profile_photo.py @@ -20,8 +20,8 @@ from pyrogram.api import functions from ...ext import BaseClient -class SetPhoto(BaseClient): - def set_photo( +class SetProfilePhoto(BaseClient): + def set_profile_photo( self, photo: str ) -> bool: