don't fetch sidebar for multireddit urls

This commit is contained in:
teddit 2020-11-29 22:23:45 +01:00
parent 8336a3d424
commit 5e33387e5b
1 changed files with 58 additions and 54 deletions

View File

@ -2,62 +2,66 @@ module.exports = function() {
this.processSubredditSidebar = (subreddit, redis, fetch, RedditAPI) => { this.processSubredditSidebar = (subreddit, redis, fetch, RedditAPI) => {
return new Promise(resolve => { return new Promise(resolve => {
(async () => { (async () => {
let key = `${subreddit}:sidebar` if(subreddit && !subreddit.includes('+')) {
redis.get(key, (error, json) => { let key = `${subreddit}:sidebar`
if(error) { redis.get(key, (error, json) => {
console.error(`Error getting the ${subreddit}:sidebar key from redis.`, error) if(error) {
resolve(null) console.error(`Error getting the ${subreddit}:sidebar key from redis.`, error)
} resolve(null)
if(json) {
json = JSON.parse(json)
let obj = {
title: json.data.title,
public_description_html: json.data.public_description_html,
active_user_count: json.data.active_user_count,
subscribers: json.data.subscribers,
created_utc: json.data.created_utc,
over18: json.data.over18,
description_html: json.data.description_html
} }
resolve(obj) if(json) {
} else { json = JSON.parse(json)
fetch(`https://oauth.reddit.com/r/${subreddit}/about`, redditApiGETHeaders()) let obj = {
.then(result => { title: json.data.title,
if(result.status === 200) { public_description_html: json.data.public_description_html,
result.json() active_user_count: json.data.active_user_count,
.then(json => { subscribers: json.data.subscribers,
redis.setex(key, setexs.sidebar, JSON.stringify(json), (error) => { created_utc: json.data.created_utc,
if(error) { over18: json.data.over18,
console.error('Error setting the sidebar key to redis.', error) description_html: json.data.description_html
return res.render('index', { json: null, user_preferences: req.cookies })
} else {
console.log('Fetched the sidebar from reddit API.');
(async () => {
let obj = {
title: json.data.title,
public_description_html: json.data.public_description_html,
active_user_count: json.data.active_user_count,
subscribers: json.data.subscribers,
created_utc: json.data.created_utc,
over18: json.data.over18,
description_html: json.data.description_html
}
resolve(obj)
})()
}
})
})
} else {
console.error(`Something went wrong while fetching data from reddit API. ${result.status} ${result.statusText}`)
console.error(reddit_api_error_text)
resolve(null)
} }
}).catch(error => { resolve(obj)
console.error('Error fetching the sidebar.', error) } else {
resolve(null) fetch(`https://oauth.reddit.com/r/${subreddit}/about`, redditApiGETHeaders())
}) .then(result => {
} if(result.status === 200) {
}) result.json()
.then(json => {
redis.setex(key, setexs.sidebar, JSON.stringify(json), (error) => {
if(error) {
console.error('Error setting the sidebar key to redis.', error)
return res.render('index', { json: null, user_preferences: req.cookies })
} else {
console.log('Fetched the sidebar from reddit API.');
(async () => {
let obj = {
title: json.data.title,
public_description_html: json.data.public_description_html,
active_user_count: json.data.active_user_count,
subscribers: json.data.subscribers,
created_utc: json.data.created_utc,
over18: json.data.over18,
description_html: json.data.description_html
}
resolve(obj)
})()
}
})
})
} else {
console.error(`Something went wrong while fetching data from reddit API. ${result.status} ${result.statusText}`)
console.error(reddit_api_error_text)
resolve(null)
}
}).catch(error => {
console.error('Error fetching the sidebar.', error)
resolve(null)
})
}
})
} else {
resolve(null)
}
})() })()
}) })
} }