rDrama/files/routes/comments.py

862 lines
21 KiB
Python
Raw Normal View History

2021-08-04 15:35:10 +00:00
from files.helpers.wrappers import *
from files.helpers.filters import *
from files.helpers.alerts import *
from files.helpers.images import *
from files.helpers.session import *
2021-08-21 11:06:28 +00:00
from files.helpers.const import *
2021-08-04 15:35:10 +00:00
from files.classes import *
from files.routes.front import comment_idlist
2021-08-11 02:54:52 +00:00
from pusher_push_notifications import PushNotifications
2021-07-21 01:12:26 +00:00
from flask import *
2021-08-04 15:35:10 +00:00
from files.__main__ import app, limiter
2021-10-13 15:13:37 +00:00
from .posts import filter_title
2021-09-23 23:48:49 +00:00
2021-07-21 01:12:26 +00:00
2021-08-05 14:41:32 +00:00
site = environ.get("DOMAIN").strip()
2021-08-02 14:27:20 +00:00
2021-07-21 01:12:26 +00:00
beams_client = PushNotifications(
2021-08-21 11:06:28 +00:00
instance_id=PUSHER_INSTANCE_ID,
2021-07-21 01:12:26 +00:00
secret_key=PUSHER_KEY,
)
2021-07-27 22:31:28 +00:00
@app.get("/comment/<cid>")
@app.get("/post/<pid>/<anything>/<cid>")
2021-08-13 01:38:51 +00:00
@app.get("/logged_out/comment/<cid>")
2021-08-13 02:55:52 +00:00
@app.get("/logged_out/post/<pid>/<anything>/<cid>")
2021-07-21 01:12:26 +00:00
@auth_desired
def post_pid_comment_cid(cid, pid=None, anything=None, v=None):
2021-09-24 20:08:40 +00:00
if not v and not request.path.startswith('/logged_out'): return redirect(f"/logged_out{request.full_path}")
2021-08-13 01:38:51 +00:00
2021-10-01 20:11:44 +00:00
if v and request.path.startswith('/logged_out'): v = None
2021-08-13 18:53:11 +00:00
2021-07-30 06:17:38 +00:00
try: cid = int(cid)
2021-07-31 06:59:18 +00:00
except:
try: cid = int(cid, 36)
except: abort(404)
2021-07-30 06:17:38 +00:00
2021-07-21 01:12:26 +00:00
comment = get_comment(cid, v=v)
2021-09-13 21:43:34 +00:00
if comment.post and comment.post.club and not (v and v.paid_dues): abort(403)
2021-09-12 01:57:48 +00:00
2021-08-19 18:32:37 +00:00
if not comment.parent_submission and not (v and (comment.author.id == v.id or comment.sentto == v.id)) and not (v and v.admin_level == 6) : abort(403)
2021-07-21 01:12:26 +00:00
if not pid:
if comment.parent_submission: pid = comment.parent_submission
2021-10-09 07:22:52 +00:00
elif "rama" in request.host: pid = 6489
2021-09-13 17:03:46 +00:00
elif 'pcmemes.net' in request.host: pid = 382
2021-09-05 09:37:05 +00:00
else: pid = 1
2021-07-21 01:12:26 +00:00
try: pid = int(pid)
except: abort(404)
post = get_post(pid, v=v)
2021-07-26 17:47:52 +00:00
if post.over_18 and not (v and v.over_18) and not session.get('over_18', 0) >= int(time.time()):
2021-07-31 05:28:05 +00:00
if request.headers.get("Authorization"): return {'error': f'This content is not suitable for some users and situations.'}
else: render_template("errors/nsfw.html", v=v)
2021-07-21 01:12:26 +00:00
2021-09-19 13:11:34 +00:00
try: context = int(request.values.get("context", 0))
2021-07-22 14:54:19 +00:00
except: context = 0
2021-08-07 23:14:32 +00:00
comment_info = comment
2021-08-07 23:23:10 +00:00
c = comment
while context > 0 and c.level > 1:
2021-09-22 18:36:03 +00:00
c = c.parent_comment
2021-07-21 01:12:26 +00:00
context -= 1
2021-08-07 23:23:10 +00:00
top_comment = c
2021-07-21 01:12:26 +00:00
if v: defaultsortingcomments = v.defaultsortingcomments
else: defaultsortingcomments = "top"
2021-09-19 13:11:34 +00:00
sort=request.values.get("sort", defaultsortingcomments)
2021-07-21 01:12:26 +00:00
2021-08-07 23:14:32 +00:00
post.replies=[top_comment]
2021-07-21 01:12:26 +00:00
2021-08-11 03:24:13 +00:00
if v:
votes = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id).subquery()
2021-08-11 03:24:13 +00:00
blocking = v.blocking.subquery()
2021-08-11 03:24:13 +00:00
blocked = v.blocked.subquery()
2021-08-11 03:24:13 +00:00
comments = g.db.query(
Comment,
votes.c.vote_type,
blocking.c.id,
blocked.c.id,
)
2021-09-14 18:23:47 +00:00
if not (v and v.shadowbanned) and not (v and v.admin_level == 6):
2021-09-30 19:40:33 +00:00
shadowbanned = [x[0] for x in g.db.query(User.id).options(lazyload('*')).filter(User.shadowbanned != None).all()]
2021-09-28 19:45:17 +00:00
comments = comments.filter(Comment.author_id.notin_(shadowbanned))
2021-08-11 03:24:13 +00:00
comments=comments.filter(
2021-10-07 04:03:54 +00:00
Comment.parent_submission == post.id,
Comment.author_id != AUTOPOLLER_ACCOUNT
2021-08-11 03:24:13 +00:00
).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
)
2021-09-22 18:36:03 +00:00
output = []
2021-08-11 03:24:13 +00:00
for c in comments:
comment = c[0]
comment.voted = c[1] or 0
2021-09-22 18:36:03 +00:00
comment.is_blocking = c[2] or 0
comment.is_blocked = c[3] or 0
output.append(comment)
2021-08-11 03:24:13 +00:00
2021-09-22 18:36:03 +00:00
post.preloaded_comments = output
2021-08-07 23:14:32 +00:00
if request.headers.get("Authorization"): return top_comment.json
else: return post.rendered_page(v=v, sort=sort, comment=top_comment, comment_info=comment_info)
2021-07-31 05:59:25 +00:00
2021-07-21 01:12:26 +00:00
2021-07-31 06:55:22 +00:00
@app.post("/comment")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-10-07 06:04:29 +00:00
@limiter.limit("6/minute")
2021-07-21 01:12:26 +00:00
@is_not_banned
@validate_formkey
def api_comment(v):
2021-10-03 19:36:29 +00:00
if request.content_length > 4 * 1024 * 1024: return "Max file size is 4 MB.", 413
2021-07-21 01:12:26 +00:00
2021-10-06 23:14:51 +00:00
parent_submission = request.values.get("submission").strip()
parent_fullname = request.values.get("parent_fullname").strip()
parent_post = get_post(parent_submission, v=v)
if parent_post.club and not (v and v.paid_dues): abort(403)
if parent_fullname.startswith("t2_"):
2021-07-21 01:12:26 +00:00
parent = parent_post
parent_comment_id = None
level = 1
2021-10-06 23:14:51 +00:00
elif parent_fullname.startswith("t3_"):
parent = get_comment(parent_fullname.split("_")[1], v=v)
2021-07-21 01:12:26 +00:00
parent_comment_id = parent.id
level = parent.level + 1
2021-10-06 23:14:51 +00:00
else: abort(400)
2021-07-21 01:12:26 +00:00
2021-10-12 05:23:17 +00:00
body = request.values.get("body", "").strip()[:10000]
2021-07-21 01:12:26 +00:00
body = body.strip()
2021-08-11 17:01:19 +00:00
if not body and not request.files.get('file'): return {"error":"You need to actually write something!"}, 400
2021-07-21 01:12:26 +00:00
2021-10-09 17:15:47 +00:00
for i in re.finditer('^(https:\/\/.*\.(png|jpg|jpeg|gif|webp|PNG|JPG|JPEG|GIF|WEBP|9999))', body, re.MULTILINE):
2021-09-15 12:43:46 +00:00
if "wikipedia" not in i.group(1): body = body.replace(i.group(1), f'![]({i.group(1)})')
2021-10-12 05:28:40 +00:00
body = re.sub('([^\n])\n([^\n])', r'\1\n\n\2', body)
2021-10-07 04:03:54 +00:00
2021-10-07 05:55:27 +00:00
body_md = body
2021-10-07 04:03:54 +00:00
options = []
2021-10-07 05:55:27 +00:00
for i in re.finditer('\s*\$\$([^\$\n]+)\$\$\s*', body_md):
2021-10-07 04:03:54 +00:00
options.append(i.group(1))
2021-10-07 05:55:27 +00:00
body_md = body_md.replace(i.group(0), "")
2021-10-07 04:03:54 +00:00
2021-10-12 05:28:40 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body_md))
2021-08-21 12:57:16 +00:00
body_html = sanitize(body_md)
2021-07-21 01:12:26 +00:00
bans = filter_comment_html(body_html)
if bans:
ban = bans[0]
reason = f"Remove the {ban.domain} link from your comment and try again."
2021-10-12 13:42:10 +00:00
if ban.reason: reason += f" {ban.reason}"
2021-08-11 17:01:19 +00:00
return {"error": reason}, 401
2021-07-21 01:12:26 +00:00
2021-09-24 02:21:41 +00:00
existing = g.db.query(Comment).options(lazyload('*')).filter(Comment.author_id == v.id,
2021-07-21 01:12:26 +00:00
Comment.deleted_utc == 0,
Comment.parent_comment_id == parent_comment_id,
Comment.parent_submission == parent_submission,
2021-09-24 02:21:41 +00:00
Comment.body == body
2021-09-23 23:48:49 +00:00
).first()
2021-07-21 01:12:26 +00:00
if existing:
2021-08-11 17:01:19 +00:00
return {"error": f"You already made that comment: {existing.permalink}"}, 409
2021-07-21 01:12:26 +00:00
if parent.author.any_block_exists(v) and not v.admin_level>=3:
2021-08-11 17:01:19 +00:00
return {"error": "You can't reply to users who have blocked you, or users you have blocked."}, 403
2021-07-21 01:12:26 +00:00
is_bot = request.headers.get("X-User-Type","")=="Bot"
if not is_bot:
now = int(time.time())
cutoff = now - 60 * 60 * 24
similar_comments = g.db.query(Comment
).options(
lazyload('*')
2021-09-24 02:21:41 +00:00
).filter(
2021-07-21 01:12:26 +00:00
Comment.author_id == v.id,
2021-09-24 02:21:41 +00:00
Comment.body.op(
2021-07-21 01:12:26 +00:00
'<->')(body) < app.config["COMMENT_SPAM_SIMILAR_THRESHOLD"],
Comment.created_utc > cutoff
2021-09-23 23:48:49 +00:00
).all()
2021-07-21 01:12:26 +00:00
threshold = app.config["COMMENT_SPAM_COUNT_THRESHOLD"]
if v.age >= (60 * 60 * 24 * 7):
threshold *= 3
elif v.age >= (60 * 60 * 24):
threshold *= 2
if len(similar_comments) > threshold:
2021-08-04 16:00:57 +00:00
text = "Your account has been suspended for 1 day for the following reason:\n\n> Too much spam!"
2021-08-21 11:06:28 +00:00
send_notification(NOTIFICATIONS_ACCOUNT, v, text)
2021-07-21 01:12:26 +00:00
v.ban(reason="Spamming.",
days=1)
for alt in v.alts:
if not alt.is_suspended:
2021-07-30 08:37:55 +00:00
alt.ban(reason="Spamming.", days=1)
2021-07-21 01:12:26 +00:00
for comment in similar_comments:
comment.is_banned = True
comment.ban_reason = "Automatic spam removal. This happened because the post's creator submitted too much similar content too quickly."
g.db.add(comment)
ma=ModAction(
2021-08-21 11:06:28 +00:00
user_id=AUTOJANNY_ACCOUNT,
2021-07-21 01:12:26 +00:00
target_comment_id=comment.id,
kind="ban_comment",
note="spam"
)
g.db.add(ma)
2021-08-11 17:01:19 +00:00
return {"error": "Too much spam!"}, 403
2021-07-21 01:12:26 +00:00
2021-07-31 09:00:56 +00:00
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
2021-07-27 21:35:50 +00:00
file=request.files["file"]
2021-08-11 17:01:19 +00:00
if not file.content_type.startswith('image/'): return {"error": "That wasn't an image!"}, 400
2021-09-10 04:49:36 +00:00
2021-10-13 13:18:32 +00:00
name = f'/images/{int(time.time())}{secrets.token_urlsafe(2)}.gif'
2021-10-03 19:05:59 +00:00
file.save(name)
2021-10-03 21:51:39 +00:00
url = request.host_url[:-1] + process_image(name)
2021-07-27 21:35:50 +00:00
2021-09-19 13:11:34 +00:00
body = request.values.get("body") + f"\n![]({url})"
2021-09-22 22:27:08 +00:00
body = re.sub('([^\n])\n([^\n])', r'\1\n\n\2', body)
2021-10-06 00:49:51 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body))
2021-08-21 12:57:16 +00:00
body_html = sanitize(body_md)
2021-07-21 01:12:26 +00:00
2021-09-24 19:41:08 +00:00
if len(body_html) > 20000: abort(400)
2021-07-21 01:12:26 +00:00
2021-09-24 02:21:41 +00:00
c = Comment(author_id=v.id,
parent_submission=parent_submission,
parent_comment_id=parent_comment_id,
level=level,
over_18=parent_post.over_18 or request.values.get("over_18","")=="true",
is_bot=is_bot,
app_id=v.client.application.id if v.client else None,
body_html=body_html,
body=body[:10000]
)
2021-10-05 23:44:38 +00:00
c.upvotes = 1
2021-09-24 02:21:41 +00:00
g.db.add(c)
2021-07-21 01:12:26 +00:00
g.db.flush()
2021-10-07 04:03:54 +00:00
for option in options:
c_option = Comment(author_id=AUTOPOLLER_ACCOUNT,
parent_submission=parent_submission,
parent_comment_id=c.id,
level=level+1,
2021-10-13 15:17:52 +00:00
body_html=filter_title(option)
2021-10-07 04:03:54 +00:00
)
g.db.add(c_option)
2021-09-24 02:21:41 +00:00
if 'pcmemes.net' in request.host and c.body.lower().startswith("based"):
2021-09-03 22:24:25 +00:00
pill = re.match("based and (.{1,20}?)(-| )pilled", body, re.IGNORECASE)
2021-09-03 22:21:47 +00:00
2021-09-03 21:49:08 +00:00
if level == 1: basedguy = get_account(c.post.author_id)
2021-09-03 21:45:30 +00:00
else: basedguy = get_account(c.parent_comment.author_id)
2021-09-03 21:04:04 +00:00
basedguy.basedcount += 1
2021-09-03 22:27:55 +00:00
if pill: basedguy.pills += f"{pill.group(1)}, "
2021-09-03 21:04:04 +00:00
g.db.add(basedguy)
2021-09-03 22:21:47 +00:00
body2 = BASED_MSG.format(username=basedguy.username, basedcount=basedguy.basedcount, pills=basedguy.pills)
2021-09-03 21:04:04 +00:00
2021-09-13 11:54:20 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body2))
2021-09-03 21:04:04 +00:00
body_based_html = sanitize(body_md)
2021-09-24 02:21:41 +00:00
c_based = Comment(author_id=BASEDBOT_ACCOUNT,
parent_submission=parent_submission,
distinguish_level=6,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
2021-09-03 21:04:04 +00:00
body_html=body_based_html,
body=body2
2021-09-24 02:21:41 +00:00
)
g.db.add(c_based)
2021-09-03 21:04:04 +00:00
g.db.flush()
2021-09-03 21:08:53 +00:00
2021-09-03 21:04:04 +00:00
n = Notification(comment_id=c_based.id, user_id=v.id)
g.db.add(n)
2021-10-09 07:22:52 +00:00
if "rama" in request.host and "ivermectin" in c.body.lower():
2021-09-01 20:51:47 +00:00
c.is_banned = True
c.ban_reason = "ToS Violation"
g.db.add(c)
2021-09-24 02:21:41 +00:00
body2 = VAXX_MSG.format(username=v.username)
body_md = CustomRenderer().render(mistletoe.Document(body2))
body_jannied_html = sanitize(body_md)
2021-09-01 20:51:47 +00:00
c_jannied = Comment(author_id=AUTOJANNY_ACCOUNT,
parent_submission=parent_submission,
distinguish_level=6,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
2021-09-24 02:21:41 +00:00
body_html=body_jannied_html,
body=body2
2021-09-01 20:51:47 +00:00
)
g.db.add(c_jannied)
g.db.flush()
n = Notification(comment_id=c_jannied.id, user_id=v.id)
g.db.add(n)
2021-09-24 02:21:41 +00:00
if v.agendaposter and "trans lives matter" not in c.body_html.lower():
2021-07-21 21:36:46 +00:00
c.is_banned = True
c.ban_reason = "ToS Violation"
g.db.add(c)
2021-09-24 02:21:41 +00:00
body = AGENDAPOSTER_MSG.format(username=v.username)
2021-10-06 00:49:51 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body))
2021-09-24 02:21:41 +00:00
body_jannied_html = sanitize(body_md)
2021-08-21 11:06:28 +00:00
c_jannied = Comment(author_id=AUTOJANNY_ACCOUNT,
2021-07-21 21:36:46 +00:00
parent_submission=parent_submission,
distinguish_level=6,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
2021-09-24 02:21:41 +00:00
body_html=body_jannied_html,
body=body
2021-07-21 21:36:46 +00:00
)
g.db.add(c_jannied)
g.db.flush()
2021-09-24 02:21:41 +00:00
2021-07-21 21:36:46 +00:00
n = Notification(comment_id=c_jannied.id, user_id=v.id)
g.db.add(n)
2021-09-22 15:01:18 +00:00
if v.id == 2424:
cratvote = CommentVote(user_id=747, comment_id=c.id, vote_type=1)
g.db.add(cratvote)
2021-09-22 11:43:21 +00:00
v.coins += 1
v.truecoins += 1
g.db.add(v)
c.upvotes += 1
g.db.add(c)
2021-10-14 12:40:44 +00:00
if "rama" in request.host and len(c.body) >= 1000 and v.username != "Snappy" and "</blockquote>" not in body_html:
2021-09-24 02:21:41 +00:00
body = random.choice(LONGPOST_REPLIES)
body = re.sub('([^\n])\n([^\n])', r'\1\n\n\2', body)
2021-10-06 00:49:51 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body))
2021-09-24 02:21:41 +00:00
body_html2 = sanitize(body_md)
2021-08-21 11:06:28 +00:00
c2 = Comment(author_id=LONGPOSTBOT_ACCOUNT,
2021-07-21 01:12:26 +00:00
parent_submission=parent_submission,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
2021-09-24 02:21:41 +00:00
body_html=body_html2,
body=body
2021-07-21 01:12:26 +00:00
)
g.db.add(c2)
g.db.flush()
2021-09-24 02:21:41 +00:00
2021-07-21 01:12:26 +00:00
n = Notification(comment_id=c2.id, user_id=v.id)
g.db.add(n)
2021-10-09 07:22:52 +00:00
if "rama" in request.host and random.random() < 0.001 and v.username != "Snappy" and v.username != "zozbot":
2021-09-24 02:21:41 +00:00
body = "zoz"
2021-10-06 00:49:51 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body))
2021-09-24 02:21:41 +00:00
body_html2 = sanitize(body_md)
2021-07-21 01:12:26 +00:00
c2 = Comment(author_id=1833,
parent_submission=parent_submission,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
2021-09-24 02:21:41 +00:00
body_html=body_html2,
body=body
2021-07-21 01:12:26 +00:00
)
g.db.add(c2)
g.db.flush()
2021-09-24 02:21:41 +00:00
2021-07-21 01:12:26 +00:00
n = Notification(comment_id=c2.id, user_id=v.id)
g.db.add(n)
2021-09-24 02:21:41 +00:00
body = "zle"
2021-10-06 00:49:51 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body))
2021-09-24 02:21:41 +00:00
body_html2 = sanitize(body_md)
2021-07-21 01:12:26 +00:00
c3 = Comment(author_id=1833,
parent_submission=parent_submission,
parent_comment_id=c2.id,
level=level+2,
is_bot=True,
2021-09-24 02:21:41 +00:00
body_html=body_html2,
body=body,
2021-07-21 01:12:26 +00:00
)
g.db.add(c3)
g.db.flush()
2021-09-24 02:21:41 +00:00
body = "zozzle"
2021-10-06 00:49:51 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body))
2021-09-24 02:21:41 +00:00
body_html2 = sanitize(body_md)
2021-07-21 01:12:26 +00:00
c4 = Comment(author_id=1833,
parent_submission=parent_submission,
parent_comment_id=c3.id,
level=level+3,
is_bot=True,
2021-09-24 02:21:41 +00:00
body_html=body_html2,
body=body
2021-07-21 01:12:26 +00:00
)
g.db.add(c4)
g.db.flush()
2021-07-26 02:45:35 +00:00
if not v.shadowbanned:
2021-07-22 11:32:47 +00:00
notify_users = set()
2021-09-17 08:29:05 +00:00
for x in g.db.query(Subscription.user_id).options(lazyload('*')).filter_by(submission_id=c.parent_submission).all():
2021-08-25 17:42:35 +00:00
notify_users.add(x[0])
2021-07-22 11:32:47 +00:00
if parent.author.id != v.id: notify_users.add(parent.author.id)
soup = BeautifulSoup(body_html, features="html.parser")
mentions = soup.find_all("a", href=re.compile("^/@(\w+)"))
for mention in mentions:
username = mention["href"].split("@")[1]
2021-09-17 08:29:05 +00:00
user = g.db.query(User).options(lazyload('*')).filter_by(username=username).first()
2021-07-22 11:32:47 +00:00
if user:
if v.any_block_exists(user):
continue
if user.id != v.id:
notify_users.add(user.id)
for x in notify_users:
n = Notification(comment_id=c.id, user_id=x)
g.db.add(n)
2021-08-25 17:38:37 +00:00
g.db.flush()
2021-07-22 11:32:47 +00:00
if parent.author.id != v.id:
try:
beams_client.publish_to_interests(
interests=[str(parent.author.id)],
publish_body={
'web': {
'notification': {
'title': f'New reply by @{v.username}',
'body': c.body,
2021-10-11 15:05:49 +00:00
'deep_link': f'https://{site}{c.permalink}?context=10#context',
},
},
2021-07-22 11:32:47 +00:00
},
)
2021-08-11 02:54:52 +00:00
except Exception as e:
print(e)
2021-07-21 01:12:26 +00:00
vote = CommentVote(user_id=v.id,
comment_id=c.id,
vote_type=1
)
g.db.add(vote)
2021-10-05 23:44:38 +00:00
2021-07-21 01:12:26 +00:00
cache.delete_memoized(comment_idlist)
2021-09-27 21:46:35 +00:00
v.comment_count = g.db.query(Comment.id).options(lazyload('*')).filter(Comment.author_id == v.id, Comment.parent_submission != None).filter_by(is_banned=False, deleted_utc=0).count()
2021-07-28 03:55:47 +00:00
g.db.add(v)
2021-07-31 06:51:58 +00:00
2021-10-05 23:45:37 +00:00
parent_post.comment_count += 1
2021-08-22 11:58:41 +00:00
g.db.add(parent_post)
2021-07-31 06:51:58 +00:00
2021-09-22 18:58:55 +00:00
c.voted = 1
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-31 06:55:22 +00:00
if request.headers.get("Authorization"): return c.json
2021-10-03 23:10:39 +00:00
else: return render_template("comments.html", v=v, comments=[c])
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/edit_comment/<cid>")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-08-22 20:31:12 +00:00
@auth_required
2021-07-31 06:27:21 +00:00
@validate_formkey
2021-07-21 01:12:26 +00:00
def edit_comment(cid, v):
2021-10-03 19:36:29 +00:00
if request.content_length > 4 * 1024 * 1024: return "Max file size is 4 MB.", 413
2021-07-21 01:12:26 +00:00
c = get_comment(cid, v=v)
if not c.author_id == v.id: abort(403)
if c.is_banned or c.deleted_utc > 0: abort(403)
2021-10-12 05:23:17 +00:00
body = request.values.get("body", "").strip()[:10000]
2021-10-09 17:15:47 +00:00
for i in re.finditer('^(https:\/\/.*\.(png|jpg|jpeg|gif|webp|PNG|JPG|JPEG|GIF|WEBP|9999))', body, re.MULTILINE):
2021-09-15 12:43:46 +00:00
if "wikipedia" not in i.group(1): body = body.replace(i.group(1), f'![]({i.group(1)})')
2021-10-06 00:49:51 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body))
2021-08-21 12:57:16 +00:00
body_html = sanitize(body_md)
2021-07-21 01:12:26 +00:00
bans = filter_comment_html(body_html)
if bans:
ban = bans[0]
reason = f"Remove the {ban.domain} link from your comment and try again."
2021-10-12 13:42:10 +00:00
if ban.reason: reason += f" {ban.reason}"
2021-07-21 01:12:26 +00:00
2021-07-31 05:59:25 +00:00
if request.headers.get("Authorization"): return {'error': f'A blacklisted domain was used.'}, 400
else: return render_template("comment_failed.html",
2021-07-30 05:31:38 +00:00
action=f"/edit_comment/{c.id}",
2021-10-06 22:12:04 +00:00
badlinks=[x.domain for x in bans],
2021-07-21 01:12:26 +00:00
body=body,
v=v
2021-07-31 05:59:25 +00:00
)
2021-07-21 01:12:26 +00:00
now = int(time.time())
cutoff = now - 60 * 60 * 24
similar_comments = g.db.query(Comment
).options(
lazyload('*')
2021-09-24 02:21:41 +00:00
).filter(
2021-07-21 01:12:26 +00:00
Comment.author_id == v.id,
2021-09-24 02:21:41 +00:00
Comment.body.op(
2021-07-21 01:12:26 +00:00
'<->')(body) < app.config["SPAM_SIMILARITY_THRESHOLD"],
Comment.created_utc > cutoff
2021-09-23 23:48:49 +00:00
).all()
2021-07-21 01:12:26 +00:00
threshold = app.config["SPAM_SIMILAR_COUNT_THRESHOLD"]
if v.age >= (60 * 60 * 24 * 30):
threshold *= 4
elif v.age >= (60 * 60 * 24 * 7):
threshold *= 3
elif v.age >= (60 * 60 * 24):
threshold *= 2
if len(similar_comments) > threshold:
2021-08-04 16:00:57 +00:00
text = "Your account has been suspended for 1 day for the following reason:\n\n> Too much spam!"
2021-08-21 11:06:28 +00:00
send_notification(NOTIFICATIONS_ACCOUNT, v, text)
2021-07-21 01:12:26 +00:00
v.ban(reason="Spamming.",
days=1)
for comment in similar_comments:
comment.is_banned = True
comment.ban_reason = "Automatic spam removal. This happened because the post's creator submitted too much similar content too quickly."
g.db.add(comment)
2021-07-31 05:28:05 +00:00
return {"error": "Too much spam!"}, 403
2021-07-21 01:12:26 +00:00
2021-07-31 09:00:56 +00:00
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
2021-07-27 21:35:50 +00:00
file=request.files["file"]
2021-07-31 05:28:05 +00:00
if not file.content_type.startswith('image/'): return {"error": "That wasn't an image!"}, 400
2021-09-10 04:49:36 +00:00
2021-10-13 13:18:32 +00:00
name = f'/images/{int(time.time())}{secrets.token_urlsafe(2)}.gif'
2021-10-03 19:05:59 +00:00
file.save(name)
2021-10-03 21:51:39 +00:00
url = request.host_url[:-1] + process_image(name)
2021-07-27 21:35:50 +00:00
body += f"\n![]({url})"
2021-10-06 00:49:51 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body))
2021-08-21 12:57:16 +00:00
body_html = sanitize(body_md)
2021-07-21 01:12:26 +00:00
2021-09-24 19:41:08 +00:00
if len(body_html) > 20000: abort(400)
2021-09-02 14:38:33 +00:00
2021-08-31 17:34:05 +00:00
c.body = body[:10000]
2021-09-02 14:38:33 +00:00
c.body_html = body_html
2021-07-21 01:12:26 +00:00
2021-10-09 07:22:52 +00:00
if "rama" in request.host and "ivermectin" in c.body_html.lower():
2021-09-01 20:51:47 +00:00
c.is_banned = True
c.ban_reason = "ToS Violation"
g.db.add(c)
2021-09-24 02:21:41 +00:00
body = VAXX_MSG.format(username=v.username)
2021-10-06 00:49:51 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body))
2021-09-24 02:21:41 +00:00
body_jannied_html = sanitize(body_md)
2021-09-01 20:51:47 +00:00
c_jannied = Comment(author_id=AUTOJANNY_ACCOUNT,
parent_submission=c.parent_submission,
distinguish_level=6,
parent_comment_id=c.id,
level=c.level+1,
is_bot=True,
2021-09-24 02:21:41 +00:00
body_html=body_jannied_html,
body=body
2021-09-01 20:51:47 +00:00
)
g.db.add(c_jannied)
g.db.flush()
n = Notification(comment_id=c_jannied.id, user_id=v.id)
g.db.add(n)
2021-07-21 21:36:46 +00:00
if v.agendaposter and "trans lives matter" not in c.body_html.lower():
c.is_banned = True
c.ban_reason = "ToS Violation"
g.db.add(c)
2021-09-24 02:21:41 +00:00
body = AGENDAPOSTER_MSG.format(username=v.username)
2021-10-06 00:49:51 +00:00
body_md = CustomRenderer().render(mistletoe.Document(body))
2021-09-24 02:21:41 +00:00
body_jannied_html = sanitize(body_md)
2021-08-21 11:06:28 +00:00
c_jannied = Comment(author_id=AUTOJANNY_ACCOUNT,
2021-07-21 21:36:46 +00:00
parent_submission=c.parent_submission,
distinguish_level=6,
parent_comment_id=c.id,
level=c.level+1,
is_bot=True,
2021-09-24 02:21:41 +00:00
body_html=body_jannied_html,
body=body,
2021-07-21 21:36:46 +00:00
)
g.db.add(c_jannied)
g.db.flush()
n = Notification(comment_id=c_jannied.id, user_id=v.id)
g.db.add(n)
2021-07-21 01:12:26 +00:00
if int(time.time()) - c.created_utc > 60 * 3: c.edited_utc = int(time.time())
g.db.add(c)
2021-08-11 17:01:19 +00:00
g.db.flush()
2021-07-21 01:12:26 +00:00
notify_users = set()
soup = BeautifulSoup(body_html, features="html.parser")
mentions = soup.find_all("a", href=re.compile("^/@(\w+)"))
if len(mentions) > 0:
notifs = g.db.query(Notification)
for mention in mentions:
username = mention["href"].split("@")[1]
2021-09-17 08:29:05 +00:00
user = g.db.query(User).options(lazyload('*')).filter_by(username=username).first()
2021-07-21 01:12:26 +00:00
if user:
if v.any_block_exists(user):
continue
if user.id != v.id:
notify_users.add(user.id)
for x in notify_users:
notif = notifs.filter_by(comment_id=c.id, user_id=x).first()
if not notif:
n = Notification(comment_id=c.id, user_id=x)
g.db.add(n)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-10-03 23:10:39 +00:00
return c.body_html
2021-07-31 06:55:22 +00:00
2021-07-21 01:12:26 +00:00
2021-07-28 03:55:47 +00:00
@app.post("/delete/comment/<cid>")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def delete_comment(cid, v):
2021-09-17 08:29:05 +00:00
c = g.db.query(Comment).options(lazyload('*')).filter_by(id=cid).first()
2021-07-21 01:12:26 +00:00
2021-10-08 15:11:22 +00:00
if not c: abort(404)
2021-07-21 01:12:26 +00:00
2021-10-08 15:11:22 +00:00
if not c.author_id == v.id: abort(403)
2021-07-21 01:12:26 +00:00
c.deleted_utc = int(time.time())
g.db.add(c)
2021-07-27 22:24:06 +00:00
2021-08-11 17:01:19 +00:00
cache.delete_memoized(comment_idlist)
2021-07-21 01:12:26 +00:00
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 10:57:56 +00:00
return {"message": "Comment deleted!"}
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/undelete/comment/<cid>")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def undelete_comment(cid, v):
2021-09-17 08:29:05 +00:00
c = g.db.query(Comment).options(lazyload('*')).filter_by(id=cid).first()
2021-07-21 01:12:26 +00:00
if not c:
abort(404)
if not c.author_id == v.id:
abort(403)
c.deleted_utc = 0
g.db.add(c)
2021-08-11 17:01:19 +00:00
cache.delete_memoized(comment_idlist)
2021-07-21 01:12:26 +00:00
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 10:57:56 +00:00
return {"message": "Comment undeleted!"}
2021-07-31 05:28:05 +00:00
2021-07-21 01:12:26 +00:00
2021-09-26 10:55:06 +00:00
@app.post("/pin_comment/<cid>")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
2021-09-26 10:55:06 +00:00
def toggle_pin_comment(cid, v):
2021-07-21 01:12:26 +00:00
comment = get_comment(cid, v=v)
2021-07-28 22:11:18 +00:00
if v.admin_level < 1 and v.id != comment.post.author_id:
2021-07-21 01:12:26 +00:00
abort(403)
2021-09-23 19:45:11 +00:00
if comment.is_pinned: comment.is_pinned = None
else: comment.is_pinned = v.username
2021-07-21 01:12:26 +00:00
g.db.add(comment)
2021-08-11 17:01:19 +00:00
g.db.flush()
2021-07-21 01:12:26 +00:00
if v.admin_level == 6:
ma=ModAction(
kind="pin_comment" if comment.is_pinned else "unpin_comment",
user_id=v.id,
target_comment_id=comment.id
)
g.db.add(ma)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 22:09:45 +00:00
if comment.is_pinned: return {"message": "Comment pinned!"}
else: return {"message": "Comment unpinned!"}
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/save_comment/<cid>")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def save_comment(cid, v):
comment=get_comment(cid)
2021-10-10 04:25:34 +00:00
save=g.db.query(SaveRelationship).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment.id, type=2).first()
2021-07-21 01:12:26 +00:00
2021-09-18 17:44:21 +00:00
if not save:
2021-10-10 04:25:34 +00:00
new_save=SaveRelationship(user_id=v.id, comment_id=comment.id, type=2)
2021-09-18 17:44:21 +00:00
g.db.add(new_save)
2021-10-09 06:17:13 +00:00
try: g.db.commit()
except: g.db.rollback()
2021-09-16 17:02:58 +00:00
2021-09-08 11:05:31 +00:00
return {"message": "Comment saved!"}
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/unsave_comment/<cid>")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def unsave_comment(cid, v):
comment=get_comment(cid)
2021-10-10 04:25:34 +00:00
save=g.db.query(SaveRelationship).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment.id, type=2).first()
2021-07-21 01:12:26 +00:00
2021-09-18 19:59:21 +00:00
if save:
g.db.delete(save)
g.db.commit()
2021-09-16 17:02:58 +00:00
2021-09-08 11:05:31 +00:00
return {"message": "Comment unsaved!"}