Skip to content

Commit 7e70ffd

Browse files
authored
Merge pull request #124 from praw-dev/moderator-subreddits
Restore User.moderator_subreddits
2 parents 636b8ab + 9315911 commit 7e70ffd

File tree

5 files changed

+297
-1
lines changed

5 files changed

+297
-1
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Unreleased
1414
- :meth:`.Inbox.mark_all_read` to mark all messages as read with one API call.
1515
- :meth:`~.InboxableMixin.unblock_subreddit` to unblock a subreddit.
1616
- :meth:`.update_crowd_control_level` to update the crowd control level of a post.
17+
- :meth:`.moderator_subreddits`, which returns information about the subreddits that the
18+
authenticated user moderates, has been restored.
1719
- The configuration setting ``refresh_token`` has been added back. See
1820
https://www.reddit.com/r/redditdev/comments/olk5e6/followup_oauth2_api_changes_regarding_refresh/
1921
for more info.

asyncpraw/models/reddit/redditor.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ async def moderated(self) -> List["asyncpraw.models.Subreddit"]:
291291
:returns: A ``list`` of :class:`~asyncpraw.models.Subreddit` objects. Return
292292
``[]`` if the redditor has no moderated subreddits.
293293
294+
:raises: ``asyncprawcore.ServerError`` in certain cicumstances. See the note
295+
below.
296+
294297
.. note::
295298
296299
The redditor's own user profile subreddit will not be returned, but other
@@ -305,6 +308,30 @@ async def moderated(self) -> List["asyncpraw.models.Subreddit"]:
305308
print(subreddit.display_name)
306309
print(subreddit.title)
307310
311+
.. note::
312+
313+
A ``asyncprawcore.ServerError`` exception may be raised if the redditor
314+
moderates a large number of subreddits. If that happens, try switching to
315+
:ref:`read-only mode <read_only_application>`. For example,
316+
317+
.. code-block:: python
318+
319+
reddit.read_only = True
320+
redditor = await reddit.redditor("reddit")
321+
async for subreddit in redditor.moderated():
322+
print(str(subreddit))
323+
324+
It is possible that requests made in read-only mode will also raise a
325+
``asyncprawcore.ServerError`` exception.
326+
327+
When used in read-only mode, this method does not retrieve information about
328+
subreddits that require certain special permissions to access, e.g., private
329+
subreddits and premium-only subreddits.
330+
331+
.. seealso::
332+
333+
:meth:`.User.moderator_subreddits`
334+
308335
"""
309336
return await self._reddit.get(API_PATH["moderated"].format(user=self)) or []
310337

asyncpraw/models/user.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,18 @@ def contributor_subreddits(
6565
) -> AsyncIterator["asyncpraw.models.Subreddit"]:
6666
"""Return a :class:`.ListingGenerator` of contributor subreddits.
6767
68-
These are subreddits that the user is a contributor of.
68+
These are subreddits in which the user is an approved user.
6969
7070
Additional keyword arguments are passed in the initialization of
7171
:class:`.ListingGenerator`.
7272
73+
To print a list of the subreddits that you are an approved user in, try:
74+
75+
.. code-block:: python
76+
77+
async for subreddit in reddit.user.contributor_subreddits(limit=None):
78+
print(str(subreddit))
79+
7380
"""
7481
return ListingGenerator(
7582
self._reddit, API_PATH["my_contributor"], **generator_kwargs
@@ -157,6 +164,30 @@ async def me(
157164
self._me = Redditor(self._reddit, _data=user_data)
158165
return self._me
159166

167+
def moderator_subreddits(
168+
self, **generator_kwargs: Union[str, int, Dict[str, str]]
169+
) -> AsyncIterator["asyncpraw.models.Subreddit"]:
170+
"""Return a :class:`.ListingGenerator` subreddits that the user moderates.
171+
172+
Additional keyword arguments are passed in the initialization of
173+
:class:`.ListingGenerator`.
174+
175+
To print a list of the names of the subreddits you moderate, try:
176+
177+
.. code-block:: python
178+
179+
async for subreddit in reddit.user.moderator_subreddits(limit=None):
180+
print(str(subreddit))
181+
182+
.. seealso::
183+
184+
:meth:`.Redditor.moderated`
185+
186+
"""
187+
return ListingGenerator(
188+
self._reddit, API_PATH["my_moderator"], **generator_kwargs
189+
)
190+
160191
async def multireddits(self) -> List["asyncpraw.models.Multireddit"]:
161192
"""Return a list of multireddits belonging to the user."""
162193
return await self._reddit.get(API_PATH["my_multireddits"])
@@ -169,6 +200,13 @@ def subreddits(
169200
Additional keyword arguments are passed in the initialization of
170201
:class:`.ListingGenerator`.
171202
203+
To print a list of the subreddits that you are subscribed to, try:
204+
205+
.. code-block:: python
206+
207+
async for subreddit in reddit.user.subreddits(limit=None):
208+
print(str(subreddit))
209+
172210
"""
173211
return ListingGenerator(
174212
self._reddit, API_PATH["my_subreddits"], **generator_kwargs

tests/integration/cassettes/TestUser.test_moderator_subreddits.json

Lines changed: 219 additions & 0 deletions
Large diffs are not rendered by default.

tests/integration/models/test_user.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ async def test_me__bypass_cache(self, _):
8181
me = await self.reddit.user.me(use_cache=False)
8282
assert not hasattr(me, "praw_is_cached")
8383

84+
@mock.patch("asyncio.sleep", return_value=None)
85+
async def test_moderator_subreddits(self, _):
86+
self.reddit.read_only = False
87+
with self.use_cassette():
88+
mod_subs = await self.async_list(
89+
self.reddit.user.moderator_subreddits(limit=None)
90+
)
91+
assert mod_subs
92+
assert all(isinstance(x, Subreddit) for x in mod_subs)
93+
8494
async def test_multireddits(self):
8595
self.reddit.read_only = False
8696
with self.use_cassette():

0 commit comments

Comments
 (0)