rDrama/files/routes/comments.py

910 lines
28 KiB
Python
Raw Normal View History

2021-10-15 14:08:27 +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.const import *
from files.classes import *
from files.routes.front import comment_idlist
from pusher_push_notifications import PushNotifications
from flask import *
from files.__main__ import app, limiter
2021-12-07 23:18:06 +00:00
from files.helpers.sanitize import filter_emojis_only
2021-12-18 02:59:40 +00:00
import requests
2022-01-24 23:40:34 +00:00
from shutil import copyfile
2022-01-22 10:41:37 +00:00
from json import loads
2021-10-15 14:08:27 +00:00
2021-12-28 04:47:02 +00:00
IMGUR_KEY = environ.get("IMGUR_KEY").strip()
2021-10-15 14:08:27 +00:00
2022-01-18 11:19:32 +00:00
if PUSHER_ID: beams_client = PushNotifications(instance_id=PUSHER_ID, secret_key=PUSHER_KEY)
2021-10-15 14:08:27 +00:00
2022-01-23 23:06:34 +00:00
CF_KEY = environ.get("CF_KEY", "").strip()
CF_ZONE = environ.get("CF_ZONE", "").strip()
CF_HEADERS = {"Authorization": f"Bearer {CF_KEY}", "Content-Type": "application/json"}
2021-10-15 14:08:27 +00:00
@app.get("/comment/<cid>")
@app.get("/post/<pid>/<anything>/<cid>")
@app.get("/logged_out/comment/<cid>")
@app.get("/logged_out/post/<pid>/<anything>/<cid>")
2022-01-11 22:57:05 +00:00
@auth_desired
2021-10-15 14:08:27 +00:00
def post_pid_comment_cid(cid, pid=None, anything=None, v=None):
2022-01-24 19:40:58 +00:00
if not v and not request.path.startswith('/logged_out'): return redirect(f"{SITE_FULL}/logged_out{request.full_path}")
2021-10-15 14:08:27 +00:00
if v and request.path.startswith('/logged_out'): v = None
try: cid = int(cid)
except:
try: cid = int(cid, 36)
except: abort(404)
comment = get_comment(cid, v=v)
2021-10-20 14:37:46 +00:00
if v and request.values.get("read"):
2022-01-02 00:06:46 +00:00
notif = g.db.query(Notification).filter_by(comment_id=cid, user_id=v.id, read=False).one_or_none()
2021-10-20 14:37:46 +00:00
if notif:
notif.read = True
g.db.add(notif)
g.db.commit()
2021-12-03 17:02:34 +00:00
if comment.post and comment.post.club and not (v and (v.paid_dues or v.id in [comment.author_id, comment.post.author_id])): abort(403)
2021-10-15 14:08:27 +00:00
2022-01-28 06:34:09 +00:00
if comment.post and comment.post.private and not (v and (v.admin_level > 1 or v.id == comment.post.author.id)): abort(403)
2022-01-11 17:42:56 +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 > 1) : abort(403)
2021-10-15 14:08:27 +00:00
if not pid:
if comment.parent_submission: pid = comment.parent_submission
2022-01-01 22:06:53 +00:00
elif request.host == "rdrama.net": pid = 6489
2022-01-03 04:08:46 +00:00
elif request.host == 'pcmemes.net': pid = 2487
2021-10-15 14:08:27 +00:00
else: pid = 1
try: pid = int(pid)
except: abort(404)
post = get_post(pid, v=v)
if post.over_18 and not (v and v.over_18) and not session.get('over_18', 0) >= int(time.time()):
2022-01-07 21:03:14 +00:00
if request.headers.get("Authorization"): return {'error': 'This content is not suitable for some users and situations.'}
2022-01-14 12:09:05 +00:00
else: render_template("errors/nsfw.html", v=v)
2021-10-15 14:08:27 +00:00
try: context = int(request.values.get("context", 0))
except: context = 0
comment_info = comment
c = comment
2022-01-12 01:19:13 +00:00
while context and c.level > 1:
2021-10-15 14:08:27 +00:00
c = c.parent_comment
context -= 1
top_comment = c
if v: defaultsortingcomments = v.defaultsortingcomments
else: defaultsortingcomments = "top"
sort=request.values.get("sort", defaultsortingcomments)
if v:
2021-11-06 15:52:48 +00:00
votes = g.db.query(CommentVote).filter_by(user_id=v.id).subquery()
2021-10-15 14:08:27 +00:00
blocking = v.blocking.subquery()
blocked = v.blocked.subquery()
comments = g.db.query(
Comment,
votes.c.vote_type,
blocking.c.id,
blocked.c.id,
2021-11-06 15:52:48 +00:00
)
2021-10-15 14:08:27 +00:00
2021-11-15 22:13:29 +00:00
if not (v and v.shadowbanned) and not (v and v.admin_level > 1):
2021-11-06 00:33:32 +00:00
comments = comments.join(User, User.id == Comment.author_id).filter(User.shadowbanned == None)
2021-10-15 14:08:27 +00:00
comments=comments.filter(
Comment.parent_submission == post.id,
2021-12-11 02:27:46 +00:00
Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID))
2021-10-15 14:08:27 +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
)
output = []
for c in comments:
comment = c[0]
comment.voted = c[1] or 0
comment.is_blocking = c[2] or 0
comment.is_blocked = c[3] or 0
output.append(comment)
2021-10-22 00:21:51 +00:00
post.replies=[top_comment]
2021-10-15 14:08:27 +00:00
if request.headers.get("Authorization"): return top_comment.json
2021-10-22 00:19:23 +00:00
else:
2021-11-15 22:19:59 +00:00
if post.is_banned and not (v and (v.admin_level > 1 or post.author_id == v.id)): template = "submission_banned.html"
2021-10-22 00:19:23 +00:00
else: template = "submission.html"
2022-01-17 21:26:03 +00:00
return render_template(template, v=v, p=post, sort=sort, comment_info=comment_info, render_replies=True)
2021-10-15 14:08:27 +00:00
@app.post("/comment")
2022-01-27 20:15:05 +00:00
@limiter.limit("1/second;20/minute;200/hour;1000/day")
2022-01-06 16:46:09 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def api_comment(v):
2022-01-06 16:46:09 +00:00
if v.is_suspended: return {"error": "You can't perform this action while banned."}, 403
2021-11-11 19:14:11 +00:00
if v and v.patron:
2022-01-16 05:53:29 +00:00
if request.content_length > 8 * 1024 * 1024: return {"error":"Max file size is 8 MB."}, 413
elif request.content_length > 4 * 1024 * 1024: return {"error":"Max file size is 4 MB."}, 413
2021-10-15 14:08:27 +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)
2021-12-02 18:45:03 +00:00
if parent_post.club and not (v and (v.paid_dues or v.id == parent_post.author_id)): abort(403)
2021-10-15 14:08:27 +00:00
if parent_fullname.startswith("t2_"):
parent = parent_post
parent_comment_id = None
2021-12-05 02:21:38 +00:00
top_comment_id = None
2021-10-15 14:08:27 +00:00
level = 1
elif parent_fullname.startswith("t3_"):
parent = get_comment(parent_fullname.split("_")[1], v=v)
parent_comment_id = parent.id
level = parent.level + 1
2021-12-05 02:21:38 +00:00
if level == 2: top_comment_id = parent.id
else: top_comment_id = parent.top_comment_id
2021-10-15 14:08:27 +00:00
else: abort(400)
2021-12-11 16:22:25 +00:00
body = request.values.get("body", "").strip()[:10000]
2022-01-23 23:06:34 +00:00
if v.admin_level == 3 and parent_post.id == 37749:
with open(f"snappy_{SITE_NAME}.txt", "a") as f:
f.write('\n{[para]}\n' + body)
2022-01-22 00:08:55 +00:00
2021-10-27 00:37:34 +00:00
if v.marseyawarded:
2022-01-27 17:04:02 +00:00
marregex = list(re.finditer("^(:[!#]{0,2}m\w+:\s*)+$", body, flags=re.A))
2022-01-14 08:01:19 +00:00
if len(marregex) == 0: return {"error":"You can only type marseys!"}, 403
2021-10-27 00:37:34 +00:00
2022-01-14 08:01:19 +00:00
if v.longpost and len(body) < 280 or ' [](' in body or body.startswith('[]('): return {"error":"You have to type more than 280 characters!"}, 403
elif v.bird and len(body) > 140: return {"error":"You have to type less than 140 characters!"}, 403
2021-11-18 20:50:03 +00:00
2021-10-15 14:08:27 +00:00
if not body and not request.files.get('file'): return {"error":"You need to actually write something!"}, 400
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)})')
options = []
2022-01-27 17:04:02 +00:00
for i in re.finditer('\s*\$\$([^\$\n]+)\$\$\s*', body, flags=re.A):
2021-10-15 14:08:27 +00:00
options.append(i.group(1))
2021-11-04 20:34:08 +00:00
body = body.replace(i.group(0), "")
2021-10-15 14:08:27 +00:00
2021-10-18 18:41:56 +00:00
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
file=request.files["file"]
2021-12-18 02:59:40 +00:00
if file.content_type.startswith('image/'):
2022-01-24 23:40:34 +00:00
oldname = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp'
file.save(oldname)
image = process_image(oldname)
2022-01-22 18:51:33 +00:00
if image == "": return {"error":"Image upload failed"}
2022-01-21 20:17:27 +00:00
if v.admin_level == 3:
if parent_post.id == 37696:
filename = 'files/assets/images/Drama/sidebar/' + str(len(listdir('files/assets/images/Drama/sidebar'))+1) + '.webp'
2022-01-24 23:40:34 +00:00
copyfile(oldname, filename)
process_image(filename, 400)
2022-01-21 20:17:27 +00:00
elif parent_post.id == 37697:
2022-01-22 00:52:25 +00:00
filename = 'files/assets/images/Drama/banners/' + str(len(listdir('files/assets/images/Drama/banners'))+1) + '.webp'
2022-01-24 23:40:34 +00:00
copyfile(oldname, filename)
process_image(filename)
2022-01-24 00:02:50 +00:00
elif parent_post.id == 37833:
2022-01-24 23:40:34 +00:00
try:
badge_def = loads(body.lower())
name = badge_def["name"]
badge = g.db.query(BadgeDef).filter_by(name=name).first()
if not badge:
badge = BadgeDef(name=name, description=badge_def["description"])
g.db.add(badge)
g.db.flush()
filename = f'files/assets/images/badges/{badge.id}.webp'
copyfile(oldname, filename)
process_image(filename, 200)
requests.post(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/purge_cache', headers=CF_HEADERS, data={'files': [f"https://{request.host}/static/assets/images/badges/{badge.id}.webp"]})
except Exception as e:
print(e)
return {"error": "You didn't follow the format retard"}, 400
2022-01-27 21:26:20 +00:00
elif v.admin_level > 2 and parent_post.id == 37838:
2022-01-24 23:40:34 +00:00
try:
marsey = loads(body.lower())
name = marsey["name"]
if not g.db.query(Marsey.name).filter_by(name=name).first():
marsey = Marsey(name=marsey["name"], author_id=marsey["author_id"], tags=marsey["tags"], count=0)
g.db.add(marsey)
filename = f'files/assets/images/emojis/{name}.webp'
copyfile(oldname, filename)
process_image(filename, 200)
requests.post(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/purge_cache', headers=CF_HEADERS, data={'files': [f"https://{request.host}/static/assets/images/emojis/{name}.webp"]})
except Exception as e:
print(e)
return {"error": "You didn't follow the format retard"}, 400
2022-01-23 23:06:34 +00:00
body += f"\n\n![]({image})"
2021-12-18 02:59:40 +00:00
elif file.content_type.startswith('video/'):
file.save("video.mp4")
with open("video.mp4", 'rb') as f:
2022-01-06 14:20:45 +00:00
try: url = requests.request("POST", "https://api.imgur.com/3/upload", headers={'Authorization': f'Client-ID {IMGUR_KEY}'}, files=[('video', f)]).json()['data']['link']
except: return {"error": "Imgur error"}, 400
2022-01-06 17:57:59 +00:00
if url.endswith('.'): url += 'mp4'
2021-12-18 04:48:10 +00:00
body += f"\n\n{url}"
2022-01-07 21:03:14 +00:00
else: return {"error": "Image/Video files only"}, 400
2021-10-18 18:41:56 +00:00
2021-12-29 06:43:20 +00:00
if v.agendaposter and not v.marseyawarded: body = torture_ap(body, v.username)
2021-11-24 17:49:17 +00:00
2022-01-28 02:54:50 +00:00
if '#fortune' in body:
body = body.replace('#fortune', '')
body += '\n\n<p>' + random.choice(FORTUNE_REPLIES) + '</p>'
2022-01-13 23:44:55 +00:00
body_html = sanitize(body, comment=True)
2021-10-15 14:08:27 +00:00
2022-01-27 17:04:02 +00:00
if v.marseyawarded and len(list(re.finditer('>[^<\s+]|[^>\s+]<', body_html, flags=re.A))): return {"error":"You can only type marseys!"}, 403
2021-10-27 20:12:16 +00:00
2021-11-23 22:36:38 +00:00
if v.longpost:
if len(body) < 280 or ' [](' in body or body.startswith('[]('): return {"error":"You have to type more than 280 characters!"}, 403
elif v.bird:
if len(body) > 140 : return {"error":"You have to type less than 140 characters!"}, 403
2021-11-18 20:50:03 +00:00
2021-10-15 14:08:27 +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."
if ban.reason: reason += f" {ban.reason}"
return {"error": reason}, 401
2022-01-25 18:56:50 +00:00
if parent_post.id not in (37696,37697,37749,37833,37838) and not body.startswith('!slots') and not body.startswith('!casino'):
2022-01-22 20:03:46 +00:00
existing = g.db.query(Comment.id).filter(Comment.author_id == v.id,
Comment.deleted_utc == 0,
Comment.parent_comment_id == parent_comment_id,
Comment.parent_submission == parent_submission,
Comment.body_html == body_html
).one_or_none()
if existing: return {"error": f"You already made that comment: /comment/{existing.id}"}, 409
2021-10-15 14:08:27 +00:00
2021-11-15 22:19:59 +00:00
if parent.author.any_block_exists(v) and v.admin_level < 2: return {"error": "You can't reply to users who have blocked you, or users you have blocked."}, 403
2021-10-15 14:08:27 +00:00
2021-12-16 17:59:30 +00:00
is_bot = bool(request.headers.get("Authorization"))
2021-10-15 14:08:27 +00:00
if not is_bot and not v.marseyawarded and AGENDAPOSTER_PHRASE not in body.lower() and len(body) > 10:
2021-10-15 14:08:27 +00:00
now = int(time.time())
cutoff = now - 60 * 60 * 24
2021-11-07 13:37:26 +00:00
similar_comments = g.db.query(Comment).filter(
2021-10-15 14:08:27 +00:00
Comment.author_id == v.id,
Comment.body.op(
'<->')(body) < app.config["COMMENT_SPAM_SIMILAR_THRESHOLD"],
Comment.created_utc > cutoff
).all()
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:
text = "Your account has been suspended for 1 day for the following reason:\n\n> Too much spam!"
2021-12-20 20:03:59 +00:00
send_repeatable_notification(v.id, text)
2021-10-15 14:08:27 +00:00
v.ban(reason="Spamming.",
days=1)
for comment in similar_comments:
comment.is_banned = True
2021-11-05 14:40:14 +00:00
comment.ban_reason = "AutoJanny"
2021-10-15 14:08:27 +00:00
g.db.add(comment)
ma=ModAction(
2021-11-18 14:21:19 +00:00
user_id=AUTOJANNY_ID,
2021-10-15 14:08:27 +00:00
target_comment_id=comment.id,
kind="ban_comment",
2021-10-25 18:08:03 +00:00
_note="spam"
2021-10-15 14:08:27 +00:00
)
g.db.add(ma)
return {"error": "Too much spam!"}, 403
if len(body_html) > 20000: abort(400)
c = Comment(author_id=v.id,
parent_submission=parent_submission,
parent_comment_id=parent_comment_id,
2021-12-05 02:21:38 +00:00
top_comment_id=top_comment_id,
2021-10-15 14:08:27 +00:00
level=level,
2022-01-22 09:58:22 +00:00
over_18=parent_post.over_18 or request.values.get("over_18")=="true",
2021-10-15 14:08:27 +00:00
is_bot=is_bot,
app_id=v.client.application.id if v.client else None,
body_html=body_html,
2022-01-27 22:43:59 +00:00
body=body[:10000],
ghost=parent_post.ghost
2021-10-15 14:08:27 +00:00
)
c.upvotes = 1
g.db.add(c)
g.db.flush()
for option in options:
2021-11-18 14:21:19 +00:00
c_option = Comment(author_id=AUTOPOLLER_ID,
2021-10-15 14:08:27 +00:00
parent_submission=parent_submission,
parent_comment_id=c.id,
level=level+1,
2021-12-07 23:18:06 +00:00
body_html=filter_emojis_only(option),
2022-01-22 09:58:22 +00:00
upvotes=0,
is_bot=True
2021-10-15 14:08:27 +00:00
)
g.db.add(c_option)
2022-01-03 04:08:46 +00:00
if request.host == 'pcmemes.net' and c.body.lower().startswith("based"):
2021-10-15 14:08:27 +00:00
pill = re.match("based and (.{1,20}?)(-| )pilled", body, re.IGNORECASE)
2022-01-27 22:43:59 +00:00
if level == 1: basedguy = get_account(parent_post.author_id)
2021-10-15 14:08:27 +00:00
else: basedguy = get_account(c.parent_comment.author_id)
basedguy.basedcount += 1
2021-11-08 12:39:24 +00:00
if pill:
if basedguy.pills: basedguy.pills += f", {pill.group(1)}"
else: basedguy.pills += f"{pill.group(1)}"
2021-10-15 14:08:27 +00:00
g.db.add(basedguy)
2021-11-08 12:39:24 +00:00
body2 = f"@{basedguy.username}'s Based Count has increased by 1. Their Based Count is now {basedguy.basedcount}."
2021-11-08 12:44:58 +00:00
if basedguy.pills: body2 += f"\n\nPills: {basedguy.pills}"
2021-11-08 12:39:24 +00:00
2022-01-11 19:46:50 +00:00
body_based_html = sanitize(body2)
2021-10-15 14:08:27 +00:00
2021-11-18 14:21:19 +00:00
c_based = Comment(author_id=BASEDBOT_ID,
2021-10-15 14:08:27 +00:00
parent_submission=parent_submission,
distinguish_level=6,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
body_html=body_based_html,
2022-01-27 22:43:59 +00:00
top_comment_id=c.top_comment_id,
ghost=parent_post.ghost
2021-10-15 14:08:27 +00:00
)
g.db.add(c_based)
g.db.flush()
n = Notification(comment_id=c_based.id, user_id=v.id)
g.db.add(n)
if v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in c.body.lower():
2021-10-15 14:08:27 +00:00
c.is_banned = True
2021-11-03 16:15:06 +00:00
c.ban_reason = "AutoJanny"
2021-10-15 14:08:27 +00:00
g.db.add(c)
2022-01-18 11:19:32 +00:00
body = AGENDAPOSTER_MSG.format(username=v.username, type='comment', AGENDAPOSTER_PHRASE=AGENDAPOSTER_PHRASE)
2021-10-15 14:08:27 +00:00
2022-01-11 19:46:50 +00:00
body_jannied_html = sanitize(body)
2021-10-15 14:08:27 +00:00
2021-12-28 06:28:18 +00:00
c_jannied = Comment(author_id=NOTIFICATIONS_ID,
2021-10-15 14:08:27 +00:00
parent_submission=parent_submission,
distinguish_level=6,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
body_html=body_jannied_html,
2022-01-27 22:43:59 +00:00
top_comment_id=c.top_comment_id,
ghost=parent_post.ghost
2021-10-15 14:08:27 +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)
2022-01-01 22:06:53 +00:00
if request.host == "rdrama.net" and len(c.body) >= 1000 and "<" not in body and "</blockquote>" not in body_html:
2021-10-15 14:08:27 +00:00
body = random.choice(LONGPOST_REPLIES)
2022-01-11 19:46:50 +00:00
body_html2 = sanitize(body)
2021-10-15 14:08:27 +00:00
2021-11-18 14:21:19 +00:00
c2 = Comment(author_id=LONGPOSTBOT_ID,
2021-10-15 14:08:27 +00:00
parent_submission=parent_submission,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
body_html=body_html2,
2022-01-27 22:43:59 +00:00
top_comment_id=c.top_comment_id,
ghost=parent_post.ghost
2021-10-15 14:08:27 +00:00
)
g.db.add(c2)
2021-10-27 20:38:10 +00:00
2022-01-02 00:06:46 +00:00
longpostbot = g.db.query(User).filter_by(id = LONGPOSTBOT_ID).one_or_none()
2021-10-27 20:40:05 +00:00
longpostbot.comment_count += 1
2021-12-27 01:08:06 +00:00
longpostbot.coins += 1
2021-10-27 20:38:10 +00:00
g.db.add(longpostbot)
2021-10-15 14:08:27 +00:00
g.db.flush()
n = Notification(comment_id=c2.id, user_id=v.id)
g.db.add(n)
2022-01-01 22:06:53 +00:00
if request.host == "rdrama.net" and random.random() < 0.001:
2021-10-15 14:08:27 +00:00
body = "zoz"
2022-01-11 19:46:50 +00:00
body_html2 = sanitize(body)
2021-10-15 14:08:27 +00:00
2021-11-18 14:21:19 +00:00
c2 = Comment(author_id=ZOZBOT_ID,
2021-10-15 14:08:27 +00:00
parent_submission=parent_submission,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
body_html=body_html2,
2022-01-27 22:43:59 +00:00
top_comment_id=c.top_comment_id,
ghost=parent_post.ghost
2021-10-15 14:08:27 +00:00
)
g.db.add(c2)
g.db.flush()
n = Notification(comment_id=c2.id, user_id=v.id)
g.db.add(n)
body = "zle"
2022-01-11 19:46:50 +00:00
body_html2 = sanitize(body)
2021-10-15 14:08:27 +00:00
2021-11-18 14:21:19 +00:00
c3 = Comment(author_id=ZOZBOT_ID,
2021-10-15 14:08:27 +00:00
parent_submission=parent_submission,
parent_comment_id=c2.id,
level=level+2,
is_bot=True,
body_html=body_html2,
2022-01-27 22:43:59 +00:00
top_comment_id=c.top_comment_id,
ghost=parent_post.ghost
2021-10-15 14:08:27 +00:00
)
g.db.add(c3)
2021-12-27 01:08:06 +00:00
g.db.flush()
2021-10-15 14:08:27 +00:00
body = "zozzle"
2022-01-11 19:46:50 +00:00
body_html2 = sanitize(body)
2021-10-15 14:08:27 +00:00
2021-11-18 14:21:19 +00:00
c4 = Comment(author_id=ZOZBOT_ID,
2021-10-15 14:08:27 +00:00
parent_submission=parent_submission,
parent_comment_id=c3.id,
level=level+3,
is_bot=True,
body_html=body_html2,
2022-01-27 22:43:59 +00:00
top_comment_id=c.top_comment_id,
ghost=parent_post.ghost
2021-10-15 14:08:27 +00:00
)
g.db.add(c4)
2021-10-27 20:38:10 +00:00
2022-01-02 00:06:46 +00:00
zozbot = g.db.query(User).filter_by(id = ZOZBOT_ID).one_or_none()
2021-10-27 20:40:05 +00:00
zozbot.comment_count += 3
2021-10-29 17:07:00 +00:00
zozbot.coins += 3
2021-10-27 20:38:10 +00:00
g.db.add(zozbot)
2021-10-15 14:08:27 +00:00
2022-01-22 18:47:09 +00:00
if not v.shadowbanned and parent_post.id not in (37696,37697,37749,37833,37838):
2021-12-29 06:43:20 +00:00
notify_users = NOTIFY_USERS(body_html, v)
2021-10-15 14:08:27 +00:00
2021-11-06 15:52:48 +00:00
for x in g.db.query(Subscription.user_id).filter_by(submission_id=c.parent_submission).all(): notify_users.add(x[0])
2021-10-15 14:08:27 +00:00
2021-11-18 14:21:19 +00:00
if parent.author.id not in [v.id, BASEDBOT_ID, AUTOJANNY_ID, SNAPPY_ID, LONGPOSTBOT_ID, ZOZBOT_ID, AUTOPOLLER_ID]: notify_users.add(parent.author.id)
2021-10-15 14:08:27 +00:00
for x in notify_users:
n = Notification(comment_id=c.id, user_id=x)
g.db.add(n)
2022-01-18 11:19:32 +00:00
if parent.author.id != v.id and PUSHER_ID:
2022-01-16 07:42:05 +00:00
if len(c.body) > 500: notifbody = c.body[:500] + '...'
2022-01-27 17:07:10 +00:00
else: notifbody = c.body or 'no body'
2022-01-17 15:13:35 +00:00
2022-01-21 20:06:39 +00:00
beams_client.publish_to_interests(
interests=[f'{request.host}{parent.author.id}'],
publish_body={
2022-01-17 15:13:35 +00:00
'web': {
'notification': {
2022-01-17 21:53:43 +00:00
'title': f'New reply by @{c.author_name}',
2022-01-17 15:13:35 +00:00
'body': notifbody,
2022-01-24 17:37:37 +00:00
'deep_link': f'{SITE_FULL}/comment/{c.id}?context=9&read=true#context',
'icon': f'{SITE_FULL}/assets/images/{SITE_NAME}/icon.webp',
2022-01-17 15:13:35 +00:00
}
2021-10-15 14:08:27 +00:00
},
2022-01-17 15:13:35 +00:00
'fcm': {
'notification': {
2022-01-17 21:53:43 +00:00
'title': f'New reply by @{c.author_name}',
2022-01-17 15:13:35 +00:00
'body': notifbody,
},
'data': {
2022-01-21 11:14:24 +00:00
'url': f'/comment/{c.id}?context=9&read=true#context',
2022-01-17 15:13:35 +00:00
}
2022-01-15 08:22:49 +00:00
}
2022-01-21 20:06:39 +00:00
},
)
2021-10-15 14:08:27 +00:00
vote = CommentVote(user_id=v.id,
comment_id=c.id,
2021-11-30 14:18:16 +00:00
vote_type=1,
2021-10-15 14:08:27 +00:00
)
g.db.add(vote)
cache.delete_memoized(comment_idlist)
2021-11-06 15:52:48 +00:00
v.comment_count = g.db.query(Comment.id).filter(Comment.author_id == v.id, Comment.parent_submission != None).filter_by(is_banned=False, deleted_utc=0).count()
2021-10-15 14:08:27 +00:00
g.db.add(v)
parent_post.comment_count += 1
g.db.add(parent_post)
c.voted = 1
2022-01-17 11:47:30 +00:00
if v.id == PIZZASHILL_ID:
2022-01-14 11:53:38 +00:00
autovote = CommentVote(user_id=CARP_ID, comment_id=c.id, vote_type=1)
2022-01-11 19:46:50 +00:00
g.db.add(autovote)
2022-01-14 11:53:38 +00:00
autovote = CommentVote(user_id=AEVANN_ID, comment_id=c.id, vote_type=1)
g.db.add(autovote)
autovote = CommentVote(user_id=CRAT_ID, comment_id=c.id, vote_type=1)
g.db.add(autovote)
v.coins += 3
v.truecoins += 3
2022-01-11 19:46:50 +00:00
g.db.add(v)
2022-01-14 11:53:38 +00:00
c.upvotes += 3
2022-01-11 19:46:50 +00:00
g.db.add(c)
2022-01-17 11:47:30 +00:00
elif v.id == HIL_ID:
autovote = CommentVote(user_id=CARP_ID, comment_id=c.id, vote_type=1)
g.db.add(autovote)
v.coins += 1
v.truecoins += 1
g.db.add(v)
c.upvotes += 1
g.db.add(c)
2022-01-11 19:46:50 +00:00
slots = Slots(g)
2022-01-24 15:34:29 +00:00
slots.check_for_slots_command(body, v, c)
blackjack = Blackjack(g)
blackjack.check_for_blackjack_command(body, v, c)
2022-01-22 13:04:30 +00:00
g.db.commit()
2022-01-24 15:34:29 +00:00
2021-10-15 14:08:27 +00:00
if request.headers.get("Authorization"): return c.json
2022-01-14 12:04:34 +00:00
return render_template("comments.html", v=v, comments=[c])
2021-10-15 14:08:27 +00:00
@app.post("/edit_comment/<cid>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def edit_comment(cid, v):
2021-11-11 19:14:11 +00:00
if v and v.patron:
2022-01-16 05:53:29 +00:00
if request.content_length > 8 * 1024 * 1024: return {"error":"Max file size is 8 MB."}, 413
elif request.content_length > 4 * 1024 * 1024: return {"error":"Max file size is 4 MB."}, 413
2021-10-15 14:08:27 +00:00
c = get_comment(cid, v=v)
2022-01-22 09:58:22 +00:00
if c.author_id != v.id: abort(403)
2021-10-15 14:08:27 +00:00
2021-12-11 16:22:25 +00:00
body = request.values.get("body", "").strip()[:10000]
2021-12-10 19:53:16 +00:00
2022-01-27 21:26:20 +00:00
if len(body) < 1 and not (request.files.get("file") and request.headers.get("cf-ipcountry") != "T1"):
return {"error":"You have to actually type something!"}, 400
2021-10-27 00:37:34 +00:00
2021-12-30 05:47:09 +00:00
if body != c.body or request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
2021-10-28 22:33:08 +00:00
if v.marseyawarded:
2022-01-27 17:04:02 +00:00
marregex = list(re.finditer("^(:[!#]{0,2}m\w+:\s*)+$", body, flags=re.A))
2022-01-14 08:01:19 +00:00
if len(marregex) == 0: return {"error":"You can only type marseys!"}, 403
if v.longpost and len(body) < 280 or ' [](' in body or body.startswith('[]('): return {"error":"You have to type more than 280 characters!"}, 403
elif v.bird and len(body) > 140: return {"error":"You have to type less than 140 characters!"}, 403
2021-11-18 20:50:03 +00:00
2021-10-28 22:33:08 +00:00
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)})')
2021-11-24 17:33:44 +00:00
2021-12-29 06:43:20 +00:00
if v.agendaposter and not v.marseyawarded: body = torture_ap(body, v.username)
2021-11-24 17:49:17 +00:00
2021-12-03 23:34:03 +00:00
if not c.options:
2022-01-27 17:04:02 +00:00
for i in re.finditer('\s*\$\$([^\$\n]+)\$\$\s*', body, flags=re.A):
2021-12-03 23:34:03 +00:00
body = body.replace(i.group(0), "")
c_option = Comment(author_id=AUTOPOLLER_ID,
parent_submission=c.parent_submission,
parent_comment_id=c.id,
level=c.level+1,
2021-12-07 23:18:06 +00:00
body_html=filter_emojis_only(i.group(1)),
2022-01-22 09:58:22 +00:00
upvotes=0,
is_bot=True
2021-12-03 23:34:03 +00:00
)
g.db.add(c_option)
2022-01-19 06:20:05 +00:00
body_html = sanitize(body, edit=True)
2021-10-15 14:08:27 +00:00
2022-01-27 17:04:02 +00:00
if v.marseyawarded and len(list(re.finditer('>[^<\s+]|[^>\s+]<', body_html, flags=re.A))): return {"error":"You can only type marseys!"}, 403
2021-10-27 20:12:16 +00:00
2021-11-23 22:36:38 +00:00
if v.longpost:
if len(body) < 280 or ' [](' in body or body.startswith('[]('): return {"error":"You have to type more than 280 characters!"}, 403
elif v.bird:
if len(body) > 140 : return {"error":"You have to type less than 140 characters!"}, 403
2021-11-18 20:50:03 +00:00
2021-10-28 22:33:08 +00:00
bans = filter_comment_html(body_html)
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
if bans:
ban = bans[0]
reason = f"Remove the {ban.domain} link from your comment and try again."
if ban.reason: reason += f" {ban.reason}"
2021-10-15 14:08:27 +00:00
2022-01-07 21:03:14 +00:00
if request.headers.get("Authorization"): return {'error': 'A blacklisted domain was used.'}, 400
2022-01-14 12:04:34 +00:00
return render_template("comment_failed.html",
2021-10-28 22:33:08 +00:00
action=f"/edit_comment/{c.id}",
badlinks=[x.domain for x in bans],
body=body,
v=v
)
if AGENDAPOSTER_PHRASE not in body.lower():
2021-10-28 22:33:08 +00:00
now = int(time.time())
cutoff = now - 60 * 60 * 24
2021-10-22 23:50:00 +00:00
2021-10-28 22:33:08 +00:00
similar_comments = g.db.query(Comment
).filter(
Comment.author_id == v.id,
Comment.body.op(
'<->')(body) < app.config["SPAM_SIMILARITY_THRESHOLD"],
Comment.created_utc > cutoff
).all()
2021-10-22 23:50:00 +00:00
2021-10-28 22:33:08 +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
2021-10-22 23:50:00 +00:00
2021-10-28 22:33:08 +00:00
if len(similar_comments) > threshold:
text = "Your account has been suspended for 1 day for the following reason:\n\n> Too much spam!"
2021-12-20 20:03:59 +00:00
send_repeatable_notification(v.id, text)
2021-10-22 23:50:00 +00:00
2021-10-28 22:33:08 +00:00
v.ban(reason="Spamming.",
days=1)
2021-10-22 23:50:00 +00:00
2021-10-28 22:33:08 +00:00
for comment in similar_comments:
comment.is_banned = True
2021-11-05 14:40:14 +00:00
comment.ban_reason = "AutoJanny"
2021-10-28 22:33:08 +00:00
g.db.add(comment)
2021-10-22 23:50:00 +00:00
2021-10-28 22:33:08 +00:00
return {"error": "Too much spam!"}, 403
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
file=request.files["file"]
2021-12-18 02:59:40 +00:00
if file.content_type.startswith('image/'):
2022-01-24 23:40:34 +00:00
name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp'
file.save(name)
url = process_image(name)
body += f"\n\n![]({url})"
2021-12-18 02:59:40 +00:00
elif file.content_type.startswith('video/'):
file.save("video.mp4")
with open("video.mp4", 'rb') as f:
2022-01-06 14:20:45 +00:00
try: url = requests.request("POST", "https://api.imgur.com/3/upload", headers={'Authorization': f'Client-ID {IMGUR_KEY}'}, files=[('video', f)]).json()['data']['link']
except: return {"error": "Imgur error"}, 400
2022-01-06 17:57:59 +00:00
if url.endswith('.'): url += 'mp4'
2021-12-18 04:48:10 +00:00
body += f"\n\n{url}"
2022-01-07 21:03:14 +00:00
else: return {"error": "Image/Video files only"}, 400
2021-12-18 02:59:40 +00:00
2022-01-19 06:20:05 +00:00
body_html = sanitize(body, edit=True)
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
if len(body_html) > 20000: abort(400)
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
c.body = body[:10000]
c.body_html = body_html
2021-10-15 14:08:27 +00:00
if v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in c.body.lower():
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
c.is_banned = True
2021-11-03 16:15:06 +00:00
c.ban_reason = "AutoJanny"
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
g.db.add(c)
2021-10-15 14:08:27 +00:00
2022-01-18 11:19:32 +00:00
body = AGENDAPOSTER_MSG.format(username=v.username, type='comment', AGENDAPOSTER_PHRASE=AGENDAPOSTER_PHRASE)
2021-10-15 14:08:27 +00:00
2022-01-11 19:46:50 +00:00
body_jannied_html = sanitize(body)
2021-10-15 14:08:27 +00:00
2021-12-28 06:28:18 +00:00
c_jannied = Comment(author_id=NOTIFICATIONS_ID,
2021-10-28 22:33:08 +00:00
parent_submission=c.parent_submission,
distinguish_level=6,
parent_comment_id=c.id,
level=c.level+1,
is_bot=True,
body_html=body_jannied_html,
2022-01-27 22:43:59 +00:00
top_comment_id=c.top_comment_id,
ghost=c.ghost
2021-10-28 22:33:08 +00:00
)
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
g.db.add(c_jannied)
g.db.flush()
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
n = Notification(comment_id=c_jannied.id, user_id=v.id)
g.db.add(n)
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
if int(time.time()) - c.created_utc > 60 * 3: c.edited_utc = int(time.time())
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
g.db.add(c)
2021-12-29 06:43:20 +00:00
notify_users = NOTIFY_USERS(body_html, v)
2021-12-26 02:29:01 +00:00
2021-11-13 14:21:53 +00:00
for x in notify_users:
2022-01-02 00:06:46 +00:00
notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=x).one_or_none()
2021-11-13 14:21:53 +00:00
if not notif:
n = Notification(comment_id=c.id, user_id=x)
g.db.add(n)
2021-10-15 14:08:27 +00:00
2021-10-28 22:33:08 +00:00
g.db.commit()
2021-10-15 14:08:27 +00:00
2022-01-19 14:08:45 +00:00
return c.realbody(v)
2021-10-15 14:08:27 +00:00
@app.post("/delete/comment/<cid>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def delete_comment(cid, v):
2022-01-02 00:06:46 +00:00
c = g.db.query(Comment).filter_by(id=cid).one_or_none()
2021-10-15 14:08:27 +00:00
if not c: abort(404)
2022-01-22 09:58:22 +00:00
if c.author_id != v.id: abort(403)
2021-10-15 14:08:27 +00:00
c.deleted_utc = int(time.time())
g.db.add(c)
cache.delete_memoized(comment_idlist)
g.db.commit()
return {"message": "Comment deleted!"}
@app.post("/undelete/comment/<cid>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def undelete_comment(cid, v):
2022-01-02 00:06:46 +00:00
c = g.db.query(Comment).filter_by(id=cid).one_or_none()
2021-10-15 14:08:27 +00:00
2022-01-25 18:31:24 +00:00
if not c: abort(404)
2021-10-15 14:08:27 +00:00
2022-01-22 09:58:22 +00:00
if c.author_id != v.id:
2021-10-15 14:08:27 +00:00
abort(403)
c.deleted_utc = 0
g.db.add(c)
cache.delete_memoized(comment_idlist)
g.db.commit()
return {"message": "Comment undeleted!"}
@app.post("/pin_comment/<cid>")
@auth_required
2021-12-26 01:03:21 +00:00
def pin_comment(cid, v):
2021-10-15 14:08:27 +00:00
comment = get_comment(cid, v=v)
2022-01-25 18:31:24 +00:00
if not comment: abort(404)
2021-12-26 01:03:21 +00:00
if v.id != comment.post.author_id: abort(403)
comment.is_pinned = v.username + " (OP)"
2021-10-15 14:08:27 +00:00
g.db.add(comment)
2021-12-26 01:03:21 +00:00
if v.id != comment.author_id:
message = f"@{v.username} (OP) has pinned your [comment]({comment.permalink})!"
send_repeatable_notification(comment.author_id, message)
2021-10-15 14:08:27 +00:00
g.db.commit()
2021-12-26 01:03:21 +00:00
return {"message": "Comment pinned!"}
2021-10-15 14:08:27 +00:00
2021-12-26 01:03:21 +00:00
@app.post("/unpin_comment/<cid>")
@auth_required
def unpin_comment(cid, v):
2021-10-15 14:08:27 +00:00
2021-12-26 01:03:21 +00:00
comment = get_comment(cid, v=v)
2021-10-15 14:08:27 +00:00
2022-01-25 18:31:24 +00:00
if not comment: abort(404)
2021-12-26 01:03:21 +00:00
if v.id != comment.post.author_id: abort(403)
if not comment.is_pinned.endswith(" (OP)"):
return {"error": "You can only unpin comments you have pinned!"}
comment.is_pinned = None
g.db.add(comment)
if v.id != comment.author_id:
message = f"@{v.username} (OP) has unpinned your [comment]({comment.permalink})!"
send_repeatable_notification(comment.author_id, message)
g.db.commit()
return {"message": "Comment unpinned!"}
2021-10-15 14:08:27 +00:00
@app.post("/save_comment/<cid>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def save_comment(cid, v):
comment=get_comment(cid)
2022-01-02 00:06:46 +00:00
save=g.db.query(SaveRelationship).filter_by(user_id=v.id, comment_id=comment.id, type=2).one_or_none()
2021-10-15 14:08:27 +00:00
if not save:
new_save=SaveRelationship(user_id=v.id, comment_id=comment.id, type=2)
g.db.add(new_save)
2021-11-15 22:49:48 +00:00
2021-10-15 14:08:27 +00:00
try: g.db.commit()
except: g.db.rollback()
return {"message": "Comment saved!"}
@app.post("/unsave_comment/<cid>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-10-15 14:08:27 +00:00
@auth_required
def unsave_comment(cid, v):
comment=get_comment(cid)
2022-01-02 00:06:46 +00:00
save=g.db.query(SaveRelationship).filter_by(user_id=v.id, comment_id=comment.id, type=2).one_or_none()
2021-10-15 14:08:27 +00:00
if save:
g.db.delete(save)
g.db.commit()
return {"message": "Comment unsaved!"}
@app.post("/blackjack/<cid>")
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def handle_blackjack_action(cid, v):
comment = get_comment(cid)
action = request.values.get("action", "")
blackjack = Blackjack(g)
if action == 'hit':
blackjack.player_hit(comment)
elif action == 'stay':
blackjack.player_stayed(comment)
return { "message" : "..." }