From 7f55351d893df5df4a9469ebebffb8d8d51e15e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B5=D0=BE=D0=BB=D0=B0=D0=B9=D0=BD?= <135314083+iineolineii@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:40:29 +0300 Subject: [PATCH] Fix stories types and obscure behavior (#77) * Fix typos * Fix obscure undocumented behavior `edit_story_privacy` method by explicitly defaulting `privacy` parameter to `enums.StoriesPrivacyRules.PUBLIC` --- .../methods/stories/edit_story_privacy.py | 66 +++++++++---------- pyrogram/types/messages_and_media/story.py | 8 +-- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/pyrogram/methods/stories/edit_story_privacy.py b/pyrogram/methods/stories/edit_story_privacy.py index a57adc50..3e114a0b 100644 --- a/pyrogram/methods/stories/edit_story_privacy.py +++ b/pyrogram/methods/stories/edit_story_privacy.py @@ -26,7 +26,7 @@ class EditStoryPrivacy: self: "pyrogram.Client", chat_id: Union[int, str], story_id: int, - privacy: "enums.StoriesPrivacyRules" = None, + privacy: "enums.StoriesPrivacyRules" = enums.StoriesPrivacyRules.PUBLIC, allowed_users: List[Union[int, str]] = None, disallowed_users: List[Union[int, str]] = None, ) -> "types.Story": @@ -44,7 +44,7 @@ class EditStoryPrivacy: Story identifier in the chat specified in chat_id. privacy (:obj:`~pyrogram.enums.StoriesPrivacyRules`, *optional*): - Story privacy. + Story privacy. Defaults to :obj:`~pyrogram.enums.StoriesPrivacyRules.PUBLIC`. allowed_users (List of ``int`` | ``str``, *optional*): List of user_id or chat_id of chat users who are allowed to view stories. @@ -76,39 +76,39 @@ class EditStoryPrivacy: """ privacy_rules = [] - if privacy: - if privacy == enums.StoriesPrivacyRules.PUBLIC: - privacy_rules.append(raw.types.InputPrivacyValueAllowAll()) - if disallowed_users: - users = [await self.resolve_peer(user_id) for user_id in disallowed_users] - privacy_rules.append(raw.types.InputPrivacyValueDisallowUsers(users=users)) - elif privacy == enums.StoriesPrivacyRules.CONTACTS: - privacy_rules = [raw.types.InputPrivacyValueAllowContacts()] - if disallowed_users: - users = [await self.resolve_peer(user_id) for user_id in disallowed_users] - privacy_rules.append(raw.types.InputPrivacyValueDisallowUsers(users=users)) - elif privacy == enums.StoriesPrivacyRules.CLOSE_FRIENDS: - privacy_rules = [raw.types.InputPrivacyValueAllowCloseFriends()] - if allowed_users: - users = [await self.resolve_peer(user_id) for user_id in allowed_users] - privacy_rules.append(raw.types.InputPrivacyValueAllowUsers(users=users)) - elif privacy == enums.StoriesPrivacyRules.SELECTED_USERS: - _allowed_users = [] - _allowed_chats = [] + if not privacy: + privacy = enums.StoriesPrivacyRules.PUBLIC - for user in allowed_users: - peer = await self.resolve_peer(user) - if isinstance(peer, raw.types.InputPeerUser): - _allowed_users.append(peer) - elif isinstance(peer, (raw.types.InputPeerChat, raw.types.InputPeerChannel)): - _allowed_chats.append(peer) - - if _allowed_users: - privacy_rules.append(raw.types.InputPrivacyValueAllowUsers(users=_allowed_users)) - if _allowed_chats: - privacy_rules.append(raw.types.InputPrivacyValueAllowChatParticipants(chats=_allowed_chats)) - else: + if privacy == enums.StoriesPrivacyRules.PUBLIC: privacy_rules.append(raw.types.InputPrivacyValueAllowAll()) + if disallowed_users: + users = [await self.resolve_peer(user_id) for user_id in disallowed_users] + privacy_rules.append(raw.types.InputPrivacyValueDisallowUsers(users=users)) + elif privacy == enums.StoriesPrivacyRules.CONTACTS: + privacy_rules = [raw.types.InputPrivacyValueAllowContacts()] + if disallowed_users: + users = [await self.resolve_peer(user_id) for user_id in disallowed_users] + privacy_rules.append(raw.types.InputPrivacyValueDisallowUsers(users=users)) + elif privacy == enums.StoriesPrivacyRules.CLOSE_FRIENDS: + privacy_rules = [raw.types.InputPrivacyValueAllowCloseFriends()] + if allowed_users: + users = [await self.resolve_peer(user_id) for user_id in allowed_users] + privacy_rules.append(raw.types.InputPrivacyValueAllowUsers(users=users)) + elif privacy == enums.StoriesPrivacyRules.SELECTED_USERS: + _allowed_users = [] + _allowed_chats = [] + + for user in allowed_users: + peer = await self.resolve_peer(user) + if isinstance(peer, raw.types.InputPeerUser): + _allowed_users.append(peer) + elif isinstance(peer, (raw.types.InputPeerChat, raw.types.InputPeerChannel)): + _allowed_chats.append(peer) + + if _allowed_users: + privacy_rules.append(raw.types.InputPrivacyValueAllowUsers(users=_allowed_users)) + if _allowed_chats: + privacy_rules.append(raw.types.InputPrivacyValueAllowChatParticipants(chats=_allowed_chats)) r = await self.invoke( diff --git a/pyrogram/types/messages_and_media/story.py b/pyrogram/types/messages_and_media/story.py index 04402bf0..e6f9e9b4 100644 --- a/pyrogram/types/messages_and_media/story.py +++ b/pyrogram/types/messages_and_media/story.py @@ -104,7 +104,7 @@ class Story(Object, Update): forwards (``int``, *optional*): Stories forwards. - privacy (:obj:`~pyrogram.enums.StoryPrivacyRules`, *optional*): + privacy (:obj:`~pyrogram.enums.StoriesPrivacyRules`, *optional*): Story privacy. allowed_users (List of ``int`` | ``str``, *optional*): @@ -158,7 +158,7 @@ class Story(Object, Update): caption_entities: List["types.MessageEntity"] = None, views: int = None, forwards: int = None, - privacy: "enums.StoryPrivacyRules" = None, + privacy: "enums.StoriesPrivacyRules" = None, allowed_users: List[Union[int, str]] = None, disallowed_users: List[Union[int, str]] = None, reactions: List["types.Reaction"] = None, @@ -1605,7 +1605,7 @@ class Story(Object, Update): async def edit_privacy( self, - privacy: "enums.StoriesPrivacyRules" = None, + privacy: "enums.StoriesPrivacyRules" = enums.StoriesPrivacyRules.PUBLIC, allowed_users: List[Union[int, str]] = None, disallowed_users: List[Union[int, str]] = None, ) -> "types.Story": @@ -1627,7 +1627,7 @@ class Story(Object, Update): Parameters: privacy (:obj:`~pyrogram.enums.StoriesPrivacyRules`, *optional*): - Story privacy. + Story privacy. Defaults to :obj:`~pyrogram.enums.StoriesPrivacyRules.PUBLIC`. allowed_users (List of ``int`` | ``str``, *optional*): List of user_id or chat_id of chat users who are allowed to view stories.