fix load more comments (morechildren-endpoint), fixes #122

This commit is contained in:
teddit
2021-04-10 15:42:01 +02:00
parent 006ac93de5
commit 567e6f7797
5 changed files with 171 additions and 157 deletions
+49 -17
View File
@@ -1,5 +1,5 @@
module.exports = function() {
this.compilePostCommentsHtml = (comments, next_comment, post_id, post_url, morechildren_ids, post_author, viewing_comment, user_preferences) => {
this.compilePostCommentsHtml = (comments, next_comment, post_id, post_url, morechildren_ids, post_author, viewing_comment, user_preferences, last_known_depth) => {
return new Promise((resolve, reject) => {
(async () => {
let comments_html
@@ -13,7 +13,7 @@ module.exports = function() {
if(!user_preferences)
user_preferences = {}
if(comments.author !== undefined && comments.body_html !== undefined) {
let classlist = []
let submitter_link = ''
@@ -68,31 +68,49 @@ module.exports = function() {
if(comments.children.length > 0) {
let parent_id = comments.parent_id.split('_')[1]
if(post_id === parent_id && !morechildren_ids) {
let more_comments = []
if(comments.children.length > 100) {
more_comments = comments.children.slice(0, 100)
} else {
more_comments = comments.children
}
comments_html = `
<form method="POST">
<button type="submit">load more comments (${comments.count})</button>
<input type="hidden" name="url" id="url" value="${post_url}">
<input type="hidden" name="all_ids" id="all_ids" value="${comments.children.join()}">
<input type="hidden" name="comment_ids" id="comment_ids" value="${more_comments.join()}">
</form>
`
} else {
let load_comms_href = parent_id
if(!morechildren_ids) {
let load_comms_href = parent_id
comments_html = `
<div class="load-more-comments">
<a href="${load_comms_href}#c">load more comments (${comments.count})</a>
</div>
`
} else {
morechildren_ids = JSON.parse(morechildren_ids)
comments_html = `
<form method="POST">
<button type="submit">load more comments (${morechildren_ids.length})</button>
<input type="hidden" name="url" id="url" value="${post_url}">
<input type="hidden" name="all_ids" id="all_ids" value='${morechildren_ids.join()}'>
</form>
`
if(next_comment === false) {
let more_comments = morechildren_ids[morechildren_ids.length - 1].data.children
if(more_comments.length > 100) {
more_comments = more_comments.slice(0, 100)
} else {
more_comments = more_comments
}
comments_html = `
<form method="POST">
<button type="submit">load more comments (${more_comments.length})</button>
<input type="hidden" name="comment_ids" id="comment_ids" value="${more_comments.join()}">
</form>
`
} else {
comments_html = `
<div class="load-more-comments">
<a href="${load_comms_href}#c">load more comments (${comments.count})</a>
</div>
`
}
}
}
} else {
@@ -105,7 +123,21 @@ module.exports = function() {
}
}
}
if(morechildren_ids) {
if(next_comment.depth != undefined) {
if(next_comment.depth < last_known_depth) {
let times = last_known_depth - next_comment.depth
if(next_comment.depth == 0) {
times = last_known_depth
}
for(var i = 0; i < times; i++) {
comments_html += `</details></div>`
}
}
}
}
if(comments.replies) {
for(var i = 0; i < comments.replies.length; i++) {
let comment = comments.replies[i]
@@ -163,7 +195,7 @@ module.exports = function() {
if(comment.replies) {
if(comment.replies.length) {
for(var j = 0; j < comment.replies.length; j++) {
let next_reply
let next_reply = false
if(comment.replies[j+1]) {
next_reply = comment.replies[j+1]
}
@@ -200,7 +232,7 @@ module.exports = function() {
next_comment_parent_id = next_comment.parent_id.split('_')[1]
}
}
if((comments.replies || comments.author !== undefined) && next_comment_parent_id !== comments.id) {
comments_html += `</details></div>`
}
+10 -5
View File
@@ -76,13 +76,13 @@ module.exports = function(fetch) {
}
}
obj = await processPostMedia(obj, post, post.media, has_gif, reddit_video, gif_to_mp4, user_preferences)
obj = await processPostMedia(obj, post, post.media, has_gif, reddit_video, gif_to_mp4)
if(post.crosspost_parent_list) {
post.crosspost = post.crosspost_parent_list[0]
}
if(post.crosspost) {
obj = await processPostMedia(obj, post.crosspost, post.crosspost.media, has_gif, reddit_video, gif_to_mp4, user_preferences)
obj = await processPostMedia(obj, post.crosspost, post.crosspost.media, has_gif, reddit_video, gif_to_mp4)
obj.crosspost = {
author: post.crosspost.author,
created: post.crosspost.created_utc,
@@ -164,7 +164,7 @@ module.exports = function(fetch) {
score_hidden: comment.score_hidden,
edited: comment.edited,
replies: [],
depth: 0,
depth: comment.depth,
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : ''),
controversiality: (user_preferences.highlight_controversial != 'false' ? comment.controversiality : '')
}
@@ -206,12 +206,17 @@ module.exports = function(fetch) {
this.finalizeJsonPost = async (processed_json, post_id, post_url, morechildren_ids, viewing_comment, user_preferences) => {
let comments_html = `<div class="comments">`
let comments = processed_json.comments
let last_known_depth = undefined
for(var i = 0; i < comments.length; i++) {
let next_comment
let next_comment = false
if(comments[i+1]) {
next_comment = comments[i+1]
}
comments_html += await compilePostCommentsHtml(comments[i], next_comment, post_id, post_url, morechildren_ids, processed_json.author, viewing_comment, user_preferences)
if(comments[i].depth != undefined) {
last_known_depth = comments[i].depth
}
comments_html += await compilePostCommentsHtml(comments[i], next_comment, post_id, post_url, morechildren_ids, processed_json.author, viewing_comment, user_preferences, last_known_depth)
}
comments_html += `</div>`
+54
View File
@@ -0,0 +1,54 @@
module.exports = function() {
const config = require('../config')
this.moreComments = (fetch, redis, post_url, comment_ids, id) => {
return new Promise(resolve => {
(async () => {
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)
resolve(false)
}
if(json) {
json = JSON.parse(json)
resolve(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)
resolve(false)
} else {
console.log(`Fetched the JSON from Reddit (endpoint "morechildren") for URL: ${post_url}. (moreComments())`)
resolve(comments)
}
})
} else {
resolve(false)
}
} else {
resolve(false)
}
})
} else {
console.error(`Something went wrong while fetching data from Reddit. ${result.status} ${result.statusText} (moreComments())`)
resolve(false)
}
}).catch(error => {
console.log(`Error fetching the JSON from Reddit (endpoint "morechildren") with url: ${url}. (moreComments())`, error)
resolve(false)
})
}
})
})()
})
}
}1