From 873e56a23b600d64c07b0f0749ffdb5e06cbe4d7 Mon Sep 17 00:00:00 2001 From: teddit Date: Thu, 9 Sep 2021 19:19:52 +0200 Subject: [PATCH] use async for processMoreComments --- inc/processMoreComments.js | 83 ++++++++++++++++++-------------------- routes/subreddit.js | 4 +- 2 files changed, 41 insertions(+), 46 deletions(-) diff --git a/inc/processMoreComments.js b/inc/processMoreComments.js index 2cc0e54..bea346c 100644 --- a/inc/processMoreComments.js +++ b/inc/processMoreComments.js @@ -1,56 +1,51 @@ const config = require('../config'); const { redisAsync } = require('./redis'); -async function moreComments(fetch, redis, post_url, comment_ids, id) { +async function processMoreComments(fetch, redis, post_url, comment_ids, id) { if (post_url) { let key = `${post_url}:morechildren:comment_ids:${comment_ids}` - redis.get(key, (error, json) => { - if (error) { - console.error(`Error getting the ${key} key from redis (moreComments()).`, error) + + try { + const cached = await redisAsync.get(key); + + if (cached !== null) { + return JSON.parse(cached); + } + let url = `https://oauth.reddit.com/api/morechildren?api_type=json&children=${comment_ids}&limit_children=false&link_id=t3_${id}` + const moreCommentsRequest = await fetch(url, redditApiGETHeaders()); + + if (moreCommentsRequest.ok) { + let response = await moreCommentsRequest.json(); + + if (response.json.data) { + if (response.json.data.things) { + let comments = response.json.data.things + await redisAsync.setex( + key, + config.setexs.posts, + JSON.stringify(comments) + ); + console.log(`Fetched more comments.`); + + return comments; + } + } + } else { + console.error( + `Something went wrong while fetching data from Reddit: + ${moreCommentsRequest.status} – ${moreCommentsRequest.statusText}` + ); + console.error(config.reddit_api_error_text); return null; } - if (json) { - json = JSON.parse(json) - return json; - } else { - let url = `https://oauth.reddit.com/api/morechildren?api_type=json&children=${comment_ids}&limit_children=false&link_id=t3_${id}` - fetch(encodeURI(url), redditApiGETHeaders()) - .then(result => { - if (result.status === 200) { - result.json() - .then(json => { - if (json.json.data) { - if (json.json.data.things) { - let comments = json.json.data.things - redis.setex(key, config.setexs.posts, JSON.stringify(comments), (error) => { - if (error) { - console.error(`Error setting the ${key} key to redis (moreComments()).`, error) - return null; - } else { - console.log(`Fetched the JSON from Reddit (endpoint "morechildren") for URL: ${post_url}. (moreComments())`) - return comments; - } - }) - } else { - return null; - } - } else { - return null; - } - }) - } else { - console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText} (moreComments())`) - return null; - } - }).catch(error => { - console.log(`Error fetching the JSON from Reddit (endpoint "morechildren") with url: ${url}. (moreComments())`, error) - return null; - }) - } - }) + } catch (error) { + console.error('Error fetching more comments: ', error); + + return null; + } } else { return null; } } -module.exports = moreComments; +module.exports = processMoreComments; diff --git a/routes/subreddit.js b/routes/subreddit.js index d2239d1..13992d9 100644 --- a/routes/subreddit.js +++ b/routes/subreddit.js @@ -624,7 +624,7 @@ subredditRoutes.get( let more_comments = null; if (comment_ids) { let key = `${post_url}:morechildren:comment_ids:${comment_ids}`; - more_comments = await moreComments( + more_comments = processMoreComments( fetch, redis, post_url, @@ -697,7 +697,7 @@ subredditRoutes.get( let more_comments = null; if (comment_ids) { let key = `${post_url}:morechildren:comment_ids:${comment_ids}`; - more_comments = await moreComments( + more_comments = await processMoreComments( fetch, redis, post_url,