remotes/1693045480750635534/spooky-22
Aevann1 2021-08-06 14:44:24 +02:00
parent 991a72ab42
commit b981888f90
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")
post = relationship("Submission")
flags = relationship("CommentFlag", lazy="dynamic")
flags = relationship("CommentFlag", lazy="joined")
votes = relationship(
"CommentVote",
lazy="dynamic",
lazy="joined",
primaryjoin="CommentVote.comment_id==Comment.id")
author = relationship(
@ -222,10 +222,9 @@ class Comment(Base, Age_times, Scores, Stndrd, Fuzzing):
return data
def voted(self, v):
x = self.votes.filter_by(user_id=v.id).first()
if x: return x.vote_type
else: return 0
@property
def voted(self):
return self.__dict__.get("_voted")
@property
def is_blocking(self):

View File

@ -53,8 +53,8 @@ class Submission(Base, Stndrd, Age_times, Scores, Fuzzing):
"Comment",
lazy="joined",
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)
over_18 = Column(Boolean, default=False)
author = relationship(

View File

@ -168,37 +168,34 @@ def get_posts(pids, v=None):
def get_comment(i, v=None, graceful=False, **kwargs):
if v:
blocking = v.blocking.subquery()
blocked = v.blocked.subquery()
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 x and not graceful: abort(404)
if not comment and not graceful: abort(404)
block = g.db.query(UserBlock).filter(
or_(
and_(
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
)
)
).first()
x._is_blocking = block and block.user_id == v.id
x._is_blocked = block and block.target_id == v.id
comment._is_blocking = block and block.user_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:
x = g.db.query(Comment).filter(Comment.id == i).first()
if not x and not graceful:abort(404)
comment = g.db.query(Comment).filter(Comment.id == i).first()
if not comment and not graceful:abort(404)
return x
return comment
def get_comments(cids, v=None):
@ -207,15 +204,47 @@ def get_comments(cids, v=None):
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))
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()
return output
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):

View File

@ -80,16 +80,16 @@ def post_id(pid, anything=None, v=None):
blocked = v.blocked.subquery()
comms = g.db.query(
comments = g.db.query(
Comment,
votes.c.vote_type,
blocking.c.id,
blocked.c.id,
)
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
).join(
votes,
@ -106,17 +106,17 @@ def post_id(pid, anything=None, v=None):
)
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":
comments = sorted(comms.all(), key=lambda x: x[0].score)
comments = sorted(comments.all(), key=lambda x: x[0].score)
elif sort == "new":
comments = comms.order_by(Comment.created_utc.desc()).all()
comments = comments.order_by(Comment.created_utc.desc()).all()
elif sort == "old":
comments = comms.order_by(Comment.created_utc.asc()).all()
comments = comments.order_by(Comment.created_utc.asc()).all()
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":
c = comms.all()
c = comments.all()
comments = random.sample(c, k=len(c))
else:
abort(422)
@ -133,24 +133,24 @@ def post_id(pid, anything=None, v=None):
post._preloaded_comments = output
else:
comms = g.db.query(
comments = g.db.query(
Comment
).filter(
Comment.parent_submission == post.id
)
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":
comments = sorted(comms.all(), key=lambda x: x.score)
comments = sorted(comments.all(), key=lambda x: x.score)
elif sort == "new":
comments = comms.order_by(Comment.created_utc.desc()).all()
comments = comments.order_by(Comment.created_utc.desc()).all()
elif sort == "old":
comments = comms.order_by(Comment.created_utc.asc()).all()
comments = comments.order_by(Comment.created_utc.asc()).all()
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":
c = comms.all()
c = comments.all()
comments = random.sample(c, k=len(c))
else:
abort(422)
@ -400,23 +400,6 @@ def thumbs(new_post):
soup=BeautifulSoup(x.content, 'html.parser')
#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
thumb_candidate_urls=[]

View File

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