Skip to content

Commit

Permalink
Fix stories types and obscure behavior (#77)
Browse files Browse the repository at this point in the history
* Fix typos

* Fix obscure undocumented  behavior `edit_story_privacy` method by explicitly defaulting `privacy` parameter to `enums.StoriesPrivacyRules.PUBLIC`
  • Loading branch information
iineolineii committed Jul 7, 2024
1 parent 1e0f5f8 commit 7f55351
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
68 changes: 34 additions & 34 deletions pyrogram/methods/stories/edit_story_privacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def edit_story_privacy(
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":
Expand All @@ -44,7 +44,7 @@ async def edit_story_privacy(
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.
Expand Down Expand Up @@ -76,39 +76,39 @@ async def edit_story_privacy(
"""
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 = []

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 not privacy:
privacy = enums.StoriesPrivacyRules.PUBLIC

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(
Expand Down
8 changes: 4 additions & 4 deletions pyrogram/types/messages_and_media/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*):
Expand Down Expand Up @@ -158,7 +158,7 @@ def __init__(
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,
Expand Down Expand Up @@ -1605,7 +1605,7 @@ async def edit_caption(

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":
Expand All @@ -1627,7 +1627,7 @@ async def edit_privacy(
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.
Expand Down

0 comments on commit 7f55351

Please sign in to comment.