reddark
reddark copied to clipboard
Represent "restricted" subreddits as private & code cleanup
I have implemented a system to detect subreddits that are not accepting submissions and mark them as private accordingly by taking advantage of the details provided at /r/<subreddit>/about.json
. This strategy allows us to use the same number of requests as the current system to achieve this. Thank you to @DavidCano98 for pointing out the existence of about.json
, as it is far more elegant than the xpath-scraping solution I had created beforehand.
Many subreddits are opting to restrict submissions instead of privating the subreddit to allow their "protest" mod posts to reach the front page. Many large subreddits are doing this, such as r/Pics, so this fixes an enormous gap in representation.
This pull request also includes a lot of code cleanup, additional comments, and standardization which will make contribution more approachable to others.
The crux of the restriction identification system can be seen in this excerpt:
const resp = JSON.parse(data);
let isPrivate = resp["error"] === 403 // this case occurs when subreddit is private
let isRestricted = resp["data"] !== undefined &&
resp["data"]["subreddit_type"] === "restricted" // when subreddit is restricted
let effectivelyPrivate = isPrivate || isRestricted; // if either is true, it's participating
let subredditPreviouslyPrivate = subreddits[section][subreddit].status === "private";
if (resp['error'] === 500) {
console.log("We're probably getting blocked... (500) - " + resp);
return;
}
// If previously public subreddit has become private
if (effectivelyPrivate && !subredditPreviouslyPrivate) {
subreddits[section][subreddit].status = "private";
if (!firstCheck)
io.emit("update", subreddits[section][subreddit]);
else
io.emit("updatenew", subreddits[section][subreddit]);
console.log(`PRIVATE: ${subreddits[section][subreddit].name}`)
// console.log(resp);
// if subreddit status was private and is now public
} else if (!effectivelyPrivate && subredditPreviouslyPrivate) {
console.log(`PUBLIC: ${subreddits[section][subreddit].name}`)
// console.log(resp);
subreddits[section][subreddit].status = "public";
io.emit("updatenew", subreddits[section][subreddit]);
}
@DavidCano98 kindly pointed out to me that there is a way to access the subreddit status trivially through a GET request. I will be updating this pull request accordingly.
I have updated the original pull request message to match the new code. It is ready to be merged.
I see a number of changes have been made to the main branch, breaking this PR's ability to merge. I am working on catching it up to date.
I have locally merged my code with the latest code on origin/main
. @Tanza3D, please consider merging this in, it would be an enormous leap in functionality for the site.