Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reddit: restrict nsfw subreddit(s) #1157

Merged
merged 6 commits into from Oct 17, 2020
Merged

Conversation

RohanJnr
Copy link
Member

@RohanJnr RohanJnr commented Sep 15, 2020

closes #1153

Changes made to Reddit Cog:

  • Check if the subreddit is NSFW or similar by using the over_18 boolean value got with the API response.

  • if True, then send an error message.

  • if False, the fetched data is sent to the user.

  • A small change to the return format of the fetch_posts() method, instead of returning an empty list in case of an error, it now returns a list, with a dict containing the error message. This is done in order to distinguish between the NSFW restriction error message and network/connection/failed to connect to API error.

Pending

  • Unfortunately, I cannot test this as my account is restricted to view any NSFW subreddits due to age and country (whenever I send a request to an NSFW subreddit, I get a 451 status code - Unavailable For Legal Reasons).
    https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/451
  • If anyone above 18 years of age, having a Reddit account, and living in a country that allows users to view NSFW content on Reddit, can test this feature, it will be helpful!

Edit: this should be working.

RohanJnr and others added 2 commits September 15, 2020 12:26
… be over 18).

Changed the return format a little bit for the fetch_posts() function, instead of returning an empty list, it returns a list with a dict holding the error message.
@RohanJnr RohanJnr marked this pull request as ready for review September 22, 2020 18:37
@RohanJnr RohanJnr requested a review from a team as a code owner September 22, 2020 18:37
@RohanJnr RohanJnr requested review from eivl and aeros and removed request for a team September 22, 2020 18:37
@ghost ghost added the needs 2 approvals label Sep 22, 2020
@Numerlor
Copy link
Contributor

I believe filtering posts and only sending the NSFW error message when no posts pass the filter would be a better option here, currently if the first fetched post happens to be NSFW while none of the others are, the user will get no output.

@RohanJnr
Copy link
Member Author

I think a better approach will be to check if any 1 of the posts does not pass the filter, then send nothing.
This is because, if we query for NSFW subreddit posts from the bot, then we don't want to send any posts, even tho is post is not entirely NSFW, we want to block the subreddit.

Copy link
Member

@kosayoda kosayoda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request!

  1. Regarding NSFW subreddits: Subreddits marked as over 18 will have all the posts marked over 18, so it shouldn't be an issue.
  2. Rather than telling the user about the subreddit being out of scope, the error message when no posts are found should just be ... couldn't find any SFW posts from that subreddit.... That way it is clearer and easier to handle in the code.
  3. Error handling should be in get_top_posts rather than fetch_posts, which would basically be what it is originally.

@@ -140,12 +140,32 @@ async def fetch_posts(self, route: str, *, amount: int = 25, params: dict = None
# Got appropriate response - process and return.
content = await response.json()
posts = content["data"]["children"]

for post in posts:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't mutate a list while you're looping over that same list. The loop is currently skipping posts because of it.

Either create a new list like filtered_posts or use a list comprehension to assign the filtered list to posts.

@ghost ghost added s: waiting for author Waiting for author to address a review or respond to a comment and removed needs 2 approvals labels Sep 30, 2020
@ghost ghost added needs 2 approvals and removed s: waiting for author Waiting for author to address a review or respond to a comment labels Oct 1, 2020
@RohanJnr RohanJnr requested a review from kosayoda October 1, 2020 10:06
@MarkKoz MarkKoz added a: information Related to information commands: (doc, help, information, reddit, site, tags) p: 1 - high High Priority t: feature New feature or request labels Oct 1, 2020
@@ -163,12 +166,11 @@ async def get_top_posts(self, subreddit: Subreddit, time: str = "all", amount: i
amount=amount,
params={"t": time}
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for the removal of the newline here?

Copy link
Member

@kosayoda kosayoda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well in testing now, awesomesauce.

Copy link
Contributor

@kwzrd kwzrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great, thanks for the fix. There is a good opportunity to test against r/test's top weekly posts:

image

Also works against NSFW-only subs.

@ghost ghost removed the needs 1 approval label Oct 17, 2020
@kwzrd kwzrd changed the title Restrict nsfw subreddit(s) or similar Reddit: restrict nsfw subreddit(s) Oct 17, 2020
@kwzrd kwzrd merged commit ef68d4e into python-discord:master Oct 17, 2020
@kwzrd
Copy link
Contributor

kwzrd commented Oct 17, 2020

Since this is somewhat critical I decided to merge it even with an unresolved comment from @Numerlor. It only applies to code style and can be addressed separately if necessary. Feel free to continue the conversation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: information Related to information commands: (doc, help, information, reddit, site, tags) p: 1 - high High Priority t: feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

!reddit command doesn’t filter nsfw subreddits.
5 participants