more query optimization v3

remotes/1693045480750635534/spooky-22
Aevann1 2022-07-03 13:56:40 +02:00
parent 3e87b20ed0
commit ed86ec1c14
5 changed files with 51 additions and 23 deletions

View File

@ -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

View File

@ -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 = []

View File

@ -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("/@<username>/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)

View File

@ -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) %}

View File

@ -77,7 +77,7 @@
<i class="fas fa-broom text-admin align-middle ml-2" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Admin"></i>
</span>
{% endif %}
{% if v and v.has_follower(u) %}
{% if v and v.id != u.id and v.has_follower(u) %}
<span class="followsyou badge badge-secondary text-small align-middle ml-2" id="profile--follows-you">Follows you</span>
{% endif %}
<div class="profile-actions align-middle d-none ml-2">
@ -405,7 +405,7 @@
<i class="fas fa-broom text-admin align-middle ml-1" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Admin"></i>
</span>
{% 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 %}
<span class="followsyou badge badge-secondary text-small align-middle mx-1" id="profile--follows-you">Follows you</span>
{% endif %}
{% if u.customtitle %}<p style="color: #{{u.titlecolor}}" id="profile--flair">{{u.customtitle | safe}}</p>