teddit/routes.js

984 lines
36 KiB
JavaScript
Raw Normal View History

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')();
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) => {
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) => {
res.clearCookie('theme')
res.clearCookie('flairs')
res.clearCookie('nsfw_enabled')
res.clearCookie("highlight_controversial")
2020-12-19 12:20:43 +01:00
return res.redirect('/preferences')
})
app.get('/privacy', (req, res, next) => {
return res.render('privacypolicy', { user_preferences: req.cookies })
})
app.get('/search', (req, res, next) => {
let q = req.query.q
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 = ''
}
if(restrict_sr !== 'on') {
restrict_sr = 'off'
}
if(nsfw !== 'on') {
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}`)
})
2020-12-19 12:14:10 +01:00
app.get('/:sort?', (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
2020-12-19 12:14:10 +01: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
2020-11-17 21:44:32 +01:00
let d = `&after=${after}`
if(before) {
d = `&before=${before}`
}
2020-12-19 12:14:10 +01:00
if(!sortby) {
sortby = 'hot'
}
if(!['new', 'rising', 'controversial', 'top', 'gilded', 'hot'].includes(sortby)) {
console.error(`Got invalid sort.`, req.originalUrl)
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
2020-12-19 12:14:10 +01:00
let key = `/after:${after}:before:${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 () => {
2020-12-19 20:52:22 +01:00
if(api_req) {
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,
user_preferences: req.cookies
})
}
2020-11-17 21:44:32 +01:00
})()
} else {
let url = ''
if(config.use_reddit_oauth)
url = `https://oauth.reddit.com/${sortby}?api_type=json&g=GLOBAL&t=${past}${d}`
else
url = `https://reddit.com/${sortby}.json?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.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 () => {
2020-12-19 20:52:22 +01:00
if(api_req) {
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,
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)
})
}
})
})
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
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)
if(post_url)
return res.redirect(json[0].data.children[0].data.permalink)
else
return res.redirect(json[1].data.children[0].data.permalink)
} else {
let url = ''
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.')
2020-12-09 19:06:25 +01:00
if(post_url)
return res.redirect(json[0].data.children[0].data.permalink)
else
return res.redirect(json[1].data.children[0].data.permalink)
}
})
})
} 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
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}`
}
if(restrict_sr !== 'on') {
restrict_sr = 'off'
}
if(nsfw !== 'on') {
nsfw = 'off'
}
let key = `search:${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)
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,
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 {
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 => {
2020-12-01 20:16:51 +01:00
redis.setex(key, config.setexs.searches, JSON.stringify(json), (error) => {
2020-11-17 21:44:32 +01:00
if(error) {
console.error('Error setting the searches 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 search results from Reddit.');
2020-11-17 21:44:32 +01:00
(async () => {
2021-01-01 18:18:54 +01:00
let processed_json = await processSearchResults(json, true, after, before, req.cookies)
2020-11-17 21:44:32 +01:00
return res.render('search', {
json: processed_json,
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: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-01-05 02:41:57 +01:00
app.get('/r/:subreddit/wiki/:page?', (req, res, next) => {
let subreddit = req.params.subreddit
let page = req.params.page
if(!page)
page = 'index'
let key = `${subreddit.toLowerCase()}:wiki:page:${page}`
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)
return res.render('subreddit_wiki', {
content_html: unescape(json.data.content_html),
subreddit: subreddit,
user_preferences: req.cookies
})
} else {
let url = ''
if(config.use_reddit_oauth)
url = `https://oauth.reddit.com/r/${subreddit}/wiki/${page}?api_type=json`
else
url = `https://reddit.com/r/${subreddit}/wiki/${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: unescape(json.data.content_html),
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)
}
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)
})
}
})
})
app.get('/r/:subreddit/w/: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
if(!page)
page = 'index'
return res.redirect(`/r/${subreddit}/wiki/${page}`)
})
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)) {
console.error(`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}`
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 () => {
2020-12-19 20:52:22 +01:00
if(api_req) {
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,
instance_nsfw_enabled: config.nsfw_enabled
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 {
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 () => {
2020-12-19 20:52:22 +01:00
if(api_req) {
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,
instance_nsfw_enabled: config.nsfw_enabled
2020-12-19 20:52:22 +01:00
})
}
2020-11-17 21:44:32 +01:00
})()
}
})
})
} else {
if(result.status === 404) {
console.log('404  Subreddit not found')
} 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)
})
}
})
})
app.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)
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
let more_comments_cursor = req.query.cursor
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)) {
2020-12-23 12:41:10 +01:00
console.error(`Got invalid sort.`, req.originalUrl)
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 () => {
if(!more_comments_cursor) {
2020-12-24 22:13:08 +01:00
let processed_json = await processJsonPost(json, false, req.cookies)
2020-12-23 19:42:53 +01:00
let finalized_json = await finalizeJsonPost(processed_json, id, post_url, null, 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,
user_preferences: req.cookies,
instance_nsfw_enabled: config.nsfw_enabled
2020-11-17 21:44:32 +01:00
})
} else {
2020-12-23 12:41:10 +01:00
let key = `morechildren:${post_url};1`
redis.get(key, (error, json) => {
if(error) {
console.error(`Error getting the ${key} key from redis.`, error)
return res.render('index', { json: null, user_preferences: req.cookies })
}
if(json) {
console.log(`Got ${key} key from redis.`);
redis.get(post_url, (error, post_json) => {
if(error) {
console.error(`Error getting the ${post_url} key from redis.`, error)
return res.render('index', { json: null, user_preferences: req.cookies })
}
if(post_json) {
redis.get(`morechildren_ids:${post_url}`, (error, morechildren_ids) => {
(async () => {
post_json = JSON.parse(post_json)
json = JSON.parse(json)
post_json[1].data.children = json
2020-12-24 22:13:08 +01:00
let processed_json = await processJsonPost(post_json, true, req.cookies)
2020-12-23 12:41:10 +01:00
let finalized_json = await finalizeJsonPost(processed_json, id, post_url, morechildren_ids)
return res.render('post', {
post: finalized_json.post_data,
comments: finalized_json.comments,
viewing_comment: false,
post_url: post_url,
subreddit: req.params.subreddit,
sortby: sortby,
more_comments_page: 1,
user_preferences: req.cookies,
instance_nsfw_enabled: config.nsfw_enabled
2020-12-23 12:41:10 +01:00
})
})()
})
}
})
}
})
2020-11-17 21:44:32 +01:00
}
})()
} 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())
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 () => {
2020-12-24 22:13:08 +01:00
let processed_json = await processJsonPost(json, true, req.cookies)
2020-12-23 19:42:53 +01:00
let finalized_json = await finalizeJsonPost(processed_json, id, post_url, null, 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,
user_preferences: req.cookies,
instance_nsfw_enabled: config.nsfw_enabled
2020-11-17 21:44:32 +01:00
})
})()
}
})
})
} else {
if(result.status === 404) {
console.log('404  Post not found')
} 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)
})
}
})
})
app.get('/user/:user', (req, res, next) => {
res.redirect(`/u/${req.params.user}`)
})
app.get('/u/:user/:sort?', (req, res, next) => {
let user = req.params.user
let after = req.query.after
let before = req.query.before
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}`
}
let sortby = req.query.sort
let past = req.query.t
if(!sortby) {
sortby = 'new'
}
if(!['hot', 'new', 'controversial', 'top'].includes(sortby)) {
console.error(`Got invalid sort.`, req.originalUrl)
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 = ''
}
}
let key = `${user}:${after}:${before}:sort:${sortby}:past:${past}`
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-06 16:06:11 +01:00
if(api_req) {
return handleTedditApiUser(json, req, res, 'redis', api_type, api_target, user, after, before)
} else {
let processed_json = await processJsonUser(json, false, after, before, req.cookies)
return res.render('user', {
data: processed_json,
sortby: sortby,
past: past,
user_preferences: req.cookies
})
}
2020-11-17 21:44:32 +01:00
})()
} else {
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
let url = ''
if(config.use_reddit_oauth)
url = `https://oauth.reddit.com/user/${user}/overview?limit=26${d}&sort=${sortby}&t=${past}`
else
url = `https://reddit.com/user/${user}.json?limit=26${d}&sort=${sortby}&t=${past}`
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-06 16:06:11 +01:00
if(api_req) {
return handleTedditApiUser(user_data, req, res, 'online', api_type, api_target, user, after, before)
} else {
let processed_json = await processJsonUser(user_data, true, after, before, req.cookies)
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) {
console.log('404  User not found')
} 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)
})
}
})
})
/**
* 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
let nsfw_enabled = req.body.nsfw_enabled
let highlight_controversial = req.body.highlight_controversial
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 })
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 })
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 })
2020-11-21 13:50:12 +01:00
return res.redirect('/preferences')
})
2020-11-17 21:44:32 +01:00
app.post('/r/:subreddit/comments/:id/:snippet', (req, res, next) => {
/* morechildren route */
let all_ids = req.body.all_ids
let post_url = req.body.url
if(!all_ids || !post_url || !post_url.startsWith('/r/')) {
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 {
let post_id = post_url.split('/')[4]
let ids_to_show = ''
all_ids = all_ids.split(',')
// TODO: paging
let page = 1
if(all_ids.length > 100) {
ids_to_show = all_ids.slice(0,100)
ids_to_show = ids_to_show.join()
}
let key = `morechildren:${post_url};1`
redis.get(key, (error, json) => {
if(error) {
console.error(`Error getting the ${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(`Redirecting to ${post_url} with cursor...`);
return res.redirect(`${post_url}?cursor=${page}&page=${page}`)
} else {
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.`)
2020-12-23 12:41:10 +01:00
let url = `https://oauth.reddit.com/api/morechildren?api_type=json&children=${ids_to_show}&limit_children=false&link_id=t3_${post_id}`
2020-12-06 21:59:40 +01:00
fetch(encodeURI(url), redditApiGETHeaders())
2020-11-17 21:44:32 +01:00
.then(result => {
if(result.status === 200) {
result.json()
.then(json => {
if(json.json.data) {
if(json.json.data.things) {
let comments = json.json.data.things
redis.setex(key, config.setexs.posts, JSON.stringify(comments), (error) => {
if(error) {
console.error(`Error setting the ${key} key to redis.`, error)
return res.render('post', { post: null, user_preferences: req.cookies })
} else {
redis.setex(`morechildren_ids:${post_url}`, config.setexs.posts, JSON.stringify(all_ids))
2020-12-30 18:22:55 +01:00
console.log(`Fetched the JSON from Reddit (endpoint "morechildren") with url: ${url}.`)
console.log(`Redirecting to ${post_url} with cursor...`)
return res.redirect(`${post_url}?cursor=${page}&page=${page}`)
}
})
2020-11-17 21:44:32 +01:00
} else {
return res.redirect(post_url)
2020-11-17 21:44:32 +01:00
}
} else {
return res.redirect(post_url)
}
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 => {
2020-12-30 18:22:55 +01:00
console.log(`Error fetching the JSON from Reddit (endpoint "morechildren") with url: ${url}.`, 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
})
}
})
}
})
}