2020-11-17 21:44:32 +01:00
|
|
|
|
module.exports = function() {
|
2021-04-10 15:42:01 +02:00
|
|
|
|
this.compilePostCommentsHtml = (comments, next_comment, post_id, post_url, morechildren_ids, post_author, viewing_comment, user_preferences, last_known_depth) => {
|
2020-11-17 21:44:32 +01:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
(async () => {
|
|
|
|
|
let comments_html
|
2020-12-14 15:08:38 +01:00
|
|
|
|
function commentAuthor(comment, classlist, submitter, moderator) {
|
|
|
|
|
let classes = classlist.join(' ')
|
|
|
|
|
if (comment.author === '[deleted]')
|
|
|
|
|
return `<span class="${classes}">[deleted]</span>`
|
|
|
|
|
else
|
|
|
|
|
return `<a href="/u/${comment.author}" class="${classes}">${comment.author}</a>${submitter || ''}${moderator || ''}`
|
|
|
|
|
}
|
2021-03-16 18:07:27 +01:00
|
|
|
|
|
|
|
|
|
if(!user_preferences)
|
|
|
|
|
user_preferences = {}
|
2021-04-10 15:42:01 +02:00
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
if(comments.author !== undefined && comments.body_html !== undefined) {
|
|
|
|
|
let classlist = []
|
|
|
|
|
let submitter_link = ''
|
2020-11-22 14:09:43 +01:00
|
|
|
|
let moderator = false
|
|
|
|
|
let submitter = false
|
2020-11-22 20:08:20 +01:00
|
|
|
|
let edited_span = ''
|
2021-01-04 22:50:47 +01:00
|
|
|
|
let controversial_span = ''
|
2020-12-21 12:27:31 +01:00
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
if(post_author === comments.author) {
|
|
|
|
|
classlist.push('submitter')
|
|
|
|
|
submitter_link = `<a href="${post_url}" title="submitter">[S]</a>`
|
2020-11-22 14:09:43 +01:00
|
|
|
|
submitter = true
|
2020-11-17 21:44:32 +01:00
|
|
|
|
}
|
2020-11-22 14:09:43 +01:00
|
|
|
|
if(comments.distinguished === 'moderator') {
|
2020-11-17 21:44:32 +01:00
|
|
|
|
classlist.push('green')
|
2020-11-22 14:09:43 +01:00
|
|
|
|
moderator_badge = ` <span class="green" title="moderator of this subreddit">[M]</span>`
|
|
|
|
|
moderator = true
|
2020-11-17 21:44:32 +01:00
|
|
|
|
}
|
2020-11-22 19:57:17 +01:00
|
|
|
|
if(comments.score_hidden) {
|
|
|
|
|
ups = `<span class="score-hidden">[score hidden]</span>`
|
|
|
|
|
} else {
|
|
|
|
|
ups = `${kFormatter(comments.ups)} points`
|
|
|
|
|
}
|
2020-11-22 20:08:20 +01:00
|
|
|
|
if(comments.edited) {
|
2021-01-10 10:59:04 +01:00
|
|
|
|
edited_span = `<span title="last edited ${toUTCString(comments.edited)}">*</span>`
|
2020-11-22 20:08:20 +01:00
|
|
|
|
}
|
2021-01-04 22:50:47 +01:00
|
|
|
|
if(comments.controversiality > 0) {
|
2021-01-05 01:26:25 +01:00
|
|
|
|
controversial_span = `<span class="controversial" title="this comment is voted as controversial">†</span>`
|
2021-01-04 22:50:47 +01:00
|
|
|
|
}
|
2020-11-17 21:44:32 +01:00
|
|
|
|
comments_html = `
|
2020-12-17 23:17:20 +01:00
|
|
|
|
<div class="comment ${comments.depth % 2 === 0 ? 'even-depth' : 'odd-depth'}" id="${comments.id}">
|
2021-04-10 18:03:10 +02:00
|
|
|
|
<details ${user_preferences.collapse_child_comments === 'true' && comments.depth > 0 && comments.depth < 2 ? '' : 'open'}>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
<summary>
|
2020-12-14 15:08:38 +01:00
|
|
|
|
<p class="author">${commentAuthor(comments, classlist, submitter && submitter_link, moderator && moderator_badge)}</p>
|
2020-11-22 19:57:17 +01:00
|
|
|
|
<p class="ups">${ups}</p>
|
2020-11-22 20:08:20 +01:00
|
|
|
|
<p class="created" title="${toUTCString(comments.created)}">${timeDifference(comments.created)}${edited_span}</p>
|
2020-11-22 14:09:43 +01:00
|
|
|
|
<p class="stickied">${comments.stickied ? 'stickied comment' : ''}</p>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
</summary>
|
|
|
|
|
<div class="meta">
|
2020-12-14 15:08:38 +01:00
|
|
|
|
<p class="author">${commentAuthor(comments, classlist, submitter && submitter_link, moderator && moderator_badge)}</p>
|
2020-12-21 12:27:31 +01:00
|
|
|
|
<p>${comments.user_flair}</p>
|
2021-01-04 22:50:47 +01:00
|
|
|
|
<p class="ups">${ups}${controversial_span}</p>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
<p class="created" title="${toUTCString(comments.created)}">
|
2021-01-15 18:02:50 +01:00
|
|
|
|
<a href="${comments.permalink}#c">${timeDifference(comments.created)}${edited_span}</a>
|
2020-11-22 14:09:43 +01:00
|
|
|
|
</p>
|
|
|
|
|
<p class="stickied">${comments.stickied ? 'stickied comment' : ''}</p>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
</div>
|
|
|
|
|
<div class="body">${unescape(comments.body_html)}</div>
|
|
|
|
|
`
|
|
|
|
|
} else {
|
|
|
|
|
if(comments.children) {
|
|
|
|
|
if(comments.children.length > 0) {
|
|
|
|
|
let parent_id = comments.parent_id.split('_')[1]
|
|
|
|
|
if(post_id === parent_id && !morechildren_ids) {
|
2021-04-10 15:42:01 +02:00
|
|
|
|
let more_comments = []
|
|
|
|
|
if(comments.children.length > 100) {
|
|
|
|
|
more_comments = comments.children.slice(0, 100)
|
|
|
|
|
} else {
|
|
|
|
|
more_comments = comments.children
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
comments_html = `
|
|
|
|
|
<form method="POST">
|
|
|
|
|
<button type="submit">load more comments (${comments.count})</button>
|
2021-04-10 15:42:01 +02:00
|
|
|
|
<input type="hidden" name="comment_ids" id="comment_ids" value="${more_comments.join()}">
|
2020-11-17 21:44:32 +01:00
|
|
|
|
</form>
|
|
|
|
|
`
|
|
|
|
|
} else {
|
2021-04-10 15:42:01 +02:00
|
|
|
|
let load_comms_href = parent_id
|
2020-11-17 21:44:32 +01:00
|
|
|
|
if(!morechildren_ids) {
|
|
|
|
|
comments_html = `
|
|
|
|
|
<div class="load-more-comments">
|
2020-12-28 21:14:25 +01:00
|
|
|
|
<a href="${load_comms_href}#c">load more comments (${comments.count})</a>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
</div>
|
|
|
|
|
`
|
|
|
|
|
} else {
|
2021-04-10 15:42:01 +02:00
|
|
|
|
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>
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-01-04 20:38:12 +01:00
|
|
|
|
let link = comments.parent_id.split('_')[1]
|
2020-11-17 21:44:32 +01:00
|
|
|
|
comments_html = `
|
2021-01-04 20:38:12 +01:00
|
|
|
|
<div class="load-more-comments">
|
|
|
|
|
<a href="${link}/#c">continue this thread</a>
|
|
|
|
|
</div>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-10 15:42:01 +02:00
|
|
|
|
|
|
|
|
|
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>`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
if(comments.replies) {
|
|
|
|
|
for(var i = 0; i < comments.replies.length; i++) {
|
|
|
|
|
let comment = comments.replies[i]
|
|
|
|
|
if(comment.type !== 'load_more') {
|
|
|
|
|
let classlist = []
|
|
|
|
|
let submitter_link = ''
|
2020-11-22 14:09:43 +01:00
|
|
|
|
let moderator = false
|
|
|
|
|
let submitter = false
|
2020-11-22 19:57:17 +01:00
|
|
|
|
let ups = ''
|
2020-11-22 20:08:20 +01:00
|
|
|
|
let edited_span = ''
|
2021-01-04 22:50:47 +01:00
|
|
|
|
let controversial_span = ''
|
2020-12-21 12:27:31 +01:00
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
if(post_author === comment.author) {
|
|
|
|
|
classlist.push('submitter')
|
|
|
|
|
submitter_link = `<a href="${post_url}" title="submitter">[S]</a>`
|
2020-11-22 14:09:43 +01:00
|
|
|
|
submitter = true
|
2020-11-17 21:44:32 +01:00
|
|
|
|
}
|
2020-11-22 19:57:17 +01:00
|
|
|
|
if(comment.distinguished === 'moderator') {
|
2020-11-17 21:44:32 +01:00
|
|
|
|
classlist.push('green')
|
2020-11-22 14:09:43 +01:00
|
|
|
|
moderator_badge = ` <span class="green" title="moderator of this subreddit">[M]</span>`
|
|
|
|
|
moderator = true
|
2020-11-17 21:44:32 +01:00
|
|
|
|
}
|
2020-11-22 19:57:17 +01:00
|
|
|
|
if(comment.score_hidden) {
|
|
|
|
|
ups = `<span class="score-hidden">[score hidden]</span>`
|
|
|
|
|
} else {
|
|
|
|
|
ups = `${kFormatter(comment.ups)} points`
|
|
|
|
|
}
|
2020-11-22 20:08:20 +01:00
|
|
|
|
if(comment.edited) {
|
2021-04-16 22:08:55 +02:00
|
|
|
|
edited_span = `<span title="last edited ${toUTCString(comment.edited)}">*</span>`
|
2020-11-22 20:08:20 +01:00
|
|
|
|
}
|
2021-01-04 22:50:47 +01:00
|
|
|
|
if(comment.controversiality > 0) {
|
2021-01-05 01:26:25 +01:00
|
|
|
|
controversial_span = `<span class="controversial" title="this comment is voted as controversial">†</span>`
|
2021-01-04 22:50:47 +01:00
|
|
|
|
}
|
2020-11-17 21:44:32 +01:00
|
|
|
|
comments_html += `
|
2020-12-18 18:20:31 +01:00
|
|
|
|
<div class="comment ${comment.depth % 2 === 0 ? 'even-depth' : 'odd-depth'}" id="${comment.id}">
|
2021-03-16 18:07:27 +01:00
|
|
|
|
<details ${user_preferences.collapse_child_comments === 'true' && comments.depth === 0 ? '' : 'open'}>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
<summary>
|
2020-12-14 15:08:38 +01:00
|
|
|
|
<p class="author">${commentAuthor(comment, classlist, submitter && submitter_link, moderator && moderator_badge)}</p>
|
2020-11-22 19:57:17 +01:00
|
|
|
|
<p class="ups">${ups}</p>
|
2020-11-22 20:08:20 +01:00
|
|
|
|
<p class="created" title="${toUTCString(comment.created)}">${timeDifference(comment.created)}${edited_span}</p>
|
2020-11-22 14:09:43 +01:00
|
|
|
|
<p class="stickied">${comment.stickied ? 'stickied comment' : ''}</p>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
</summary>
|
|
|
|
|
<div class="meta">
|
2020-12-14 15:08:38 +01:00
|
|
|
|
<p class="author">${commentAuthor(comment, classlist, submitter && submitter_link, moderator && moderator_badge)}</p>
|
2020-12-21 12:27:31 +01:00
|
|
|
|
<p>${comment.user_flair}</p>
|
2021-01-04 22:50:47 +01:00
|
|
|
|
<p class="ups">${ups}${controversial_span}</p>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
<p class="created" title="${toUTCString(comment.created)}">
|
2021-01-15 18:02:50 +01:00
|
|
|
|
<a href="${comment.permalink}#c">${timeDifference(comment.created)}${edited_span}</a>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
</p>
|
2020-11-22 14:09:43 +01:00
|
|
|
|
<p class="stickied">${comment.stickied ? 'stickied comment' : ''}</p>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
</div>
|
|
|
|
|
<div class="body">${unescape(comment.body_html)}</div>
|
|
|
|
|
`
|
|
|
|
|
let replies_html = ''
|
|
|
|
|
if(comment.replies) {
|
|
|
|
|
if(comment.replies.length) {
|
|
|
|
|
for(var j = 0; j < comment.replies.length; j++) {
|
2021-04-10 15:42:01 +02:00
|
|
|
|
let next_reply = false
|
2020-11-17 21:44:32 +01:00
|
|
|
|
if(comment.replies[j+1]) {
|
|
|
|
|
next_reply = comment.replies[j+1]
|
|
|
|
|
}
|
2021-03-16 18:07:27 +01:00
|
|
|
|
replies_html += await compilePostCommentsHtml(comment.replies[j], next_reply, post_id, post_url, null, post_author, viewing_comment, user_preferences)
|
2020-11-17 21:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
comments_html += replies_html + '</details></div>'
|
|
|
|
|
} else {
|
|
|
|
|
if(comment.children.length > 0) {
|
2020-12-23 19:42:53 +01:00
|
|
|
|
let parent_id = comment.parent_id.split('_')[1]
|
|
|
|
|
let load_comms_href = parent_id
|
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
comments_html += `
|
|
|
|
|
<div class="load-more-comments">
|
2020-12-28 21:14:25 +01:00
|
|
|
|
<a href="${load_comms_href}#c">load more comments (${comment.count})</a>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
</div>
|
|
|
|
|
`
|
|
|
|
|
} else {
|
2021-01-10 10:59:04 +01:00
|
|
|
|
let link = comment.parent_id.split('_')[1]
|
2021-01-04 20:38:12 +01:00
|
|
|
|
comments_html = `
|
2020-11-17 21:44:32 +01:00
|
|
|
|
<div class="load-more-comments">
|
2021-01-04 20:38:12 +01:00
|
|
|
|
<a href="${link}/#c">continue this thread</a>
|
2020-11-17 21:44:32 +01:00
|
|
|
|
</div>
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let next_comment_parent_id = null
|
|
|
|
|
if(next_comment) {
|
|
|
|
|
if(next_comment.parent_id) {
|
|
|
|
|
next_comment_parent_id = next_comment.parent_id.split('_')[1]
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-10 15:42:01 +02:00
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
if((comments.replies || comments.author !== undefined) && next_comment_parent_id !== comments.id) {
|
|
|
|
|
comments_html += `</details></div>`
|
|
|
|
|
}
|
|
|
|
|
next_comment_parent_id = null
|
2020-12-21 12:27:31 +01:00
|
|
|
|
|
2020-11-17 21:44:32 +01:00
|
|
|
|
resolve(comments_html)
|
|
|
|
|
})()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|