diff --git a/files/templates/comments.html b/files/templates/comments.html index 006372919..b9570a96d 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -383,11 +383,6 @@ {% macro single_comment(c, level=1) %} - - {% set ups=c.upvotes %} {% set downs=c.downvotes %} {% set score=ups-downs %} @@ -882,6 +877,13 @@ {% endif %} + + {% endmacro %} {% for comment in comments %} diff --git a/files/templates/default.html b/files/templates/default.html index 55d633b53..c52d3b78a 100644 --- a/files/templates/default.html +++ b/files/templates/default.html @@ -207,6 +207,83 @@ } } + + // New comment counts + + // comment counts format: {"123412": {c: 12, t: 23532456263}} + const COMMENT_COUNTS_ID = "comment-counts" + + /** + * Display the number of new comments present since the last time the post was opened + */ + function showNewCommentCounts(postId, lastTotalComs) { + const comments = JSON.parse(localStorage.getItem(COMMENT_COUNTS_ID)) || {} + + const lastCount = comments["" + postId] + if (lastCount) { + const newComments = lastTotalComs - lastCount.p + if (newComments > 0) { + document.querySelectorAll(`#post-${postId} .new-comments`).forEach(elem => { + elem.textContent = ` (+${newComments})` + elem.classList.remove("d-none") + }) + } + } + } + + /** + * Saves the comment count to the localStorage + * + * @param postId The id of the post associated with the comments + * @param lastTotalComs The new amount, If null it will just increment the previous amount + */ + function saveCommentsCount(postId, lastTotalComs = null) { + const comments = JSON.parse(localStorage.getItem(COMMENT_COUNTS_ID)) || {} + + lastTotalComs = lastTotalComs || (comments["" + postId] || { c: 0 }).c + 1 + + comments["" + postId] = { p: lastTotalComs, t: Date.now() } + + window.localStorage.setItem(COMMENT_COUNTS_ID, JSON.stringify(comments)) + + //TODO: increment count on submit response and new comment + + //TODO: restrict on display unread comments flag + } + + /** + * Cleans the expired entries (5 days). It runs every hour. + */ + function cleanCommentsCache() { + const LAST_CACHE_CLEAN_ID = "last-cache-clean" + const EXPIRE_INTERVAL_MILLIS = 5 * 24 * 60 * 60 * 1000 + const CACHE_CLEAN_INTERVAL = 60 * 60 * 1000 // 1 hour + + function cleanCache() { + const lastCacheClean = JSON.parse(localStorage.getItem(LAST_CACHE_CLEAN_ID)) || Date.now() + const now = Date.now() + + if (now - lastCacheClean > CACHE_CLEAN_INTERVAL) { + const comments = JSON.parse(localStorage.getItem(COMMENT_COUNTS_ID)) || {} + + for (let [key, value] of Object.entries(comments)) { + if (now - value.t > EXPIRE_INTERVAL_MILLIS) { + delete comments[key] + } + } + + window.localStorage.setItem(COMMENT_COUNTS_ID, JSON.stringify(comments)) + } + + window.localStorage.setItem(LAST_CACHE_CLEAN_ID, JSON.stringify(now)) + } + + // So it does not slow the load of the main page with the clean up + setTimeout(cleanCache, 500) + } + + cleanCommentsCache() + {% endif %} diff --git a/files/templates/submission.html b/files/templates/submission.html index da328d96e..0bfe8d048 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -76,15 +76,6 @@ {% endif %} - - @@ -450,7 +441,12 @@
+ + {% if p.active_flags %}
Reported by: