2020-11-29 21:52:26 +01:00
|
|
|
|
module.exports = function() {
|
2020-12-01 20:20:01 +01:00
|
|
|
|
const config = require('../config')
|
2020-11-29 21:52:26 +01:00
|
|
|
|
this.processSubredditSidebar = (subreddit, redis, fetch, RedditAPI) => {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
(async () => {
|
2020-11-29 22:23:45 +01:00
|
|
|
|
if(subreddit && !subreddit.includes('+')) {
|
|
|
|
|
let key = `${subreddit}:sidebar`
|
|
|
|
|
redis.get(key, (error, json) => {
|
|
|
|
|
if(error) {
|
|
|
|
|
console.error(`Error getting the ${subreddit}:sidebar key from redis.`, error)
|
2020-11-29 21:52:26 +01:00
|
|
|
|
resolve(null)
|
2020-11-29 22:23:45 +01:00
|
|
|
|
}
|
|
|
|
|
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
|
2020-11-29 21:52:26 +01:00
|
|
|
|
}
|
2020-11-29 22:23:45 +01:00
|
|
|
|
resolve(obj)
|
|
|
|
|
} else {
|
2020-12-08 18:54:30 +01:00
|
|
|
|
if(subreddit !== 'all') {
|
|
|
|
|
fetch(encodeURI(`https://oauth.reddit.com/r/${subreddit}/about`), redditApiGETHeaders())
|
|
|
|
|
.then(result => {
|
|
|
|
|
if(result.status === 200) {
|
|
|
|
|
result.json()
|
|
|
|
|
.then(json => {
|
|
|
|
|
redis.setex(key, config.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)
|
|
|
|
|
})()
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-11-29 22:23:45 +01:00
|
|
|
|
})
|
2020-12-08 18:54:30 +01:00
|
|
|
|
} else {
|
|
|
|
|
console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`)
|
|
|
|
|
console.error(config.reddit_api_error_text)
|
|
|
|
|
resolve(null)
|
|
|
|
|
}
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
console.error('Error fetching the sidebar.', error)
|
|
|
|
|
resolve(null)
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2020-11-29 22:23:45 +01:00
|
|
|
|
resolve(null)
|
2020-12-08 18:54:30 +01:00
|
|
|
|
}
|
2020-11-29 22:23:45 +01:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
resolve(null)
|
|
|
|
|
}
|
2020-11-29 21:52:26 +01:00
|
|
|
|
})()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|