mirror of
https://codeberg.org/tacerus/teddit.git
synced 2024-11-22 14:59:26 +01:00
convert processMoreComments to use async function
This commit is contained in:
parent
4cc8dd7bd5
commit
813fb3566c
@ -1,54 +1,56 @@
|
|||||||
module.exports = function() {
|
const config = require('../config');
|
||||||
const config = require('../config')
|
const { redisAsync } = require('./redis');
|
||||||
this.moreComments = (fetch, redis, post_url, comment_ids, id) => {
|
|
||||||
return new Promise(resolve => {
|
async function moreComments(fetch, redis, post_url, comment_ids, id) {
|
||||||
(async () => {
|
if (post_url) {
|
||||||
let key = `${post_url}:morechildren:comment_ids:${comment_ids}`
|
let key = `${post_url}:morechildren:comment_ids:${comment_ids}`
|
||||||
redis.get(key, (error, json) => {
|
redis.get(key, (error, json) => {
|
||||||
if(error) {
|
if (error) {
|
||||||
console.error(`Error getting the ${key} key from redis (moreComments()).`, error)
|
console.error(`Error getting the ${key} key from redis (moreComments()).`, error)
|
||||||
resolve(false)
|
return null;
|
||||||
}
|
}
|
||||||
if(json) {
|
if (json) {
|
||||||
json = JSON.parse(json)
|
json = JSON.parse(json)
|
||||||
resolve(json)
|
return json;
|
||||||
} else {
|
} else {
|
||||||
let url = `https://oauth.reddit.com/api/morechildren?api_type=json&children=${comment_ids}&limit_children=false&link_id=t3_${id}`
|
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())
|
fetch(encodeURI(url), redditApiGETHeaders())
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if(result.status === 200) {
|
if (result.status === 200) {
|
||||||
result.json()
|
result.json()
|
||||||
.then(json => {
|
.then(json => {
|
||||||
if(json.json.data) {
|
if (json.json.data) {
|
||||||
if(json.json.data.things) {
|
if (json.json.data.things) {
|
||||||
let comments = json.json.data.things
|
let comments = json.json.data.things
|
||||||
redis.setex(key, config.setexs.posts, JSON.stringify(comments), (error) => {
|
redis.setex(key, config.setexs.posts, JSON.stringify(comments), (error) => {
|
||||||
if(error) {
|
if (error) {
|
||||||
console.error(`Error setting the ${key} key to redis (moreComments()).`, error)
|
console.error(`Error setting the ${key} key to redis (moreComments()).`, error)
|
||||||
resolve(false)
|
return null;
|
||||||
} else {
|
} else {
|
||||||
console.log(`Fetched the JSON from Reddit (endpoint "morechildren") for URL: ${post_url}. (moreComments())`)
|
console.log(`Fetched the JSON from Reddit (endpoint "morechildren") for URL: ${post_url}. (moreComments())`)
|
||||||
resolve(comments)
|
return comments;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
resolve(false)
|
return null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
resolve(false)
|
return null;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText} (moreComments())`)
|
console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText} (moreComments())`)
|
||||||
resolve(false)
|
return null;
|
||||||
}
|
}
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.log(`Error fetching the JSON from Reddit (endpoint "morechildren") with url: ${url}. (moreComments())`, error)
|
console.log(`Error fetching the JSON from Reddit (endpoint "morechildren") with url: ${url}. (moreComments())`, error)
|
||||||
resolve(false)
|
return null;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})()
|
} else {
|
||||||
})
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = moreComments;
|
||||||
|
Loading…
Reference in New Issue
Block a user