make teddit hostable without reddit oauth api

This commit is contained in:
teddit 2020-12-30 18:15:04 +01:00
parent 653ed87da1
commit 7464569b5d
6 changed files with 68 additions and 14 deletions

View File

@ -1,6 +1,6 @@
const config = {
domain: process.env.DOMAIN || '127.0.0.1', // Or for example 'teddit.net'
reddit_app_id: process.env.REDDIT_APP_ID || 'ABfYqdDc9qPh1w', // You should obtain your own Reddit app ID. For testing purposes it's okay to use this project's default app ID. Create your Reddit app here: https://old.reddit.com/prefs/apps/. Make sure to create an "installed app" type of app.
use_reddit_oauth: process.env.USE_REDDIT_OAUTH === "true" || false, // If false, teddit uses Reddit's public API. If true, you need to have your own Reddit app ID (enter the app ID to the "reddit_app_id" config key).
cert_dir: process.env.CERT_DIR || '', // For example '/home/teddit/letsencrypt/live/teddit.net', if you are using https. No trailing slash.
flairs_enabled: process.env.FLAIRS_ENABLED !== "true" || true, // Enables the rendering of user and link flairs on teddit
api_enabled: process.env.API_ENABLED !== "true" || true, // Teddit API feature. Might increase loads significantly on your instance.
@ -22,6 +22,7 @@ const config = {
trust_proxy_address: process.env.TRUST_PROXY_ADDRESS || '127.0.0.1',
nsfw_enabled: process.env.NSFW_ENABLED !== "true" || true, // Enable NSFW (over 18) content. If false, a warning is shown to the user before opening any NSFW post. When the NFSW content is disabled, NSFW posts are hidden from subreddits and from user page feeds. Note: Users can set this to true or false from their preferences.
post_comments_sort: process.env.POST_COMMENTS_SORT || 'confidence', // "confidence" is the default sorting in Reddit. Must be one of: confidence, top, new, controversial, old, random, qa, live.
reddit_app_id: process.env.REDDIT_APP_ID || 'ABfYqdDc9qPh1w', // If "use_reddit_oauth" config key is set to true, you have to obtain your Reddit app ID. For testing purposes it's okay to use this project's default app ID. Create your Reddit app here: https://old.reddit.com/prefs/apps/. Make sure to create an "installed app" type of app.
setexs: {
/**,
* Redis cache expiration values (in seconds).

View File

@ -1,5 +1,9 @@
module.exports = function(fetch) {
const config = require('../config');
this.initRedditApi = function() {
if(!config.use_reddit_oauth)
return null
let options = {
body: `grant_type=https://oauth.reddit.com/grants/installed_client&device_id=DO_NOT_TRACK_THIS_DEVICE&duration=permanent`,
headers: {
@ -69,6 +73,9 @@ module.exports = function(fetch) {
})
}
this.redditApiGETHeaders = function() {
if(!config.use_reddit_oauth)
return { method: 'GET' }
return {
headers: {
Authorization: `Bearer ${reddit_access_token}`

View File

@ -24,7 +24,12 @@ module.exports = function() {
resolve(obj)
} else {
if(subreddit !== 'all') {
fetch(encodeURI(`https://oauth.reddit.com/r/${subreddit}/about`), redditApiGETHeaders())
let url = ''
if(config.use_reddit_oauth)
url = `https://oauth.reddit.com/r/${subreddit}/about`
else
url = `https://reddit.com/r/${subreddit}/about.json`
fetch(encodeURI(url), redditApiGETHeaders())
.then(result => {
if(result.status === 200) {
result.json()

View File

@ -1,6 +1,6 @@
{
"name": "teddit",
"version": "0.0.8",
"version": "0.1.1",
"description": "A free and open source alternative Reddit front-end focused on privacy.",
"homepage": "https://teddit.net",
"bugs": {

View File

@ -122,7 +122,12 @@ module.exports = (app, redis, fetch, RedditAPI) => {
}
})()
} else {
fetch(encodeURI(`https://oauth.reddit.com/${sortby}?api_type=json&g=GLOBAL&t=${past}${d}`), redditApiGETHeaders())
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())
.then(result => {
if(result.status === 200) {
result.json()
@ -196,10 +201,18 @@ module.exports = (app, redis, fetch, RedditAPI) => {
return res.redirect(json[1].data.children[0].data.permalink)
} else {
let url = ''
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`
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`
}
fetch(encodeURI(url), redditApiGETHeaders())
.then(result => {
if(result.status === 200) {
@ -284,7 +297,12 @@ module.exports = (app, redis, fetch, RedditAPI) => {
})
})()
} else {
fetch(encodeURI(`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}`), redditApiGETHeaders())
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())
.then(result => {
if(result.status === 200) {
result.json()
@ -407,7 +425,12 @@ module.exports = (app, redis, fetch, RedditAPI) => {
}
})()
} else {
fetch(encodeURI(`https://oauth.reddit.com/r/${subreddit}/${sortby}?api_type=json&count=25&g=GLOBAL&t=${past}${d}`), redditApiGETHeaders())
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())
.then(result => {
if(result.status === 200) {
result.json()
@ -551,7 +574,13 @@ module.exports = (app, redis, fetch, RedditAPI) => {
}
})()
} else {
fetch(encodeURI(`https://oauth.reddit.com${comments_url}?api_type=json&sort=${sortby}&context=${context}`), redditApiGETHeaders())
let url = ''
if(config.use_reddit_oauth)
url = `https://oauth.reddit.com${comments_url}?api_type=json&sort=${sortby}&context=${context}`
else
url = `https://reddit.com${comments_url}.json?api_type=json&sort=${sortby}&context=${context}`
fetch(encodeURI(url), redditApiGETHeaders())
.then(result => {
if(result.status === 200) {
result.json()
@ -667,13 +696,23 @@ module.exports = (app, redis, fetch, RedditAPI) => {
})
})()
} else {
fetch(encodeURI(`https://oauth.reddit.com/user/${user}/about`), redditApiGETHeaders())
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())
.then(result => {
if(result.status === 200) {
result.json()
.then(json => {
user_data.about = json
fetch(encodeURI(`https://oauth.reddit.com/user/${user}/overview?limit=26${d}&sort=${sortby}&t=${past}`), redditApiGETHeaders())
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())
.then(result => {
if(result.status === 200) {
result.json()
@ -790,6 +829,8 @@ module.exports = (app, redis, fetch, RedditAPI) => {
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.`)
let url = `https://oauth.reddit.com/api/morechildren?api_type=json&children=${ids_to_show}&limit_children=false&link_id=t3_${post_id}`
fetch(encodeURI(url), redditApiGETHeaders())
.then(result => {

View File

@ -24,5 +24,5 @@ html
.bottom
a(href="https://en.wikipedia.org/wiki/Piratbyr%C3%A5n#Kopimi", target="_blank")
img(src="kopimi.gif")
p.version v.0.0.8
p.version v.0.1.1
include includes/footer.pug