From ed86ec1c1435f50db4733d738839fa0bd2c11002 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sun, 3 Jul 2022 13:56:40 +0200 Subject: [PATCH] more query optimization v3 --- files/classes/user.py | 9 +++------ files/helpers/get.py | 25 +++++++++++++++++++++---- files/routes/users.py | 26 +++++++++++++++++++------- files/templates/comments.html | 10 ++++++---- files/templates/userpage.html | 4 ++-- 5 files changed, 51 insertions(+), 23 deletions(-) diff --git a/files/classes/user.py b/files/classes/user.py index fc7b86fa28..051add2070 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -128,7 +128,7 @@ class User(Base): custom_filter_list = Column(String) discord_id = Column(String) ban_evade = Column(Integer, default=0) - original_username = deferred(Column(String)) + original_username = Column(String) referred_by = Column(Integer, ForeignKey("users.id")) can_gamble = Column(Boolean, default=True) currently_held_lottery_tickets = Column(Integer, default=0) @@ -433,11 +433,8 @@ class User(Base): awards = {} - posts_idlist = [x[0] for x in g.db.query(Submission.id).filter_by(author_id=self.id).all()] - comments_idlist = [x[0] for x in g.db.query(Comment.id).filter_by(author_id=self.id).all()] - - post_awards = g.db.query(AwardRelationship).filter(AwardRelationship.submission_id.in_(posts_idlist)).all() - comment_awards = g.db.query(AwardRelationship).filter(AwardRelationship.comment_id.in_(comments_idlist)).all() + post_awards = g.db.query(AwardRelationship).join(AwardRelationship.post).filter(Submission.author_id == self.id).all() + comment_awards = g.db.query(AwardRelationship).join(AwardRelationship.comment).filter(Comment.author_id == self.id).all() total_awards = post_awards + comment_awards diff --git a/files/helpers/get.py b/files/helpers/get.py index b413e4a512..b2bfeaff83 100644 --- a/files/helpers/get.py +++ b/files/helpers/get.py @@ -1,6 +1,6 @@ from files.classes import * from flask import g -from sqlalchemy.orm import joinedload +from sqlalchemy.orm import joinedload, undefer def get_id(username, v=None, graceful=False): @@ -24,7 +24,7 @@ def get_id(username, v=None, graceful=False): return user[0] -def get_user(username, v=None, graceful=False): +def get_user(username, v=None, graceful=False, rendered=False): if not username: if not graceful: abort(404) @@ -39,13 +39,25 @@ def get_user(username, v=None, graceful=False): User.username.ilike(username), User.original_username.ilike(username) ) - ).one_or_none() + ) + + if rendered: + user = user.options( + undefer('reserved'), + undefer('profilecss'), + undefer('bio'), + undefer('friends_html'), + undefer('enemies_html'), + joinedload(User.badges) + ) + + user = user.one_or_none() if not user: if not graceful: abort(404) else: return None - if v: + if rendered v and v.id != user.id: block = g.db.query(UserBlock).filter( or_( and_( @@ -289,6 +301,11 @@ def get_comments(cids, v=None, load_parent=False): blocked, blocked.c.user_id == Comment.author_id, isouter=True + ).options( + joinedload(Comment.post), + joinedload(Comment.flags), + joinedload(Comment.awards), + joinedload(Comment.options).joinedload(CommentOption.votes) ).all() output = [] diff --git a/files/routes/users.py b/files/routes/users.py index e9062f751b..4152c4d60b 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -872,7 +872,14 @@ def u_username(username, v=None): if not v and not request.path.startswith('/logged_out'): return redirect(f"/logged_out{request.full_path}") if v and request.path.startswith('/logged_out'): return redirect(request.full_path.replace('/logged_out','')) - u = get_user(username, v=v) + u = get_user(username, v=v, rendered=True) + + if v and username == v.username: + is_following = False + u.is_blocked = False + u.is_blocking = False + else: + is_following = (v and u.has_follower(v)) if username != u.username: @@ -885,7 +892,7 @@ def u_username(username, v=None): if u.shadowbanned and not (v and v.admin_level >= 2) and not (v and v.id == u.id): abort(404) - if v and v.id not in (u.id,DAD_ID) and (u.patron or u.admin_level > 1): + if v and v.id not in (u.id, DAD_ID) and (u.patron or u.admin_level > 1): view = g.db.query(ViewerRelationship).filter_by(viewer_id=v.id, user_id=u.id).one_or_none() if view: view.last_view_utc = int(time.time()) @@ -939,7 +946,7 @@ def u_username(username, v=None): sort=sort, t=t, next_exists=next_exists, - is_following=(v and u.has_follower(v))) + is_following=is_following) @@ -952,7 +959,7 @@ def u_username(username, v=None): sort=sort, t=t, next_exists=next_exists, - is_following=(v and u.has_follower(v))) + is_following=is_following) @app.get("/@/comments") @@ -963,7 +970,14 @@ def u_username_comments(username, v=None): if not v and not request.path.startswith('/logged_out'): return redirect(f"/logged_out{request.full_path}") if v and request.path.startswith('/logged_out'): return redirect(request.full_path.replace('/logged_out','')) - user = get_user(username, v=v) + user = get_user(username, v=v, rendered=True) + + if v and username == v.username: + is_following = False + user.is_blocked = False + user.is_blocking = False + else: + is_following = (v and user.has_follower(v)) if username != user.username: return redirect(f'/@{user.username}/comments') @@ -1029,8 +1043,6 @@ def u_username_comments(username, v=None): listing = get_comments(ids, v=v) - is_following = (v and user.has_follower(v)) - if request.headers.get("Authorization"): return {"data": [c.json for c in listing]} return render_template("userpage_comments.html", u=user, v=v, listing=listing, page=page, sort=sort, t=t,next_exists=next_exists, is_following=is_following, standalone=True) diff --git a/files/templates/comments.html b/files/templates/comments.html index 46bece0ad9..3ec3b2a149 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -57,10 +57,12 @@ {% set downs=c.downvotes %} {% set score=ups-downs %} -{% if v and (v.shadowbanned or v.admin_level >= 2) %} - {% set replies=c.replies3(sort) %} -{% else %} - {% set replies=c.replies(sort) %} +{% if render_replies %} + {% if v and (v.shadowbanned or v.admin_level >= 2) %} + {% set replies=c.replies3(sort) %} + {% else %} + {% set replies=c.replies(sort) %} + {% endif %} {% endif %} {% if c.is_blocking or (c.is_banned or c.deleted_utc) and not (v and v.admin_level > 1) and not (v and v.id==c.author_id) %} diff --git a/files/templates/userpage.html b/files/templates/userpage.html index 6690d9885c..87f8924912 100644 --- a/files/templates/userpage.html +++ b/files/templates/userpage.html @@ -77,7 +77,7 @@ {% endif %} - {% if v and v.has_follower(u) %} + {% if v and v.id != u.id and v.has_follower(u) %} Follows you {% endif %}
@@ -405,7 +405,7 @@ {% endif %} - {% if v and v.has_follower(u) and not v.is_nofollow %} + {% if v and v.id != u.id and v.has_follower(u) and not v.is_nofollow %} Follows you {% endif %} {% if u.customtitle %}

{{u.customtitle | safe}}