From a490e5a44e047b8661b18880ed0806cadb3e7c2c Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Mon, 6 Dec 2021 00:52:39 +0200 Subject: [PATCH] sfdfds --- files/assets/js/viewmore.js | 29 ++++++++++++++++++++++++++++ files/routes/posts.py | 32 ++++++++++++++++++++++++++----- files/templates/comments.html | 8 ++++++++ files/templates/submission.html | 34 +-------------------------------- 4 files changed, 65 insertions(+), 38 deletions(-) create mode 100644 files/assets/js/viewmore.js diff --git a/files/assets/js/viewmore.js b/files/assets/js/viewmore.js new file mode 100644 index 000000000..f249a9293 --- /dev/null +++ b/files/assets/js/viewmore.js @@ -0,0 +1,29 @@ +function viewmore(pid,sort,offset) { + btn = document.getElementById("viewbtn"); + btn.disabled = true; + btn.innerHTML = "Requesting..."; + var form = new FormData(); + var xhr = new XMLHttpRequest(); + xhr.open("post", `/viewmore/${pid}/${sort}/${offset}`); + xhr.withCredentials=true; + xhr.onload=function(){ + if (xhr.status==200) { + document.getElementById(`viewmore-${offset}`).innerHTML = xhr.response.replace(/data-src/g, 'src').replace(/data-cfsrc/g, 'src').replace(/style="display:none;visibility:hidden;"/g, ''); + var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')); + tooltipTriggerList.map(function(element){ + return new bootstrap.Tooltip(element); + }); + + const popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]')); + const popoverList = popoverTriggerList.map(function(popoverTriggerEl) { + const popoverId = popoverTriggerEl.getAttribute('data-content-id'); + const contentEl = document.getElementById(popoverId).innerHTML; + return new bootstrap.Popover(popoverTriggerEl, { + content: contentEl, + html: true, + }); + }) + } + } + xhr.send(form) +} \ No newline at end of file diff --git a/files/routes/posts.py b/files/routes/posts.py index 7d068a044..d08357e95 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -190,7 +190,7 @@ def post_id(pid, anything=None, v=None): offset = 0 - if post.comment_count > 60: + if len(comments) > 60: comments2 = [] count = 0 if post.created_utc > 1638672040: @@ -229,6 +229,7 @@ def post_id(pid, anything=None, v=None): @limiter.limit("1/second") @auth_desired def viewmore(v, pid, sort, offset): + offset = int(offset) if v: votes = g.db.query(CommentVote).filter_by(user_id=v.id).subquery() @@ -283,7 +284,7 @@ def viewmore(v, pid, sort, offset): elif sort == "bottom": comments = comments.order_by(Comment.upvotes - Comment.downvotes) - if offset: comments = comments.offset(int(offset)) + comments = comments.offset(offset) comments = [c[0] for c in comments.all()] else: @@ -300,11 +301,32 @@ def viewmore(v, pid, sort, offset): elif sort == "bottom": comments = comments.order_by(Comment.upvotes - Comment.downvotes) - if offset: comments = comments.offset(int(offset)) - + comments = comments.offset(offset) + comments = comments.all() - return render_template("comments.html", v=v, comments=comments, render_replies=True) + if len(comments) > 60: + comments2 = [] + count = 0 + post = get_post(pid, v=v) + if post.created_utc > 1638672040: + for comment in comments: + comments2.append(comment) + count += g.db.query(Comment.id).filter_by(parent_submission=post.id, top_comment_id=comment.id).count() + 1 + offset += 1 + if count > 50: break + else: + for comment in comments: + comments2.append(comment) + count += g.db.query(Comment.id).filter_by(parent_submission=post.id, parent_comment_id=comment.id).count() + 1 + offset += 1 + if count > 10: break + + if len(comments) == len(comments2): offset = None + comments = comments2 + else: offset = None + + return render_template("comments.html", v=v, comments=comments, render_replies=True, pid=pid, sort=sort, offset=offset) @app.post("/edit_post/") diff --git a/files/templates/comments.html b/files/templates/comments.html index 5309c4227..c0c652f7b 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -800,4 +800,12 @@ {% endif %} +{% if offset %} + {% if p %} + {% set pid = p.id %} + {% endif %} +
+
+{% endif %} + \ No newline at end of file diff --git a/files/templates/submission.html b/files/templates/submission.html index cc6bc7009..23f24ac6d 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -825,39 +825,7 @@ {% if offset %} - -
-
+ {% endif %}