Skip to content

Commit

Permalink
Merge pull request #107 from praw-dev/trust
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Jun 17, 2021
2 parents ecfcabf + e845241 commit 9dc53ae
Show file tree
Hide file tree
Showing 7 changed files with 527 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Unreleased

- :class:`.UserSubreddit` for the ``subreddit`` attribute of :class:`.Redditor`.
- :meth:`.Reddit.username_available` checks if a username is available.
- :meth:`.trusted` to retrieve a :class:`.RedditorList` of trusted users.
- :meth:`.trust` to add a user to the trusted list.
- :meth:`.distrust` to remove a user from the trusted list.

**Changed**

Expand Down
3 changes: 3 additions & 0 deletions asyncpraw/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"about_unmoderated": "r/{subreddit}/about/unmoderated/",
"accept_mod_invite": "r/{subreddit}/api/accept_moderator_invite",
"add_subreddit_rule": "api/add_subreddit_rule",
"add_whitelisted": "api/add_whitelisted",
"approve": "api/approve/",
"award_thing": "api/v2/gold/gild",
"block": "api/block",
Expand Down Expand Up @@ -143,6 +144,7 @@
"removal_reason": "api/v1/{subreddit}/removal_reasons/{id}",
"removal_reasons_list": "api/v1/{subreddit}/removal_reasons",
"remove_subreddit_rule": "api/remove_subreddit_rule",
"remove_whitelisted": "api/remove_whitelisted",
"remove": "api/remove/",
"reorder_subreddit_rules": "api/reorder_subreddit_rules",
"report": "api/report/",
Expand Down Expand Up @@ -184,6 +186,7 @@
"subscribe": "api/subscribe/",
"suggested_sort": "api/set_suggested_sort/",
"trophies": "api/v1/user/{user}/trophies",
"trusted": "prefs/trusted",
"uncollapse": "api/uncollapse_message/",
"unfriend": "r/{subreddit}/api/unfriend/",
"unhide": "api/unhide/",
Expand Down
67 changes: 67 additions & 0 deletions asyncpraw/models/reddit/redditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,36 @@ async def block(self):
redditor = await reddit.redditor("spez")
await redditor.block()
.. note::
Blocking a trusted user will remove that user from your trusted list.
.. seealso::
:meth:`.trust`
"""
await self._reddit.post(API_PATH["block_user"], params={"name": self.name})

async def distrust(self):
"""Remove the Redditor from your whitelist of trusted users.
For example, to remove Redditor ``spez`` from your whitelist:
.. code-block:: python
redditor = await reddit.redditor("spez")
await redditor.distrust()
.. seealso::
:meth:`.trust`
"""
await self._reddit.post(
API_PATH["remove_whitelisted"], data={"name": self.name}
)

async def friend(self, note: str = None):
"""Friend the Redditor.
Expand Down Expand Up @@ -312,6 +339,46 @@ async def trophies(self) -> List["asyncpraw.models.Trophy"]:
"""
return list(await self._reddit.get(API_PATH["trophies"].format(user=self)))

async def trust(self):
"""Add the Redditor to your whitelist of trusted users.
Trusted users will always be able to send you PMs.
Example usage:
.. code-block:: python
redditor = await reddit.redditor("AaronSw")
await redditor.trust()
Use the ``accept_pms`` parameter of :meth:`.Preferences.update` to toggle your
``accept_pms`` setting between ``"everyone"`` and ``"whitelisted"``. For
example:
.. code-block:: python
# Accept private messages from everyone:
await reddit.user.preferences.update(accept_pms="everyone")
# Only accept private messages from trusted users:
await reddit.user.preferences.update(accept_pms="whitelisted")
You may trust a user even if your ``accept_pms`` setting is switched to
``"everyone"``.
.. note::
You are allowed to have a user on your blocked list and your friends list at
the same time. However, you cannot trust a user who is on your blocked list.
.. seealso::
- :meth:`.distrust`
- :meth:`.Preferences.update`
- :meth:`.trusted`
"""
await self._reddit.post(API_PATH["add_whitelisted"], data={"name": self.name})

async def unblock(self):
"""Unblock the Redditor.
Expand Down
15 changes: 15 additions & 0 deletions asyncpraw/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,18 @@ def subreddits(
return ListingGenerator(
self._reddit, API_PATH["my_subreddits"], **generator_kwargs
)

async def trusted(self) -> List["asyncpraw.models.Redditor"]:
"""Return a RedditorList of trusted Redditors.
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}")
"""
return await self._reddit.get(API_PATH["trusted"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
{
"interactions": [
{
"request": {
"body": [
[
"grant_type",
"password"
],
[
"password",
"<PASSWORD>"
],
[
"username",
"<USERNAME>"
]
],
"headers": {
"AUTHORIZATION": [
"Basic <BASIC_AUTH>"
],
"Accept-Encoding": [
"identity"
],
"Connection": [
"close"
],
"User-Agent": [
"<USER_AGENT> Async PRAW/7.2.1.dev0 asyncprawcore/2.2.0"
]
},
"method": "POST",
"uri": "https://www.reddit.com/api/v1/access_token"
},
"response": {
"body": {
"string": "{\"access_token\": \"<ACCESS_TOKEN>\", \"token_type\": \"bearer\", \"expires_in\": 3600, \"scope\": \"*\"}"
},
"headers": {
"Accept-Ranges": "bytes",
"Cache-Control": "max-age=0, must-revalidate",
"Connection": "close",
"Content-Length": "121",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Wed, 16 Jun 2021 23:11:00 GMT",
"Server": "snooserv",
"Set-Cookie": "edgebucket=HLr1AELfgZhlrI9pfX; Domain=reddit.com; Max-Age=63071999; Path=/; secure",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Via": "1.1 varnish",
"X-Clacks-Overhead": "GNU Terry Pratchett",
"X-Moose": "majestic",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-ratelimit-remaining": "278",
"x-ratelimit-reset": "540",
"x-ratelimit-used": "22",
"x-xss-protection": "1; mode=block"
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://www.reddit.com/api/v1/access_token"
}
},
{
"request": {
"body": [
[
"api_type",
"json"
],
[
"name",
"PyAPITestUser3"
]
],
"headers": {
"Accept-Encoding": [
"identity"
],
"Authorization": [
"bearer <ACCESS_TOKEN>"
],
"User-Agent": [
"<USER_AGENT> Async PRAW/7.2.1.dev0 asyncprawcore/2.2.0"
]
},
"method": "POST",
"uri": "https://oauth.reddit.com/api/add_whitelisted?raw_json=1"
},
"response": {
"body": {
"string": "{\"json\": {\"errors\": []}}"
},
"headers": {
"Accept-Ranges": "bytes",
"Cache-Control": "private, s-maxage=0, max-age=0, must-revalidate, no-store, max-age=0, must-revalidate",
"Connection": "keep-alive",
"Content-Length": "24",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Wed, 16 Jun 2021 23:11:01 GMT",
"Expires": "-1",
"Server": "snooserv",
"Set-Cookie": "session_tracker=fnrcrrppgpcrgmmrqe.0.1623885061043.Z0FBQUFBQmd5b1VGMHpzWVRIT2U4U0NVVi1HbkRZYmIwcWQ0UG9YV1pqWE0tTHI4eFE2TGd5ZWZuY0ZOWk5iYm5OMUR3MV83RmFHdWx6MEs4WVNBTmxUMFN4VDU1dDN5QVNYSzVsTUhiRGl2b0RJbjNBYjF0dUdfN0FtcU1LNU1HYWhJbUUxX0IycUU; Domain=reddit.com; Max-Age=7199; Path=/; expires=Thu, 17-Jun-2021 01:11:01 GMT; secure",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Via": "1.1 varnish",
"X-Clacks-Overhead": "GNU Terry Pratchett",
"X-Moose": "majestic",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-ratelimit-remaining": "598.0",
"x-ratelimit-reset": "539",
"x-ratelimit-used": "2",
"x-ua-compatible": "IE=edge",
"x-xss-protection": "1; mode=block"
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://oauth.reddit.com/api/add_whitelisted?raw_json=1"
}
},
{
"request": {
"body": null,
"headers": {
"Accept-Encoding": [
"identity"
],
"Authorization": [
"bearer <ACCESS_TOKEN>"
],
"User-Agent": [
"<USER_AGENT> Async PRAW/7.2.1.dev0 asyncprawcore/2.2.0"
]
},
"method": "GET",
"uri": "https://oauth.reddit.com/prefs/trusted?raw_json=1"
},
"response": {
"body": {
"string": "{\"kind\": \"UserList\", \"data\": {\"children\": [{\"date\": 1623885061.0, \"rel_id\": \"r9_2no6bp\", \"name\": \"PyAPITestUser3\", \"id\": \"t2_6c1xj\"}]}}"
},
"headers": {
"Accept-Ranges": "bytes",
"Cache-Control": "private, s-maxage=0, max-age=0, must-revalidate, no-store, max-age=0, must-revalidate",
"Connection": "keep-alive",
"Content-Length": "135",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Wed, 16 Jun 2021 23:11:01 GMT",
"Expires": "-1",
"Server": "snooserv",
"Set-Cookie": "csv=1; Max-Age=63072000; Domain=.reddit.com; Path=/; Secure; SameSite=None",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Via": "1.1 varnish",
"X-Clacks-Overhead": "GNU Terry Pratchett",
"X-Moose": "majestic",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-ratelimit-remaining": "597.0",
"x-ratelimit-reset": "539",
"x-ratelimit-used": "3",
"x-ua-compatible": "IE=edge",
"x-xss-protection": "1; mode=block"
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://oauth.reddit.com/prefs/trusted?raw_json=1"
}
},
{
"request": {
"body": [
[
"api_type",
"json"
],
[
"name",
"PyAPITestUser3"
]
],
"headers": {
"Accept-Encoding": [
"identity"
],
"Authorization": [
"bearer <ACCESS_TOKEN>"
],
"User-Agent": [
"<USER_AGENT> Async PRAW/7.2.1.dev0 asyncprawcore/2.2.0"
]
},
"method": "POST",
"uri": "https://oauth.reddit.com/api/remove_whitelisted?raw_json=1"
},
"response": {
"body": {
"string": "{}"
},
"headers": {
"Accept-Ranges": "bytes",
"Cache-Control": "private, s-maxage=0, max-age=0, must-revalidate, no-store, max-age=0, must-revalidate",
"Connection": "keep-alive",
"Content-Length": "2",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Wed, 16 Jun 2021 23:11:01 GMT",
"Expires": "-1",
"Server": "snooserv",
"Set-Cookie": "session_tracker=fnrcrrppgpcrgmmrqe.0.1623885061325.Z0FBQUFBQmd5b1VGWUNyTzZVYkpfLXVWa3NfcUt2ZlZqb1FzWDNqdGdBMlVUQ0NuWTFtVWFlcnc1M0tKVDZSeWdpeW1vWWp2d29nMXJreDlFY3phSlZFNU5XQXRDWDk2TEVrb2NNZ2Q5VmpMWlRtamJMcnN6N1lnNTV0TWpXU2MySEN4Sk9MRlZ0UVg; Domain=reddit.com; Max-Age=7199; Path=/; expires=Thu, 17-Jun-2021 01:11:01 GMT; secure",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Via": "1.1 varnish",
"X-Clacks-Overhead": "GNU Terry Pratchett",
"X-Moose": "majestic",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-ratelimit-remaining": "596.0",
"x-ratelimit-reset": "539",
"x-ratelimit-used": "4",
"x-ua-compatible": "IE=edge",
"x-xss-protection": "1; mode=block"
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://oauth.reddit.com/api/remove_whitelisted?raw_json=1"
}
}
],
"recorded_at": "2021-06-16T18:11:01",
"version": 1
}

0 comments on commit 9dc53ae

Please sign in to comment.