2020-11-17 21:44:32 +01:00
/ * *
* Lots of routes . . would be good idea to do some separation I guess .
* /
module . exports = ( app , redis , fetch , RedditAPI ) => {
2020-12-01 20:16:51 +01:00
const config = require ( './config' ) ;
2020-11-17 21:44:32 +01:00
let processSubreddit = require ( './inc/processJsonSubreddit.js' ) ( ) ;
let processPost = require ( './inc/processJsonPost.js' ) ( ) ;
let processUser = require ( './inc/processJsonUser.js' ) ( ) ;
let processSearches = require ( './inc/processSearchResults.js' ) ( ) ;
2020-12-28 00:16:45 +01:00
let processAbout = require ( './inc/processSubredditAbout.js' ) ( ) ;
2020-12-19 20:52:22 +01:00
let tedditApiSubreddit = require ( './inc/teddit_api/handleSubreddit.js' ) ( ) ;
2021-01-06 16:06:11 +01:00
let tedditApiUser = require ( './inc/teddit_api/handleUser.js' ) ( ) ;
2021-01-17 17:29:49 +01:00
let processSubredditsExplore = require ( './inc/processSubredditsExplore.js' ) ( ) ;
2021-04-10 15:42:01 +02:00
let processMoreComments = require ( './inc/processMoreComments.js' ) ( ) ;
2021-03-27 21:09:18 +01:00
app . all ( '*' , ( req , res , next ) => {
2021-04-02 15:04:57 +02:00
let themeOverride = req . query . theme
if ( themeOverride ) {
// Convert Dark to dark since the stylesheet has it lower case
themeOverride = themeOverride . toLowerCase ( )
// This override here will set it for the current request
req . cookies . theme = themeOverride
// this will set it for future requests
res . cookie ( 'theme' , themeOverride , { maxAge : 31536000 , httpOnly : true } )
} else if ( ! req . cookies . theme && req . cookies . theme !== '' ) {
req . cookies . theme = config . theme
}
let flairsOverride = req . query . flairs
if ( flairsOverride ) {
req . cookies . flairs = flairsOverride
res . cookie ( 'flairs' , flairsOverride , { maxAge : 31536000 , httpOnly : true } )
}
let nsfwEnabledOverride = req . query . nsfw _enabled
if ( nsfwEnabledOverride ) {
req . cookies . nsfw _enabled = nsfwEnabledOverride
res . cookie ( 'nsfw_enabled' , nsfwEnabledOverride , { maxAge : 31536000 , httpOnly : true } )
}
let highlightControversialOverride = req . query . highlight _controversial
if ( highlightControversialOverride ) {
req . cookies . highlight _controversial = highlightControversialOverride
res . cookie ( 'highlight_controversial' , highlightControversialOverride , { maxAge : 31536000 , httpOnly : true } )
}
let postMediaMaxHeight = req . query . post _media _max _height
if ( postMediaMaxHeight ) {
if ( config . post _media _max _heights . hasOwnProperty ( postMediaMaxHeight ) || ! isNaN ( postMediaMaxHeight ) ) {
req . cookies . post _media _max _height = postMediaMaxHeight
res . cookie ( 'post_media_max_height' , postMediaMaxHeight , { maxAge : 31536000 , httpOnly : true } )
}
}
let collapseChildComments = req . query . collapse _child _comments
if ( collapseChildComments ) {
req . cookies . collapse _child _comments = collapseChildComments
res . cookie ( 'collapse_child_comments' , collapseChildComments , { maxAge : 31536000 , httpOnly : true } )
}
let showUpvotedPercentage = req . query . show _upvoted _percentage
if ( showUpvotedPercentage ) {
req . cookies . show _upvoted _percentage = showUpvotedPercentage
res . cookie ( 'show_upvoted_percentage' , showUpvotedPercentage , { maxAge : 31536000 , httpOnly : true } )
}
2021-04-13 16:40:20 +02:00
let domainTwitter = req . query . domain _twitter
if ( domainTwitter ) {
req . cookies . domain _twitter = domainTwitter
res . cookie ( 'domain_twitter' , domainTwitter , { maxAge : 31536000 , httpOnly : true } )
}
let domainYoutube = req . query . domain _youtube
if ( domainYoutube ) {
req . cookies . domain _youtube = domainYoutube
res . cookie ( 'domain_youtube' , domainYoutube , { maxAge : 31536000 , httpOnly : true } )
}
let domainInstagram = req . query . domain _instagram
if ( domainInstagram ) {
req . cookies . domain _instagram = domainInstagram
res . cookie ( 'domain_instagram' , domainInstagram , { maxAge : 31536000 , httpOnly : true } )
}
2021-06-01 23:17:06 +02:00
let videosMuted = req . query . videos _muted
if ( videosMuted ) {
req . cookies . videos _muted = videosMuted
res . cookie ( 'videos_muted' , videosMuted , { maxAge : 31536000 , httpOnly : true } )
}
if ( ! config . rate _limiting ) {
return next ( )
}
2021-04-26 18:34:56 +02:00
const valid _reddit _starts = [ '/https://old.reddit.com' , '/https://reddit.com' , '/https://www.reddit.com' , '/old.reddit.com' , '/reddit.com' , '/www.reddit.com' ]
for ( var i = 0 ; i < valid _reddit _starts . length ; i ++ ) {
if ( req . url . startsWith ( valid _reddit _starts [ i ] ) ) {
req . url = req . url . substring ( 1 )
const redditRegex = /([A-z.]+\.)?(reddit(\.com))/gm ;
let teddified _url = req . url . replace ( redditRegex , '' )
if ( teddified _url . includes ( '://' ) ) {
teddified _url = teddified _url . split ( '://' ) [ 1 ]
}
if ( teddified _url == '' ) {
teddified _url = '/'
}
return res . redirect ( teddified _url )
}
}
2021-03-27 21:09:18 +01:00
if ( config . rate _limiting . enabled ) {
/ * *
* This route enforces request limits based on an IP address if
* config . rate _limiting . enabled is true . By default it ' s false .
* /
let ip = String ( req . headers [ 'x-forwarded-for' ] || req . connection . remoteAddress || 'unknown' )
if ( ip === 'unknown' ) {
2021-03-31 19:48:15 +02:00
return next ( )
2021-03-27 21:09:18 +01:00
}
if ( ratelimit _counts [ ip ] == undefined ) {
ratelimit _counts [ ip ] = 0
}
if ( ratelimit _timestamps [ ip ] == undefined ) {
ratelimit _timestamps [ ip ] = Date . now ( )
}
let diff = Date . now ( ) - ratelimit _timestamps [ ip ]
let credit = ( diff / 60000 ) * config . rate _limiting . limit _after _limited
ratelimit _counts [ ip ] -= credit
if ( ratelimit _counts [ ip ] < 0 ) {
ratelimit _counts [ ip ] = 0
}
ratelimit _counts [ ip ] ++
ratelimit _timestamps [ ip ] = Date . now ( )
if ( ratelimit _counts [ ip ] > config . rate _limiting . initial _limit ) {
console . log ( ` RATE LIMITED IP ADDRESS: ${ ip } ` )
return res . send ( ` Hold your horses! You have hit the request limit. You should be able to refresh this page in a couple of seconds. If you think you are wrongfully limited, create an issue at https://codeberg.org/teddit/teddit. Rate limiting is highly experimental feature. ` )
} else {
2021-03-31 19:48:15 +02:00
return next ( )
2021-03-27 21:09:18 +01:00
}
} else {
2021-03-31 19:48:15 +02:00
return next ( )
2021-03-27 21:09:18 +01:00
}
} )
2020-11-17 21:44:32 +01:00
2020-12-19 12:20:43 +01:00
app . get ( '/about' , ( req , res , next ) => {
return res . render ( 'about' , { user _preferences : req . cookies } )
} )
app . get ( '/preferences' , ( req , res , next ) => {
2020-12-27 23:25:39 +01:00
return res . render ( 'preferences' , { user _preferences : req . cookies , instance _config : config } )
2020-12-19 12:20:43 +01:00
} )
app . get ( '/resetprefs' , ( req , res , next ) => {
2021-06-01 23:17:06 +02:00
resetPreferences ( res )
2020-12-19 12:20:43 +01:00
return res . redirect ( '/preferences' )
} )
2021-01-30 22:16:56 +01:00
app . get ( '/import_prefs/:key' , ( req , res , next ) => {
let key = req . params . key
if ( ! key )
return res . redirect ( '/' )
if ( key . length !== 10 )
return res . redirect ( '/' )
key = ` prefs_key: ${ key } `
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting the preferences import key ${ key } from redis. ` , error )
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
}
if ( json ) {
try {
let prefs = JSON . parse ( json )
let subbed _subreddits _is _set = false
for ( var setting in prefs ) {
if ( prefs . hasOwnProperty ( setting ) ) {
res . cookie ( setting , prefs [ setting ] , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
if ( setting === 'subbed_subreddits' )
subbed _subreddits _is _set = true
}
}
if ( ! subbed _subreddits _is _set )
res . clearCookie ( 'subbed_subreddits' )
2021-02-13 13:29:47 +01:00
return res . redirect ( '/' )
2021-01-30 22:16:56 +01:00
} catch ( e ) {
console . error ( ` Error setting imported preferences to the cookies. Key: ${ key } . ` , error )
}
} else {
return res . redirect ( '/preferences' )
}
} )
} )
2020-12-19 12:20:43 +01:00
app . get ( '/privacy' , ( req , res , next ) => {
return res . render ( 'privacypolicy' , { user _preferences : req . cookies } )
} )
2021-01-31 16:43:07 +01:00
app . get ( '/gallery/:id' , ( req , res , next ) => {
return res . redirect ( ` /comments/ ${ req . params . id } ` )
2021-03-21 23:06:05 +01:00
} )
app . get ( '/poll/:id' , ( req , res , next ) => {
return res . redirect ( ` /comments/ ${ req . params . id } ` )
2021-01-31 16:43:07 +01:00
} )
2021-02-06 20:17:36 +01:00
app . get ( '/saved' , ( req , res , next ) => {
let saved = req . cookies . saved
2021-02-06 20:27:21 +01:00
if ( ! saved || ! Array . isArray ( saved ) ) {
return res . render ( 'saved' , {
json : null ,
user _preferences : req . cookies ,
} )
}
2021-02-06 20:17:36 +01:00
let key = ` saved_posts: ${ saved . join ( ',' ) } `
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting saved_post ${ saved _post } key from redis. ` , error )
return res . redirect ( '/' )
}
if ( json ) {
( async ( ) => {
let processed _json = await processJsonSubreddit ( json , 'redis' , null , req . cookies , true )
if ( ! processed _json . error ) {
return res . render ( 'saved' , {
json : processed _json ,
user _preferences : req . cookies ,
} )
} else {
return res . render ( 'subreddit' , {
json : null ,
error : true ,
data : processed _json ,
user _preferences : req . cookies
} )
}
} ) ( )
}
} )
} )
app . get ( '/save/:id' , ( req , res , next ) => {
let post _id = req . params . id
let redis _key = req . query . rk
let back = req . query . b
let saved = req . cookies . saved
let fetched = req . query . f
if ( ! post _id || ! redis _key )
return res . redirect ( '/saved' )
if ( ! saved || ! Array . isArray ( saved ) )
saved = [ ]
if ( saved . length > 100 )
return res . send ( 'You can not save more than 100 posts.' )
redis . get ( redis _key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting the ${ redis _key } key from redis (via /save/). ` , error )
return res . redirect ( '/' )
}
if ( json ) {
json = JSON . parse ( json )
2021-03-16 18:07:27 +01:00
if ( fetched === 'true' || redis _key . includes ( '/comments/' ) )
2021-02-06 20:17:36 +01:00
json = json [ 0 ]
let post _to _save = false
for ( var i = 0 ; i < json . data . children . length ; i ++ ) {
let post = json . data . children [ i ]
if ( post . data . id === post _id ) {
post _to _save = post
break
}
}
if ( post _to _save ) {
if ( ! saved || ! Array . isArray ( saved ) )
saved = [ ]
for ( var i = 0 ; i < saved . length ; i ++ ) {
if ( post _to _save . data . id === saved [ i ] )
return res . redirect ( '/saved' )
}
let key = ` saved_posts: ${ saved . join ( ',' ) } `
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting saved_posts ${ key } key from redis. ` , error )
return res . redirect ( '/' )
}
links = JSON . parse ( json )
if ( ! links )
links = [ ]
links . unshift ( post _to _save )
saved . unshift ( post _to _save . data . id )
res . cookie ( 'saved' , saved , { maxAge : 3 * 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
let new _key = ` saved_posts: ${ saved . join ( ',' ) } `
redis . set ( new _key , JSON . stringify ( links ) , ( error ) => {
if ( error )
console . error ( ` Error saving ${ new _key } to redis. ` , error )
if ( ! back )
return res . redirect ( '/saved' )
else {
back = back . replace ( /§2/g , '?' ) . replace ( /§1/g , '&' )
return res . redirect ( back )
}
} )
} )
} else {
return res . redirect ( ` /comments/ ${ post _id } /?save=true&b= ${ back } ` )
}
} else {
return res . redirect ( ` /comments/ ${ post _id } /?save=true&b= ${ back } ` )
}
} )
} )
app . get ( '/unsave/:id' , ( req , res , next ) => {
let post _id = req . params . id
let back = req . query . b
let saved = req . cookies . saved
if ( ! post _id )
return res . redirect ( '/saved' )
if ( ! saved || ! Array . isArray ( saved ) )
return res . redirect ( '/saved' )
let key = ` saved_posts: ${ saved . join ( ',' ) } `
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting the ${ key } key from redis (via /save/). ` , error )
return res . redirect ( '/' )
}
if ( json ) {
json = JSON . parse ( json )
let post _found = false
for ( var i = 0 ; i < json . length ; i ++ ) {
if ( json [ i ] . data . id === post _id ) {
post _found = true
json . splice ( i , 1 )
for ( var j = 0 ; j < saved . length ; j ++ ) {
if ( saved [ j ] === post _id )
saved . splice ( j , 1 )
}
}
}
if ( post _found ) {
res . cookie ( 'saved' , saved , { maxAge : 3 * 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
let new _key = ` saved_posts: ${ saved . join ( ',' ) } `
redis . set ( new _key , JSON . stringify ( json ) , ( error ) => {
if ( error )
console . error ( ` Error saving ${ new _key } to redis. ` , error )
if ( ! back )
return res . redirect ( '/saved' )
else {
back = back . replace ( /§2/g , '?' ) . replace ( /§1/g , '&' )
return res . redirect ( back )
}
} )
2021-03-16 18:07:27 +01:00
} else {
2021-02-06 20:17:36 +01:00
return res . redirect ( ` /saved ` )
}
} else {
return res . redirect ( ` /saved ` )
}
} )
} )
2021-01-17 17:29:49 +01:00
app . 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 } `
}
2021-03-16 18:07:27 +01:00
if ( nsfw !== 'on' ) {
2021-01-17 17:29:49 +01:00
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 )
return res . render ( 'index' , { 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 ) {
2021-03-16 18:07:27 +01:00
console . log ( '404 – Subreddits not found' )
2021-01-17 17:29:49 +01:00
} else {
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
console . error ( config . reddit _api _error _text )
}
return res . render ( 'index' , {
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-01-08 21:39:46 +01:00
app . get ( '/subscribe/:subreddit' , ( req , res , next ) => {
let subreddit = req . params . subreddit
let subbed = req . cookies . subbed _subreddits
let back = req . query . b
if ( ! subreddit )
return res . redirect ( '/' )
if ( ! subbed || ! Array . isArray ( subbed ) )
subbed = [ ]
if ( ! subbed . includes ( subreddit ) )
subbed . push ( subreddit )
res . cookie ( 'subbed_subreddits' , subbed , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
2021-01-17 17:29:49 +01:00
2021-01-08 21:39:46 +01:00
if ( ! back )
return res . redirect ( '/r/' + subreddit )
else {
2021-01-17 17:29:49 +01:00
back = back . replace ( /,/g , '+' ) . replace ( /§1/g , '&' )
2021-01-08 21:39:46 +01:00
return res . redirect ( back )
}
} )
app . get ( '/import_subscriptions/:subreddits' , ( req , res , next ) => {
let subreddits = req . params . subreddits
let subbed = req . cookies . subbed _subreddits
let back = req . query . b
if ( ! subreddits )
return res . redirect ( '/' )
if ( ! subbed || ! Array . isArray ( subbed ) )
subbed = [ ]
subreddits = subreddits . split ( '+' )
for ( var i = 0 ; i < subreddits . length ; i ++ ) {
if ( ! subbed . includes ( subreddits [ i ] ) )
subbed . push ( subreddits [ i ] )
}
res . cookie ( 'subbed_subreddits' , subbed , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
if ( ! back )
return res . redirect ( '/r/' + subreddits )
else {
back = back . replace ( /,/g , '+' ) . replace ( / /g , '+' )
return res . redirect ( back )
}
} )
app . get ( '/unsubscribe/:subreddit' , ( req , res , next ) => {
let subreddit = req . params . subreddit
let subbed = req . cookies . subbed _subreddits
let back = req . query . b
if ( ! subreddit || ! subbed || ! Array . isArray ( subbed ) ) {
res . clearCookie ( 'subbed_subreddits' )
return res . redirect ( '/' )
}
var index = subbed . indexOf ( subreddit )
if ( index !== - 1 )
subbed . splice ( index , 1 )
if ( subbed . length <= 0 )
res . clearCookie ( 'subbed_subreddits' )
else
res . cookie ( 'subbed_subreddits' , subbed , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
if ( ! back )
return res . redirect ( '/r/' + subreddit )
else {
2021-01-17 17:29:49 +01:00
back = back . replace ( /,/g , '+' ) . replace ( /§1/g , '&' )
2021-01-08 21:39:46 +01:00
return res . redirect ( back )
}
} )
2020-12-19 12:20:43 +01:00
app . get ( '/search' , ( req , res , next ) => {
let q = req . query . q
2021-01-07 13:18:39 +01:00
if ( typeof q === "undefined" ) {
return res . render ( 'search' , {
json : { posts : [ ] } ,
no _query : true ,
q : '' ,
restrict _sr : undefined ,
nsfw : undefined ,
subreddit : 'all' ,
sortby : undefined ,
past : undefined ,
user _preferences : req . cookies
} )
}
2020-12-19 12:20:43 +01:00
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 = ''
}
2021-03-16 18:07:27 +01:00
if ( restrict _sr !== 'on' ) {
2020-12-19 12:20:43 +01:00
restrict _sr = 'off'
}
2021-03-16 18:07:27 +01:00
if ( nsfw !== 'on' ) {
2020-12-19 12:20:43 +01:00
nsfw = 'off'
}
let d = ` &after= ${ after } `
if ( before ) {
d = ` &before= ${ before } `
}
return res . redirect ( ` /r/all/search?q= ${ q } &restrict_sr= ${ restrict _sr } &nsfw= ${ nsfw } &sort= ${ sortby } &t= ${ past } ${ d } ` )
} )
2021-04-24 21:24:19 +02:00
app . get ( '/:sort?' , async ( req , res , next ) => {
2020-11-17 21:44:32 +01:00
let past = req . query . t
let before = req . query . before
let after = req . query . after
2021-04-24 21:24:19 +02:00
let sortby = req . params . sort || ''
2020-12-19 20:52:22 +01:00
let api _req = req . query . api
let api _type = req . query . type
let api _target = req . query . target
2021-04-24 21:24:19 +02:00
let proxyable = ( sortby . includes ( '.jpg' ) || sortby . includes ( '.png' ) || sortby . includes ( '.jpeg' ) ) ? true : false
if ( proxyable ) {
let params = new URLSearchParams ( req . query ) . toString ( )
let image _url = ` https://preview.redd.it/ ${ sortby } ? ${ params } `
let proxied _image = await downloadAndSave ( image _url )
if ( proxied _image ) {
return res . redirect ( proxied _image )
} else {
return res . redirect ( '/' )
}
}
2020-11-17 21:44:32 +01:00
let d = ` &after= ${ after } `
if ( before ) {
d = ` &before= ${ before } `
}
2021-04-24 21:24:19 +02:00
if ( sortby == '' ) {
2020-12-19 12:14:10 +01:00
sortby = 'hot'
}
2021-03-31 20:34:59 +02:00
if ( [ 'apple-touch-icon.png' , 'apple-touch-icon-precomposed.png' , 'apple-touch-icon-120x120.png' , 'apple-touch-icon-120x120-precomposed.png' ] . includes ( sortby ) ) {
return res . sendStatus ( 404 ) // return 404 on shitty apple favicon stuff
}
2020-12-19 12:14:10 +01:00
if ( ! [ 'new' , 'rising' , 'controversial' , 'top' , 'gilded' , 'hot' ] . includes ( sortby ) ) {
2021-03-31 20:34:59 +02:00
console . log ( ` Got invalid sort. ` , req . originalUrl )
2020-12-19 12:14:10 +01:00
return res . redirect ( '/' )
}
2020-11-17 21:44:32 +01:00
if ( past ) {
2020-12-19 12:14:10 +01:00
if ( sortby === 'controversial' || sortby === 'top' ) {
if ( ! [ 'hour' , 'day' , 'week' , 'month' , 'year' , 'all' ] . includes ( past ) ) {
console . error ( ` Got invalid past. ` , req . originalUrl )
return res . redirect ( ` / ` )
}
} else {
past = undefined
2020-11-17 21:44:32 +01:00
}
} else {
2020-12-19 12:14:10 +01:00
if ( sortby === 'controversial' || sortby === 'top' ) {
past = 'day'
}
2020-11-17 21:44:32 +01:00
}
2020-12-19 20:52:22 +01:00
if ( req . query . hasOwnProperty ( 'api' ) )
api _req = true
else
api _req = false
2021-01-08 21:39:46 +01:00
2020-12-19 12:14:10 +01:00
let key = ` /after: ${ after } :before: ${ before } :sort: ${ sortby } :past: ${ past } `
2021-01-08 21:39:46 +01:00
let subbed _subreddits = req . cookies . subbed _subreddits
let get _subbed _subreddits = false
if ( subbed _subreddits && Array . isArray ( subbed _subreddits ) ) {
get _subbed _subreddits = true
subbed _subreddits = subbed _subreddits . join ( '+' )
key = ` ${ subbed _subreddits . toLowerCase ( ) } : ${ after } : ${ before } :sort: ${ sortby } :past: ${ past } `
}
2020-11-17 21:44:32 +01:00
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( 'Error getting the frontpage key from redis.' , error )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
2020-11-17 21:44:32 +01:00
}
if ( json ) {
console . log ( 'Got frontpage key from redis.' ) ;
( async ( ) => {
2021-03-16 18:07:27 +01:00
if ( api _req ) {
2020-12-19 20:52:22 +01:00
return handleTedditApiSubreddit ( json , req , res , 'redis' , api _type , api _target , '/' )
} else {
2020-12-24 22:13:08 +01:00
let processed _json = await processJsonSubreddit ( json , 'redis' , null , req . cookies )
2020-12-19 20:52:22 +01:00
return res . render ( 'index' , {
json : processed _json ,
sortby : sortby ,
past : past ,
2021-02-06 20:17:36 +01:00
user _preferences : req . cookies ,
redis _key : key
2020-12-19 20:52:22 +01:00
} )
}
2020-11-17 21:44:32 +01:00
} ) ( )
} else {
2020-12-30 18:15:04 +01:00
let url = ''
2021-01-08 21:39:46 +01:00
if ( config . use _reddit _oauth ) {
if ( get _subbed _subreddits )
url = ` https://oauth.reddit.com/r/ ${ subbed _subreddits } / ${ sortby } ?api_type=json&count=25&g=GLOBAL&t= ${ past } ${ d } `
else
url = ` https://oauth.reddit.com/ ${ sortby } ?api_type=json&g=GLOBAL&t= ${ past } ${ d } `
} else {
if ( get _subbed _subreddits )
url = ` https://reddit.com/r/ ${ subbed _subreddits } / ${ sortby } .json?api_type=json&count=25&g=GLOBAL&t= ${ past } ${ d } `
else
url = ` https://reddit.com/ ${ sortby } .json?g=GLOBAL&t= ${ past } ${ d } `
}
2020-12-30 18:15:04 +01:00
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
2020-11-17 21:44:32 +01:00
. then ( result => {
if ( result . status === 200 ) {
result . json ( )
. then ( json => {
2020-12-01 20:16:51 +01:00
redis . setex ( key , config . setexs . frontpage , JSON . stringify ( json ) , ( error ) => {
2020-11-17 21:44:32 +01:00
if ( error ) {
console . error ( 'Error setting the frontpage key to redis.' , error )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
2020-11-17 21:44:32 +01:00
} else {
2020-12-30 18:22:55 +01:00
console . log ( 'Fetched the frontpage from Reddit.' ) ;
2020-11-17 21:44:32 +01:00
( async ( ) => {
2021-03-16 18:07:27 +01:00
if ( api _req ) {
2020-12-19 20:52:22 +01:00
return handleTedditApiSubreddit ( json , req , res , 'from_online' , api _type , api _target , '/' )
} else {
2020-12-24 22:13:08 +01:00
let processed _json = await processJsonSubreddit ( json , 'from_online' , null , req . cookies )
2020-12-19 20:52:22 +01:00
return res . render ( 'index' , {
json : processed _json ,
sortby : sortby ,
past : past ,
2021-02-06 20:17:36 +01:00
user _preferences : req . cookies ,
redis _key : key
2020-12-19 20:52:22 +01:00
} )
}
2020-11-17 21:44:32 +01:00
} ) ( )
}
} )
} )
} else {
2020-12-30 18:22:55 +01:00
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
2020-12-01 20:16:51 +01:00
console . error ( config . reddit _api _error _text )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , {
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies
} )
2020-11-17 21:44:32 +01:00
}
} ) . catch ( error => {
console . error ( 'Error fetching the frontpage JSON file.' , error )
} )
}
} )
} )
2020-12-09 19:06:25 +01:00
app . get ( '/comments/:post_id/:comment?/:comment_id?' , ( req , res , next ) => {
let post _id = req . params . post _id
let comment = req . params . comment
let comment _id = req . params . comment _id
2021-02-06 20:17:36 +01:00
let back = req . query . b
let save = req . query . save
2020-12-09 19:06:25 +01:00
let post _url = false
let comment _url = false
if ( comment )
if ( comment !== 'comment' || ! comment _id )
return res . redirect ( '/' )
if ( comment )
comment _url = true
else
post _url = true
let key = ` /shorturl:post: ${ post _id } :comment: ${ comment _id } `
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( 'Error getting the short URL for post key from redis.' , error )
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
}
if ( json ) {
console . log ( 'Got short URL for post key from redis.' )
json = JSON . parse ( json )
2021-02-06 20:17:36 +01:00
if ( post _url ) {
if ( save === 'true' )
return res . redirect ( ` /save/ ${ post _id } /?rk= ${ key } &b= ${ back } &f=true ` )
2020-12-09 19:06:25 +01:00
return res . redirect ( json [ 0 ] . data . children [ 0 ] . data . permalink )
2021-02-06 20:17:36 +01:00
} else {
2020-12-09 19:06:25 +01:00
return res . redirect ( json [ 1 ] . data . children [ 0 ] . data . permalink )
2021-02-06 20:17:36 +01:00
}
2020-12-09 19:06:25 +01:00
} else {
let url = ''
2020-12-30 18:15:04 +01:00
if ( config . use _reddit _oauth ) {
if ( post _url )
url = ` https://oauth.reddit.com/comments/ ${ post _id } ?api_type=json `
else
url = ` https://oauth.reddit.com/comments/ ${ post _id } /comment/ ${ comment _id } ?api_type=json `
} else {
if ( post _url )
url = ` https://reddit.com/comments/ ${ post _id } .json?api_type=json `
else
url = ` https://reddit.com/comments/ ${ post _id } /comment/ ${ comment _id } .json?api_type=json `
}
2020-12-09 19:06:25 +01:00
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
. then ( result => {
if ( result . status === 200 ) {
result . json ( )
. then ( json => {
redis . setex ( key , config . setexs . shorts , JSON . stringify ( json ) , ( error ) => {
if ( error ) {
console . error ( 'Error setting the short URL for post key to redis.' , error )
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
} else {
2020-12-30 18:22:55 +01:00
console . log ( 'Fetched the short URL for post from Reddit.' )
2021-02-06 20:17:36 +01:00
if ( post _url ) {
if ( save === 'true' )
return res . redirect ( ` /save/ ${ post _id } /?rk= ${ key } &b= ${ back } &f=true ` )
2020-12-09 19:06:25 +01:00
return res . redirect ( json [ 0 ] . data . children [ 0 ] . data . permalink )
2021-02-06 20:17:36 +01:00
} else {
2020-12-09 19:06:25 +01:00
return res . redirect ( json [ 1 ] . data . children [ 0 ] . data . permalink )
2021-02-06 20:17:36 +01:00
}
2020-12-09 19:06:25 +01:00
}
} )
} )
} else {
2020-12-30 18:22:55 +01:00
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
2020-12-09 19:06:25 +01:00
console . error ( config . reddit _api _error _text )
return res . render ( 'index' , {
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies
} )
}
} ) . catch ( error => {
console . error ( 'Error fetching the short URL for post with sortby JSON file.' , error )
} )
}
} )
} )
2020-11-17 21:44:32 +01:00
app . get ( '/r/:subreddit/search' , ( req , res , next ) => {
let subreddit = req . params . subreddit
let q = req . query . q
2021-03-19 19:20:12 +01:00
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
} )
}
2020-11-17 21:44:32 +01:00
let restrict _sr = req . query . restrict _sr
let nsfw = req . query . nsfw
2020-11-19 20:53:29 +01:00
let sortby = req . query . sort
let past = req . query . t
2020-11-17 21:44:32 +01:00
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 } `
}
2021-03-16 18:07:27 +01:00
if ( restrict _sr !== 'on' ) {
2020-11-17 21:44:32 +01:00
restrict _sr = 'off'
}
2021-03-16 18:07:27 +01:00
if ( nsfw !== 'on' ) {
2020-11-17 21:44:32 +01:00
nsfw = 'off'
}
2021-03-23 19:40:26 +01:00
let key = ` search: ${ subreddit } : ${ q } : ${ restrict _sr } : ${ sortby } : ${ past } : ${ after } : ${ before } : ${ nsfw } `
2020-11-17 21:44:32 +01:00
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( 'Error getting the search key from redis.' , error )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
2020-11-17 21:44:32 +01:00
}
if ( json ) {
console . log ( 'Got search key from redis.' ) ;
( async ( ) => {
2021-01-01 18:18:54 +01:00
let processed _json = await processSearchResults ( json , false , after , before , req . cookies )
2020-11-17 21:44:32 +01:00
return res . render ( 'search' , {
json : processed _json ,
2021-01-07 13:18:39 +01:00
no _query : false ,
2020-11-17 21:44:32 +01:00
q : q ,
restrict _sr : restrict _sr ,
nsfw : nsfw ,
subreddit : subreddit ,
sortby : sortby ,
2020-11-21 13:50:12 +01:00
past : past ,
user _preferences : req . cookies
2020-11-17 21:44:32 +01:00
} )
} ) ( )
} else {
2020-12-30 18:15:04 +01:00
let url = ''
if ( config . use _reddit _oauth )
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 } ${ d } `
else
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 } ${ d } `
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
2020-11-17 21:44:32 +01:00
. then ( result => {
if ( result . status === 200 ) {
result . json ( )
. then ( json => {
2021-04-22 20:44:23 +02:00
( 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 `
2021-04-22 20:57:32 +02:00
const response = await fetch ( encodeURI ( url ) )
2021-04-22 20:44:23 +02:00
const data = await response . json ( )
json . suggested _subreddits = data
2020-11-17 21:44:32 +01:00
}
2021-04-22 20:44:23 +02:00
redis . setex ( key , config . setexs . searches , JSON . stringify ( json ) , ( error ) => {
if ( error ) {
console . error ( 'Error setting the searches key to redis.' , error )
return res . render ( 'index' , { 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
} )
} ) ( )
}
} )
} ) ( )
2020-11-17 21:44:32 +01:00
} )
} else {
2020-12-30 18:22:55 +01:00
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
2020-12-01 20:16:51 +01:00
console . error ( config . reddit _api _error _text )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , {
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies
} )
2020-11-17 21:44:32 +01:00
}
} ) . catch ( error => {
console . error ( 'Error fetching the frontpage JSON file.' , error )
} )
}
} )
} )
2021-04-24 22:42:58 +02:00
app . get ( '/r/:subreddit/wiki/:page?/:sub_page?' , ( req , res , next ) => {
2021-01-05 02:41:57 +01:00
let subreddit = req . params . subreddit
let page = req . params . page
2021-04-24 22:42:58 +02:00
let sub _page = req . params . sub _page || ''
2021-01-05 02:41:57 +01:00
if ( ! page )
page = 'index'
2021-04-11 12:10:19 +02:00
2021-04-24 22:42:58 +02:00
if ( sub _page != '' )
sub _page = ` / ${ sub _page } `
2021-04-11 12:10:19 +02:00
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
}
2021-01-05 02:41:57 +01:00
2021-04-24 22:42:58 +02:00
let key = ` ${ subreddit . toLowerCase ( ) } :wiki:page: ${ page } :sub_page: ${ sub _page } `
2021-01-05 02:41:57 +01:00
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting the ${ subreddit } wiki key from redis. ` , error )
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
}
if ( json ) {
console . log ( ` Got /r/ ${ subreddit } wiki key from redis. ` )
json = JSON . parse ( json )
2021-04-11 12:13:22 +02:00
return res . render ( 'subreddit_wiki' , {
content _html : ( page !== 'pages' ? unescape ( json . data . content _html ) : formatWikipagelisting ( json , subreddit ) ) ,
subreddit : subreddit ,
user _preferences : req . cookies
} )
2021-01-05 02:41:57 +01:00
} else {
let url = ''
if ( config . use _reddit _oauth )
2021-04-24 22:42:58 +02:00
url = ` https://oauth.reddit.com/r/ ${ subreddit } /wiki/ ${ page } ${ sub _page } ?api_type=json `
2021-01-05 02:41:57 +01:00
else
2021-04-24 22:42:58 +02:00
url = ` https://reddit.com/r/ ${ subreddit } /wiki/ ${ page } ${ sub _page } .json?api_type=json `
2021-01-05 02:41:57 +01:00
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. ` )
2021-04-11 12:13:22 +02:00
return res . render ( 'subreddit_wiki' , {
content _html : ( page !== 'pages' ? unescape ( json . data . content _html ) : formatWikipagelisting ( json , subreddit ) ) ,
subreddit : subreddit ,
user _preferences : req . cookies
} )
2021-01-05 02:41:57 +01:00
}
} )
} )
} else {
if ( result . status === 404 ) {
2021-03-16 18:07:27 +01:00
console . log ( '404 – Subreddit wiki not found' )
2021-01-05 02:41:57 +01:00
} else {
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
console . error ( config . reddit _api _error _text )
}
return res . render ( 'index' , {
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 )
} )
}
} )
} )
2021-04-24 22:42:58 +02:00
app . get ( '/r/:subreddit/w/:page?/:sub_page?' , ( req , res , next ) => {
2021-01-05 02:41:57 +01:00
/* "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
2021-04-24 22:42:58 +02:00
let sub _page = req . params . sub _page || ''
2021-01-05 02:41:57 +01:00
if ( ! page )
page = 'index'
2021-04-24 22:42:58 +02:00
if ( sub _page != '' )
sub _page = ` / ${ sub _page } `
return res . redirect ( ` /r/ ${ subreddit } /wiki/ ${ page } ${ sub _page } ` )
2021-01-05 02:41:57 +01:00
} )
2021-01-17 13:41:25 +01:00
app . 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 )
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
}
} )
} else {
if ( result . status === 404 ) {
2021-03-16 18:07:27 +01:00
console . log ( '404 – Subreddit not found' )
2021-01-17 13:41:25 +01:00
} else {
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
console . error ( config . reddit _api _error _text )
}
return res . render ( 'index' , {
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies
} )
}
} ) . catch ( error => {
2021-01-17 17:29:49 +01:00
console . error ( ` Error fetching the JSON file from reddit.com/r/random. ` , error )
2021-01-17 13:41:25 +01:00
} )
} )
2020-11-17 21:44:32 +01:00
app . 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
2020-12-19 20:52:22 +01:00
let api _req = req . query . api
let api _type = req . query . type
let api _target = req . query . target
if ( req . query . hasOwnProperty ( 'api' ) )
api _req = true
else
api _req = false
2020-11-17 21:44:32 +01:00
let d = ` &after= ${ after } `
if ( before ) {
d = ` &before= ${ before } `
}
if ( ! sortby ) {
sortby = 'hot'
}
if ( ! [ 'new' , 'rising' , 'controversial' , 'top' , 'gilded' , 'hot' ] . includes ( sortby ) ) {
2021-03-31 20:34:59 +02:00
console . log ( ` Got invalid sort. ` , req . originalUrl )
2020-11-17 21:44:32 +01:00
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'
}
}
2020-11-22 19:57:17 +01:00
let key = ` ${ subreddit . toLowerCase ( ) } : ${ after } : ${ before } :sort: ${ sortby } :past: ${ past } `
2020-11-17 21:44:32 +01:00
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting the ${ subreddit } key from redis. ` , error )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
2020-11-17 21:44:32 +01:00
}
if ( json ) {
console . log ( ` Got /r/ ${ subreddit } key from redis. ` ) ;
( async ( ) => {
2021-03-16 18:07:27 +01:00
if ( api _req ) {
2020-12-19 20:52:22 +01:00
return handleTedditApiSubreddit ( json , req , res , 'redis' , api _type , api _target , subreddit )
2020-11-17 21:44:32 +01:00
} else {
2020-12-24 22:13:08 +01:00
let processed _json = await processJsonSubreddit ( json , 'redis' , null , req . cookies )
2020-12-28 00:16:45 +01:00
let subreddit _about = await processSubredditAbout ( subreddit , redis , fetch , RedditAPI )
2020-12-19 20:52:22 +01:00
if ( ! processed _json . error ) {
return res . render ( 'subreddit' , {
json : processed _json ,
subreddit : subreddit ,
2020-12-28 00:16:45 +01:00
subreddit _about : subreddit _about ,
2020-12-19 20:52:22 +01:00
subreddit _front : ( ! before && ! after ) ? true : false ,
sortby : sortby ,
past : past ,
2020-12-28 00:16:45 +01:00
user _preferences : req . cookies ,
2021-02-06 20:17:36 +01:00
instance _nsfw _enabled : config . nsfw _enabled ,
redis _key : key ,
after : req . query . after ,
before : req . query . before
2020-12-19 20:52:22 +01:00
} )
} else {
return res . render ( 'subreddit' , {
json : null ,
error : true ,
data : processed _json ,
user _preferences : req . cookies
} )
}
2020-11-17 21:44:32 +01:00
}
} ) ( )
} else {
2020-12-30 18:15:04 +01:00
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 } `
else
url = ` https://reddit.com/r/ ${ subreddit } / ${ sortby } .json?api_type=json&count=25&g=GLOBAL&t= ${ past } ${ d } `
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
2020-11-17 21:44:32 +01:00
. then ( result => {
if ( result . status === 200 ) {
result . json ( )
. then ( json => {
2020-12-01 20:16:51 +01:00
redis . setex ( key , config . setexs . subreddit , JSON . stringify ( json ) , ( error ) => {
2020-11-17 21:44:32 +01:00
if ( error ) {
console . error ( ` Error setting the ${ subreddit } key to redis. ` , error )
2020-11-21 13:50:12 +01:00
return res . render ( 'subreddit' , { json : null , user _preferences : req . cookies } )
2020-11-17 21:44:32 +01:00
} else {
console . log ( ` Fetched the JSON from reddit.com/r/ ${ subreddit } . ` ) ;
( async ( ) => {
2021-03-16 18:07:27 +01:00
if ( api _req ) {
2020-12-19 20:52:22 +01:00
return handleTedditApiSubreddit ( json , req , res , 'from_online' , api _type , api _target , subreddit )
} else {
2020-12-24 22:13:08 +01:00
let processed _json = await processJsonSubreddit ( json , 'from_online' , null , req . cookies )
2020-12-28 00:16:45 +01:00
let subreddit _about = await processSubredditAbout ( subreddit , redis , fetch , RedditAPI )
2020-12-19 20:52:22 +01:00
return res . render ( 'subreddit' , {
json : processed _json ,
subreddit : subreddit ,
2020-12-28 00:16:45 +01:00
subreddit _about : subreddit _about ,
2020-12-19 20:52:22 +01:00
subreddit _front : ( ! before && ! after ) ? true : false ,
sortby : sortby ,
past : past ,
2020-12-28 00:16:45 +01:00
user _preferences : req . cookies ,
2021-02-06 20:17:36 +01:00
instance _nsfw _enabled : config . nsfw _enabled ,
redis _key : key ,
after : req . query . after ,
before : req . query . before
2020-12-19 20:52:22 +01:00
} )
}
2020-11-17 21:44:32 +01:00
} ) ( )
}
} )
} )
} else {
if ( result . status === 404 ) {
2021-03-16 18:07:27 +01:00
console . log ( '404 – Subreddit not found' )
2020-11-17 21:44:32 +01:00
} else {
2020-12-30 18:22:55 +01:00
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
2020-12-01 20:16:51 +01:00
console . error ( config . reddit _api _error _text )
2020-11-17 21:44:32 +01:00
}
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , {
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies
} )
2020-11-17 21:44:32 +01:00
}
} ) . catch ( error => {
console . error ( ` Error fetching the JSON file from reddit.com/r/ ${ subreddit } . ` , error )
} )
}
} )
} )
2021-01-19 11:21:13 +01:00
app . get ( '/r/:subreddit/comments/:id/:snippet?/:comment_id?' , ( req , res , next ) => {
2020-11-17 21:44:32 +01:00
let subreddit = req . params . subreddit
let id = req . params . id
let snippet = encodeURIComponent ( req . params . snippet )
2020-12-23 12:41:10 +01:00
let sortby = req . query . sort
2020-11-17 21:44:32 +01:00
let comment _id = ''
let viewing _comment = false
2021-04-10 15:42:01 +02:00
let comment _ids = req . query . comment _ids
2020-11-17 21:44:32 +01:00
let context = parseInt ( req . query . context )
if ( req . params . comment _id ) {
comment _id = ` ${ req . params . comment _id } / `
viewing _comment = true
}
2020-12-23 12:41:10 +01:00
if ( ! sortby ) {
sortby = config . post _comments _sort
}
2020-12-27 18:56:27 +01:00
if ( ! [ 'confidence' , 'top' , 'new' , 'controversial' , 'old' , 'qa' , 'random' ] . includes ( sortby ) ) {
2021-03-31 20:34:59 +02:00
console . log ( ` Got invalid sort. ` , req . originalUrl )
2020-12-23 12:41:10 +01:00
return res . redirect ( '/' )
}
2020-11-17 21:44:32 +01:00
let comments _url = ` /r/ ${ subreddit } /comments/ ${ id } / ${ snippet } / ${ comment _id } `
let post _url = ` /r/ ${ subreddit } /comments/ ${ id } / ${ snippet } / `
2020-12-23 12:41:10 +01:00
let comments _key = ` ${ comments _url } :sort: ${ sortby } `
redis . get ( comments _key , ( error , json ) => {
2020-11-17 21:44:32 +01:00
if ( error ) {
console . error ( ` Error getting the ${ comments _url } key from redis. ` , error )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , { post : null , user _preferences : req . cookies } )
2020-11-17 21:44:32 +01:00
}
if ( json ) {
console . log ( ` Got ${ comments _url } key from redis. ` ) ;
( async ( ) => {
2021-04-10 15:42:01 +02:00
let parsed = false
let more _comments = null
if ( comment _ids ) {
let key = ` ${ post _url } :morechildren:comment_ids: ${ comment _ids } `
more _comments = await moreComments ( 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
}
2020-11-17 21:44:32 +01:00
}
2021-04-10 15:42:01 +02:00
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 ,
2021-06-01 23:17:06 +02:00
instance _videos _muted : config . videos _muted ,
2021-04-10 15:42:01 +02:00
post _media _max _heights : config . post _media _max _heights ,
redis _key : comments _key
} )
2020-11-17 21:44:32 +01:00
} ) ( )
} else {
2020-12-30 18:15:04 +01:00
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 ( ) )
2020-11-17 21:44:32 +01:00
. then ( result => {
if ( result . status === 200 ) {
result . json ( )
. then ( json => {
2020-12-23 12:41:10 +01:00
redis . setex ( comments _key , config . setexs . posts , JSON . stringify ( json ) , ( error ) => {
2020-11-17 21:44:32 +01:00
if ( error ) {
console . error ( ` Error setting the ${ comments _url } key to redis. ` , error )
2020-11-21 13:50:12 +01:00
return res . render ( 'post' , { post : null , user _preferences : req . cookies } )
2020-11-17 21:44:32 +01:00
} else {
console . log ( ` Fetched the JSON from reddit.com ${ comments _url } . ` ) ;
( async ( ) => {
2021-04-10 15:42:01 +02:00
let more _comments = null
if ( comment _ids ) {
let key = ` ${ post _url } :morechildren:comment_ids: ${ comment _ids } `
more _comments = await moreComments ( fetch , redis , post _url , comment _ids , id )
if ( more _comments === false ) {
return res . redirect ( post _url )
} else {
json [ 1 ] . data . children = more _comments
}
}
2020-12-24 22:13:08 +01:00
let processed _json = await processJsonPost ( json , true , req . cookies )
2021-04-10 15:42:01 +02:00
let finalized _json = await finalizeJsonPost ( processed _json , id , post _url , more _comments , viewing _comment )
2020-11-17 21:44:32 +01:00
return res . render ( 'post' , {
post : finalized _json . post _data ,
comments : finalized _json . comments ,
viewing _comment : viewing _comment ,
post _url : post _url ,
2020-11-21 13:50:12 +01:00
subreddit : subreddit ,
2020-12-23 12:41:10 +01:00
sortby : sortby ,
2020-12-27 23:25:39 +01:00
user _preferences : req . cookies ,
2021-02-06 20:17:36 +01:00
instance _nsfw _enabled : config . nsfw _enabled ,
2021-06-01 23:17:06 +02:00
instance _videos _muted : config . videos _muted ,
2021-03-01 19:01:02 +01:00
post _media _max _heights : config . post _media _max _heights ,
2021-02-06 20:17:36 +01:00
redis _key : comments _key
2020-11-17 21:44:32 +01:00
} )
} ) ( )
}
} )
} )
} else {
if ( result . status === 404 ) {
2021-03-16 18:07:27 +01:00
console . log ( '404 – Post not found' )
2020-11-17 21:44:32 +01:00
} else {
2020-12-30 18:22:55 +01:00
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
2020-12-01 20:16:51 +01:00
console . error ( config . reddit _api _error _text )
2020-11-17 21:44:32 +01:00
}
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , {
json : null ,
http _status _code : result . status ,
http _statustext : result . statusText ,
user _preferences : req . cookies
} )
2020-11-17 21:44:32 +01:00
}
} ) . catch ( error => {
console . error ( ` Error fetching the JSON file from reddit.com ${ comments _url } . ` , error )
} )
}
} )
} )
2021-01-29 20:09:26 +01:00
app . get ( '/user/:user/:kind?' , ( req , res , next ) => {
let kind = ''
if ( req . params . kind )
kind = ` / ${ req . params . kind } `
let q = ''
if ( req . query . sort )
q += ` ?sort= ${ req . query . sort } & `
if ( req . query . t )
q += ` t= ${ req . query . t } `
res . redirect ( ` /u/ ${ req . params . user } ${ kind } ${ q } ` )
2020-11-17 21:44:32 +01:00
} )
2021-01-29 20:09:26 +01:00
app . get ( '/u/:user/:kind?' , ( req , res , next ) => {
2020-11-17 21:44:32 +01:00
let user = req . params . user
let after = req . query . after
let before = req . query . before
2021-01-29 19:41:32 +01:00
let post _type = req . params . kind
let kind = post _type
2020-11-17 21:44:32 +01:00
let user _data = { }
2021-01-06 16:06:11 +01:00
let api _req = req . query . api
let api _type = req . query . type
let api _target = req . query . target
if ( req . query . hasOwnProperty ( 'api' ) )
api _req = true
else
api _req = false
2020-11-17 21:44:32 +01:00
if ( ! after ) {
after = ''
}
if ( ! before ) {
before = ''
}
let d = ` &after= ${ after } `
if ( before ) {
d = ` &before= ${ before } `
}
2021-01-27 22:41:56 +01:00
2021-01-29 19:41:32 +01:00
post _type = ` / ${ post _type } `
switch ( post _type ) {
case '/comments' :
kind = 't1'
break ;
case '/submitted' :
kind = 't3'
break ;
default :
post _type = ''
kind = ''
2021-01-27 22:41:56 +01:00
}
2020-11-17 21:44:32 +01:00
let sortby = req . query . sort
let past = req . query . t
if ( ! sortby ) {
sortby = 'new'
}
if ( ! [ 'hot' , 'new' , 'controversial' , 'top' ] . includes ( sortby ) ) {
2021-03-31 20:34:59 +02:00
console . log ( ` Got invalid sort. ` , req . originalUrl )
2020-11-17 21:44:32 +01:00
return res . redirect ( ` /u/ ${ user } ` )
}
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 ( ` /u/ ${ user } / ${ sortby } ` )
}
} else {
past = ''
}
} else {
if ( sortby === 'controversial' || sortby === 'top' ) {
past = 'all'
} else {
past = ''
}
}
2021-01-29 19:41:32 +01:00
let key = ` ${ user } : ${ after } : ${ before } :sort: ${ sortby } :past: ${ past } :post_type: ${ post _type } `
2020-11-17 21:44:32 +01:00
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting the user ${ key } key from redis. ` , error )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
2020-11-17 21:44:32 +01:00
}
if ( json ) {
console . log ( ` Got user ${ user } key from redis. ` ) ;
( async ( ) => {
2021-01-29 19:41:32 +01:00
if ( api _req ) {
2021-01-06 16:06:11 +01:00
return handleTedditApiUser ( json , req , res , 'redis' , api _type , api _target , user , after , before )
} else {
2021-01-27 22:41:56 +01:00
let processed _json = await processJsonUser ( json , false , after , before , req . cookies , kind , post _type )
2021-01-06 16:06:11 +01:00
return res . render ( 'user' , {
data : processed _json ,
sortby : sortby ,
past : past ,
user _preferences : req . cookies
} )
}
2020-11-17 21:44:32 +01:00
} ) ( )
} else {
2020-12-30 18:15:04 +01:00
let url = ''
if ( config . use _reddit _oauth )
url = ` https://oauth.reddit.com/user/ ${ user } /about `
else
url = ` https://reddit.com/user/ ${ user } /about.json `
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
2020-11-17 21:44:32 +01:00
. then ( result => {
if ( result . status === 200 ) {
result . json ( )
. then ( json => {
user _data . about = json
2020-12-30 18:15:04 +01:00
let url = ''
2021-01-29 19:41:32 +01:00
if ( config . use _reddit _oauth ) {
let endpoint = '/overview'
if ( post _type !== '' )
endpoint = post _type
url = ` https://oauth.reddit.com/user/ ${ user } ${ post _type } ?limit=26 ${ d } &sort= ${ sortby } &t= ${ past } `
} else {
2021-01-27 22:41:56 +01:00
url = ` https://reddit.com/user/ ${ user } ${ post _type } .json?limit=26 ${ d } &sort= ${ sortby } &t= ${ past } `
2021-01-29 19:41:32 +01:00
}
2020-12-30 18:15:04 +01:00
fetch ( encodeURI ( url ) , redditApiGETHeaders ( ) )
2020-11-17 21:44:32 +01:00
. then ( result => {
if ( result . status === 200 ) {
result . json ( )
. then ( json => {
user _data . overview = json
2020-12-01 20:16:51 +01:00
redis . setex ( key , config . setexs . user , JSON . stringify ( user _data ) , ( error ) => {
2020-11-17 21:44:32 +01:00
if ( error ) {
console . error ( ` Error setting the user ${ key } key to redis. ` , error )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , { post : null , user _preferences : req . cookies } )
2020-11-17 21:44:32 +01:00
} else {
( async ( ) => {
2021-01-29 19:41:32 +01:00
if ( api _req ) {
2021-01-06 16:06:11 +01:00
return handleTedditApiUser ( user _data , req , res , 'online' , api _type , api _target , user , after , before )
} else {
2021-01-27 22:41:56 +01:00
let processed _json = await processJsonUser ( user _data , true , after , before , req . cookies , kind , post _type )
2021-01-06 16:06:11 +01:00
return res . render ( 'user' , {
data : processed _json ,
sortby : sortby ,
past : past ,
user _preferences : req . cookies
} )
}
2020-11-17 21:44:32 +01:00
} ) ( )
}
} )
} )
} else {
2020-12-30 18:22:55 +01:00
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
2020-12-01 20:16:51 +01:00
console . error ( config . reddit _api _error _text )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , {
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies
} )
2020-11-17 21:44:32 +01:00
}
} ) . catch ( error => {
console . error ( ` Error fetching the overview JSON file from reddit.com/u/ ${ user } ` , error )
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , {
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies
} )
2020-11-17 21:44:32 +01:00
} )
} )
} else {
if ( result . status === 404 ) {
2021-01-29 19:41:32 +01:00
console . log ( '404 – User not found' )
2020-11-17 21:44:32 +01:00
} else {
2020-12-30 18:22:55 +01:00
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
2020-12-01 20:16:51 +01:00
console . error ( config . reddit _api _error _text )
2020-11-17 21:44:32 +01:00
}
2020-11-21 13:50:12 +01:00
return res . render ( 'index' , {
json : null ,
http _status _code : result . status ,
http _statustext : result . statusText ,
user _preferences : req . cookies
} )
2020-11-17 21:44:32 +01:00
}
} ) . catch ( error => {
console . error ( ` Error fetching the about JSON file from reddit.com/u/ ${ user } ` , error )
} )
}
} )
} )
2021-03-15 18:38:30 +01:00
app . get ( '/user/:user/m/:custom_feed' , ( req , res , next ) => {
res . redirect ( ` /u/ ${ req . params . user } /m/ ${ req . params . custom _feed } ` )
} )
app . get ( '/u/:user/m/:custom_feed/:sort?' , ( req , res , next ) => {
let user = req . params . user
let custom _feed = req . params . custom _feed
let subreddit = ` u/ ${ user } /m/ ${ custom _feed } `
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
if ( req . query . hasOwnProperty ( 'api' ) )
api _req = true
else
api _req = false
let d = ` &after= ${ after } `
if ( before ) {
d = ` &before= ${ before } `
}
if ( ! sortby ) {
sortby = 'hot'
}
if ( ! [ 'new' , 'rising' , 'controversial' , 'top' , 'gilded' , 'hot' ] . includes ( sortby ) ) {
2021-03-31 20:34:59 +02:00
console . log ( ` Got invalid sort. ` , req . originalUrl )
2021-03-15 18:38:30 +01:00
return res . redirect ( ` /u/ ${ user } ` )
}
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 ( ` /u/ ${ user } / ${ sortby } ` )
}
} else {
past = undefined
}
} else {
if ( sortby === 'controversial' || sortby === 'top' ) {
past = 'day'
}
}
let key = ` ${ user . toLowerCase ( ) } :m: ${ custom _feed } : ${ after } : ${ before } :sort: ${ sortby } :past: ${ past } `
redis . get ( key , ( error , json ) => {
if ( error ) {
console . error ( ` Error getting the ${ user } custom_feed key from redis. ` , error )
return res . render ( 'index' , { json : null , user _preferences : req . cookies } )
}
if ( json ) {
console . log ( ` Got /u/ ${ user } custom_feed key from redis. ` ) ;
( async ( ) => {
2021-03-16 18:07:27 +01:00
if ( api _req ) {
2021-03-15 18:38:30 +01:00
return handleTedditApiSubreddit ( json , req , res , 'redis' , api _type , api _target , subreddit )
} else {
let processed _json = await processJsonSubreddit ( json , 'redis' , null , req . cookies )
if ( ! processed _json . error ) {
return res . render ( 'subreddit' , {
json : processed _json ,
subreddit : '../' + subreddit ,
subreddit _about : null ,
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/ ${ subreddit } / ${ sortby } ?api_type=json&count=25&g=GLOBAL&t= ${ past } ${ d } `
else
url = ` https://reddit.com/ ${ subreddit } / ${ sortby } .json?api_type=json&count=25&g=GLOBAL&t= ${ past } ${ d } `
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 ( ) => {
2021-03-16 18:07:27 +01:00
if ( api _req ) {
2021-03-15 18:38:30 +01:00
return handleTedditApiSubreddit ( json , req , res , 'from_online' , api _type , api _target , subreddit )
} else {
let processed _json = await processJsonSubreddit ( json , 'from_online' , null , req . cookies )
return res . render ( 'subreddit' , {
json : processed _json ,
subreddit : '../' + subreddit ,
subreddit _about : null ,
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 ) {
2021-03-16 18:07:27 +01:00
console . log ( '404 – Subreddit not found' )
2021-03-15 18:38:30 +01:00
} else {
console . error ( ` Something went wrong while fetching data from Reddit. ${ result . status } – ${ result . statusText } ` )
console . error ( config . reddit _api _error _text )
}
return res . render ( 'index' , {
json : null ,
http _status _code : result . status ,
user _preferences : req . cookies
} )
}
} ) . catch ( error => {
console . error ( ` Error fetching the JSON file from reddit.com/ ${ subreddit } . ` , error )
} )
}
} )
} )
2020-11-17 21:44:32 +01:00
/ * *
* POSTS
* /
2020-11-21 13:50:12 +01:00
app . post ( '/saveprefs' , ( req , res , next ) => {
let theme = req . body . theme
2020-12-24 22:13:08 +01:00
let flairs = req . body . flairs
2020-12-27 23:25:39 +01:00
let nsfw _enabled = req . body . nsfw _enabled
2021-01-04 22:50:47 +01:00
let highlight _controversial = req . body . highlight _controversial
2021-03-01 19:01:02 +01:00
let post _media _max _height = req . body . post _media _max _height
2021-03-16 18:07:27 +01:00
let collapse _child _comments = req . body . collapse _child _comments
2021-03-18 18:31:41 +01:00
let show _upvoted _percentage = req . body . show _upvoted _percentage
2021-03-21 21:14:11 +01:00
let domain _twitter = req . body . domain _twitter
let domain _youtube = req . body . domain _youtube
let domain _instagram = req . body . domain _instagram
2021-06-01 23:17:06 +02:00
let videos _muted = req . body . videos _muted
2020-12-24 22:13:08 +01:00
2020-11-22 13:21:47 +01:00
res . cookie ( 'theme' , theme , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
2020-12-24 22:13:08 +01:00
if ( flairs === 'on' )
flairs = 'true'
else
flairs = 'false'
res . cookie ( 'flairs' , flairs , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
2020-12-27 23:25:39 +01:00
if ( nsfw _enabled === 'on' )
nsfw _enabled = 'true'
else
nsfw _enabled = 'false'
res . cookie ( 'nsfw_enabled' , nsfw _enabled , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
2021-01-04 22:50:47 +01:00
if ( highlight _controversial === 'on' )
highlight _controversial = 'true'
else
highlight _controversial = 'false'
res . cookie ( 'highlight_controversial' , highlight _controversial , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
2021-03-01 19:01:02 +01:00
if ( config . post _media _max _heights . hasOwnProperty ( post _media _max _height ) || ! isNaN ( post _media _max _height ) )
res . cookie ( 'post_media_max_height' , post _media _max _height , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
2021-03-16 18:07:27 +01:00
if ( collapse _child _comments === 'on' )
collapse _child _comments = 'true'
else
collapse _child _comments = 'false'
res . cookie ( 'collapse_child_comments' , collapse _child _comments , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
2021-03-18 18:31:41 +01:00
if ( show _upvoted _percentage === 'on' )
show _upvoted _percentage = 'true'
else
show _upvoted _percentage = 'false'
res . cookie ( 'show_upvoted_percentage' , show _upvoted _percentage , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
2021-06-01 23:17:06 +02:00
if ( videos _muted === 'on' )
videos _muted = 'true'
else
videos _muted = 'false'
res . cookie ( 'videos_muted' , videos _muted , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
2021-03-21 21:14:11 +01:00
res . cookie ( 'domain_twitter' , domain _twitter , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
res . cookie ( 'domain_youtube' , domain _youtube , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
res . cookie ( 'domain_instagram' , domain _instagram , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
2020-11-21 13:50:12 +01:00
return res . redirect ( '/preferences' )
} )
2021-01-30 22:16:56 +01:00
app . post ( '/export_prefs' , ( req , res , next ) => {
2021-02-13 13:29:47 +01:00
let export _saved = req . body . export _saved
let export _data = req . cookies
2021-03-19 20:40:24 +01:00
let export _to _file = req . body . export _to _file
2021-02-13 13:29:47 +01:00
if ( export _saved !== 'on' ) {
if ( req . cookies . saved )
delete export _data . saved
}
2021-03-19 20:40:24 +01:00
if ( export _to _file === 'on' ) {
res . setHeader ( 'Content-disposition' , 'attachment; filename=teddit_prefs.json' )
res . setHeader ( 'Content-type' , 'application/json' )
2021-03-21 20:20:47 +01:00
return res . send ( export _data )
2021-03-19 20:40:24 +01:00
}
2021-01-30 22:16:56 +01:00
let r = ` ${ ( Math . random ( ) . toString ( 36 ) + '00000000000000000' ) . slice ( 2 , 10 + 2 ) . toUpperCase ( ) } `
let key = ` prefs_key: ${ r } `
2021-03-21 20:20:47 +01:00
redis . set ( key , JSON . stringify ( export _data ) , ( error ) => {
2021-01-30 22:16:56 +01:00
if ( error ) {
console . error ( ` Error saving preferences to redis. ` , error )
return res . redirect ( '/preferences' )
} else {
return res . render ( 'preferences' , { user _preferences : req . cookies , instance _config : config , preferences _key : r } )
}
} )
} )
2021-03-19 20:40:24 +01:00
app . post ( '/import_prefs' , ( req , res , next ) => {
let body = ''
req . on ( 'data' , chunk => {
body += chunk . toString ( )
} )
req . on ( 'end' , ( ) => {
body = body . toString ( )
try {
2021-03-31 21:12:14 +02:00
let json = body . split ( 'Content-Type: application/json' ) [ 1 ] . trim ( ) . split ( '--' ) [ 0 ]
2021-03-19 20:40:24 +01:00
let prefs = JSON . parse ( json )
resetPreferences ( res )
for ( var setting in prefs ) {
if ( prefs . hasOwnProperty ( setting ) ) {
res . cookie ( setting , prefs [ setting ] , { maxAge : 365 * 24 * 60 * 60 * 1000 , httpOnly : true } )
}
}
return res . redirect ( '/preferences' )
} catch ( e ) {
console . error ( ` Error importing preferences from a JSON file. Please report this error on https://codeberg.org/teddit/teddit. ` , e )
}
} )
} )
2020-11-17 21:44:32 +01:00
app . post ( '/r/:subreddit/comments/:id/:snippet' , ( req , res , next ) => {
2021-04-10 15:42:01 +02:00
/ * *
* 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. ` )
2020-11-17 21:44:32 +01:00
2021-04-10 15:42:01 +02:00
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 ` )
2020-11-17 21:44:32 +01:00
} )
2021-03-19 20:40:24 +01:00
function resetPreferences ( res ) {
res . clearCookie ( 'theme' )
res . clearCookie ( 'flairs' )
res . clearCookie ( 'nsfw_enabled' )
res . clearCookie ( 'highlight_controversial' )
res . clearCookie ( 'post_media_max_height' )
res . clearCookie ( 'collapse_child_comments' )
res . clearCookie ( 'show_upvoted_percentage' )
res . clearCookie ( 'subbed_subreddits' )
2021-03-21 21:14:11 +01:00
res . clearCookie ( 'domain_twitter' )
res . clearCookie ( 'domain_youtube' )
res . clearCookie ( 'domain_instagram' )
2021-06-01 23:17:06 +02:00
res . clearCookie ( 'videos_muted' )
2021-03-19 20:40:24 +01:00
}
2020-11-17 21:44:32 +01:00
}
2021-03-16 18:07:27 +01:00
2021-06-01 23:17:06 +02:00