From 729e4a4c62f46005e31ea331f02824cf45c94012 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Wed, 16 Feb 2022 03:16:01 +0200 Subject: [PATCH] g --- files/classes/clients.py | 5 ++--- files/classes/comment.py | 7 +++---- files/classes/flags.py | 10 ++++------ files/classes/submission.py | 2 +- files/classes/subscriptions.py | 7 +++---- files/classes/user.py | 6 +++--- files/classes/votes.py | 5 ++--- files/helpers/alerts.py | 4 ++-- files/routes/front.py | 6 +++--- files/routes/reporting.py | 4 ++-- files/routes/static.py | 8 ++++---- files/routes/users.py | 10 +++++----- files/routes/votes.py | 26 +++++++++++++------------- 13 files changed, 47 insertions(+), 53 deletions(-) diff --git a/files/classes/clients.py b/files/classes/clients.py index 982668706..b07e5db94 100644 --- a/files/classes/clients.py +++ b/files/classes/clients.py @@ -66,10 +66,9 @@ class ClientAuth(Base): __tablename__ = "client_auths" - id = Column(Integer, primary_key=True) - oauth_client = Column(Integer, ForeignKey("oauth_apps.id")) + user_id = Column(Integer, ForeignKey("users.id"), primary_key=True) + oauth_client = Column(Integer, ForeignKey("oauth_apps.id"), primary_key=True) access_token = Column(String) - user_id = Column(Integer, ForeignKey("users.id")) user = relationship("User", viewonly=True) application = relationship("OauthApp", viewonly=True) diff --git a/files/classes/comment.py b/files/classes/comment.py index 5e7075ecd..41fc2af84 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -73,7 +73,7 @@ class Comment(Base): @property @lazy def flags(self): - return g.db.query(CommentFlag).filter_by(comment_id=self.id).order_by(CommentFlag.id) + return g.db.query(CommentFlag).filter_by(comment_id=self.id).order_by(CommentFlag.created_utc) @lazy def poll_voted(self, v): @@ -446,9 +446,8 @@ class Notification(Base): __tablename__ = "notifications" - id = Column(Integer, primary_key=True) - user_id = Column(Integer, ForeignKey("users.id")) - comment_id = Column(Integer, ForeignKey("comments.id")) + user_id = Column(Integer, ForeignKey("users.id"), primary_key=True) + comment_id = Column(Integer, ForeignKey("comments.id"), primary_key=True) read = Column(Boolean, default=False) created_utc = Column(Integer) diff --git a/files/classes/flags.py b/files/classes/flags.py index 11ea71260..a94990994 100644 --- a/files/classes/flags.py +++ b/files/classes/flags.py @@ -9,9 +9,8 @@ class Flag(Base): __tablename__ = "flags" - id = Column(Integer, primary_key=True) - post_id = Column(Integer, ForeignKey("submissions.id")) - user_id = Column(Integer, ForeignKey("users.id")) + post_id = Column(Integer, ForeignKey("submissions.id"), primary_key=True) + user_id = Column(Integer, ForeignKey("users.id"), primary_key=True) reason = Column(String) created_utc = Column(Integer) @@ -43,9 +42,8 @@ class CommentFlag(Base): __tablename__ = "commentflags" - id = Column(Integer, primary_key=True) - user_id = Column(Integer, ForeignKey("users.id")) - comment_id = Column(Integer, ForeignKey("comments.id")) + comment_id = Column(Integer, ForeignKey("comments.id"), primary_key=True) + user_id = Column(Integer, ForeignKey("users.id"), primary_key=True) reason = Column(String) created_utc = Column(Integer) diff --git a/files/classes/submission.py b/files/classes/submission.py index ca1ab5f4b..0aacb5870 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -81,7 +81,7 @@ class Submission(Base): @property @lazy def flags(self): - return g.db.query(Flag).filter_by(post_id=self.id).order_by(Flag.id) + return g.db.query(Flag).filter_by(post_id=self.id).order_by(Flag.created_utc) @property @lazy diff --git a/files/classes/subscriptions.py b/files/classes/subscriptions.py index fa06e8d9c..5193e7740 100644 --- a/files/classes/subscriptions.py +++ b/files/classes/subscriptions.py @@ -1,7 +1,7 @@ from sqlalchemy import * from sqlalchemy.orm import relationship from files.__main__ import Base - +import time class Subscription(Base): __tablename__ = "subscriptions" @@ -19,9 +19,8 @@ class Subscription(Base): class Follow(Base): __tablename__ = "follows" - id = Column(Integer, primary_key=True) - user_id = Column(Integer, ForeignKey("users.id")) - target_id = Column(Integer, ForeignKey("users.id")) + target_id = Column(Integer, ForeignKey("users.id"), primary_key=True) + user_id = Column(Integer, ForeignKey("users.id"), primary_key=True) created_utc = Column(Integer) user = relationship("User", uselist=False, primaryjoin="User.id==Follow.user_id", viewonly=True) diff --git a/files/classes/user.py b/files/classes/user.py index 6463efdcc..7220d0581 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -294,7 +294,7 @@ class User(Base): @property @lazy def follow_count(self): - return g.db.query(Follow.id).filter_by(user_id=self.id).count() + return g.db.query(Follow.target_id).filter_by(user_id=self.id).count() @property @lazy @@ -399,12 +399,12 @@ class User(Base): @property @lazy def notifications_count(self): - return g.db.query(Notification.id).join(Comment).filter(Notification.user_id == self.id, Notification.read == False, Comment.is_banned == False, Comment.deleted_utc == 0).count() + return g.db.query(Notification.user_id).join(Comment).filter(Notification.user_id == self.id, Notification.read == False, Comment.is_banned == False, Comment.deleted_utc == 0).count() @property @lazy def post_notifications_count(self): - return g.db.query(Notification.id).join(Comment).filter(Notification.user_id == self.id, Notification.read == False, Comment.author_id == AUTOJANNY_ID).count() + return g.db.query(Notification.user_id).join(Comment).filter(Notification.user_id == self.id, Notification.read == False, Comment.author_id == AUTOJANNY_ID).count() @property @lazy diff --git a/files/classes/votes.py b/files/classes/votes.py index 5cb36ec18..cd452cf9a 100644 --- a/files/classes/votes.py +++ b/files/classes/votes.py @@ -51,10 +51,9 @@ class CommentVote(Base): __tablename__ = "commentvotes" - id = Column(Integer, primary_key=True) - user_id = Column(Integer, ForeignKey("users.id")) + comment_id = Column(Integer, ForeignKey("comments.id"), primary_key=True) + user_id = Column(Integer, ForeignKey("users.id"), primary_key=True) vote_type = Column(Integer) - comment_id = Column(Integer, ForeignKey("comments.id")) app_id = Column(Integer, ForeignKey("oauth_apps.id")) real = Column(Boolean, default=True) created_utc = Column(Integer) diff --git a/files/helpers/alerts.py b/files/helpers/alerts.py index 24c41a66c..2ea661e70 100644 --- a/files/helpers/alerts.py +++ b/files/helpers/alerts.py @@ -27,7 +27,7 @@ def send_repeatable_notification(uid, text, autojanny=False): if existing_comment: cid = existing_comment[0] - existing_notif = g.db.query(Notification.id).filter_by(user_id=uid, comment_id=cid).one_or_none() + existing_notif = g.db.query(Notification.user_id).filter_by(user_id=uid, comment_id=cid).one_or_none() if existing_notif: cid = create_comment(text_html, autojanny) else: cid = create_comment(text_html, autojanny) @@ -55,7 +55,7 @@ def notif_comment(text, autojanny=False): def add_notif(cid, uid): - existing = g.db.query(Notification.id).filter_by(comment_id=cid, user_id=uid).one_or_none() + existing = g.db.query(Notification.user_id).filter_by(comment_id=cid, user_id=uid).one_or_none() if not existing: notif = Notification(comment_id=cid, user_id=uid) g.db.add(notif) diff --git a/files/routes/front.py b/files/routes/front.py index 2db575833..bfe918774 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -23,7 +23,7 @@ def unread(v): Comment.is_banned == False, Comment.deleted_utc == 0, Comment.author_id != AUTOJANNY_ID, - ).order_by(Notification.id.desc()).all() + ).order_by(Notification.created_utc.desc()).all() for n in v.notifications.filter_by(read=False).all(): n.read = True @@ -50,7 +50,7 @@ def notifications(v): next_exists = (len(comments) > 25) comments = comments[:25] elif posts: - notifications = v.notifications.join(Notification.comment).filter(Comment.author_id == AUTOJANNY_ID).order_by(Notification.id.desc()).offset(25 * (page - 1)).limit(101).all() + notifications = v.notifications.join(Notification.comment).filter(Comment.author_id == AUTOJANNY_ID).order_by(Notification.created_utc.desc()).offset(25 * (page - 1)).limit(101).all() listing = [] @@ -72,7 +72,7 @@ def notifications(v): Comment.is_banned == False, Comment.deleted_utc == 0, Comment.author_id != AUTOJANNY_ID, - ).order_by(Notification.id.desc()).offset(50 * (page - 1)).limit(51).all() + ).order_by(Notification.created_utc.desc()).offset(50 * (page - 1)).limit(51).all() next_exists = (len(notifications) > 50) notifications = notifications[:50] diff --git a/files/routes/reporting.py b/files/routes/reporting.py index d0b1f732f..0cf01d581 100644 --- a/files/routes/reporting.py +++ b/files/routes/reporting.py @@ -16,7 +16,7 @@ def api_flag_post(pid, v): reason = request.values.get("reason", "").strip()[:100] if not reason.startswith('!'): - existing = g.db.query(Flag.id).filter_by(user_id=v.id, post_id=post.id).one_or_none() + existing = g.db.query(Flag.post_id).filter_by(user_id=v.id, post_id=post.id).one_or_none() if existing: return "", 409 reason = filter_emojis_only(reason) @@ -43,7 +43,7 @@ def api_flag_comment(cid, v): comment = get_comment(cid) if not v.shadowbanned: - existing = g.db.query(CommentFlag.id).filter_by( user_id=v.id, comment_id=comment.id).one_or_none() + existing = g.db.query(CommentFlag.comment_id).filter_by( user_id=v.id, comment_id=comment.id).one_or_none() if existing: return "", 409 reason = request.values.get("reason", "").strip()[:100] diff --git a/files/routes/static.py b/files/routes/static.py index f1982cf33..2a3a7c064 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -89,12 +89,12 @@ def stats(): "removed comments (by admins)": g.db.query(Comment.id).filter_by(is_banned=True).count(), "deleted comments (by author)": g.db.query(Comment.id).filter(Comment.deleted_utc > 0).count(), "comments last_24h": g.db.query(Comment.id).filter(Comment.created_utc > day, Comment.author_id.notin_((AUTOJANNY_ID,NOTIFICATIONS_ID))).count(), - "post votes": g.db.query(Vote.id).count(), + "post votes": g.db.query(CommentVote.submission_id).count(), "post voting users": g.db.query(Vote.user_id).distinct().count(), - "comment votes": g.db.query(CommentVote.id).count(), + "comment votes": g.db.query(CommentVote.comment_id).count(), "comment voting users": g.db.query(CommentVote.user_id).distinct().count(), - "total upvotes": g.db.query(Vote.id).filter_by(vote_type=1).count() + g.db.query(CommentVote.id).filter_by(vote_type=1).count(), - "total downvotes": g.db.query(Vote.id).filter_by(vote_type=-1).count() + g.db.query(CommentVote.id).filter_by(vote_type=-1).count(), + "total upvotes": g.db.query(CommentVote.submission_id).filter_by(vote_type=1).count() + g.db.query(CommentVote.comment_id).filter_by(vote_type=1).count(), + "total downvotes": g.db.query(CommentVote.submission_id).filter_by(vote_type=-1).count() + g.db.query(CommentVote.comment_id).filter_by(vote_type=-1).count(), "total awards": g.db.query(AwardRelationship.id).count(), "awards given": g.db.query(AwardRelationship.id).filter(or_(AwardRelationship.submission_id != None, AwardRelationship.comment_id != None)).count(), "users who posted or commented in the past 7 days": len(active_users) diff --git a/files/routes/users.py b/files/routes/users.py index f498dba02..395a02d6e 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -658,14 +658,14 @@ def redditor_moment_redirect(username, v): @auth_required def followers(username, v): u = get_user(username, v=v) - users = g.db.query(User).join(Follow, Follow.target_id == u.id).filter(Follow.user_id == User.id).order_by(Follow.id).all() + users = g.db.query(User).join(Follow, Follow.target_id == u.id).filter(Follow.user_id == User.id).order_by(Follow.created_utc).all() return render_template("followers.html", v=v, u=u, users=users) @app.get("/@/following") @auth_required def following(username, v): u = get_user(username, v=v) - users = g.db.query(User).join(Follow, Follow.user_id == u.id).filter(Follow.target_id == User.id).order_by(Follow.id).all() + users = g.db.query(User).join(Follow, Follow.user_id == u.id).filter(Follow.target_id == User.id).order_by(Follow.created_utc).all() return render_template("following.html", v=v, u=u, users=users) @app.get("/views") @@ -907,7 +907,7 @@ def follow_user(username, v): g.db.add(new_follow) g.db.flush() - target.stored_subscriber_count = g.db.query(Follow.id).filter_by(target_id=target.id).count() + target.stored_subscriber_count = g.db.query(Follow.target_id).filter_by(target_id=target.id).count() g.db.add(target) send_notification(target.id, f"@{v.username} has followed you!") @@ -931,7 +931,7 @@ def unfollow_user(username, v): g.db.delete(follow) g.db.flush() - target.stored_subscriber_count = g.db.query(Follow.id).filter_by(target_id=target.id).count() + target.stored_subscriber_count = g.db.query(Follow.target_id).filter_by(target_id=target.id).count() g.db.add(target) send_notification(target.id, f"@{v.username} has unfollowed you!") @@ -953,7 +953,7 @@ def remove_follow(username, v): g.db.delete(follow) g.db.flush() - v.stored_subscriber_count = g.db.query(Follow.id).filter_by(target_id=v.id).count() + v.stored_subscriber_count = g.db.query(Follow.target_id).filter_by(target_id=v.id).count() g.db.add(v) send_repeatable_notification(target.id, f"@{v.username} has removed your follow!") diff --git a/files/routes/votes.py b/files/routes/votes.py index f75514ea2..aa730391b 100644 --- a/files/routes/votes.py +++ b/files/routes/votes.py @@ -28,18 +28,18 @@ def admin_vote_info_get(v): thing_id = g.db.query(Submission.id).filter_by(upvotes=thing.upvotes, downvotes=thing.downvotes).order_by(Submission.id).first()[0] else: thing_id = thing.id - ups = g.db.query(Vote).filter_by(submission_id=thing_id, vote_type=1).order_by(Vote.id).all() + ups = g.db.query(Vote).filter_by(submission_id=thing_id, vote_type=1).order_by(Vote.created_utc).all() - downs = g.db.query(Vote).filter_by(submission_id=thing_id, vote_type=-1).order_by(Vote.id).all() + downs = g.db.query(Vote).filter_by(submission_id=thing_id, vote_type=-1).order_by(Vote.created_utc).all() elif isinstance(thing, Comment): if thing.author.shadowbanned and not (v and v.admin_level): thing_id = g.db.query(Comment.id).filter_by(upvotes=thing.upvotes, downvotes=thing.downvotes).order_by(Comment.id).first()[0] else: thing_id = thing.id - ups = g.db.query(CommentVote).filter_by(comment_id=thing_id, vote_type=1).order_by(CommentVote.id).all() + ups = g.db.query(CommentVote).filter_by(comment_id=thing_id, vote_type=1).order_by(CommentVote.created_utc).all() - downs = g.db.query(CommentVote).filter_by(comment_id=thing_id, vote_type=-1 ).order_by(CommentVote.id).all() + downs = g.db.query(CommentVote).filter_by(comment_id=thing_id, vote_type=-1 ).order_by(CommentVote.created_utc).all() else: abort(400) @@ -100,9 +100,9 @@ def api_vote_post(post_id, new, v): try: g.db.flush() - post.upvotes = g.db.query(Vote.id).filter_by(submission_id=post.id, vote_type=1).count() - post.downvotes = g.db.query(Vote.id).filter_by(submission_id=post.id, vote_type=-1).count() - post.realupvotes = g.db.query(Vote.id).filter_by(submission_id=post.id, real=True).count() + post.upvotes = g.db.query(CommentVote.submission_id).filter_by(submission_id=post.id, vote_type=1).count() + post.downvotes = g.db.query(CommentVote.submission_id).filter_by(submission_id=post.id, vote_type=-1).count() + post.realupvotes = g.db.query(CommentVote.submission_id).filter_by(submission_id=post.id, real=True).count() if post.author.progressivestack: post.realupvotes *= 2 g.db.add(post) g.db.commit() @@ -164,9 +164,9 @@ def api_vote_comment(comment_id, new, v): try: g.db.flush() - comment.upvotes = g.db.query(CommentVote.id).filter_by(comment_id=comment.id, vote_type=1).count() - comment.downvotes = g.db.query(CommentVote.id).filter_by(comment_id=comment.id, vote_type=-1).count() - comment.realupvotes = g.db.query(CommentVote.id).filter_by(comment_id=comment.id, real=True).count() + comment.upvotes = g.db.query(CommentVote.comment_id).filter_by(comment_id=comment.id, vote_type=1).count() + comment.downvotes = g.db.query(CommentVote.comment_id).filter_by(comment_id=comment.id, vote_type=-1).count() + comment.realupvotes = g.db.query(CommentVote.comment_id).filter_by(comment_id=comment.id, real=True).count() if comment.author.progressivestack: comment.realupvotes *= 2 g.db.add(comment) g.db.commit() @@ -201,7 +201,7 @@ def api_vote_poll(comment_id, v): try: g.db.flush() - comment.upvotes = g.db.query(CommentVote.id).filter_by(comment_id=comment.id, vote_type=1).count() + comment.upvotes = g.db.query(CommentVote.comment_id).filter_by(comment_id=comment.id, vote_type=1).count() g.db.add(comment) g.db.commit() except: g.db.rollback() @@ -259,13 +259,13 @@ def api_vote_choice(comment_id, v): else: parent = comment.post for vote in parent.total_choice_voted(v): - vote.comment.upvotes = g.db.query(CommentVote.id).filter_by(comment_id=vote.comment.id, vote_type=1).count() - 1 + vote.comment.upvotes = g.db.query(CommentVote.comment_id).filter_by(comment_id=vote.comment.id, vote_type=1).count() - 1 g.db.add(vote.comment) g.db.delete(vote) try: g.db.flush() - comment.upvotes = g.db.query(CommentVote.id).filter_by(comment_id=comment.id, vote_type=1).count() + comment.upvotes = g.db.query(CommentVote.comment_id).filter_by(comment_id=comment.id, vote_type=1).count() g.db.add(comment) g.db.commit() except: g.db.rollback()