change callback to await in processSubredditAbout

This commit is contained in:
json 2021-09-08 09:32:26 +01:00
parent aae3e00ab1
commit 711f2450c7
1 changed files with 52 additions and 104 deletions

View File

@ -1,8 +1,7 @@
const config = require('../config'); const config = require('../config');
const { redisAsync } = require('./redis');
async function processSubredditAbout(subreddit, redis, fetch, RedditAPI) { function returnRelevantKeys(json) {
if (subreddit && !subreddit.includes('+') && subreddit !== 'all') {
function returnRelevantKeys(json) {
return { return {
title: json.data.title, title: json.data.title,
public_description_html: json.data.public_description_html, public_description_html: json.data.public_description_html,
@ -13,104 +12,53 @@ async function processSubredditAbout(subreddit, redis, fetch, RedditAPI) {
description_html: json.data.description_html, description_html: json.data.description_html,
moderators: json.moderators, moderators: json.moderators,
}; };
}
async function processSubredditAbout(subreddit, redis, fetch, RedditAPI) {
if (subreddit && !subreddit.includes('+') && subreddit !== 'all') {
const key = `${subreddit}:sidebar`;
try {
const cached = await redisAsync.get(key);
if (cached !== null) {
return returnRelevantKeys(JSON.parse(cached));
} }
let key = `${subreddit}:sidebar`;
redis.get(key, (error, json) => {
if (error) {
console.error(
`Error getting the ${subreddit}:sidebar key from redis.`,
error
);
return null;
}
if (json) {
json = JSON.parse(json);
return returnRelevantKeys(json);
} else {
let url = `https://reddit.com/r/${subreddit}/about.json`; let url = `https://reddit.com/r/${subreddit}/about.json`;
if (config.use_reddit_oauth) { if (config.use_reddit_oauth) {
url = `https://oauth.reddit.com/r/${subreddit}/about`; url = `https://oauth.reddit.com/r/${subreddit}/about`;
} }
fetch(encodeURI(url), redditApiGETHeaders())
.then((result) => { const subredditAboutRequest = await fetch(url, redditApiGETHeaders());
if (result.status === 200) {
result.json().then((json) => { if (subredditAboutRequest.ok) {
json.moderators = []; let response = await subredditAboutRequest.json();
redis.setex( response.moderators = [];
await redisAsync.setex(
key, key,
config.setexs.sidebar, config.setexs.sidebar,
JSON.stringify(json), JSON.stringify(response)
(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.');
let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json`;
if (config.use_reddit_oauth) {
moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators`;
}
return returnRelevantKeys(json);
/*
* The following code is commented out because Reddit doesn't
* anymore support fetching moderators for subreddits
* when not logged in.
* This might change in the future though.
* https://codeberg.org/teddit/teddit/issues/207
*/
/* console.log(`Fetched sidebar for ${subreddit} from reddit API`);
fetch(encodeURI(moderators_url), redditApiGETHeaders())
.then(mod_result => { return returnRelevantKeys(response);
if(mod_result.status === 200) {
mod_result.json()
.then(mod_json => {
json.moderators = mod_json
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => {
if(error) {
console.error('Error setting the sidebar with moderators key to redis.', error)
return res.render('index', { json: null, user_preferences: req.cookies })
} else {
console.log('Fetched the moderators from reddit API.')
return(returnRelevantKeys(json))
}
})
})
} else {
console.error(`Something went wrong while fetching moderators data from reddit API. ${mod_result.status} ${mod_result.statusText}`)
console.error(config.reddit_api_error_text)
return(returnRelevantKeys(json))
}
}).catch(error => {
console.error('Error fetching moderators.', error)
return(returnRelevantKeys(json))
})
*/
}
}
);
});
} else { } else {
console.error( console.error(
`Something went wrong while fetching data from reddit API. ${result.status} ${result.statusText}` `Something went wrong while fetching data from reddit API:
${subredditAboutRequest.status} ${subredditAboutRequest.statusText}`
); );
console.error(config.reddit_api_error_text); console.error(config.reddit_api_error_text);
return null; return null;
} }
}) } catch (error) {
.catch((error) => { console.error('Error fetching the sidebar: ', error);
console.error('Error fetching the sidebar.', error);
return null; return null;
});
} }
});
} else { } else {
return null; return null;
} }