forked from rDrama/rDrama
refactor "view more comments"
parent
45437997c8
commit
68da93ec02
|
@ -206,30 +206,19 @@ def post_id(pid, anything=None, v=None, sub=None):
|
||||||
second = comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body_html) <= 100).all()
|
second = comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body_html) <= 100).all()
|
||||||
comments = first + second
|
comments = first + second
|
||||||
|
|
||||||
offset = 0
|
|
||||||
ids = set()
|
|
||||||
|
|
||||||
if v and v.poorcel: threshold = 50
|
if v and v.poorcel: threshold = 50
|
||||||
else: threshold = 100
|
else: threshold = 100
|
||||||
|
|
||||||
if post.comment_count > 60 and not request.headers.get("Authorization") and not request.values.get("all"):
|
post.rest = []
|
||||||
|
if post.comment_count > threshold+25 and not request.headers.get("Authorization"):
|
||||||
comments2 = []
|
comments2 = []
|
||||||
count = 0
|
count = 0
|
||||||
if post.created_utc > 1638672040:
|
for comment in comments:
|
||||||
for comment in comments:
|
comments2.append(comment)
|
||||||
comments2.append(comment)
|
count += g.db.query(Comment).filter_by(parent_submission=post.id, top_comment_id=comment.id).count() + 1
|
||||||
ids.add(comment.id)
|
if count > threshold: break
|
||||||
count += g.db.query(Comment).filter_by(parent_submission=post.id, top_comment_id=comment.id).count() + 1
|
|
||||||
if count > 50: break
|
|
||||||
else:
|
|
||||||
for comment in comments:
|
|
||||||
comments2.append(comment)
|
|
||||||
ids.add(comment.id)
|
|
||||||
count += g.db.query(Comment).filter_by(parent_submission=post.id, parent_comment_id=comment.id).count() + 1
|
|
||||||
if count > 20: break
|
|
||||||
|
|
||||||
if len(comments) == len(comments2): offset = 0
|
post.rest = [i for i in comments if i not in comments2]
|
||||||
else: offset = 1
|
|
||||||
comments = comments2
|
comments = comments2
|
||||||
|
|
||||||
for pin in pinned:
|
for pin in pinned:
|
||||||
|
@ -247,97 +236,7 @@ def post_id(pid, anything=None, v=None, sub=None):
|
||||||
else:
|
else:
|
||||||
if post.is_banned and not (v and (v.admin_level > 1 or post.author_id == v.id)): template = "submission_banned.html"
|
if post.is_banned and not (v and (v.admin_level > 1 or post.author_id == v.id)): template = "submission_banned.html"
|
||||||
else: template = "submission.html"
|
else: template = "submission.html"
|
||||||
return render_template(template, v=v, p=post, ids=list(ids), sort=sort, render_replies=True, offset=offset, sub=post.subr, fart=app.config['SETTINGS']['Fart mode'])
|
return render_template(template, v=v, p=post, sort=sort, render_replies=True, sub=post.subr, fart=app.config['SETTINGS']['Fart mode'])
|
||||||
|
|
||||||
@app.get("/viewmore/<pid>/<sort>/<offset>")
|
|
||||||
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
||||||
@auth_desired
|
|
||||||
def viewmore(v, pid, sort, offset):
|
|
||||||
try: pid = int(pid)
|
|
||||||
except: abort(400)
|
|
||||||
post = get_post(pid, v=v)
|
|
||||||
if post.club and not (v and (v.paid_dues or v.id == post.author_id)): abort(403)
|
|
||||||
|
|
||||||
offset = int(offset)
|
|
||||||
try: ids = set(int(x) for x in request.values.get("ids").split(','))
|
|
||||||
except: abort(400)
|
|
||||||
|
|
||||||
if v:
|
|
||||||
votes = g.db.query(CommentVote.vote_type, CommentVote.comment_id).filter_by(user_id=v.id).subquery()
|
|
||||||
|
|
||||||
blocking = v.blocking.subquery()
|
|
||||||
|
|
||||||
blocked = v.blocked.subquery()
|
|
||||||
|
|
||||||
comments = g.db.query(
|
|
||||||
Comment,
|
|
||||||
votes.c.vote_type,
|
|
||||||
blocking.c.target_id,
|
|
||||||
blocked.c.target_id,
|
|
||||||
).filter(Comment.parent_submission == pid, Comment.stickied == None, Comment.id.notin_(ids))
|
|
||||||
|
|
||||||
if not (v and v.shadowbanned) and not (v and v.admin_level >= 2):
|
|
||||||
comments = comments.join(Comment.author).filter(User.shadowbanned == None)
|
|
||||||
|
|
||||||
comments=comments.join(
|
|
||||||
votes,
|
|
||||||
votes.c.comment_id == Comment.id,
|
|
||||||
isouter=True
|
|
||||||
).join(
|
|
||||||
blocking,
|
|
||||||
blocking.c.target_id == Comment.author_id,
|
|
||||||
isouter=True
|
|
||||||
).join(
|
|
||||||
blocked,
|
|
||||||
blocked.c.user_id == Comment.author_id,
|
|
||||||
isouter=True
|
|
||||||
)
|
|
||||||
|
|
||||||
output = []
|
|
||||||
for c in comments.all():
|
|
||||||
comment = c[0]
|
|
||||||
comment.voted = c[1] or 0
|
|
||||||
comment.is_blocking = c[2] or 0
|
|
||||||
comment.is_blocked = c[3] or 0
|
|
||||||
output.append(comment)
|
|
||||||
|
|
||||||
comments = comments.filter(Comment.level == 1)
|
|
||||||
|
|
||||||
comments = sort_comments(sort, comments)
|
|
||||||
|
|
||||||
first = [c[0] for c in comments.filter(or_(and_(Comment.slots_result == None, Comment.blackjack_result == None, Comment.wordle_result == None), func.length(Comment.body_html) > 100)).all()]
|
|
||||||
second = [c[0] for c in comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body_html) <= 100).all()]
|
|
||||||
comments = first + second
|
|
||||||
else:
|
|
||||||
comments = g.db.query(Comment).join(Comment.author).filter(User.shadowbanned == None, Comment.parent_submission == pid, Comment.level == 1, Comment.stickied == None, Comment.id.notin_(ids))
|
|
||||||
|
|
||||||
comments = sort_comments(sort, comments)
|
|
||||||
|
|
||||||
first = comments.filter(or_(and_(Comment.slots_result == None, Comment.blackjack_result == None, Comment.wordle_result == None), func.length(Comment.body_html) > 100)).all()
|
|
||||||
second = comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body_html) <= 100).all()
|
|
||||||
comments = first + second
|
|
||||||
comments = comments[offset:]
|
|
||||||
|
|
||||||
comments2 = []
|
|
||||||
count = 0
|
|
||||||
if post.created_utc > 1638672040:
|
|
||||||
for comment in comments:
|
|
||||||
comments2.append(comment)
|
|
||||||
ids.add(comment.id)
|
|
||||||
count += g.db.query(Comment).filter_by(parent_submission=post.id, top_comment_id=comment.id).count() + 1
|
|
||||||
if count > 100: break
|
|
||||||
else:
|
|
||||||
for comment in comments:
|
|
||||||
comments2.append(comment)
|
|
||||||
ids.add(comment.id)
|
|
||||||
count += g.db.query(Comment).filter_by(parent_submission=post.id, parent_comment_id=comment.id).count() + 1
|
|
||||||
if count > 20: break
|
|
||||||
|
|
||||||
if len(comments) == len(comments2): offset = 0
|
|
||||||
else: offset += 1
|
|
||||||
comments = comments2
|
|
||||||
|
|
||||||
return render_template("comments.html", v=v, comments=comments, p=post, ids=list(ids), render_replies=True, pid=pid, sort=sort, offset=offset, ajax=True)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/morecomments/<cid>")
|
@app.get("/morecomments/<cid>")
|
||||||
|
|
|
@ -923,12 +923,4 @@
|
||||||
</script>
|
</script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if offset %}
|
|
||||||
{% if p %}
|
|
||||||
{% set pid = p.id %}
|
|
||||||
{% endif %}
|
|
||||||
<br>
|
|
||||||
<div id="viewmore-{{offset}}"><button id="viewbtn" class="btn btn-primary" onclick="viewmore({{pid}},'{{sort}}',{{offset}},{{ids}})">VIEW MORE COMMENTS</a></div>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -1111,42 +1111,6 @@
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% if offset %}
|
|
||||||
<script>
|
|
||||||
function viewmore(pid,sort,offset,ids) {
|
|
||||||
btn = document.getElementById("viewbtn");
|
|
||||||
btn.disabled = true;
|
|
||||||
btn.innerHTML = "Requesting...";
|
|
||||||
var form = new FormData();
|
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
xhr.open("get", `/viewmore/${pid}/${sort}/${offset}?ids=${ids}`);
|
|
||||||
xhr.setRequestHeader('xhr', 'xhr');
|
|
||||||
xhr.onload=function(){
|
|
||||||
if (xhr.status==200) {
|
|
||||||
let e = document.getElementById(`viewmore-${offset}`);
|
|
||||||
e.innerHTML = xhr.response.replace(/data-src/g, 'src').replace(/data-cfsrc/g, 'src').replace(/style="display:none;visibility:hidden;"/g, '');
|
|
||||||
bs_trigger(e)
|
|
||||||
|
|
||||||
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')}
|
|
||||||
catch(e) {}
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
btn.disabled = false;
|
|
||||||
}
|
|
||||||
xhr.send(form)
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% elif not p.replies and p.deleted_utc == 0 %}
|
{% elif not p.replies and p.deleted_utc == 0 %}
|
||||||
<div class="comment-section text-center py-7">
|
<div class="comment-section text-center py-7">
|
||||||
<span class="fa-stack fa-2x text-muted mb-4">
|
<span class="fa-stack fa-2x text-muted mb-4">
|
||||||
|
@ -1216,4 +1180,18 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
})()
|
})()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
{% if p.rest %}
|
||||||
|
<button class="mt-4 mb-3 btn btn-primary" onclick="document.getElementById('rest').classList.toggle('d-none')">
|
||||||
|
VIEW MORE COMMENTS
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div id="rest" class="d-none">
|
||||||
|
{% with comments=p.rest %}
|
||||||
|
{% include "comments.html" %}
|
||||||
|
{% endwith %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -74,40 +74,18 @@
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% if offset %}
|
|
||||||
<script>
|
|
||||||
function viewmore(pid,sort,offset,ids) {
|
|
||||||
btn = document.getElementById("viewbtn");
|
|
||||||
btn.disabled = true;
|
|
||||||
btn.innerHTML = "Requesting...";
|
|
||||||
var form = new FormData();
|
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
xhr.open("get", `/viewmore/${pid}/${sort}/${offset}?ids=${ids}`);
|
|
||||||
xhr.setRequestHeader('xhr', 'xhr');
|
|
||||||
xhr.onload=function(){
|
|
||||||
if (xhr.status==200) {
|
|
||||||
let e = document.getElementById(`viewmore-${offset}`);
|
|
||||||
e.innerHTML = xhr.response.replace(/data-src/g, 'src').replace(/data-cfsrc/g, 'src').replace(/style="display:none;visibility:hidden;"/g, '');
|
|
||||||
bs_trigger(e)
|
|
||||||
|
|
||||||
comments = JSON.parse(localStorage.getItem("old-comment-counts")) || {}
|
|
||||||
lastCount = comments['{{p.id}}']
|
{% if p.rest %}
|
||||||
if (lastCount)
|
<button class="mt-4 mb-3 btn btn-primary" onclick="document.getElementById('rest').classList.toggle('d-none')">
|
||||||
{
|
VIEW MORE COMMENTS
|
||||||
{% for c in p.comments %}
|
</button>
|
||||||
{% if not (v and v.id==c.author_id) and not c.voted %}
|
|
||||||
if ({{c.created_utc*1000}} > lastCount.t)
|
<div id="rest" class="d-none">
|
||||||
try {document.getElementById("comment-{{c.id}}-only").classList.add('unread')}
|
{% with comments=p.rest %}
|
||||||
catch(e) {}
|
{% include "comments.html" %}
|
||||||
{% endif %}
|
{% endwith %}
|
||||||
{% endfor %}
|
</div>
|
||||||
}
|
|
||||||
}
|
|
||||||
btn.disabled = false;
|
|
||||||
}
|
|
||||||
xhr.send(form)
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Loading…
Reference in New Issue