mirror of
https://codeberg.org/tacerus/teddit.git
synced 2024-11-25 08:39:26 +01:00
add support for user created custom feeds #162
This commit is contained in:
parent
580774f290
commit
f33f0f9ffa
146
routes.js
146
routes.js
@ -1424,6 +1424,152 @@ module.exports = (app, redis, fetch, RedditAPI) => {
|
||||
})
|
||||
})
|
||||
|
||||
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)) {
|
||||
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 = 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 () => {
|
||||
if(api_req) {
|
||||
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 () => {
|
||||
if(api_req) {
|
||||
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) {
|
||||
console.log('404 – Subreddit not found')
|
||||
} else {
|
||||
console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText}`)
|
||||
console.error(config.reddit_api_error_text)
|
||||
}
|
||||
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)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
/**
|
||||
* POSTS
|
||||
|
Loading…
Reference in New Issue
Block a user