remotes/1693045480750635534/spooky-22
Aevann1 2021-10-06 01:11:17 +02:00
parent 927c26d3ab
commit 2f708e34bd
4 changed files with 71 additions and 7 deletions

View File

@ -2,7 +2,8 @@ import re
from urllib.parse import urlencode, urlparse, parse_qs from urllib.parse import urlencode, urlparse, parse_qs
from flask import * from flask import *
from sqlalchemy import * from sqlalchemy import *
from sqlalchemy.orm import relationship, deferred from sqlalchemy.orm import relationship, deferred, lazyload
from files.classes.votes import CommentVote
from files.helpers.lazy import lazy from files.helpers.lazy import lazy
from files.helpers.const import SLURS from files.helpers.const import SLURS
from files.__main__ import Base from files.__main__ import Base
@ -61,6 +62,11 @@ class Comment(Base):
return f"<Comment(id={self.id})>" return f"<Comment(id={self.id})>"
def poll_voted(self, v):
vote = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=self.id).first()
if vote: return vote.vote_type
else: return None
@property @property
@lazy @lazy
def created_datetime(self): def created_datetime(self):

View File

@ -6,7 +6,7 @@ from urllib.parse import urlparse
from files.helpers.lazy import lazy from files.helpers.lazy import lazy
from files.helpers.const import SLURS from files.helpers.const import SLURS
from files.__main__ import Base from files.__main__ import Base
from .flags import * from .flags import Flag
from os import environ from os import environ
import time import time
@ -35,11 +35,8 @@ class Submission(Base):
private = Column(Boolean, default=False) private = Column(Boolean, default=False)
club = Column(Boolean, default=False) club = Column(Boolean, default=False)
comment_count = Column(Integer, default=0) comment_count = Column(Integer, default=0)
comments = relationship("Comment", primaryjoin="Comment.parent_submission==Submission.id", viewonly=True)
flags = relationship("Flag", lazy="dynamic", viewonly=True)
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("User", primaryjoin="Submission.author_id==User.id")
is_bot = Column(Boolean, default=False) is_bot = Column(Boolean, default=False)
upvotes = Column(Integer, default=1) upvotes = Column(Integer, default=1)
downvotes = Column(Integer, default=0) downvotes = Column(Integer, default=0)
@ -52,6 +49,9 @@ class Submission(Base):
ban_reason = Column(String(128)) ban_reason = Column(String(128))
embed_url = Column(String(256)) embed_url = Column(String(256))
comments = relationship("Comment", lazy="dynamic", primaryjoin="Comment.parent_submission==Submission.id", viewonly=True)
flags = relationship("Flag", lazy="dynamic", viewonly=True)
author = relationship("User", primaryjoin="Submission.author_id==User.id")
oauth_app = relationship("OauthApp", viewonly=True) oauth_app = relationship("OauthApp", viewonly=True)
approved_by = relationship("User", uselist=False, primaryjoin="Submission.is_approved==User.id", viewonly=True) approved_by = relationship("User", uselist=False, primaryjoin="Submission.is_approved==User.id", viewonly=True)
awards = relationship("AwardRelationship", viewonly=True) awards = relationship("AwardRelationship", viewonly=True)
@ -71,6 +71,16 @@ class Submission(Base):
return f"<Submission(id={self.id})>" return f"<Submission(id={self.id})>"
@property
@lazy
def options(self):
return self.comments.filter_by(author_id = 3369)
@property
@lazy
def created_datetime(self):
return str(time.strftime("%d/%B/%Y %H:%M:%S UTC", time.gmtime(self.created_utc)))
@property @property
@lazy @lazy
def created_datetime(self): def created_datetime(self):

View File

