From 5dbc20c9c556d092b785f94519379508a11c5666 Mon Sep 17 00:00:00 2001 From: KurimuzonAkuma Date: Sat, 28 Oct 2023 22:23:25 +0300 Subject: [PATCH] Make privacy rules like in gui --- pyrogram/enums/stories_privacy_rules.py | 11 +-- pyrogram/methods/stories/edit_story.py | 72 +++++++++++------ pyrogram/methods/stories/send_story.py | 81 +++++++++++-------- pyrogram/types/messages_and_media/__init__.py | 3 +- .../stories_privacy_rules.py | 47 ----------- 5 files changed, 98 insertions(+), 116 deletions(-) delete mode 100644 pyrogram/types/messages_and_media/stories_privacy_rules.py diff --git a/pyrogram/enums/stories_privacy_rules.py b/pyrogram/enums/stories_privacy_rules.py index 48cf5c24..c352f2b1 100644 --- a/pyrogram/enums/stories_privacy_rules.py +++ b/pyrogram/enums/stories_privacy_rules.py @@ -26,14 +26,11 @@ class StoriesPrivacyRules(AutoName): PUBLIC = auto() "Public stories" - CLOSE_FRIENDS = auto() - "Close friends stories" - CONTACTS = auto() "Contacts only stories" - PRIVATE = auto() - "Private stories" + CLOSE_FRIENDS = auto() + "Close friends stories" - NO_CONTACTS = auto() - "Hide stories from contacts" + SELECTED_USERS = auto() + "Selected users stories" diff --git a/pyrogram/methods/stories/edit_story.py b/pyrogram/methods/stories/edit_story.py index 70c5884a..df10e661 100644 --- a/pyrogram/methods/stories/edit_story.py +++ b/pyrogram/methods/stories/edit_story.py @@ -38,10 +38,8 @@ class EditStory: supports_streaming: bool = True, file_name: str = None, privacy: "enums.StoriesPrivacyRules" = None, - allowed_users: List[int] = None, - denied_users: List[int] = None, - allowed_chats: List[int] = None, - denied_chats: List[int] = None, + allowed_users: List[Union[int, str]] = None, + disallowed_users: List[Union[int, str]] = None, parse_mode: "enums.ParseMode" = None, caption_entities: List["types.MessageEntity"] = None, progress: Callable = None, @@ -88,17 +86,16 @@ class EditStory: Story privacy. Defaults to :obj:`~pyrogram.enums.StoriesPrivacyRules.PUBLIC` - allowed_chats (List of ``int``, *optional*): - List of chat_id which participant allowed to view the story. - - denied_chats (List of ``int``, *optional*): - List of chat_id which participant denied to view the story. - allowed_users (List of ``int``, *optional*): - List of user_id whos allowed to view the story. + List of user_id or chat_id of chat users who are allowed to view stories. + Note: chat_id available only with :obj:`~pyrogram.enums.StoriesPrivacyRules.SELECTED_USERS`. + Works with :obj:`~pyrogram.enums.StoriesPrivacyRules.CLOSE_FRIENDS` + and :obj:`~pyrogram.enums.StoriesPrivacyRules.SELECTED_USERS` only - denied_users (List of ``int``, *optional*): - List of user_id whos denied to view the story. + disallowed_users (List of ``int``, *optional*): + List of user_id whos disallow to view the stories. + Note: Works with :obj:`~pyrogram.enums.StoriesPrivacyRules.PUBLIC` + and :obj:`~pyrogram.enums.StoriesPrivacyRules.CONTACTS` only parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*): By default, texts are parsed using both Markdown and HTML styles. @@ -126,7 +123,7 @@ class EditStory: # Send new photo story photo_id = "abcd12345" - await app.send_story(photo=photo_id, caption='Hello guys.') + await app.edit_story(meida=photo_id, caption='Hello guys.') Raises: ValueError: In case of invalid arguments. @@ -198,18 +195,41 @@ class EditStory: file=file, ) - if allowed_chats: - chats = [await self.resolve_peer(chat_id) for chat_id in allowed_chats] - privacy_rules.append(raw.types.InputPrivacyValueAllowChatParticipants(chats=chats)) - if denied_chats: - chats = [await self.resolve_peer(chat_id) for chat_id in denied_chats] - privacy_rules.append(raw.types.InputPrivacyValueDisallowChatParticipants(chats=chats)) - if allowed_users: - users = [await self.resolve_peer(user_id) for user_id in allowed_users] - privacy_rules.append(raw.types.InputPrivacyValueAllowUsers(users=users)) - if denied_users: - users = [await self.resolve_peer(user_id) for user_id in denied_users] - privacy_rules.append(raw.types.InputPrivacyValueDisallowUsers(users=users)) + 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): + _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: + privacy_rules.append(raw.types.InputPrivacyValueAllowAll()) while True: try: diff --git a/pyrogram/methods/stories/send_story.py b/pyrogram/methods/stories/send_story.py index bf5ce497..23b3bb75 100644 --- a/pyrogram/methods/stories/send_story.py +++ b/pyrogram/methods/stories/send_story.py @@ -38,10 +38,8 @@ class SendStory: supports_streaming: bool = True, file_name: str = None, privacy: "enums.StoriesPrivacyRules" = None, - allowed_users: List[int] = None, - denied_users: List[int] = None, - allowed_chats: List[int] = None, - denied_chats: List[int] = None, + allowed_users: List[Union[int, str]] = None, + disallowed_users: List[Union[int, str]] = None, pinned: bool = None, protect_content: bool = None, parse_mode: "enums.ParseMode" = None, @@ -59,7 +57,6 @@ class SendStory: 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). media (``str`` | ``BinaryIO``): Video or photo to send. @@ -94,17 +91,16 @@ class SendStory: Story privacy. Defaults to :obj:`~pyrogram.enums.StoriesPrivacyRules.PUBLIC` - allowed_chats (List of ``int``, *optional*): - List of chat_id which participant allowed to view the story. - - denied_chats (List of ``int``, *optional*): - List of chat_id which participant denied to view the story. - allowed_users (List of ``int``, *optional*): - List of user_id whos allowed to view the story. + List of user_id or chat_id of chat users who are allowed to view stories. + Note: chat_id available only with :obj:`~pyrogram.enums.StoriesPrivacyRules.SELECTED_USERS`. + Works with :obj:`~pyrogram.enums.StoriesPrivacyRules.CLOSE_FRIENDS` + and :obj:`~pyrogram.enums.StoriesPrivacyRules.SELECTED_USERS` only - denied_users (List of ``int``, *optional*): - List of user_id whos denied to view the story. + disallowed_users (List of ``int``, *optional*): + List of user_id whos disallow to view the stories. + Note: Works with :obj:`~pyrogram.enums.StoriesPrivacyRules.PUBLIC` + and :obj:`~pyrogram.enums.StoriesPrivacyRules.CONTACTS` only pinned (``bool``, *optional*): if True, the story will be pinned. @@ -138,20 +134,14 @@ class SendStory: Example: .. code-block:: python - # Send new photo story - photo_id = "abcd12345" - await app.send_story(photo=photo_id, caption='Hello guys.') + # Send new story + await app.send_story(media=file_id, caption='Hello guys.') Raises: ValueError: In case of invalid arguments. """ # TODO: media_areas - if privacy: - privacy_rules = [types.StoriesPrivacyRules(type=privacy)] - else: - privacy_rules = [types.StoriesPrivacyRules(type=enums.StoriesPrivacyRules.PUBLIC)] - message, entities = (await utils.parse_text_entities(self, caption, parse_mode, caption_entities)).values() try: @@ -214,18 +204,41 @@ class SendStory: file=file, ) - if allowed_chats: - chats = [await self.resolve_peer(chat_id) for chat_id in allowed_chats] - privacy_rules.append(raw.types.InputPrivacyValueAllowChatParticipants(chats=chats)) - if denied_chats: - chats = [await self.resolve_peer(chat_id) for chat_id in denied_chats] - privacy_rules.append(raw.types.InputPrivacyValueDisallowChatParticipants(chats=chats)) - if allowed_users: - users = [await self.resolve_peer(user_id) for user_id in allowed_users] - privacy_rules.append(raw.types.InputPrivacyValueAllowUsers(users=users)) - if denied_users: - users = [await self.resolve_peer(user_id) for user_id in denied_users] - privacy_rules.append(raw.types.InputPrivacyValueDisallowUsers(users=users)) + 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): + _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: + privacy_rules.append(raw.types.InputPrivacyValueAllowAll()) while True: try: diff --git a/pyrogram/types/messages_and_media/__init__.py b/pyrogram/types/messages_and_media/__init__.py index b6dcb5d5..5060e9db 100644 --- a/pyrogram/types/messages_and_media/__init__.py +++ b/pyrogram/types/messages_and_media/__init__.py @@ -38,7 +38,6 @@ from .poll_option import PollOption from .reaction import Reaction from .sticker import Sticker from .stripped_thumbnail import StrippedThumbnail -from .stories_privacy_rules import StoriesPrivacyRules from .story import Story from .story_deleted import StoryDeleted from .story_skipped import StorySkipped @@ -57,7 +56,7 @@ __all__ = [ "Animation", "Audio", "Contact", "Document", "ForumTopic", "ForumTopicCreated", "ForumTopicClosed", "ForumTopicReopened", "ForumTopicEdited", "GeneralTopicHidden", "GeneralTopicUnhidden", "Game", "Location", "Message", "MessageEntity", "Photo", "Thumbnail", - "StrippedThumbnail", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoriesPrivacyRules", "Poll", "PollOption", "Sticker", + "StrippedThumbnail", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "Poll", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice", "WebPage", "Dice", "Reaction", "WebAppData", "MessageReactions", "MessageStory" ] diff --git a/pyrogram/types/messages_and_media/stories_privacy_rules.py b/pyrogram/types/messages_and_media/stories_privacy_rules.py deleted file mode 100644 index 3e816114..00000000 --- a/pyrogram/types/messages_and_media/stories_privacy_rules.py +++ /dev/null @@ -1,47 +0,0 @@ -# Pyrogram - Telegram MTProto API Client Library for Python -# Copyright (C) 2017-present Dan -# -# This file is part of Pyrogram. -# -# Pyrogram is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pyrogram is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with Pyrogram. If not, see . - -from pyrogram import enums, raw -from ..object import Object - -class StoriesPrivacyRules(Object): - """A story privacy rules. - - Parameters: - type (:obj:`~pyrogram.enums.StoriesPrivacyRules`): - Story privacy type. - """ - - def __init__( - self, *, - type: "enums.StoriesPrivacyRules" - ): - super().__init__() - self.type = type - - def write(self): - if self.type == enums.StoriesPrivacyRules.PUBLIC: - return raw.types.InputPrivacyValueAllowAll().write() - if self.type == enums.StoriesPrivacyRules.CLOSE_FRIENDS: - return raw.types.InputPrivacyValueAllowCloseFriends().write() - if self.type == enums.StoriesPrivacyRules.CONTACTS: - return raw.types.InputPrivacyValueAllowContacts().write() - if self.type == enums.StoriesPrivacyRules.NO_CONTACTS: - return raw.types.InputPrivacyValueDisallowContacts().write() - if self.type == enums.StoriesPrivacyRules.PRIVATE: - return raw.types.InputPrivacyValueDisallowAll().write()