o
    2gW*                     @  s   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ erXddlZG dd deZdS )zProvides the User class.    )annotations)TYPE_CHECKINGIterator)warn)Conflict   )API_PATH)ReadOnlyException)Preferences)_deprecate_args)cachedproperty   )PRAWBase)ListingGenerator)Redditor)	SubredditNc                      s   e Zd ZdZed4ddZd5 fdd	Zd6ddZd7ddZe	dddd8ddZ
d9ddZe	dddd:d!d"Zd7d#d$Zd;d&d'Zddd(d<d.d/Zd7d0d1Zd6d2d3Z  ZS )=UserzOThe :class:`.User` class provides methods for the currently authenticated user.returnpraw.models.Preferencesc                 C  s
   t | jS )ap  Get an instance of :class:`.Preferences`.

        The preferences can be accessed as a ``dict`` like so:

        .. code-block:: python

            preferences = reddit.user.preferences()
            print(preferences["show_link_flair"])

        Preferences can be updated via:

        .. code-block:: python

            reddit.user.preferences.update(show_link_flair=True)

        The :meth:`.Preferences.update` method returns the new state of the preferences
        as a ``dict``, which can be used to check whether a change went through. Changes
        with invalid types or parameter names fail silently.

        .. code-block:: python

            original_preferences = reddit.user.preferences()
            new_preferences = reddit.user.preferences.update(invalid_param=123)
            print(original_preferences == new_preferences)  # True, no change

        )r
   _redditself r   H/home/garg/my-data/venv/lib/python3.10/site-packages/praw/models/user.pypreferences   s   
zUser.preferencesredditpraw.Redditc                   s   t  j|dd dS )zInitialize an :class:`.User` instance.

        This class is intended to be interfaced with through ``reddit.user``.

        N_data)super__init__)r   r   	__class__r   r   r    9   s   zUser.__init__list[praw.models.Redditor]c                 C     | j td S )zAReturn a :class:`.RedditorList` of blocked :class:`.Redditor`\ s.blockedr   getr   r   r   r   r   r%   A      zUser.blockedgenerator_kwargsstr | int | dict[str, str]Iterator[praw.models.Subreddit]c                 K     t | jtd fi |S )a  Return a :class:`.ListingGenerator` of contributor :class:`.Subreddit`\ s.

        These are subreddits in which the user is an approved user.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        To print a list of the subreddits that you are an approved user in, try:

        .. code-block:: python

            for subreddit in reddit.user.contributor_subreddits(limit=None):
                print(str(subreddit))

        my_contributorr   r   r   r   r)   r   r   r   contributor_subredditsE   s
   
zUser.contributor_subredditsuserNr1   !str | praw.models.Redditor | None1list[praw.models.Redditor] | praw.models.Redditorc                C  s0   |du rt d n	t d jt|d}| j|S )aO  Return a :class:`.RedditorList` of friends or a :class:`.Redditor` in the friends list.

        :param user: Checks to see if you are friends with the redditor. Either an
            instance of :class:`.Redditor` or a string can be given.

        :returns: A list of :class:`.Redditor`\ s, or a single :class:`.Redditor` if
            ``user`` is specified. The :class:`.Redditor` instance(s) returned also has
            friend attributes.

        :raises: An instance of :class:`.RedditAPIException` if you are not friends with
            the specified :class:`.Redditor`.

        Nfriends	friend_v1r2   )r   formatstrr   r'   )r   r1   endpointr   r   r   r5   [   s
   zUser.friends+dict[praw.models.Subreddit, dict[str, int]]c                 C  s@   i }| j td d D ]}t| j |d }|d= |||< q|S )a  Return a dictionary mapping :class:`.Subreddit`\ s to their karma.

        The returned dict contains subreddits as keys. Each subreddit key contains a
        sub-dict that have keys for ``comment_karma`` and ``link_karma``. The dict is
        sorted in descending karma order.

        .. note::

            Each key of the main dict is an instance of :class:`.Subreddit`. It is
            recommended to iterate over the dict in order to retrieve the values,
            preferably through :py:meth:`dict.items`.

        karmadatasr)r   r'   r   r   )r   	karma_maprow	subredditr   r   r   r;   s   s   
z
User.karma	use_cacheT)rA   boolpraw.models.Redditor | Nonec                C  sj   | j jr| j jjdstdtdd dS d}t|d| jvs"|s2| j t	d }t
| j |d	| _| jS )
a'  Return a :class:`.Redditor` instance for the authenticated user.

        :param use_cache: When ``True``, and if this function has been previously
            called, returned the cached version (default: ``True``).

        .. note::

            If you change the :class:`.Reddit` instance's authorization, you might want
            to refresh the cached value. Prefer using separate :class:`.Reddit`
            instances, however, for distinct authorizations.

        .. deprecated:: 7.2

            In :attr:`.read_only` mode this method returns ``None``. In PRAW 8 this
            method will raise :class:`.ReadOnlyException` when called in
            :attr:`.read_only` mode. To operate in PRAW 8 mode, set the config variable
            ``praw8_raise_exception_on_me`` to ``True``.

        praw8_raise_exception_on_mezThe 'None' return value is deprecated, and will raise a ReadOnlyException beginning with PRAW 8. See documentation for forward compatibility options.r   )category
