This commit is contained in:
Aevann1 2021-08-06 14:44:24 +02:00
parent 697f87ce47
commit 425681907f
5 changed files with 71 additions and 60 deletions

View file

@ -51,10 +51,10 @@ class Comment(Base, Age_times, Scores, Stndrd, Fuzzing):
oauth_app=relationship("OauthApp") oauth_app=relationship("OauthApp")
post = relationship("Submission") post = relationship("Submission")
flags = relationship("CommentFlag", lazy="dynamic") flags = relationship("CommentFlag", lazy="joined")
votes = relationship( votes = relationship(
"CommentVote", "CommentVote",
lazy="dynamic", lazy="joined",
primaryjoin="CommentVote.comment_id==Comment.id") primaryjoin="CommentVote.comment_id==Comment.id")
author = relationship( author = relationship(
@ -222,10 +222,9 @@ class Comment(Base, Age_times, Scores, Stndrd, Fuzzing):
return data return data
def voted(self, v): @property
x = self.votes.filter_by(user_id=v.id).first() def voted(self):
if x: return x.vote_type return self.__dict__.get("_voted")
else: return 0
@property @property
def is_blocking(self): def is_blocking(self):

View file

@ -53,8 +53,8 @@ class Submission(Base, Stndrd, Age_times, Scores, Fuzzing):
"Comment", "Comment",
lazy="joined", lazy="joined",
primaryjoin="Comment.parent_submission==Submission.id", primaryjoin="Comment.parent_submission==Submission.id",
backref="submissions") )
flags = relationship("Flag", lazy="dynamic") flags = relationship("Flag", lazy="joined")
is_approved = Column(Integer, ForeignKey("users.id"), default=0) is_approved = Column(Integer, ForeignKey("users.id"), default=0)
over_18 = Column(Boolean, default=False) over_18 = Column(Boolean, default=False)
author = relationship( author = relationship(

View file

@ -168,37 +168,34 @@ def get_posts(pids, v=None):
def get_comment(i, v=None, graceful=False, **kwargs): def get_comment(i, v=None, graceful=False, **kwargs):
if v: if v:
blocking = v.blocking.subquery()
blocked = v.blocked.subquery()
items = g.db.query(Comment) items = g.db.query(Comment)
if v.admin_level >=4: items=items.options(joinedload(Comment.oauth_app)) comment=items.filter(Comment.id == i).first()
x=items.filter(Comment.id == i).first() if not comment and not graceful: abort(404)
if not x and not graceful: abort(404)
block = g.db.query(UserBlock).filter( block = g.db.query(UserBlock).filter(
or_( or_(
and_( and_(
UserBlock.user_id == v.id, UserBlock.user_id == v.id,
UserBlock.target_id == x.author_id UserBlock.target_id == comment.author_id
), ),
and_(UserBlock.user_id == x.author_id, and_(UserBlock.user_id == comment.author_id,
UserBlock.target_id == v.id UserBlock.target_id == v.id
) )
) )
).first() ).first()
x._is_blocking = block and block.user_id == v.id comment._is_blocking = block and block.user_id == v.id
x._is_blocked = block and block.target_id == v.id comment._is_blocked = block and block.target_id == v.id
comment._voted = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=Comment.id).first().vote_type
else: else:
x = g.db.query(Comment).filter(Comment.id == i).first() comment = g.db.query(Comment).filter(Comment.id == i).first()
if not x and not graceful:abort(404) if not comment and not graceful:abort(404)
return x return comment
def get_comments(cids, v=None): def get_comments(cids, v=None):
@ -207,15 +204,47 @@ def get_comments(cids, v=None):
cids=tuple(cids) cids=tuple(cids)
output = g.db.query(Comment) if v:
votes = g.db.query(CommentVote).filter_by(user_id=v.id).subquery()
if v and v.admin_level >=4: output=output.options(joinedload(Comment.oauth_app)) blocking = v.blocking.subquery()
output = output.options(joinedload(Comment.parent_comment)).filter(Comment.id.in_(cids)).all() blocked = v.blocked.subquery()
output = sorted(output, key=lambda x: cids.index(x.id)) comments = g.db.query(
Comment,
votes.c.vote_type,
blocking.c.id,
blocked.c.id,
).filter(Comment.id.in_(cids))
return output 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
).all()
output = []
for c in comments:
comment = c[0]
if comment.author and comment.author.shadowbanned and not (v and v.id == comment.author_id): continue
comment._voted = c[1] or 0
comment._is_blocking = c[2] or 0
comment._is_blocked = c[3] or 0
output.append(comment)
else:
output = g.db.query(Comment).filter(Comment.id.in_(cids)).all()
return sorted(output, key=lambda x: cids.index(x.id))
def get_domain(s): def get_domain(s):

