rDrama/files/routes/comments.py

1086 lines
32 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 *
2022-02-14 20:29:36 +00:00
from files.helpers.slots import *
from files.helpers.blackjack import *
from files.helpers.treasure import *
2021-10-15 14:08:27 +00:00
from files.classes import *
from files.routes.front import comment_idlist
2022-01-30 22:57:09 +00:00
from files.routes.static import marsey_list
2021-10-15 14:08:27 +00:00
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
2022-02-18 09:39:12 +00:00
from collections import Counter
2022-02-22 13:34:41 +00:00
from enchant import Dict
2022-02-24 17:51:25 +00:00
import gevent
2022-02-24 17:52:44 +00:00
from sys import stdout
2022-02-22 12:40:43 +00:00
2022-02-22 13:34:41 +00:00
d = Dict("en_US")
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-03-22 15:45:52 +00:00
if PUSHER_ID != 'blahblahblah':
2022-02-21 04:04:29 +00:00
beams_client = PushNotifications(instance_id=PUSHER_ID, secret_key=PUSHER_KEY)
2021-10-15 14:08:27 +00:00
2022-02-18 09:39:12 +00:00
WORDLE_COLOR_MAPPINGS = {-1: "🟥", 0: "🟨", 1: "🟩"}
2022-01-23 23:06:34 +00:00
2022-02-24 16:48:24 +00:00
def pusher_thread(interests, c):
if len(c.body) > 500: notifbody = c.body[:500] + '...'
else: notifbody = c.body
beams_client.publish_to_interests(
interests=[interests],
publish_body={
'web': {
'notification': {
'title': f'New reply by @{c.author_name}',
'body': notifbody,
'deep_link': f'{SITE_FULL}/comment/{c.id}?context=8&read=true#context',
2022-03-02 00:05:30 +00:00
'icon': f'{SITE_FULL}/assets/images/{SITE_NAME}/icon.webp?v=1012',
2022-02-24 16:48:24 +00:00
}
},
'fcm': {
'notification': {
'title': f'New reply by @{c.author_name}',
'body': notifbody,
},
'data': {
'url': f'/comment/{c.id}?context=8&read=true#context',
}
}
},
)
stdout.flush()
2021-10-15 14:08:27 +00:00
@app.get("/comment/<cid>")
@app.get("/post/<pid>/<anything>/<cid>")
2022-03-09 02:04:37 +00:00
@app.get("/h/<sub>/comment/<cid>")
@app.get("/h/<sub>/post/<pid>/<anything>/<cid>")
2022-01-11 22:57:05 +00:00
@auth_desired
2022-02-04 18:35:39 +00:00
def post_pid_comment_cid(cid, pid=None, anything=None, v=None, sub=None):
2022-03-03 20:22:23 +00:00
2021-10-15 14:08:27 +00:00
try: cid = int(cid)
2022-02-04 08:59:12 +00:00
except: abort(404)
2021-10-15 14:08:27 +00:00
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-04-02 16:54:27 +00:00
elif SITE_NAME == 'rDrama': 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
2022-01-30 13:45:06 +00:00
try: context = min(int(request.values.get("context", 0)), 8)
2021-10-15 14:08:27 +00:00
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,
2022-02-14 22:50:27 +00:00
blocking.c.target_id,
blocked.c.target_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,
2022-02-07 11:39:26 +00:00
Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_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-02-05 21:09:17 +00:00
return render_template(template, v=v, p=post, sort=sort, comment_info=comment_info, render_replies=True, sub=post.subr)
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
2022-02-24 08:28:13 +00:00
if v.admin_level < 3:
2022-02-24 03:46:09 +00:00
if v and v.patron:
if request.content_length > 8 * 1024 * 1024: return {"error":"Max file size is 4 MB (8 MB for paypigs)."}, 413
elif request.content_length > 4 * 1024 * 1024: return {"error":"Max file size is 4 MB (8 MB for paypigs)."}, 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)
2022-02-16 04:33:13 +00:00
sub = parent_post.sub
2022-03-09 02:04:37 +00:00
if sub and v.exiled_from(sub): return {"error": f"You're exiled from /h/{sub}"}, 403
2022-02-16 04:33:13 +00:00
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
2022-02-21 06:15:33 +00:00
rts = False
2021-10-15 14:08:27 +00:00
if parent_fullname.startswith("t2_"):
parent = parent_post
parent_comment_id = None
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
2022-02-21 06:15:33 +00:00
if parent.author_id == v.id: rts = True
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-04-03 20:16:54 +00:00
if v.admin_level > 2 and parent_post.id == 37749 and level == 1:
2022-03-17 17:45:09 +00:00
with open(f"snappy_{SITE_NAME}.txt", "a", encoding="utf-8") as f:
2022-01-23 23:06:34 +00:00
f.write('\n{[para]}\n' + body)
2022-01-22 00:08:55 +00:00
2022-03-09 01:44:53 +00:00
if parent_post.id not in ADMIGGERS:
2022-03-06 19:03:50 +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
2022-02-27 21:57:44 +00:00
body = image_regex.sub(r'![](\1)', body)
2021-10-15 14:08:27 +00:00
options = []
2022-02-27 21:57:44 +00:00
for i in poll_regex.finditer(body):
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
2022-02-07 11:39:26 +00:00
choices = []
2022-02-27 21:57:44 +00:00
for i in choice_regex.finditer(body):
2022-02-07 11:39:26 +00:00
choices.append(i.group(1))
body = body.replace(i.group(0), "")
2021-10-18 18:41:56 +00:00
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
2022-02-24 19:05:04 +00:00
files = request.files.getlist('file')[:4]
for file in files:
if file.content_type.startswith('image/'):
2022-03-25 22:30:15 +00:00
oldname = f'/images/{time.time()}'.replace('.','') + '.webp'
2022-02-24 19:05:04 +00:00
file.save(oldname)
image = process_image(oldname)
if image == "": return {"error":"Image upload failed"}
if v.admin_level > 2 and level == 1:
if parent_post.id == 37696:
2022-04-03 12:57:46 +00:00
filename = 'files/assets/images/rDrama/sidebar/' + str(len(listdir('files/assets/images/rDrama/sidebar'))+1) + '.webp'
2022-02-23 05:38:56 +00:00
copyfile(oldname, filename)
2022-02-24 19:05:04 +00:00
process_image(filename, 400)
elif parent_post.id == 37697:
2022-04-03 12:57:46 +00:00
filename = 'files/assets/images/rDrama/banners/' + str(len(listdir('files/assets/images/rDrama/banners'))+1) + '.webp'
2022-02-23 05:38:56 +00:00
copyfile(oldname, filename)
2022-02-24 19:05:04 +00:00
process_image(filename)
elif parent_post.id == 37833:
try:
badge_def = loads(body)
name = badge_def["name"]
existing = g.db.query(BadgeDef).filter_by(name=name).one_or_none()
if existing: return {"error": "A badge with this name already exists!"}, 403
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)
2022-03-31 16:28:53 +00:00
requests.post(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/purge_cache', headers=CF_HEADERS, data={'files': [f"https://{request.host}/assets/images/badges/{badge.id}.webp"]}, timeout=5)
2022-02-24 19:05:04 +00:00
except Exception as e:
return {"error": str(e)}, 400
elif v.admin_level > 2 and parent_post.id == 37838:
try:
marsey = loads(body.lower())
2022-03-19 18:39:32 +00:00
2022-02-24 19:05:04 +00:00
name = marsey["name"]
2022-03-19 18:39:32 +00:00
if not marsey_regex.fullmatch(name): return {"error": "Invalid name!"}, 400
existing = g.db.query(Marsey.name).filter_by(name=name).one_or_none()
if existing: return {"error": "A marsey with this name already exists!"}, 403
2022-02-27 23:58:46 +00:00
2022-03-19 18:39:32 +00:00
tags = marsey["tags"]
if not tags_regex.fullmatch(tags): return {"error": "Invalid tags!"}, 400
2022-02-27 23:58:46 +00:00
2022-03-04 22:59:33 +00:00
if "author" in marsey: user = get_user(marsey["author"])
elif "author_id" in marsey: user = get_account(marsey["author_id"])
2022-02-24 19:05:04 +00:00
else: abort(400)
filename = f'files/assets/images/emojis/{name}.webp'
copyfile(oldname, filename)
process_image(filename, 200)
2022-03-04 22:59:33 +00:00
2022-03-19 18:39:32 +00:00
marsey = Marsey(name=name, author_id=user.id, tags=tags, count=0)
g.db.add(marsey)
2022-03-21 21:53:42 +00:00
g.db.flush()
2022-03-04 22:59:33 +00:00
all_by_author = g.db.query(Marsey.author_id).filter_by(author_id=user.id).count()
if all_by_author >= 10 and not user.has_badge(16):
new_badge = Badge(badge_id=16, user_id=user.id)
g.db.add(new_badge)
g.db.flush()
if v.id != user.id:
text = f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}"
send_notification(user.id, text)
old_badge = user.has_badge(17)
2022-03-17 10:32:22 +00:00
if old_badge: g.db.delete(old_badge)
2022-03-04 22:59:33 +00:00
2022-03-19 18:39:32 +00:00
elif all_by_author < 10 and not user.has_badge(17):
2022-03-04 22:59:33 +00:00
new_badge = Badge(badge_id=17, user_id=user.id)
g.db.add(new_badge)
g.db.flush()
if v.id != user.id:
text = f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}"
send_notification(user.id, text)
2022-02-25 12:43:25 +00:00
requests.post(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/purge_cache', headers=CF_HEADERS, data={'files': [f"https://{request.host}/e/{name}.webp"]}, timeout=5)
2022-02-24 19:05:04 +00:00
cache.delete_memoized(marsey_list)
2022-03-04 22:59:33 +00:00
2022-02-24 19:05:04 +00:00
except Exception as e:
return {"error": str(e)}, 400
body += f"\n\n![]({image})"
elif file.content_type.startswith('video/'):
file.save("video.mp4")
with open("video.mp4", 'rb') as f:
2022-03-22 03:45:32 +00:00
try: req = requests.request("POST", "https://api.imgur.com/3/upload", headers={'Authorization': f'Client-ID {IMGUR_KEY}'}, files=[('video', f)], timeout=5).json()['data']
except requests.Timeout: return {"error": "Video upload timed out, please try again!"}
2022-03-19 14:59:56 +00:00
try: url = req['link']
except: return {"error": req['error']}, 400
2022-02-24 19:05:04 +00:00
if url.endswith('.'): url += 'mp4'
body += f"\n\n{url}"
else: return {"error": "Image/Video files only"}, 400
2021-10-18 18:41:56 +00:00
2022-03-09 01:44:53 +00:00
if v.agendaposter and not v.marseyawarded and parent_post.id not in ADMIGGERS:
2022-01-29 00:38:00 +00:00
body = torture_ap(body, v.username)
2021-11-24 17:49:17 +00:00
2022-01-13 23:44:55 +00:00
body_html = sanitize(body, comment=True)
2021-10-15 14:08:27 +00:00
2022-03-09 01:44:53 +00:00
if v.marseyawarded and parent_post.id not in ADMIGGERS and marseyaward_body_regex.search(body_html):
return {"error":"You can only type marseys!"}, 403
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-03-09 01:44:53 +00:00
if parent_post.id not in ADMIGGERS and '!slots' not in body.lower() and '!blackjack' not in body.lower() and '!wordle' not in body.lower() and AGENDAPOSTER_PHRASE not in body.lower():
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
2022-01-29 00:00:05 +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
2022-03-31 15:49:12 +00:00
is_bot = bool(request.headers.get("Authorization")) or (SITE == 'pcmemes.net' and v.id == SNAPPY_ID)
2021-10-15 14:08:27 +00:00
2022-03-09 01:44:53 +00:00
if '!slots' not in body.lower() and '!blackjack' not in body.lower() and '!wordle' not in body.lower() and parent_post.id not in ADMIGGERS and 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:
2022-02-25 17:25:31 +00:00
text = "Your account has been banned 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,
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()
2022-04-04 01:41:15 +00:00
if c.level == 1: c.top_comment_id = c.id
else: c.top_comment_id = parent.top_comment_id
2021-10-15 14:08:27 +00:00
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-02-07 11:39:26 +00:00
for choice in choices:
c_choice = Comment(author_id=AUTOCHOICE_ID,
parent_submission=parent_submission,
parent_comment_id=c.id,
level=level+1,
body_html=filter_emojis_only(choice),
upvotes=0,
is_bot=True
)
g.db.add(c_choice)
2021-10-15 14:08:27 +00:00
2022-01-03 04:08:46 +00:00
if request.host == 'pcmemes.net' and c.body.lower().startswith("based"):
2022-02-27 22:05:51 +00:00
pill = based_regex.match(body)
2021-10-15 14:08:27 +00:00
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)
2022-03-09 01:44:53 +00:00
if parent_post.id not in ADMIGGERS:
2022-02-03 06:39:02 +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
2022-02-03 06:39:02 +00:00
c.is_banned = True
c.ban_reason = "AutoJanny"
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
g.db.add(c)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
body = AGENDAPOSTER_MSG.format(username=v.username, type='comment', AGENDAPOSTER_PHRASE=AGENDAPOSTER_PHRASE)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
body_jannied_html = sanitize(body)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
c_jannied = Comment(author_id=NOTIFICATIONS_ID,
parent_submission=parent_submission,
distinguish_level=6,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
body_html=body_jannied_html,
top_comment_id=c.top_comment_id,
ghost=parent_post.ghost
)
2022-02-03 06:39:02 +00:00
g.db.add(c_jannied)
g.db.flush()
2022-02-03 06:39:02 +00:00
n = Notification(comment_id=c_jannied.id, user_id=v.id)
g.db.add(n)
2022-04-02 16:54:27 +00:00
if SITE_NAME == 'rDrama' and len(c.body) >= 1000 and "<" not in body and "</blockquote>" not in body_html:
2021-10-27 20:38:10 +00:00
2022-02-03 06:39:02 +00:00
body = random.choice(LONGPOST_REPLIES)
2021-10-15 14:08:27 +00:00
2022-02-21 05:55:37 +00:00
if body.startswith('â–¼'):
body = body[1:]
vote = CommentVote(user_id=LONGPOSTBOT_ID,
vote_type=-1,
comment_id=c.id,
real = True
)
g.db.add(vote)
c.downvotes = 1
2022-02-03 06:39:02 +00:00
body_html2 = sanitize(body)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
c2 = Comment(author_id=LONGPOSTBOT_ID,
parent_submission=parent_submission,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
body_html=body_html2,
top_comment_id=c.top_comment_id,
ghost=parent_post.ghost
)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
g.db.add(c2)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
longpostbot = g.db.query(User).filter_by(id = LONGPOSTBOT_ID).one_or_none()
longpostbot.comment_count += 1
longpostbot.coins += 1
g.db.add(longpostbot)
g.db.flush()
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
n = Notification(comment_id=c2.id, user_id=v.id)
g.db.add(n)
2021-10-15 14:08:27 +00:00
2022-04-02 16:54:27 +00:00
if SITE_NAME == 'rDrama' and random.random() < 0.001:
2022-02-03 06:39:02 +00:00
body = "zoz"
body_html2 = sanitize(body)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
c2 = Comment(author_id=ZOZBOT_ID,
parent_submission=parent_submission,
parent_comment_id=c.id,
level=level+1,
is_bot=True,
body_html=body_html2,
top_comment_id=c.top_comment_id,
2022-02-17 06:44:29 +00:00
ghost=parent_post.ghost,
distinguish_level=6
2022-02-03 06:39:02 +00:00
)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
g.db.add(c2)
g.db.flush()
n = Notification(comment_id=c2.id, user_id=v.id)
g.db.add(n)
2021-10-15 14:08:27 +00:00
2021-12-27 01:08:06 +00:00
2022-02-03 06:39:02 +00:00
body = "zle"
body_html2 = sanitize(body)
2021-10-15 14:08:27 +00:00
2021-10-27 20:38:10 +00:00
2022-02-03 06:39:02 +00:00
c3 = Comment(author_id=ZOZBOT_ID,
parent_submission=parent_submission,
parent_comment_id=c2.id,
level=level+2,
is_bot=True,
body_html=body_html2,
top_comment_id=c.top_comment_id,
2022-02-17 06:44:29 +00:00
ghost=parent_post.ghost,
distinguish_level=6
2022-02-03 06:39:02 +00:00
)
2021-10-27 20:38:10 +00:00
2022-02-03 06:39:02 +00:00
g.db.add(c3)
g.db.flush()
body = "zozzle"
body_html2 = sanitize(body)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
c4 = Comment(author_id=ZOZBOT_ID,
parent_submission=parent_submission,
parent_comment_id=c3.id,
level=level+3,
is_bot=True,
body_html=body_html2,
top_comment_id=c.top_comment_id,
2022-02-17 06:44:29 +00:00
ghost=parent_post.ghost,
distinguish_level=6
2022-02-03 06:39:02 +00:00
)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
g.db.add(c4)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
zozbot = g.db.query(User).filter_by(id = ZOZBOT_ID).one_or_none()
zozbot.comment_count += 3
zozbot.coins += 3
g.db.add(zozbot)
2021-10-15 14:08:27 +00:00
2022-02-03 06:39:02 +00:00
if not v.shadowbanned:
2022-02-27 21:57:44 +00:00
notify_users = NOTIFY_USERS(body, v)
2022-02-03 06:39:02 +00:00
for x in g.db.query(Subscription.user_id).filter_by(submission_id=c.parent_submission).all(): notify_users.add(x[0])
2022-02-07 11:39:26 +00:00
if parent.author.id not in (v.id, BASEDBOT_ID, AUTOJANNY_ID, SNAPPY_ID, LONGPOSTBOT_ID, ZOZBOT_ID, AUTOPOLLER_ID, AUTOCHOICE_ID):
notify_users.add(parent.author.id)
2022-02-03 06:39:02 +00:00
for x in notify_users:
n = Notification(comment_id=c.id, user_id=x)
g.db.add(n)
2022-03-22 15:45:52 +00:00
if parent.author.id != v.id and PUSHER_ID != 'blahblahblah':
2022-04-03 15:53:20 +00:00
try: gevent.spawn(pusher_thread, f'{request.host}{parent.author.id}', c)
except: pass
2022-02-03 06:39:02 +00:00
2022-02-24 16:48:24 +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)
c.voted = 1
2022-01-17 11:47:30 +00:00
if v.id == PIZZASHILL_ID:
2022-03-23 16:01:10 +00:00
for uid in PIZZA_VOTERS:
autovote = CommentVote(user_id=uid, comment_id=c.id, vote_type=1)
g.db.add(autovote)
2022-01-14 11:53:38 +00:00
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-31 01:41:04 +00:00
if not v.rehab:
2022-02-14 20:29:36 +00:00
check_for_slots_command(body, v, c)
2022-02-14 20:29:36 +00:00
check_for_blackjack_commands(body, v, c)
2022-02-14 20:29:36 +00:00
check_for_treasure(body, c)
2022-02-21 06:15:33 +00:00
if not c.slots_result and not c.blackjack_result and not c.wordle_result and not rts:
2022-01-31 00:58:08 +00:00
parent_post.comment_count += 1
g.db.add(parent_post)
2022-02-14 20:29:36 +00:00
if "!wordle" in body:
2022-02-18 21:21:51 +00:00
answer = random.choice(WORDLE_LIST)
2022-02-14 20:29:36 +00:00
c.wordle_result = f'_active_{answer}'
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-02-24 09:27:42 +00:00
return {"comment": render_template("comments.html", v=v, comments=[c], ajax=True)}
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):
2022-02-24 03:46:09 +00:00
2022-02-24 08:28:13 +00:00
if v.admin_level < 3:
2022-02-24 03:46:09 +00:00
if v and v.patron:
if request.content_length > 8 * 1024 * 1024: return {"error":"Max file size is 4 MB (8 MB for paypigs)."}, 413
elif request.content_length > 4 * 1024 * 1024: return {"error":"Max file size is 4 MB (8 MB for paypigs)."}, 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":
2022-03-06 19:03:50 +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
2022-02-27 21:57:44 +00:00
body = image_regex.sub(r'![](\1)', body)
2021-11-24 17:33:44 +00:00
2022-01-29 02:02:15 +00:00
if v.agendaposter and not v.marseyawarded:
2022-01-29 00:38:00 +00:00
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-02-27 21:57:44 +00:00
for i in poll_regex.finditer(body):
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-02-07 11:39:26 +00:00
if not c.choices:
2022-02-27 21:57:44 +00:00
for i in choice_regex.finditer(body):
2022-02-07 11:39:26 +00:00
body = body.replace(i.group(0), "")
c_choice = Comment(author_id=AUTOCHOICE_ID,
parent_submission=c.parent_submission,
parent_comment_id=c.id,
level=c.level+1,
body_html=filter_emojis_only(i.group(1)),
upvotes=0,
is_bot=True
)
g.db.add(c_choice)
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
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-02-01 05:35:05 +00:00
return {'error': reason}, 400
if '!slots' not in body.lower() and '!blackjack' not in body.lower() and '!wordle' not in body.lower() and 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:
2022-02-25 17:25:31 +00:00
text = "Your account has been banned 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":
2022-02-24 19:05:04 +00:00
files = request.files.getlist('file')[:4]
for file in files:
if file.content_type.startswith('image/'):
2022-03-25 22:30:15 +00:00
name = f'/images/{time.time()}'.replace('.','') + '.webp'
2022-02-24 19:05:04 +00:00
file.save(name)
url = process_image(name)
body += f"\n\n![]({url})"
elif file.content_type.startswith('video/'):
file.save("video.mp4")
with open("video.mp4", 'rb') as f:
2022-03-22 03:45:32 +00:00
try: req = requests.request("POST", "https://api.imgur.com/3/upload", headers={'Authorization': f'Client-ID {IMGUR_KEY}'}, files=[('video', f)], timeout=5).json()['data']
except requests.Timeout: return {"error": "Video upload timed out, please try again!"}
2022-03-19 14:59:56 +00:00
try: url = req['link']
except: return {"error": req['error']}, 400
2022-02-24 19:05:04 +00:00
if url.endswith('.'): url += 'mp4'
body += f"\n\n{url}"
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
2022-03-09 01:44:53 +00:00
if v.marseyawarded and marseyaward_body_regex.search(body_html):
return {"error":"You can only type marseys!"}, 403
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,
2022-02-01 23:19:41 +00:00
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
2022-02-01 04:37:10 +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
2022-02-01 04:37:10 +00:00
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)
2022-02-27 21:57:44 +00:00
notify_users = NOTIFY_USERS(body, 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-02-24 09:24:22 +00:00
return {"comment": 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-02-13 01:42:48 +00:00
c = get_comment(cid, v=v)
2021-10-15 14:08:27 +00:00
2022-02-13 01:42:48 +00:00
if not c.deleted_utc:
2021-10-15 14:08:27 +00:00
if c.author_id != v.id: abort(403)
2021-10-15 14:08:27 +00:00
c.deleted_utc = int(time.time())
2021-10-15 14:08:27 +00:00
g.db.add(c)
cache.delete_memoized(comment_idlist)
2021-10-15 14:08:27 +00:00
g.db.commit()
2021-10-15 14:08:27 +00:00
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-02-13 01:42:48 +00:00
c = get_comment(cid, v=v)
2021-10-15 14:08:27 +00:00
2022-02-13 01:42:48 +00:00
if c.deleted_utc:
if c.author_id != v.id: abort(403)
2021-10-15 14:08:27 +00:00
c.deleted_utc = 0
2021-10-15 14:08:27 +00:00
g.db.add(c)
2021-10-15 14:08:27 +00:00
cache.delete_memoized(comment_idlist)
2021-10-15 14:08:27 +00:00
g.db.commit()
2021-10-15 14:08:27 +00:00
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)
if not comment.is_pinned:
if v.id != comment.post.author_id: abort(403)
2022-03-09 01:44:53 +00:00
if comment.post.ghost: comment.is_pinned = "(OP)"
else: comment.is_pinned = v.username + " (OP)"
2021-10-15 14:08:27 +00:00
g.db.add(comment)
2021-10-15 14:08:27 +00:00
2022-02-16 04:58:10 +00:00
if v.id != comment.author_id:
2022-02-24 13:20:48 +00:00
if comment.post.ghost: message = f"OP has pinned your [comment]({comment.shortlink})!"
else: message = f"@{v.username} (OP) has pinned your [comment]({comment.shortlink})!"
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
if comment.is_pinned:
if v.id != comment.post.author_id: abort(403)
2022-01-25 18:31:24 +00:00
if not comment.is_pinned.endswith(" (OP)"):
return {"error": "You can only unpin comments you have pinned!"}
2021-12-26 01:03:21 +00:00
comment.is_pinned = None
g.db.add(comment)
2021-12-26 01:03:21 +00:00
if v.id != comment.author_id:
2022-02-24 13:20:48 +00:00
message = f"@{v.username} (OP) has unpinned your [comment]({comment.shortlink})!"
send_repeatable_notification(comment.author_id, message)
g.db.commit()
2021-12-26 01:03:21 +00:00
return {"message": "Comment unpinned!"}
2022-02-11 23:32:14 +00:00
@app.post("/mod_pin/<cid>")
@auth_required
def mod_pin(cid, v):
comment = get_comment(cid, v=v)
if not comment.is_pinned:
if not (comment.post.sub and v.mods(comment.post.sub)): abort(403)
comment.is_pinned = v.username + " (Mod)"
2022-02-11 23:32:14 +00:00
g.db.add(comment)
2022-02-11 23:32:14 +00:00
if v.id != comment.author_id:
2022-02-24 13:20:48 +00:00
message = f"@{v.username} (Mod) has pinned your [comment]({comment.shortlink})!"
send_repeatable_notification(comment.author_id, message)
2022-02-11 23:32:14 +00:00
g.db.commit()
2022-02-11 23:32:14 +00:00
return {"message": "Comment pinned!"}
@app.post("/unmod_pin/<cid>")
2022-02-11 23:32:14 +00:00
@auth_required
def mod_unpin(cid, v):
comment = get_comment(cid, v=v)
if comment.is_pinned:
if not (comment.post.sub and v.mods(comment.post.sub)): abort(403)
2022-02-11 23:32:14 +00:00
comment.is_pinned = None
g.db.add(comment)
2022-02-11 23:32:14 +00:00
if v.id != comment.author_id:
2022-02-24 13:20:48 +00:00
message = f"@{v.username} (Mod) has unpinned your [comment]({comment.shortlink})!"
send_repeatable_notification(comment.author_id, message)
g.db.commit()
2022-02-11 23:32:14 +00:00
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-02-14 19:02:05 +00:00
save=g.db.query(CommentSaveRelationship).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
2021-10-15 14:08:27 +00:00
if not save:
2022-02-14 19:02:05 +00:00
new_save=CommentSaveRelationship(user_id=v.id, comment_id=comment.id)
2021-10-15 14:08:27 +00:00
g.db.add(new_save)
2021-11-15 22:49:48 +00:00
2022-02-16 01:42:11 +00:00
g.db.commit()
2021-10-15 14:08:27 +00:00
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-02-14 19:02:05 +00:00
save=g.db.query(CommentSaveRelationship).filter_by(user_id=v.id, comment_id=comment.id).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)
2022-02-15 23:02:48 +00:00
if 'active' in comment.blackjack_result:
2022-03-06 03:25:23 +00:00
try: action = request.values.get("thing").strip().lower()
except: abort(400)
2022-02-15 23:02:48 +00:00
if action == 'hit': player_hit(comment)
elif action == 'stay': player_stayed(comment)
g.db.add(comment)
g.db.add(v)
g.db.commit()
2022-03-06 03:25:23 +00:00
return {"response" : comment.blackjack_html(v)}
2022-02-18 09:39:12 +00:00
def diff_words(answer, guess):
"""
Return a list of numbers corresponding to the char's relevance.
-1 means char is not in solution or the character appears too many times in the guess
0 means char is in solution but in the wrong spot
1 means char is in the correct spot
"""
diffs = [
1 if cs == cg else -1 for cs, cg in zip(answer, guess)
]
char_freq = Counter(
c_guess for c_guess, diff, in zip(answer, diffs) if diff == -1
)
for i, cg in enumerate(guess):
if diffs[i] == -1 and cg in char_freq and char_freq[cg] > 0:
char_freq[cg] -= 1
diffs[i] = 0
return diffs
2022-03-06 03:25:23 +00:00
@app.post("/wordle/<cid>")
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def handle_wordle_action(cid, v):
2022-02-18 09:39:12 +00:00
comment = get_comment(cid)
2022-02-14 20:29:36 +00:00
guesses, status, answer = comment.wordle_result.split("_")
count = len(guesses.split(" -> "))
2022-03-06 03:25:23 +00:00
try: guess = request.values.get("thing").strip().lower()
2022-02-14 20:29:36 +00:00
except: abort(400)
2022-02-22 13:34:41 +00:00
if len(guess) != 5 or not d.check(guess) and guess not in WORDLE_LIST:
2022-02-18 21:21:51 +00:00
return {"error": "Not a valid guess!"}, 400
if status == "active":
2022-02-18 09:39:12 +00:00
guesses += "".join(cg + WORDLE_COLOR_MAPPINGS[diff] for cg, diff in zip(guess, diff_words(answer, guess)))
2022-02-14 20:29:36 +00:00
if (guess == answer): status = "won"
elif (count == 6): status = "lost"
else: guesses += ' -> '
comment.wordle_result = f'{guesses}_{status}_{answer}'
g.db.add(comment)
g.db.commit()
2022-03-06 03:25:23 +00:00
return {"response" : comment.wordle_html(v)}