teddit-reddit-frontend-alte.../inc/processMoreComments.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

const config = require('../config');
const { redisAsync } = require('./redis');
2021-09-09 19:19:52 +02:00
async function processMoreComments(fetch, redis, post_url, comment_ids, id) {
if (post_url) {
let key = `${post_url}:morechildren:comment_ids:${comment_ids}`
2021-09-09 19:19:52 +02:00
try {
const cached = await redisAsync.get(key);
if (cached !== null) {
return JSON.parse(cached);
}
2021-09-09 19:19:52 +02:00
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;
}
2021-09-09 19:19:52 +02:00
}
} else {
console.error(
`Something went wrong while fetching data from Reddit:
${moreCommentsRequest.status} ${moreCommentsRequest.statusText}`
);
console.error(config.reddit_api_error_text);
return null;
}
2021-09-09 19:19:52 +02:00
} catch (error) {
console.error('Error fetching more comments: ', error);
return null;
}
} else {
return null;
}
2021-04-10 15:44:39 +02:00
}
2021-09-09 19:19:52 +02:00
module.exports = processMoreComments;