2021-09-02 20:56:37 +02:00
const config = require ( '../config' ) ;
const { redis , fetch , RedditAPI } = require ( '../app' ) ;
const subredditRoutes = require ( 'express' ) . Router ( ) ;
2021-09-07 14:44:34 +02:00
const {
processJsonPost ,
finalizeJsonPost ,
} = require ( '../inc/processJsonPost.js' ) ;
2021-09-07 15:24:10 +02:00
const processSubredditAbout = require ( '../inc/processSubredditAbout.js' ) ;
2021-09-08 12:47:25 +02:00
const processSearchResults = require ( '../inc/processSearchResults.js' ) ;
2021-09-08 13:03:35 +02:00
const processJsonSubreddit = require ( '../inc/processJsonSubreddit.js' ) ;
2021-09-02 22:23:28 +02:00
const tedditApiSubreddit = require ( '../inc/teddit_api/handleSubreddit.js' ) ( ) ;
2021-09-09 18:25:20 +02:00
const processMoreComments = require ( '../inc/processMoreComments.js' ) ;
2021-09-08 13:28:45 +02:00
const processJsonSubredditsExplore = require ( '../inc/processSubredditsExplore.js' ) ;
2021-09-02 20:56:37 +02:00
subredditRoutes . get ( '/r/:subreddit/search' , ( req , res , next ) => {
let subreddit = req . params . subreddit ;
let q = req . query . q ;
if ( typeof q === 'undefined' ) {
return res . render ( 'search' , {
json : { posts : [ ] } ,
no _query : true ,
q : '' ,
restrict _sr : undefined ,
nsfw : undefined ,
subreddit : subreddit ,
sortby : undefined ,
past : undefined ,
user _preferences : req . cookies ,
} ) ;
}
let restrict _sr = req . query . restrict _sr ;
let nsfw = req . query . nsfw ;
let sortby = req . query . sort ;
let past = req . query . t ;
let after = req . query . after ;
let before = req . query . before ;
if ( ! after ) {
after = '' ;
}
if ( ! before ) {
before = '' ;
}
let d = ` &after= ${ after } ` ;
if ( before ) {
d = ` &before= ${ before } ` ;
}
if ( restrict _sr !== 'on' ) {
restrict _sr = 'off' ;
}
if ( nsfw !== 'on' ) {
nsfw = 'off' ;
}
2022-01-30 01:33:58 +01:00
let count = '&count=25' ;
if ( after == '' ) {
count = '' ;
}
2021-09-02 20:56:37 +02:00
let key = ` search: ${ subreddit } : ${ q } : ${ restrict _sr } : ${ sortby } : ${ past } : ${ after } : ${ before } : ${ nsfw } ` ;
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( 'Error getting the search key from redis.' , error ) ;
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
json : null ,
user _preferences : req . cookies ,
} ) ;
}
if ( json ) {
console . log ( 'Got search key from redis.' ) ;
( async ( ) => {
let processed _json = await processSearchResults (
json ,
false ,
after ,
before ,
req . cookies
) ;
return res . render ( 'search' , {
json : processed _json ,
no _query : false ,
q : q ,
restrict _sr : restrict _sr ,
nsfw : nsfw ,
subreddit : subreddit ,
sortby : sortby ,
past : past ,
user _preferences : req . cookies ,
} ) ;
} ) ( ) ;
} else {
let url = '' ;
if ( config . use _reddit _oauth )
2022-01-30 01:33:58 +01:00
url = ` https://oauth.reddit.com/r/ ${ subreddit } /search?api_type=json&q= ${ q } &restrict_sr= ${ restrict _sr } &include_over_18= ${ nsfw } &sort= ${ sortby } &t= ${ past } ${ count } ${ d } ` ;
2021-09-02 20:56:37 +02:00
else
2022-01-30 01:33:58 +01:00
url = ` https://reddit.com/r/ ${ subreddit } /search.json?api_type=json&q= ${ q } &restrict_sr= ${ restrict _sr } &include_over_18= ${ nsfw } &sort= ${ sortby } &t= ${ past } ${ count } ${ d } ` ;
2021-09-02 20:56:37 +02:00
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
. then ( ( result ) => {
if ( result . status === 200 ) {
result . json ( ) . then ( ( json ) => {
( async ( ) => {
/ * *
* Fetch suggested subreddits when the restrict _sr option is
* turned off ( "limit my search to" ) and we are on the first search
* page ( just like in Reddit ) .
* /
json . suggested _subreddits = { } ;
if ( restrict _sr === 'off' && before == '' && after == '' ) {
let url = ` https://reddit.com/subreddits/search.json?q= ${ q } &include_over_18= ${ nsfw } &limit=3 ` ;
const response = await fetch ( encodeURI ( url ) ) ;
const data = await response . json ( ) ;
json . suggested _subreddits = data ;
}
redis . setex (
key ,
config . setexs . searches ,
JSON . stringify ( json ) ,
( error ) => {
if ( error ) {
console . error (
'Error setting the searches key to redis.' ,
error
) ;
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
json : null ,
user _preferences : req . cookies ,
} ) ;
} else {
console . log ( 'Fetched search results from Reddit.' ) ;
( async ( ) => {
let processed _json = await processSearchResults (
json ,
true ,
after ,
before ,
req . cookies
) ;
return res . render ( 'search' , {
no _query : false ,
json : processed _json ,
q : q ,
restrict _sr : restrict _sr ,
nsfw : nsfw ,
subreddit : subreddit ,
sortby : sortby ,
past : past ,
user _preferences : req . cookies ,
} ) ;
} ) ( ) ;
}
}
) ;
} ) ( ) ;
} ) ;
} else {
console . error (
` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } `
) ;
console . error ( config . reddit _api _error _text ) ;
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies ,
} ) ;
}
} )
. catch ( ( error ) => {
console . error ( 'Error fetching the frontpage JSON file.' , error ) ;
} ) ;
}
} ) ;
} ) ;
subredditRoutes . get (
'/r/:subreddit/wiki/:page?/:sub_page?' ,
( req , res , next ) => {
let subreddit = req . params . subreddit ;
let page = req . params . page ;
let sub _page = req . params . sub _page || '' ;
if ( ! page ) page = 'index' ;
if ( sub _page != '' ) sub _page = ` / ${ sub _page } ` ;
function formatWikipagelisting ( json , subreddit ) {
let html = '<ul class="wikipagelisting">' ;
if ( json . kind === 'wikipagelisting' && json . data ) {
for ( var i = 0 ; i < json . data . length ; i ++ ) {
let d = json . data [ i ] ;
html += ` <li><a href="/r/ ${ subreddit } /wiki/ ${ d } "> ${ d } </a></li> ` ;
}
}
html += '</ul>' ;
return html ;
}
let key = ` ${ subreddit . toLowerCase ( ) } :wiki:page: ${ page } :sub_page: ${ sub _page } ` ;
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error (
` Error getting the ${ subreddit } wiki key from redis. ` ,
error
) ;
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
json : null ,
user _preferences : req . cookies ,
} ) ;
}
if ( json ) {
console . log ( ` Got /r/ ${ subreddit } wiki key from redis. ` ) ;
json = JSON . parse ( json ) ;
return res . render ( 'subreddit_wiki' , {
content _html :
page !== 'pages'
? unescape ( json . data . content _html )
: formatWikipagelisting ( json , subreddit ) ,
subreddit : subreddit ,
user _preferences : req . cookies ,
} ) ;
} else {
let url = '' ;
if ( config . use _reddit _oauth )
url = ` https://oauth.reddit.com/r/ ${ subreddit } /wiki/ ${ page } ${ sub _page } ?api_type=json ` ;
else
url = ` https://reddit.com/r/ ${ subreddit } /wiki/ ${ page } ${ sub _page } .json?api_type=json ` ;
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
. then ( ( result ) => {
if ( result . status === 200 ) {
result . json ( ) . then ( ( json ) => {
redis . setex (
key ,
config . setexs . wikis ,
JSON . stringify ( json ) ,
( error ) => {
if ( error ) {
console . error (
` Error setting the ${ subreddit } wiki key to redis. ` ,
error
) ;
return res . render ( 'subreddit' , {
json : null ,
user _preferences : req . cookies ,
} ) ;
} else {
console . log (
` Fetched the JSON from reddit.com/r/ ${ subreddit } /wiki. `
) ;
return res . render ( 'subreddit_wiki' , {
content _html :
page !== 'pages'
? unescape ( json . data . content _html )
: formatWikipagelisting ( json , subreddit ) ,
subreddit : subreddit ,
user _preferences : req . cookies ,
} ) ;
}
}
) ;
} ) ;
} else {
if ( result . status === 404 ) {
console . log ( '404 – Subreddit wiki not found' ) ;
} else {
console . error (
` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } `
) ;
console . error ( config . reddit _api _error _text ) ;
}
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies ,
} ) ;
}
} )
. catch ( ( error ) => {
console . error (
` Error fetching the JSON file from reddit.com/r/ ${ subreddit } /wiki. ` ,
error
) ;
} ) ;
}
} ) ;
}
) ;
subredditRoutes . get ( '/r/:subreddit/w/:page?/:sub_page?' , ( req , res , next ) => {
/* "w" is a shorturl for wikis for example https://old.reddit.com/r/privacytoolsIO/w/index */
let subreddit = req . params . subreddit ;
let page = req . params . page ;
let sub _page = req . params . sub _page || '' ;
if ( ! page ) page = 'index' ;
if ( sub _page != '' ) sub _page = ` / ${ sub _page } ` ;
return res . redirect ( ` /r/ ${ subreddit } /wiki/ ${ page } ${ sub _page } ` ) ;
} ) ;
subredditRoutes . get ( '/r/random' , ( req , res , next ) => {
let url = '' ;
if ( config . use _reddit _oauth )
url = ` https://oauth.reddit.com/r/random?api_type=json&count=25&g=GLOBAL ` ;
else url = ` https://reddit.com/r/random.json?api_type=json&count=25&g=GLOBAL ` ;
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
. then ( ( result ) => {
if ( result . status === 200 ) {
result . json ( ) . then ( ( json ) => {
let subreddit = json . data . children [ 0 ] . data . subreddit ;
if ( subreddit ) {
let key = ` ${ subreddit . toLowerCase ( ) } :undefined:undefined:sort:hot:past:undefined ` ;
redis . setex (
key ,
config . setexs . subreddit ,
JSON . stringify ( json ) ,
( error ) => {
if ( error ) {
console . error (
` Error setting the random subreddit key to redis. ` ,
error
) ;
return res . render ( 'subreddit' , {
json : null ,
user _preferences : req . cookies ,
} ) ;
} else {
console . log (
` Fetched the JSON from reddit.com/r/ ${ subreddit } . `
) ;
return res . redirect ( ` /r/ ${ subreddit } ` ) ;
}
}
) ;
} else {
console . error ( ` Fetching random subreddit failed. ` , json ) ;
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
json : null ,
user _preferences : req . cookies ,
} ) ;
}
} ) ;
} else {
if ( result . status === 404 ) {
console . log ( '404 – Subreddit not found' ) ;
} else {
console . error (
` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } `
) ;
console . error ( config . reddit _api _error _text ) ;
}
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies ,
} ) ;
}
} )
. catch ( ( error ) => {
console . error (
` Error fetching the JSON file from reddit.com/r/random. ` ,
error
) ;
} ) ;
} ) ;
subredditRoutes . get ( '/r/:subreddit/:sort?' , ( req , res , next ) => {
let subreddit = req . params . subreddit ;
let sortby = req . params . sort ;
let past = req . query . t ;
let before = req . query . before ;
let after = req . query . after ;
let api _req = req . query . api ;
let api _type = req . query . type ;
let api _target = req . query . target ;
2021-12-27 18:18:21 +01:00
let api _mode = req . query . mode ;
2021-09-02 20:56:37 +02:00
if ( req . query . hasOwnProperty ( 'api' ) ) api _req = true ;
else api _req = false ;
let raw _json = api _req && req . query . raw _json == '1' ? 1 : 0 ;
let d = ` &after= ${ after } ` ;
if ( before ) {
d = ` &before= ${ before } ` ;
}
if ( ! sortby ) {
sortby = 'hot' ;
}
if (
! [ 'new' , 'rising' , 'controversial' , 'top' , 'gilded' , 'hot' ] . includes ( sortby )
) {
console . log ( ` Got invalid sort. ` , req . originalUrl ) ;
return res . redirect ( ` /r/ ${ subreddit } ` ) ;
}
if ( past ) {
if ( sortby === 'controversial' || sortby === 'top' ) {
if ( ! [ 'hour' , 'day' , 'week' , 'month' , 'year' , 'all' ] . includes ( past ) ) {
console . error ( ` Got invalid past. ` , req . originalUrl ) ;
return res . redirect ( ` /r/ ${ subreddit } / ${ sortby } ` ) ;
}
} else {
past = undefined ;
}
} else {
if ( sortby === 'controversial' || sortby === 'top' ) {
past = 'day' ;
}
}
let key = ` ${ subreddit . toLowerCase ( ) } : ${ after } : ${ before } :sort: ${ sortby } :past: ${ past } :raw_json: ${ raw _json } ` ;
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting the ${ subreddit } key from redis. ` , error ) ;
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
json : null ,
user _preferences : req . cookies ,
} ) ;
}
if ( json ) {
console . log ( ` Got /r/ ${ subreddit } key from redis. ` ) ;
( async ( ) => {
if ( api _req ) {
return handleTedditApiSubreddit (
json ,
req ,
res ,
'redis' ,
api _type ,
api _target ,
2021-12-27 18:18:21 +01:00
subreddit ,
api _mode
2021-09-02 20:56:37 +02:00
) ;
} else {
let processed _json = await processJsonSubreddit (
json ,
'redis' ,
null ,
req . cookies
) ;
let subreddit _about = await processSubredditAbout (
subreddit ,
redis ,
fetch ,
RedditAPI
) ;
if ( ! processed _json . error ) {
return res . render ( 'subreddit' , {
json : processed _json ,
subreddit : subreddit ,
subreddit _about : subreddit _about ,
subreddit _front : ! before && ! after ? true : false ,
sortby : sortby ,
past : past ,
user _preferences : req . cookies ,
instance _nsfw _enabled : config . nsfw _enabled ,
redis _key : key ,
after : req . query . after ,
before : req . query . before ,
} ) ;
} else {
return res . render ( 'subreddit' , {
json : null ,
error : true ,
data : processed _json ,
user _preferences : req . cookies ,
} ) ;
}
}
} ) ( ) ;
} else {
let url = '' ;
if ( config . use _reddit _oauth )
url = ` https://oauth.reddit.com/r/ ${ subreddit } / ${ sortby } ?api_type=json&count=25&g=GLOBAL&t= ${ past } ${ d } &raw_json= ${ raw _json } ` ;
else
url = ` https://reddit.com/r/ ${ subreddit } / ${ sortby } .json?api_type=json&count=25&g=GLOBAL&t= ${ past } ${ d } &raw_json= ${ raw _json } ` ;
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
. then ( ( result ) => {
if ( result . status === 200 ) {
result . json ( ) . then ( ( json ) => {
redis . setex (
key ,
config . setexs . subreddit ,
JSON . stringify ( json ) ,
( error ) => {
if ( error ) {
console . error (
` Error setting the ${ subreddit } key to redis. ` ,
error
) ;
return res . render ( 'subreddit' , {
json : null ,
user _preferences : req . cookies ,
} ) ;
} else {
console . log (
` Fetched the JSON from reddit.com/r/ ${ subreddit } . `
) ;
( async ( ) => {
if ( api _req ) {
return handleTedditApiSubreddit (
json ,
req ,
res ,
'from_online' ,
api _type ,
api _target ,
2021-12-27 18:18:21 +01:00
subreddit ,
api _mode
2021-09-02 20:56:37 +02:00
) ;
} else {
let processed _json = await processJsonSubreddit (
json ,
'from_online' ,
null ,
req . cookies
) ;
let subreddit _about = await processSubredditAbout (
subreddit ,
redis ,
fetch ,
RedditAPI
) ;
return res . render ( 'subreddit' , {
json : processed _json ,
subreddit : subreddit ,
subreddit _about : subreddit _about ,
subreddit _front : ! before && ! after ? true : false ,
sortby : sortby ,
past : past ,
user _preferences : req . cookies ,
instance _nsfw _enabled : config . nsfw _enabled ,
redis _key : key ,
after : req . query . after ,
before : req . query . before ,
} ) ;
}
} ) ( ) ;
}
}
) ;
} ) ;
} else {
if ( result . status === 404 ) {
console . log ( '404 – Subreddit not found' ) ;
} else {
console . error (
` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } `
) ;
console . error ( config . reddit _api _error _text ) ;
}
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies ,
} ) ;
}
} )
. catch ( ( error ) => {
console . error (
` Error fetching the JSON file from reddit.com/r/ ${ subreddit } . ` ,
error
) ;
} ) ;
}
} ) ;
} ) ;
subredditRoutes . get (
'/r/:subreddit/comments/:id/:snippet?/:comment_id?' ,
( req , res , next ) => {
let subreddit = req . params . subreddit ;
let id = req . params . id ;
let snippet = encodeURIComponent ( req . params . snippet ) ;
let sortby = req . query . sort ;
let comment _id = '' ;
let viewing _comment = false ;
let comment _ids = req . query . comment _ids ;
let context = parseInt ( req . query . context ) ;
if ( req . params . comment _id ) {
comment _id = ` ${ req . params . comment _id } / ` ;
viewing _comment = true ;
}
if ( ! sortby ) {
sortby = config . post _comments _sort ;
}
if (
! [
'confidence' ,
'top' ,
'new' ,
'controversial' ,
'old' ,
'qa' ,
'random' ,
] . includes ( sortby )
) {
console . log ( ` Got invalid sort. ` , req . originalUrl ) ;
return res . redirect ( '/' ) ;
}
let comments _url = ` /r/ ${ subreddit } /comments/ ${ id } / ${ snippet } / ${ comment _id } ` ;
let post _url = ` /r/ ${ subreddit } /comments/ ${ id } / ${ snippet } / ` ;
let comments _key = ` ${ comments _url } :sort: ${ sortby } ` ;
redis . get ( comments _key , ( error , json ) => {
if ( error ) {
console . error (
` Error getting the ${ comments _url } key from redis. ` ,
error
) ;
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
post : null ,
user _preferences : req . cookies ,
} ) ;
}
if ( json ) {
console . log ( ` Got ${ comments _url } key from redis. ` ) ;
( async ( ) => {
let parsed = false ;
let more _comments = null ;
if ( comment _ids ) {
let key = ` ${ post _url } :morechildren:comment_ids: ${ comment _ids } ` ;
2021-09-09 19:28:02 +02:00
more _comments = await processMoreComments (
2021-09-02 20:56:37 +02:00
fetch ,
redis ,
post _url ,
comment _ids ,
id
) ;
if ( more _comments === false ) {
return res . redirect ( post _url ) ;
} else {
json = JSON . parse ( json ) ;
json [ 1 ] . data . children = more _comments ;
parsed = true ;
}
}
let processed _json = await processJsonPost ( json , parsed , req . cookies ) ;
let finalized _json = await finalizeJsonPost (
processed _json ,
id ,
post _url ,
more _comments ,
viewing _comment ,
req . cookies
) ;
return res . render ( 'post' , {
post : finalized _json . post _data ,
comments : finalized _json . comments ,
viewing _comment : viewing _comment ,
post _url : post _url ,
subreddit : subreddit ,
sortby : sortby ,
user _preferences : req . cookies ,
instance _nsfw _enabled : config . nsfw _enabled ,
instance _videos _muted : config . videos _muted ,
post _media _max _heights : config . post _media _max _heights ,
redis _key : comments _key ,
} ) ;
} ) ( ) ;
} else {
let url = '' ;
if ( config . use _reddit _oauth )
url = ` https://oauth.reddit.com ${ comments _url } ?api_type=json&sort= ${ sortby } &context= ${ context } ` ;
else
url = ` https://reddit.com ${ comments _url } .json?api_type=json&sort= ${ sortby } &context= ${ context } ` ;
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
. then ( ( result ) => {
if ( result . status === 200 ) {
result . json ( ) . then ( ( json ) => {
redis . setex (
comments _key ,
config . setexs . posts ,
JSON . stringify ( json ) ,
( error ) => {
if ( error ) {
console . error (
` Error setting the ${ comments _url } key to redis. ` ,
error
) ;
return res . render ( 'post' , {
post : null ,
user _preferences : req . cookies ,
} ) ;
} else {
console . log (
` Fetched the JSON from reddit.com ${ comments _url } . `
) ;
( async ( ) => {
let more _comments = null ;
if ( comment _ids ) {
let key = ` ${ post _url } :morechildren:comment_ids: ${ comment _ids } ` ;
2021-09-09 19:19:52 +02:00
more _comments = await processMoreComments (
2021-09-02 20:56:37 +02:00
fetch ,
redis ,
post _url ,
comment _ids ,
id
) ;
if ( more _comments === false ) {
return res . redirect ( post _url ) ;
} else {
json [ 1 ] . data . children = more _comments ;
}
}
let processed _json = await processJsonPost (
json ,
true ,
req . cookies
) ;
let finalized _json = await finalizeJsonPost (
processed _json ,
id ,
post _url ,
more _comments ,
viewing _comment
) ;
return res . render ( 'post' , {
post : finalized _json . post _data ,
comments : finalized _json . comments ,
viewing _comment : viewing _comment ,
post _url : post _url ,
subreddit : subreddit ,
sortby : sortby ,
user _preferences : req . cookies ,
instance _nsfw _enabled : config . nsfw _enabled ,
instance _videos _muted : config . videos _muted ,
post _media _max _heights : config . post _media _max _heights ,
redis _key : comments _key ,
} ) ;
} ) ( ) ;
}
}
) ;
} ) ;
} else {
if ( result . status === 404 ) {
console . log ( '404 – Post not found' ) ;
} else {
console . error (
` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } `
) ;
console . error ( config . reddit _api _error _text ) ;
}
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 20:56:37 +02:00
json : null ,
http _status _code : result . status ,
http _statustext : result . statusText ,
user _preferences : req . cookies ,
} ) ;
}
} )
. catch ( ( error ) => {
console . error (
` Error fetching the JSON file from reddit.com ${ comments _url } . ` ,
error
) ;
} ) ;
}
} ) ;
}
) ;
subredditRoutes . post (
'/r/:subreddit/comments/:id/:snippet' ,
( req , res , next ) => {
/ * *
* This is the "morechildren" route . This route is called when the
* "load more comments" button at the bottom of some post is clicked .
* /
if ( ! config . use _reddit _oauth )
return res . send (
` This instance is using Reddit's public API (non-OAuth), and therefore this endpoint is not supported. In other words, this feature is only available if the instance is using Reddit OAuth API. `
) ;
let subreddit = req . params . subreddit ;
let id = req . params . id ;
let snippet = encodeURIComponent ( req . params . snippet ) ;
let post _url = ` /r/ ${ subreddit } /comments/ ${ id } / ${ snippet } / ` ;
let page = req . query . page ;
let comment _ids = req . body . comment _ids ;
return res . redirect ( ` ${ post _url } ?comment_ids= ${ comment _ids } &page=1 ` ) ;
}
) ;
2021-09-02 21:28:42 +02:00
subredditRoutes . get ( '/subreddits/:sort?' , ( req , res , next ) => {
let q = req . query . q ;
let nsfw = req . query . nsfw ;
let after = req . query . after ;
let before = req . query . before ;
let sortby = req . params . sort ;
let searching = false ;
if ( ! after ) {
after = '' ;
}
if ( ! before ) {
before = '' ;
}
let d = ` &after= ${ after } ` ;
if ( before ) {
d = ` &before= ${ before } ` ;
}
if ( nsfw !== 'on' ) {
nsfw = 'off' ;
}
if ( ! sortby ) {
sortby = '' ;
}
let key = ` subreddits:sort: ${ sortby } ${ d } ` ;
if ( sortby === 'search' ) {
if ( typeof q == 'undefined' || q == '' ) return res . redirect ( '/subreddits' ) ;
key = ` subreddits:search:q: ${ q } :nsfw: ${ nsfw } ${ d } ` ;
searching = true ;
}
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting the subreddits key from redis. ` , error ) ;
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 21:28:42 +02:00
json : null ,
user _preferences : req . cookies ,
} ) ;
}
if ( json ) {
console . log ( ` Got subreddits key from redis. ` ) ;
( async ( ) => {
let processed _json = await processJsonSubredditsExplore (
json ,
'redis' ,
null ,
req . cookies
) ;
if ( ! processed _json . error ) {
return res . render ( 'subreddits_explore' , {
json : processed _json ,
sortby : sortby ,
after : after ,
before : before ,
q : q ,
nsfw : nsfw ,
searching : searching ,
subreddits _front : ! before && ! after ? true : false ,
user _preferences : req . cookies ,
instance _nsfw _enabled : config . nsfw _enabled ,
} ) ;
} else {
return res . render ( 'subreddits_explore' , {
json : null ,
error : true ,
data : processed _json ,
user _preferences : req . cookies ,
} ) ;
}
} ) ( ) ;
} else {
let url = '' ;
if ( config . use _reddit _oauth ) {
if ( ! searching )
url = ` https://oauth.reddit.com/subreddits/ ${ sortby } ?api_type=json&count=25&g=GLOBAL&t= ${ d } ` ;
else
url = ` https://oauth.reddit.com/subreddits/search?api_type=json&q= ${ q } &include_over_18= ${ nsfw } ${ d } ` ;
} else {
if ( ! searching )
url = ` https://reddit.com/subreddits/ ${ sortby } .json?api_type=json&count=25&g=GLOBAL&t= ${ d } ` ;
else
url = ` https://reddit.com/subreddits/search.json?api_type=json&q= ${ q } &include_over_18= ${ nsfw } ${ d } ` ;
}
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
. then ( ( result ) => {
if ( result . status === 200 ) {
result . json ( ) . then ( ( json ) => {
let ex = config . setexs . subreddits _explore . front ;
if ( sortby === 'new' )
ex = config . setexs . subreddits _explore . new _page ;
redis . setex ( key , ex , JSON . stringify ( json ) , ( error ) => {
if ( error ) {
console . error (
` Error setting the subreddits key to redis. ` ,
error
) ;
return res . render ( 'subreddits_explore' , {
json : null ,
user _preferences : req . cookies ,
} ) ;
} else {
console . log ( ` Fetched the JSON from reddit.com/subreddits. ` ) ;
( async ( ) => {
let processed _json = await processJsonSubredditsExplore (
json ,
'from_online' ,
null ,
req . cookies
) ;
return res . render ( 'subreddits_explore' , {
json : processed _json ,
sortby : sortby ,
after : after ,
before : before ,
q : q ,
nsfw : nsfw ,
searching : searching ,
subreddits _front : ! before && ! after ? true : false ,
user _preferences : req . cookies ,
instance _nsfw _enabled : config . nsfw _enabled ,
} ) ;
} ) ( ) ;
}
} ) ;
} ) ;
} else {
if ( result . status === 404 ) {
console . log ( '404 – Subreddits not found' ) ;
} else {
console . error (
` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } `
) ;
console . error ( config . reddit _api _error _text ) ;
}
2022-06-10 20:05:36 +02:00
return res . render ( 'frontpage' , {
2021-09-02 21:28:42 +02:00
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies ,
} ) ;
}
} )
. catch ( ( error ) => {
console . error (
` Error fetching the JSON file from reddit.com/subreddits. ` ,
error
) ;
} ) ;
}
} ) ;
} ) ;
2021-09-02 20:56:37 +02:00
module . exports = subredditRoutes ;