From 68da93ec028bf42be0c3062b5e8d6a2ec3974b7c Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sun, 3 Jul 2022 11:11:18 +0200 Subject: [PATCH] refactor "view more comments" --- files/routes/posts.py | 117 ++----------------------- files/templates/comments.html | 8 -- files/templates/submission.html | 50 +++-------- files/templates/submission_banned.html | 44 +++------- 4 files changed, 33 insertions(+), 186 deletions(-) diff --git a/files/routes/posts.py b/files/routes/posts.py index 96909532b..ddb54778c 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -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() comments = first + second - offset = 0 - ids = set() - if v and v.poorcel: threshold = 50 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 = [] 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 > 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 + for comment in comments: + comments2.append(comment) + count += g.db.query(Comment).filter_by(parent_submission=post.id, top_comment_id=comment.id).count() + 1 + if count > threshold: break - if len(comments) == len(comments2): offset = 0 - else: offset = 1 + post.rest = [i for i in comments if i not in comments2] comments = comments2 for pin in pinned: @@ -247,97 +236,7 @@ def post_id(pid, anything=None, v=None, sub=None): else: 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" - 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']) - -@app.get("/viewmore///") -@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) + return render_template(template, v=v, p=post, sort=sort, render_replies=True, sub=post.subr, fart=app.config['SETTINGS']['Fart mode']) @app.get("/morecomments/") diff --git a/files/templates/comments.html b/files/templates/comments.html index e09cdf131..5a2fced0f 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -923,12 +923,4 @@ {% endif %} -{% if offset %} - {% if p %} - {% set pid = p.id %} - {% endif %} -
-
-{% endif %} - diff --git a/files/templates/submission.html b/files/templates/submission.html index 7a1bb6e02..303b23093 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -1111,42 +1111,6 @@ {% endwith %} -{% if offset %} - -{% endif %} - {% elif not p.replies and p.deleted_utc == 0 %}
@@ -1216,4 +1180,18 @@ {% endif %} })() + + +{% if p.rest %} + + +
+ {% with comments=p.rest %} + {% include "comments.html" %} + {% endwith %} +
+{% endif %} + {% endblock %} \ No newline at end of file diff --git a/files/templates/submission_banned.html b/files/templates/submission_banned.html index 442cc15e2..3fc62d33f 100644 --- a/files/templates/submission_banned.html +++ b/files/templates/submission_banned.html @@ -74,40 +74,18 @@ {% endwith %}
-{% if offset %} - + +{% if p.rest %} + + +
+ {% with comments=p.rest %} + {% include "comments.html" %} + {% endwith %} +
{% endif %} {% endblock %}