Skip to content

Commit

Permalink
Pyrofork: Add StoryPrivacy
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 committed Oct 5, 2023
1 parent 4e5be20 commit fd74fc9
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/source/api/enums/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ to apply only a valid value among the expected ones.
NextCodeType
UserStatus
StoriesPrivacyRules
StoryPrivacy

.. toctree::
:hidden:
Expand All @@ -47,3 +48,4 @@ to apply only a valid value among the expected ones.
NextCodeType
UserStatus
StoriesPrivacyRules
StoryPrivacy
2 changes: 2 additions & 0 deletions pyrogram/enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .poll_type import PollType
from .sent_code_type import SentCodeType
from .stories_privacy_rules import StoriesPrivacyRules
from .story_privacy import StoryPrivacy
from .user_status import UserStatus

__all__ = [
Expand All @@ -47,5 +48,6 @@
'PollType',
'SentCodeType',
"StoriesPrivacyRules",
"StoryPrivacy",
'UserStatus'
]
2 changes: 1 addition & 1 deletion pyrogram/enums/stories_privacy_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


class StoriesPrivacyRules(AutoName):
"""Poll type enumeration used in :obj:`~pyrogram.types.Story`."""
"""stories peivacy rules type enumeration used in :obj:`~pyrogram.method.SendStory`."""

PUBLIC = auto()
"Public stories"
Expand Down
40 changes: 40 additions & 0 deletions pyrogram/enums/story_privacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of Pyrofork.
#
# Pyrofork 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.
#
# Pyrofork 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 Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from enum import auto

from .auto_name import AutoName


class StoryPrivacy(AutoName):
"""stories peivacy type enumeration used in :obj:`~pyrogram.types.Story`."""

PUBLIC = auto()
"Public stories"

CLOSE_FRIENDS = auto()
"Close_Friends stories"

CONTACTS = auto()
"Contacts only stories"

PRIVATE = auto()
"Private stories"

NO_CONTACTS = auto()
"Hide stories from contacts"
63 changes: 61 additions & 2 deletions pyrogram/types/messages_and_media/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,24 @@ class Story(Object, Update):
views (:obj:`~pyrogram.types.StoryViews`, *optional*):
Stories views.
privacy (:obj:`~pyrogram.enums.StoryPrivacy`, *optional*):
Story privacy.
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.
denied_users (List of ``int``, *optional*):
List of user_id whos denied to view the story.
"""

# TODO: Add Privacy
# TODO: Add Media Areas

def __init__(
self,
Expand All @@ -112,7 +127,12 @@ def __init__(
selected_contacts: bool = None,
caption: str = None,
caption_entities: List["types.MessageEntity"] = None,
views: "types.StoryViews" = None
views: "types.StoryViews" = None,
privacy: "enums.StoryPrivacy" = None,
allowed_users: List[int] = None,
denied_users: List[int] = None,
allowed_chats: List[int] = None,
denied_chats: List[int] = None
):
super().__init__(client)

Expand All @@ -135,6 +155,11 @@ def __init__(
self.caption = caption
self.caption_entities = caption_entities
self.views = views
self.privay = privacy
self.allowed_users = allowed_users
self.denied_users = denied_users
self.allowed_chats = allowed_chats
self.denied_chats = denied_chats

@staticmethod
async def _parse(
Expand All @@ -153,6 +178,11 @@ async def _parse(
video = None
from_user = None
sender_chat = None
privacy = None
allowed_chats = None
allowed_users = None
denied_chats = None
denied_users = None
if stories.media:
if isinstance(stories.media, raw.types.MessageMediaPhoto):
photo = types.Photo._parse(client, stories.media.photo, stories.media.ttl_seconds)
Expand Down Expand Up @@ -181,6 +211,30 @@ async def _parse(
from_user = client.me
else:
from_user = await client.get_users(peer.user_id)

for priv in stories.privacy:
if isinstance(priv, raw.types.PrivacyValueAllowAll):
privacy = enums.StoryPrivacy.PUBLIC
elif isinstance(priv, raw.types.PrivacyValueAllowCloseFriends):
privacy = enums.StoryPrivacy.CLOSE_FRIENDS
elif isinstance(priv, raw.types.PrivacyValueAllowContacts):
privacy = enums.StoryPrivacy.CONTACTS
elif isinstance(priv, raw.types.PrivacyValueDisallowAll):
privacy = enums.StoryPrivacy.PRIVATE
elif isinstance(priv, raw.types.PrivacyValueDisallowContacts):
privacy = enums.StoryPrivacy.NO_CONTACTS
if isinstance(priv, raw.types.PrivacyValueAllowChatParticipants):
allowed_chats = []
for chat in priv.chats:
allowed_chats.append(f"-100{chat}")
if isinstance(priv, raw.types.PrivacyValueDisallowChatParticipants):
denied_chats = []
for chat in priv.chats:
denied_chats.append(f"-100{chat}")
if isinstance(priv, raw.types.PrivacyValueAllowUsers):
allowed_users = priv.users
if isinstance(priv, raw.types.PrivacyValueDisallowUsers):
denied_users = priv.users

return Story(
id=stories.id,
Expand All @@ -202,6 +256,11 @@ async def _parse(
caption=stories.caption,
caption_entities=entities or None,
views=types.StoryViews._parse(stories.views),
privacy=privacy,
allowed_chats=allowed_chats,
denied_chats=denied_chats,
allowed_users=allowed_users,
denied_users=denied_users,
client=client
)

Expand Down

0 comments on commit fd74fc9

Please sign in to comment.