From 54007e91cb0efc358a92de3a451f883271ed21b3 Mon Sep 17 00:00:00 2001 From: Yo Mama Date: Mon, 27 Sep 2021 18:58:29 +0200 Subject: [PATCH 1/6] Add unread comments counter to submission listing and submission --- files/templates/comments.html | 12 ++-- files/templates/default.html | 77 +++++++++++++++++++++++++ files/templates/submission.html | 39 +++++++++---- files/templates/submission_listing.html | 22 ++++--- 4 files changed, 125 insertions(+), 25 deletions(-) diff --git a/files/templates/comments.html b/files/templates/comments.html index 006372919f..b9570a96d7 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 55d633b53d..c52d3b78a6 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 da328d96e4..0bfe8d0489 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: From 5b65ea121bae23a08e7a4e2e0fee93ac9fbe8ff9 Mon Sep 17 00:00:00 2001 From: Yo Mama Date: Mon, 27 Sep 2021 20:14:22 +0200 Subject: [PATCH 2/6] Correct new comments on listing. On reply and submission increments the count --- files/templates/comments.html | 33 ++++++++++++++----------- files/templates/default.html | 21 ++++++++++------ files/templates/report_post_modal.html | 24 +++++++++--------- files/templates/submission.html | 10 +++++--- files/templates/submission_listing.html | 18 ++++++++++---- 5 files changed, 64 insertions(+), 42 deletions(-) diff --git a/files/templates/comments.html b/files/templates/comments.html index b9570a96d7..9a3dbea2f3 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -83,22 +83,25 @@ }; - document.getElementById('reportCommentModal').addEventListener('hidden.bs.modal', function () { + document.addEventListener("DOMContentLoaded", function() { - var button = document.getElementById("reportCommentButton"); + document.getElementById('reportCommentModal').addEventListener('hidden.bs.modal', function () { - var beforeModal = document.getElementById("reportCommentFormBefore"); - var afterModal = document.getElementById("reportCommentFormAfter"); + const button = document.getElementById("reportCommentButton"); - button.innerHTML='Report comment'; - button.disabled= false; - afterModal.classList.add('d-none'); + const beforeModal = document.getElementById("reportCommentFormBefore"); + const afterModal = document.getElementById("reportCommentFormAfter"); - if ( beforeModal.classList.contains('d-none') ) { - beforeModal.classList.remove('d-none'); - } + button.innerHTML = 'Report comment'; + button.disabled = false; + afterModal.classList.add('d-none'); - }); + if (beforeModal.classList.contains('d-none')) { + beforeModal.classList.remove('d-none'); + } + + }); + }); //Commenting form @@ -147,7 +150,7 @@ }; - post_comment=function(fullname){ + post_comment=function(fullname, postId){ var form = new FormData(); @@ -168,6 +171,8 @@ myToast.hide(); var myToast = new bootstrap.Toast(document.getElementById('toast-post-success')); myToast.show(); + + incrementCommentCount(postId) } else { var commentError = document.getElementById("comment-error-text"); @@ -758,10 +763,10 @@ Cancel - Comment + Comment
Cancel - Comment + Comment diff --git a/files/templates/default.html b/files/templates/default.html index c52d3b78a6..6cd578455f 100644 --- a/files/templates/default.html +++ b/files/templates/default.html @@ -219,11 +219,15 @@ function showNewCommentCounts(postId, lastTotalComs) { const comments = JSON.parse(localStorage.getItem(COMMENT_COUNTS_ID)) || {} - const lastCount = comments["" + postId] + console.log(postId) + const lastCount = comments[postId] + console.log(lastCount) if (lastCount) { - const newComments = lastTotalComs - lastCount.p + const newComments = lastTotalComs - lastCount.c + console.log('new:', newComments) if (newComments > 0) { document.querySelectorAll(`#post-${postId} .new-comments`).forEach(elem => { + console.log(elem) elem.textContent = ` (+${newComments})` elem.classList.remove("d-none") }) @@ -231,6 +235,10 @@ } } + function incrementCommentCount(postId) { + saveCommentsCount(postId) + } + /** * Saves the comment count to the localStorage * @@ -240,14 +248,13 @@ function saveCommentsCount(postId, lastTotalComs = null) { const comments = JSON.parse(localStorage.getItem(COMMENT_COUNTS_ID)) || {} - lastTotalComs = lastTotalComs || (comments["" + postId] || { c: 0 }).c + 1 + const newTotal = lastTotalComs || ((comments[postId] || { c: 0 }).c + 1) - comments["" + postId] = { p: lastTotalComs, t: Date.now() } + console.log(newTotal) + comments[postId] = { c: newTotal, 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 } @@ -271,10 +278,8 @@ delete comments[key] } } - window.localStorage.setItem(COMMENT_COUNTS_ID, JSON.stringify(comments)) } - window.localStorage.setItem(LAST_CACHE_CLEAN_ID, JSON.stringify(now)) } diff --git a/files/templates/report_post_modal.html b/files/templates/report_post_modal.html index 6cba89fbff..7ddd4c6587 100644 --- a/files/templates/report_post_modal.html +++ b/files/templates/report_post_modal.html @@ -31,23 +31,25 @@ } }; - document.getElementById('reportPostModal').addEventListener('hidden.bs.modal', function () { + document.addEventListener("DOMContentLoaded", function() { + document.getElementById('reportPostModal').addEventListener('hidden.bs.modal', function () { - var button = document.getElementById("reportPostButton"); + const button = document.getElementById("reportPostButton"); - var beforeModal = document.getElementById("reportPostFormBefore"); - var afterModal = document.getElementById("reportPostFormAfter"); + const beforeModal = document.getElementById("reportPostFormBefore"); + const afterModal = document.getElementById("reportPostFormAfter"); - button.innerHTML='Report post'; - button.disabled= false; + button.innerHTML = 'Report post'; + button.disabled = false; - afterModal.classList.add('d-none'); + afterModal.classList.add('d-none'); - if ( beforeModal.classList.contains('d-none') ) { - beforeModal.classList.remove('d-none'); - } + if (beforeModal.classList.contains('d-none')) { + beforeModal.classList.remove('d-none'); + } - }); + }); + }) diff --git a/files/templates/submission.html b/files/templates/submission.html index 0bfe8d0489..010cc61585 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -37,7 +37,7 @@ {% if v %} diff --git a/files/templates/submission_listing.html b/files/templates/submission_listing.html index c7fdcbe690..c17027d7d4 100644 --- a/files/templates/submission_listing.html +++ b/files/templates/submission_listing.html @@ -141,8 +141,12 @@ {% if p.realbody(v) %}
  • {% endif %} -
  • {{p.comment_count}}
  • - +
  • + + {{p.comment_count}} + + +
  • Votes
  • {% if v and v.id!=p.author_id %} @@ -229,8 +233,12 @@