Merge pull request #58 from yoMamasDic/master

Add new comments since last opening of the post
remotes/1693045480750635534/spooky-22
Aevann1 2021-09-28 01:24:58 +02:00 committed by GitHub
commit b6e3f5aabe
4 changed files with 158 additions and 36 deletions

View File

@ -33,6 +33,8 @@
<script src="/assets/js/comments_admin.js"></script>
{% endif %}
{% include 'embeds/new_comments_count.html' %}
<script>
function collapse_comment(comment_id) {
@ -64,11 +66,6 @@
{% macro single_comment(c, level=1) %}
<!---beginning of comment {{c.id}}--->
<script>
var date = new Date('{{c.created_datetime}}');
document.getElementById('timestamp-{{c.id}}').title = date.toString();
</script>
{% set ups=c.upvotes %}
{% set downs=c.downvotes %}
{% set score=ups-downs %}
@ -93,9 +90,9 @@
<div id="comment-{{c.id}}-only">
<div class="user-info">
<span class="comment-collapse d-md-none" onclick="collapse_comment('{{c.id}}')"></span>
{% if standalone and c.over_18 %}<span class="badge badge-danger">+18</span> {% endif %}
[{% if c.is_banned %}Removed by admins{% elif c.deleted_utc > 0 %}Deleted by author{% elif c.is_blocking %}You are blocking @{{c.author.username}}{% elif c.is_blocked %}This user has blocked you{% endif %}]
<span class="comment-collapse d-md-none" onclick="collapse_comment('{{c.id}}')"></span>
{% if standalone and c.over_18 %}<span class="badge badge-danger">+18</span> {% endif %}
[{% if c.is_banned %}Removed by admins{% elif c.deleted_utc > 0 %}Deleted by author{% elif c.is_blocking %}You are blocking @{{c.author.username}}{% elif c.is_blocked %}This user has blocked you{% endif %}]
</div>
@ -434,10 +431,10 @@
<input id="file-upload-reply-{{c.fullname}}" type="file" name="file" accept="image/*" onchange="document.getElementById('filename-show-reply-{{c.id}}').innerHTML='image';" hidden>
</label>
<a href="javascript:void(0)" onclick="document.getElementById('reply-to-{{c.id}}').classList.add('d-none')" class="d-none d-md-block btn btn-link text-muted ml-auto cancel-form">Cancel</a>
<a id="save-reply-to-{{c.fullname}}" class="d-none d-md-block btn btn-primary text-white ml-2" onclick="post_comment('{{c.fullname}}');" href="javascript:void(0)">Comment</a>
<a id="save-reply-to-{{c.fullname}}" class="d-none d-md-block btn btn-primary text-white ml-2" onclick="post_comment('{{c.fullname}}', '{{c.post.id}}');" href="javascript:void(0)">Comment</a>
</div>
<a href="javascript:void(0)" onclick="document.getElementById('reply-to-{{c.id}}').classList.add('d-none')" class="d-block d-md-none btn btn-link text-muted ml-auto cancel-form">Cancel</a>
<a id="save-reply-to-{{c.fullname}}" class="d-block d-md-none btn btn-primary text-white ml-2" onclick="post_comment('{{c.fullname}}');" href="javascript:void(0)">Comment</a>
<a id="save-reply-to-{{c.fullname}}" class="d-block d-md-none btn btn-primary text-white ml-2" onclick="post_comment('{{c.fullname}}', '{{c.post.id}}');" href="javascript:void(0)">Comment</a>
</form>
</div>
@ -551,6 +548,13 @@
</div>
<script>
(() => {
const date = new Date('{{c.created_datetime}}');
document.getElementById('timestamp-{{c.id}}').title = date.toString();
})()
</script>
{% endif %}
{% endmacro %}

View File

@ -0,0 +1,80 @@
<script>
// only allows the script to execute once
if (typeof showNewCommentCounts === 'undefined') {
// localstorage comment counts format: {"<postId>": {c: <totalComments>, t: <timestampUpdated>}}
const COMMENT_COUNTS_ID = "comment-counts"
/**
* Display the number of new comments present since the last time the post was opened
*/
function showNewCommentCounts(postId, newTotal) {
const comments = JSON.parse(localStorage.getItem(COMMENT_COUNTS_ID)) || {}
const lastCount = comments[postId]
if (lastCount) {
const newComments = newTotal - lastCount.c
if (newComments > 0) {
document.querySelectorAll(`#post-${postId} .new-comments`).forEach(elem => {
elem.textContent = ` (+${newComments})`
elem.classList.remove("d-none")
})
}
}
}
function incrementCommentCount(postId) {
saveCommentsCount(postId)
}
/**
* 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)) || {}
const newTotal = lastTotalComs || ((comments[postId] || { c: 0 }).c + 1)
comments[postId] = { c: newTotal, t: Date.now() }
window.localStorage.setItem(COMMENT_COUNTS_ID, JSON.stringify(comments))
}
/**
* 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()
}
</script>

View File

@ -33,15 +33,6 @@
</script>
{% endif %}
<script>
var date = new Date('{{p.created_datetime}}');
document.getElementById('timestamp').title = date.toString();
{% if p.edited_utc %}
var date = new Date('{{p.edited_datetime}}');
document.getElementById('edited_timestamp').title = date.toString();
{% endif %}
</script>
{% if p.award_count("shit") %}
<script src="/assets/js/bug-min.js"></script>
{% set minbugs = 10*p.award_count("shit") if p.award_count("shit") < 3 else 20 %}
@ -73,6 +64,8 @@
</script>
{% endif %}
{% include 'embeds/new_comments_count.html' %}
<meta charset="utf-8" />
<meta property="og:type" content="article" />
@ -282,7 +275,7 @@
<a {% if v %}href="{{p.author.url}}"{% else %}href="/logged_out{{p.author.url}}"{% endif %} style="color: #{{p.author.namecolor}}; font-weight: bold;" class="user-name"><img loading="lazy" src="{{p.author.profile_url}}" class="profile-pic-25 mr-2"/><span {% if p.author.patron and not p.distinguish_level %}class="patron" style="background-color:#{{p.author.namecolor}};"{% elif p.distinguish_level and 'rdrama' in request.host %}class="mod"{% endif %}>{{p.author.username}}</span></a>{% if p.author.customtitle %}&nbsp;<bdi style="color: #{{p.author.titlecolor}}">&nbsp;{% if p.author.quadrant %}<img loading="lazy" height="20" src="/assets/images/PCM/quadrants/{{p.author.quadrant}}.webp">{% endif %}{{p.author.customtitle | safe}}</bdi>{% endif %}
<span data-bs-toggle="tooltip" data-bs-placement="bottom" id="timestamp" title="">&nbsp;{{p.age_string}}</span>
({% if p.realurl(v) %}<a href="/search/posts/?q=domain%3A{{p.domain}}&sort=new&t=all" {% if not v or v.newtabexternal %}target="_blank"{% endif %}>{{p.domain}}</a>{% else %}text post{% endif %})
{% if p.edited_utc %}&nbsp;&nbsp;Edited <span data-bs-toggle="tooltip" data-bs-placement="bottom" id="edited_timestamp" title="">{{p.edited_string}}</span>{% endif %}
&nbsp;&nbsp;{{p.views}} views
</div>
@ -402,7 +395,12 @@
<div class="post-actions mt-2">
<ul class="list-inline text-right d-flex">
<li class="list-inline-item"><a {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}><i class="fas fa-comment-dots"></i>{{p.comment_count}}</a></li>
<li class="list-inline-item">
<a {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}>
<i class="fas fa-comment-dots"></i>{{p.comment_count}}
<span class="text-info d-none new-comments"></span>
</a>
</li>
{% if v and v.id==p.author_id %}
{% if p.private %}
@ -540,7 +538,12 @@
<div class="post-actions">
<ul class="list-inline text-right d-flex">
<li class="list-inline-item mr-auto"><a {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}><i class="fas fa-comment-dots"></i>{{p.comment_count}}</a></li>
<li class="list-inline-item mr-auto">
<a {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}>
<i class="fas fa-comment-dots"></i>{{p.comment_count}}
<span class="text-info d-none new-comments"></span>
</a>
</li>
<li class="list-inline-item"><a href="javascript:void(0);" role="button" class="copy-link" data-clipboard-text="{% if 'rdrama' in request.host %}https://taytay.life{{p.permalink}}{% else %}{{p.permalink | full_link}}{% endif %}"><i class="fas fa-link"></i>Copy link</a></li>
{% if v %}
@ -577,6 +580,25 @@
</div>
<script>
// closure to avoid variable conflicts
(() => {
const date = new Date('{{p.created_datetime}}');
document.getElementById('timestamp').title = date.toString();
{% if p.edited_utc %}
const dateEdited = new Date('{{p.edited_datetime}}');
document.getElementById('edited_timestamp').title = dateEdited.toString();
{% endif %}
{% if (not v or v.highlightcomments) %}
showNewCommentCounts('{{p.id}}', {{p.comment_count}})
{% endif %}
saveCommentsCount('{{p.id}}', {{p.comment_count}})
})()
</script>
<div class="row border-md-0 comment-section pb-3">
<div class="col border-top">
<div class="comments-count py-3">
@ -634,7 +656,7 @@
<input type="checkbox" class="custom-control-input" id="nsfwCheck-{{p.fullname}}" name="over_18" form="reply-to-{{p.fullname}}">
<label class="custom-control-label" for="nsfwCheck">+18</label>
</div>
--> <a id="save-reply-to-{{p.fullname}}" href="javascript:void(0)" form="reply-to-{{p.fullname}}" class="btn btn-primary text-whitebtn ml-auto" onclick="post_comment('{{p.fullname}}')">Comment</a>
--> <a id="save-reply-to-{{p.fullname}}" href="javascript:void(0)" form="reply-to-{{p.fullname}}" class="btn btn-primary text-whitebtn ml-auto" onclick="post_comment('{{p.fullname}}', '{{p.id}}')">Comment</a>
</div>
</form>
</div>

View File

@ -15,16 +15,9 @@
});
</script>
{% for p in listing %}
{% include 'embeds/new_comments_count.html' %}
<script>
var date = new Date('{{p.created_datetime}}');
document.getElementById('timestamp-{{p.id}}').title = date.toString();
{% if p.edited_utc %}
var date = new Date('{{p.edited_datetime}}');
document.getElementById('edited_timestamp-{{p.id}}').title = date.toString();
{% endif %}
</script>
{% for p in listing %}
{% set ups=p.upvotes %}
{% set downs=p.downvotes %}
@ -147,8 +140,12 @@
{% if p.realbody(v) %}
<li class="list-inline-item"><a href="javascript:void(0)" class="text-expand" data-bs-id="{{p.id}}"><i class="fas fa-expand-alt mr-0 text-expand-icon-{{p.id}}"></i></a></li>
{% endif %}
<li class="list-inline-item"><a {% if v and v.newtab %}target="_blank"{% endif %} {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}><i class="fas fa-comment-dots"></i>{{p.comment_count}}</a></li>
<li class="list-inline-item">
<a {% if v and v.newtab %}target="_blank"{% endif %} {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}>
<i class="fas fa-comment-dots"></i>{{p.comment_count}}
<span class="text-info d-none new-comments"></span>
</a>
</li>
<li class="list-inline-item"><a href="/votes?link={{p.fullname}}"><i class="fas fa-arrows-v"></i>Votes</a></li>
{% if v and v.id!=p.author_id %}
@ -233,8 +230,12 @@
<div class="card-footer d-block d-md-none mt-2">
<div class="post-actions">
<ul class="list-inline text-right d-flex">
<li class="list-inline-item mr-auto"><a {% if v and v.newtab %}target="_blank"{% endif %} {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}><i class="fas fa-comment-dots"></i>{{p.comment_count}}</a></li>
<li class="list-inline-item mr-auto">
<a {% if v and v.newtab %}target="_blank"{% endif %} {% if v %}href="{{p.permalink}}"{% else %}href="/logged_out{{p.permalink}}"{% endif %}>
<i class="fas fa-comment-dots"></i>{{p.comment_count}}
<span class="text-info d-none new-comments"></span>
</a>
</li>
<li class="list-inline-item"><a href="javascript:void(0);" role="button" class="copy-link" data-clipboard-text="{% if 'rdrama' in request.host %}https://taytay.life{{p.permalink}}{% else %}{{p.permalink | full_link}}{% endif %}"><i class="fas fa-link"></i>Copy link</a></li>
{% if p.realbody(v) and request.path != "/changelog"%}
@ -395,6 +396,21 @@
</div>
<script>
(() => {
const date = new Date('{{p.created_datetime}}');
document.getElementById('timestamp-{{p.id}}').title = date.toString();
{% if p.edited_utc %}
const dateEdited = new Date('{{p.edited_datetime}}');
document.getElementById('edited_timestamp-{{p.id}}').title = dateEdited.toString();
{% endif %}
{% if (not v or v.highlightcomments) %}
showNewCommentCounts('{{p.id}}', {{p.comment_count}})
{% endif %}
})()
</script>
{% if p.active_flags %}
<div id="flaggers-{{p.id}}" class="flaggers d-none">
<strong><i class="far fa-fw fa-flag"></i> Reported by:</strong>