stacklevelNz+`user.me()` does not work in read_only mode_memer   )r   	read_onlyconfigcustomr'   r   DeprecationWarningr	   __dict__r   r   rG   )r   rA   msg	user_datar   r   r   rH      s   zUser.mec                 K  r,   )a  Return a :class:`.ListingGenerator` subreddits that the user moderates.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        To print a list of the names of the subreddits you moderate, try:

        .. code-block:: python

            for subreddit in reddit.user.moderator_subreddits(limit=None):
                print(str(subreddit))

        .. seealso::

            :meth:`.Redditor.moderated`

        my_moderatorr.   r/   r   r   r   moderator_subreddits   s
   
zUser.moderator_subredditslist[praw.models.Multireddit]c                 C  r$   )z@Return a list of :class:`.Multireddit`\ s belonging to the user.my_multiredditsr&   r   r   r   r   multireddits   r(   zUser.multireddits)numstate
submissionpraw.models.SubmissionrU   intrV   c                C  s<   |j ||dd}z| jjtd |dW S  ty   Y dS w )aK  Set the pin state of a submission on the authenticated user's profile.

        :param submission: An instance of :class:`.Submission` that will be
            pinned/unpinned.
        :param num: If specified, the slot in which the submission will be pinned into.
            If there is a submission already in the specified slot, it will be replaced.
            If ``None`` or there is not a submission in the specified slot, the first
            available slot will be used (default: ``None``). If all slots are used the
            following will occur:

            - Old Reddit:

              1. The submission in the last slot will be unpinned.
              2. The remaining pinned submissions will be shifted down a slot.
              3. The new submission will be pinned in the first slot.

            - New Reddit:

              1. The submission in the first slot will be unpinned.
              2. The remaining pinned submissions will be shifted up a slot.
              3. The new submission will be pinned in the last slot.

            .. note::

                At the time of writing (10/22/2021), there are 4 pin slots available and
                pins are in reverse order on old Reddit. If ``num`` is an invalid value,
                Reddit will ignore it and the same behavior will occur as if ``num`` is
                ``None``.

        :param state: ``True`` pins the submission, ``False`` unpins (default:
            ``True``).

        :returns: The pinned submission.

        :raises: ``prawcore.BadRequest`` when pinning a removed or deleted submission.
        :raises: ``prawcore.Forbidden`` when pinning a submission the authenticated user
            is not the author of.

        .. code-block:: python

            submission = next(reddit.user.me().submissions.new())
            reddit.user.pin(submission)

        T)idrU   rV   
to_profilesticky_submission)r<   N)fullnamer   postr   r   )r   rW   rU   rV   r<   r   r   r   pin   s   0zUser.pinc                 K  r,   )a  Return a :class:`.ListingGenerator` of :class:`.Subreddit`\ s the user is subscribed to.

        Additional keyword arguments are passed in the initialization of
        :class:`.ListingGenerator`.

        To print a list of the subreddits that you are subscribed to, try:

        .. code-block:: python

            for subreddit in reddit.user.subreddits(limit=None):
                print(str(subreddit))

        my_subredditsr.   r/   r   r   r   
subreddits  s
   
zUser.subredditsc                 C  r$   )a{  Return a :class:`.RedditorList` of trusted :class:`.Redditor`\ s.

        To display the usernames of your trusted users and the times at which you
        decided to trust them, try:

        .. code-block:: python

            trusted_users = reddit.user.trusted()
            for user in trusted_users:
                print(f"User: {user.name}, time: {user.date}")

        trustedr&   r   r   r   r   rb     s   zUser.trusted)r   r   )r   r   )r   r#   )r)   r*   r   r+   )r1   r3   r   r4   )r   r:   )rA   rB   r   rC   )r   rR   )rW   rX   rU   rY   rV   rB   r   rX   )__name__
__module____qualname____doc__r   r   r    r%   r0   r   r5   r;   rH   rQ   rT   r_   ra   rb   __classcell__r   r   r!   r   r      s&    



%

:r   )rf   
__future__r   typingr   r   warningsr   prawcorer   constr   
exceptionsr	   modelsr
   utilr   
util.cacher   baser   listing.generatorr   reddit.redditorr   reddit.subredditr   praw.modelsprawr   r   r   r   r   <module>   s"    