@ -123,7 +123,8 @@ def post_id(pid, anything=None, v=None):
comments = comments.filter(Comment.author_id.notin_(shadowbanned)) comments = comments.filter(Comment.author_id.notin_(shadowbanned))
comments=comments.filter( comments=comments.filter(
Comment.parent_submission == post.id Comment.parent_submission == post.id,
Comment.author_id != 3369,
).join( ).join(
votes, votes,
votes.c.comment_id == Comment.id, votes.c.comment_id == Comment.id,
@ -161,7 +162,7 @@ def post_id(pid, anything=None, v=None):
else: else:
shadowbanned = [x[0] for x in g.db.query(User.id).options(lazyload('*')).filter(User.shadowbanned != None).all()] shadowbanned = [x[0] for x in g.db.query(User.id).options(lazyload('*')).filter(User.shadowbanned != None).all()]
comments = g.db.query(Comment).filter(Comment.parent_submission == post.id, Comment.author_id.notin_(shadowbanned)) comments = g.db.query(Comment).filter(Comment.parent_submission == post.id, Comment.author_id != 3369, Comment.author_id.notin_(shadowbanned))
if sort == "new": if sort == "new":
comments = comments.order_by(Comment.created_utc.desc()) comments = comments.order_by(Comment.created_utc.desc())
@ -730,9 +731,17 @@ def submit_post(v):
for i in re.finditer('^(https:\/\/.*\.(png|jpg|jpeg|gif|webp|PNG|JPG|JPEG|GIF|WEBP|9999))', body, re.MULTILINE): for i in re.finditer('^(https:\/\/.*\.(png|jpg|jpeg|gif|webp|PNG|JPG|JPEG|GIF|WEBP|9999))', body, re.MULTILINE):
if "wikipedia" not in i.group(1): body = body.replace(i.group(1), f'![]({i.group(1)})') if "wikipedia" not in i.group(1): body = body.replace(i.group(1), f'![]({i.group(1)})')
body = re.sub('([^\n])\n([^\n])', r'\1\n\n\2', body) body = re.sub('([^\n])\n([^\n])', r'\1\n\n\2', body)
options = []
for i in re.finditer('\s*\$([^\$]+)\$\s*', body):
options.append(i.group(1))
body.replace(i.group(0), "")
body_md = CustomRenderer().render(mistletoe.Document(body)) body_md = CustomRenderer().render(mistletoe.Document(body))
body_html = sanitize(body_md) body_html = sanitize(body_md)
if len(body_html) > 20000: abort(400) if len(body_html) > 20000: abort(400)
# Run safety filter # Run safety filter
@ -806,6 +815,16 @@ def submit_post(v):
g.db.add(new_post) g.db.add(new_post)
g.db.flush() g.db.flush()
for option in options:
c = Comment(author_id=3369,
parent_submission=new_post.id,
level=1,
body=option,
)
g.db.add(c)
g.db.flush()
vote = Vote(user_id=v.id, vote = Vote(user_id=v.id,
vote_type=1, vote_type=1,
submission_id=new_post.id submission_id=new_post.id

View File

@ -160,4 +160,33 @@ def api_vote_comment(comment_id, new, v):
g.db.add(comment) g.db.add(comment)
g.db.commit() g.db.commit()
except: g.db.rollback() except: g.db.rollback()
return "", 204
@app.post("/vote/poll/<comment_id>")
@auth_required
def api_vote_poll(comment_id, v):
vote = request.values.get("vote")
if vote == "true": new = 1
elif vote == "false": new = 0
else: abort(400)
comment_id = int(comment_id)
existing = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment.id).first()
if existing and existing.vote_type == vote: return "", 204
if existing:
existing.vote_type = new
g.db.add(existing)
else:
vote = CommentVote(user_id=v.id, vote_type=new, comment_id=comment_id)
g.db.add(vote)
g.db.flush()
comment.upvotes = g.db.query(CommentVote.id).options(lazyload('*')).filter_by(comment_id=comment.id, vote_type=1).count()
g.db.add(comment)
g.db.commit()
return "", 204 return "", 204