fix duplicate ~new~ indicators being sent (#642)

we also make the JS we're generating about 7% the size it was before.
This commit is contained in:
justcool393 2023-07-22 17:52:09 -07:00 committed by GitHub
parent 2541737833
commit a5a344f084
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 36 deletions

View file

@ -56,4 +56,22 @@ function expandMarkdown(t,id) {
let val = t.getElementsByTagName('span')[0]
if (val.innerHTML == 'View source') val.innerHTML = 'Hide source'
else val.innerHTML = 'View source'
};
};
function commentsAddUnreadIndicator(commentIds) {
commentIds.forEach(element => {
const commentOnly = document.getElementById(`comment-${element}-only`);
if (!commentOnly) {
console.warn(`Couldn't find comment (comment ID ${element}) in page while attempting to add an unread indicator.`);
return;
}
if (commentOnly.classList.contains("unread")) return;
commentOnly.classList.add("unread");
const commentUserInfo = document.getElementById(`comment-${element}`)?.querySelector(".comment-user-info");
if (!commentUserInfo) {
console.warn(`Couldn't find comment user info (comment ID ${element}) in page while attempting to add an unread indicator.`);
return;
}
commentUserInfo.innerHTML += "<span class=\"new-indicator\">~new~</span>";
});
}

View file

@ -201,23 +201,23 @@
<script src="{{ 'js/comments+submission_listing.js' | asset }}"></script>
<script src="{{ 'js/comments.js' | asset }}"></script>
{# See https://github.com/themotte/rDrama/pull/642#issuecomment-1646649781 for info on this section of the code #}
<script>
{% if p and (not v or v.highlightcomments) %}
comments = JSON.parse(localStorage.getItem("comment-counts")) || {}
lastCount = comments['{{p.id}}']
if (lastCount)
{
{% for c in p.comments %}
{% if not (v and v.id==c.author_id) and not c.voted %}
if ({{c.created_utc*1000}} > lastCount.t)
try {
document.getElementById("comment-{{c.id}}-only").classList.add('unread')
document.getElementById("comment-{{c.id}}").querySelector(".comment-user-info").innerHTML += "<span>~new~</span>";
}
catch(e) {}
{% endif %}
var commentsToCheck = [
{% for c in p.comments -%}
{%- if not (v and v.id==c.author_id) and not c.voted %}
[{{c.id}}, {{c.created_utc * 1000}}],
{%- endif -%}
{% endfor %}
}
];
commentsToCheck = commentsToCheck.filter(comment => comment[1] > lastCount.t)
.map(comment => comment[0]);
if (lastCount) commentsAddUnreadIndicator(commentsToCheck);
{% endif %}
{% if p and not (request.values and ('context' in request.values)) %}
@ -250,19 +250,19 @@
{% if p %}
comments = JSON.parse(localStorage.getItem("old-comment-counts")) || {}
lastCount = comments['{{p.id}}']
if (lastCount)
{
{% for c in p.comments %}
{% if not (v and v.id==c.author_id) and not c.voted %}
if ({{c.created_utc*1000}} > lastCount.t)
try {
document.getElementById("comment-{{c.id}}-only").classList.add('unread')
document.getElementById("comment-{{c.id}}").querySelector(".comment-user-info").innerHTML += "<span>~new~</span>";
}
catch(e) {}
{% endif %}
var commentsToCheck = [
{% for c in p.comments -%}
{%- if not (v and v.id==c.author_id) and not c.voted %}
[{{c.id}}, {{c.created_utc * 1000}}],
{%- endif -%}
{% endfor %}
}
];
commentsToCheck = commentsToCheck.filter(comment => comment[1] > lastCount.t)
.map(comment => comment[0]);
if (lastCount) commentsAddUnreadIndicator(commentsToCheck);
{% endif %}
}
btn.disabled = false;

View file

@ -489,19 +489,19 @@
comments = JSON.parse(localStorage.getItem("old-comment-counts")) || {}
lastCount = comments['{{p.id}}']
if (lastCount)
{
{% for c in p.comments %}
var commentsToCheck = [
{% for c in p.comments -%}
{% if not (v and v.id==c.author_id) and not c.voted %}
if ({{c.created_utc*1000}} > lastCount.t)
try {
document.getElementById("comment-{{c.id}}-only").classList.add('unread')
document.getElementById("comment-{{c.id}}").querySelector(".comment-user-info").innerHTML += "<span>~new~</span>";
}
catch(e) {}
{% endif %}
[{{c.id}}, {{c.created_utc * 1000}}],
{%- endif -%}
{% endfor %}
}
];
commentsToCheck = commentsToCheck.filter(comment => comment[1] > lastCount.t)
.map(comment => comment[0]);
if (lastCount) commentsAddUnreadIndicator(commentsToCheck);
collapsedCommentStorageApply();
}