View file

@ -80,16 +80,16 @@ def post_id(pid, anything=None, v=None):
blocked = v.blocked.subquery() blocked = v.blocked.subquery()
comms = g.db.query( comments = g.db.query(
Comment, Comment,
votes.c.vote_type, votes.c.vote_type,
blocking.c.id, blocking.c.id,
blocked.c.id, blocked.c.id,
) )
if v.admin_level >=4: if v.admin_level >=4:
comms=comms.options(joinedload(Comment.oauth_app)) comments=comments.options(joinedload(Comment.oauth_app))
comms=comms.filter( comments=comments.filter(
Comment.parent_submission == post.id Comment.parent_submission == post.id
).join( ).join(
votes, votes,
@ -106,17 +106,17 @@ def post_id(pid, anything=None, v=None):
) )
if sort == "top": if sort == "top":
comments = sorted(comms.all(), key=lambda x: x[0].score, reverse=True) comments = sorted(comments.all(), key=lambda x: x[0].score, reverse=True)
elif sort == "bottom": elif sort == "bottom":
comments = sorted(comms.all(), key=lambda x: x[0].score) comments = sorted(comments.all(), key=lambda x: x[0].score)
elif sort == "new": elif sort == "new":
comments = comms.order_by(Comment.created_utc.desc()).all() comments = comments.order_by(Comment.created_utc.desc()).all()
elif sort == "old": elif sort == "old":
comments = comms.order_by(Comment.created_utc.asc()).all() comments = comments.order_by(Comment.created_utc.asc()).all()
elif sort == "controversial": elif sort == "controversial":
comments = sorted(comms.all(), key=lambda x: x[0].score_disputed, reverse=True) comments = sorted(comments.all(), key=lambda x: x[0].score_disputed, reverse=True)
elif sort == "random": elif sort == "random":
c = comms.all() c = comments.all()
comments = random.sample(c, k=len(c)) comments = random.sample(c, k=len(c))
else: else:
abort(422) abort(422)
@ -133,24 +133,24 @@ def post_id(pid, anything=None, v=None):
post._preloaded_comments = output post._preloaded_comments = output
else: else:
comms = g.db.query( comments = g.db.query(
Comment Comment
).filter( ).filter(
Comment.parent_submission == post.id Comment.parent_submission == post.id
) )
if sort == "top": if sort == "top":
comments = sorted(comms.all(), key=lambda x: x.score, reverse=True) comments = sorted(comments.all(), key=lambda x: x.score, reverse=True)
elif sort == "bottom": elif sort == "bottom":
comments = sorted(comms.all(), key=lambda x: x.score) comments = sorted(comments.all(), key=lambda x: x.score)
elif sort == "new": elif sort == "new":
comments = comms.order_by(Comment.created_utc.desc()).all() comments = comments.order_by(Comment.created_utc.desc()).all()
elif sort == "old": elif sort == "old":
comments = comms.order_by(Comment.created_utc.asc()).all() comments = comments.order_by(Comment.created_utc.asc()).all()
elif sort == "controversial": elif sort == "controversial":
comments = sorted(comms.all(), key=lambda x: x.score_disputed, reverse=True) comments = sorted(comments.all(), key=lambda x: x.score_disputed, reverse=True)
elif sort == "random": elif sort == "random":
c = comms.all() c = comments.all()
comments = random.sample(c, k=len(c)) comments = random.sample(c, k=len(c))
else: else:
abort(422) abort(422)
@ -400,23 +400,6 @@ def thumbs(new_post):
soup=BeautifulSoup(x.content, 'html.parser') soup=BeautifulSoup(x.content, 'html.parser')
#parse html #parse html
#first, set metadata
try:
meta_title=soup.find('title')
if meta_title:
post.submission_aux.meta_title=str(meta_title.string)[:500]
meta_desc = soup.find('meta', attrs={"name":"description"})
if meta_desc:
post.submission_aux.meta_description=meta_desc['content'][:1000]
if meta_title or meta_desc:
g.db.add(post.submission_aux)
g.db.commit()
except Exception as e:
pass
#create list of urls to check #create list of urls to check
thumb_candidate_urls=[] thumb_candidate_urls=[]

View file

@ -69,7 +69,7 @@
{% set score=c.score %} {% set score=c.score %}
{% if v %} {% if v %}
{% set voted=c.voted(v) %} {% set voted=c.voted %}
{% else %} {% else %}
{% set voted=-2 %} {% set voted=-2 %}