2022-05-04 23:09:46 +00:00
|
|
|
from files.helpers.wrappers import *
|
|
|
|
from files.helpers.alerts import *
|
2022-05-22 16:13:19 +00:00
|
|
|
from files.helpers.media import *
|
2022-05-04 23:09:46 +00:00
|
|
|
from files.helpers.const import *
|
2022-06-24 14:30:59 +00:00
|
|
|
from files.helpers.regex import *
|
2022-05-04 23:09:46 +00:00
|
|
|
from files.helpers.slots import *
|
|
|
|
from files.helpers.treasure import *
|
2022-06-15 19:33:21 +00:00
|
|
|
from files.helpers.actions import *
|
2022-06-24 13:19:53 +00:00
|
|
|
from files.helpers.get import *
|
2022-05-04 23:09:46 +00:00
|
|
|
from files.classes import *
|
|
|
|
from files.routes.front import comment_idlist
|
|
|
|
from flask import *
|
|
|
|
from files.__main__ import app, limiter
|
|
|
|
from files.helpers.sanitize import filter_emojis_only
|
2022-08-27 02:57:19 +00:00
|
|
|
from files.helpers.marsify import marsify
|
2022-08-27 04:48:44 +00:00
|
|
|
from files.helpers.owoify import owoify
|
2022-05-04 23:09:46 +00:00
|
|
|
import requests
|
|
|
|
from shutil import copyfile
|
|
|
|
from json import loads
|
|
|
|
from collections import Counter
|
|
|
|
import gevent
|
2022-05-07 08:20:22 +00:00
|
|
|
import os
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
WORDLE_COLOR_MAPPINGS = {-1: "🟥", 0: "🟨", 1: "🟩"}
|
|
|
|
|
|
|
|
@app.get("/comment/<cid>")
|
|
|
|
@app.get("/post/<pid>/<anything>/<cid>")
|
|
|
|
@app.get("/h/<sub>/comment/<cid>")
|
|
|
|
@app.get("/h/<sub>/post/<pid>/<anything>/<cid>")
|
2022-05-06 04:55:15 +00:00
|
|
|
@app.get("/logged_out/comment/<cid>")
|
|
|
|
@app.get("/logged_out/post/<pid>/<anything>/<cid>")
|
|
|
|
@app.get("/logged_out/h/<sub>/comment/<cid>")
|
|
|
|
@app.get("/logged_out/h/<sub>/post/<pid>/<anything>/<cid>")
|
2022-08-05 20:40:48 +00:00
|
|
|
@auth_desired_with_logingate
|
2022-05-04 23:09:46 +00:00
|
|
|
def post_pid_comment_cid(cid, pid=None, anything=None, v=None, sub=None):
|
|
|
|
comment = get_comment(cid, v=v)
|
2022-08-19 22:36:28 +00:00
|
|
|
if not comment.can_see(v): abort(403)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-06 02:24:37 +00:00
|
|
|
if comment.author.shadowbanned and not (v and v.shadowbanned) and not (v and v.admin_level >= PERMS['USER_SHADOWBAN']):
|
2022-07-08 11:13:59 +00:00
|
|
|
abort(404)
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if v and request.values.get("read"):
|
|
|
|
notif = g.db.query(Notification).filter_by(comment_id=cid, user_id=v.id, read=False).one_or_none()
|
|
|
|
if notif:
|
|
|
|
notif.read = True
|
|
|
|
g.db.add(notif)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-10-06 02:24: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 >= PERMS['POST_COMMENT_MODERATION']) : abort(403)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if not pid:
|
|
|
|
if comment.parent_submission: pid = comment.parent_submission
|
2022-10-10 06:37:42 +00:00
|
|
|
else: pid = NOTIFICATION_THREAD
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-08 18:28:56 +00:00
|
|
|
post = get_post(pid, v=v)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if post.over_18 and not (v and v.over_18) and not session.get('over_18', 0) >= int(time.time()):
|
2022-09-12 09:52:07 +00:00
|
|
|
if request.headers.get("Authorization"): return {"error": 'This content is not suitable for some users and situations.'}, 403
|
2022-05-04 23:09:46 +00:00
|
|
|
else: return render_template("errors/nsfw.html", v=v)
|
|
|
|
|
|
|
|
try: context = min(int(request.values.get("context", 0)), 8)
|
|
|
|
except: context = 0
|
|
|
|
comment_info = comment
|
|
|
|
c = comment
|
|
|
|
while context and c.level > 1:
|
|
|
|
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:
|
2022-06-26 01:22:05 +00:00
|
|
|
votes = g.db.query(CommentVote.vote_type, CommentVote.comment_id).filter_by(user_id=v.id).subquery()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
blocking = v.blocking.subquery()
|
|
|
|
|
|
|
|
blocked = v.blocked.subquery()
|
|
|
|
|
|
|
|
comments = g.db.query(
|
|
|
|
Comment,
|
|
|
|
votes.c.vote_type,
|
|
|
|
blocking.c.target_id,
|
|
|
|
blocked.c.target_id,
|
|
|
|
)
|
|
|
|
|
2022-10-06 02:24:37 +00:00
|
|
|
if not (v and v.shadowbanned) and not (v and v.admin_level >= PERMS['USER_SHADOWBAN']):
|
2022-07-03 06:12:53 +00:00
|
|
|
comments = comments.join(Comment.author).filter(User.shadowbanned == None)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
comments=comments.filter(
|
2022-07-02 06:48:04 +00:00
|
|
|
Comment.top_comment_id == c.top_comment_id
|
2022-05-04 23:09:46 +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)
|
|
|
|
|
|
|
|
post.replies=[top_comment]
|
|
|
|
|
|
|
|
if request.headers.get("Authorization"): return top_comment.json
|
|
|
|
else:
|
2022-10-06 01:58:43 +00:00
|
|
|
if post.is_banned and not (v and (v.admin_level >= PERMS['POST_COMMENT_MODERATION'] or post.author_id == v.id)): template = "submission_banned.html"
|
2022-05-04 23:09:46 +00:00
|
|
|
else: template = "submission.html"
|
|
|
|
return render_template(template, v=v, p=post, sort=sort, comment_info=comment_info, render_replies=True, sub=post.subr)
|
|
|
|
|
|
|
|
@app.post("/comment")
|
|
|
|
@limiter.limit("1/second;20/minute;200/hour;1000/day")
|
2022-07-13 18:14:37 +00:00
|
|
|
@limiter.limit("1/second;20/minute;200/hour;1000/day", key_func=lambda:f'{SITE}-{session.get("lo_user")}')
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-08-11 04:05:23 +00:00
|
|
|
def comment(v):
|
2022-05-04 23:09:46 +00:00
|
|
|
if v.is_suspended: return {"error": "You can't perform this action while banned."}, 403
|
|
|
|
|
|
|
|
parent_submission = request.values.get("submission").strip()
|
|
|
|
parent_fullname = request.values.get("parent_fullname").strip()
|
|
|
|
|
|
|
|
parent_post = get_post(parent_submission, v=v)
|
|
|
|
sub = parent_post.sub
|
|
|
|
if sub and v.exiled_from(sub): return {"error": f"You're exiled from /h/{sub}"}, 403
|
|
|
|
|
2022-09-11 01:53:16 +00:00
|
|
|
if sub in ('furry','vampire','racist','femboy') and not v.client and not v.house.lower().startswith(sub):
|
2022-09-12 09:52:07 +00:00
|
|
|
return {"error": f"You need to be a member of House {sub.capitalize()} to comment in /h/{sub}"}, 400
|
2022-08-21 15:24:16 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if parent_post.club and not (v and (v.paid_dues or v.id == parent_post.author_id)): abort(403)
|
|
|
|
|
|
|
|
rts = False
|
2022-09-05 22:52:22 +00:00
|
|
|
if parent_fullname.startswith("p_"):
|
2022-05-04 23:09:46 +00:00
|
|
|
parent = parent_post
|
|
|
|
parent_comment_id = None
|
|
|
|
level = 1
|
2022-06-22 18:05:56 +00:00
|
|
|
|
2022-10-06 04:45:54 +00:00
|
|
|
if POLL_THREAD and parent.id == POLL_THREAD and v.admin_level < PERMS['POST_TO_POLL_THREAD']: abort(403)
|
2022-09-05 22:52:22 +00:00
|
|
|
elif parent_fullname.startswith("c_"):
|
2022-05-04 23:09:46 +00:00
|
|
|
parent = get_comment(parent_fullname.split("_")[1], v=v)
|
|
|
|
parent_comment_id = parent.id
|
|
|
|
level = parent.level + 1
|
|
|
|
if parent.author_id == v.id: rts = True
|
|
|
|
else: abort(400)
|
2022-10-09 12:54:46 +00:00
|
|
|
|
2022-10-07 10:22:07 +00:00
|
|
|
if not parent.can_see(v): abort(404)
|
2022-10-05 00:18:19 +00:00
|
|
|
if parent.deleted_utc != 0: abort(404)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-09 12:54:46 +00:00
|
|
|
if level > COMMENT_MAX_DEPTH:
|
|
|
|
return {"error": f"Max comment level is {COMMENT_MAX_DEPTH}"}, 400
|
2022-06-29 01:13:11 +00:00
|
|
|
|
2022-10-09 12:54:46 +00:00
|
|
|
body = sanitize_raw_body(request.values.get("body", ""), False)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if parent_post.id not in ADMIGGERS:
|
|
|
|
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
|
|
|
|
|
|
|
|
if not body and not request.files.get('file'): return {"error":"You need to actually write something!"}, 400
|
|
|
|
|
|
|
|
options = []
|
|
|
|
for i in poll_regex.finditer(body):
|
|
|
|
options.append(i.group(1))
|
|
|
|
body = body.replace(i.group(0), "")
|
|
|
|
|
|
|
|
choices = []
|
|
|
|
for i in choice_regex.finditer(body):
|
|
|
|
choices.append(i.group(1))
|
|
|
|
body = body.replace(i.group(0), "")
|
|
|
|
|
|
|
|
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
|
|
|
|
files = request.files.getlist('file')[:4]
|
|
|
|
for file in files:
|
|
|
|
if file.content_type.startswith('image/'):
|
|
|
|
oldname = f'/images/{time.time()}'.replace('.','') + '.webp'
|
|
|
|
file.save(oldname)
|
2022-10-06 07:30:38 +00:00
|
|
|
image = process_image(oldname, patron=v.patron)
|
2022-09-12 09:52:07 +00:00
|
|
|
if image == "": return {"error":"Image upload failed"}, 400
|
2022-10-06 08:40:21 +00:00
|
|
|
if v.admin_level >= PERMS['SITE_SETTINGS_SIDEBARS_BANNERS_BADGES'] and level == 1:
|
2022-07-13 12:56:30 +00:00
|
|
|
if parent_post.id == SIDEBAR_THREAD:
|
2022-06-11 14:39:00 +00:00
|
|
|
li = sorted(os.listdir(f'files/assets/images/{SITE_NAME}/sidebar'),
|
2022-05-08 05:01:49 +00:00
|
|
|
key=lambda e: int(e.split('.webp')[0]))[-1]
|
|
|
|
num = int(li.split('.webp')[0]) + 1
|
2022-06-11 14:39:00 +00:00
|
|
|
filename = f'files/assets/images/{SITE_NAME}/sidebar/{num}.webp'
|
2022-05-04 23:09:46 +00:00
|
|
|
copyfile(oldname, filename)
|
2022-09-26 01:15:36 +00:00
|
|
|
process_image(filename, resize=400)
|
2022-07-13 12:56:30 +00:00
|
|
|
elif parent_post.id == BANNER_THREAD:
|
2022-10-08 01:13:57 +00:00
|
|
|
banner_width = 1200 if not SITE_NAME == 'PCM' else 0
|
2022-06-11 14:39:00 +00:00
|
|
|
li = sorted(os.listdir(f'files/assets/images/{SITE_NAME}/banners'),
|
2022-05-08 05:01:49 +00:00
|
|
|
key=lambda e: int(e.split('.webp')[0]))[-1]
|
|
|
|
num = int(li.split('.webp')[0]) + 1
|
2022-06-11 14:39:00 +00:00
|
|
|
filename = f'files/assets/images/{SITE_NAME}/banners/{num}.webp'
|
2022-05-04 23:09:46 +00:00
|
|
|
copyfile(oldname, filename)
|
2022-10-08 01:13:57 +00:00
|
|
|
process_image(filename, resize=banner_width)
|
2022-07-13 12:56:30 +00:00
|
|
|
elif parent_post.id == BADGE_THREAD:
|
2022-05-04 23:09:46 +00:00
|
|
|
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)
|
2022-09-27 00:48:03 +00:00
|
|
|
process_image(filename, resize=300)
|
2022-05-23 05:04:59 +00:00
|
|
|
requests.post(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/purge_cache', headers=CF_HEADERS,
|
2022-07-13 18:14:37 +00:00
|
|
|
data=f'{{"files": ["https://{SITE}/assets/images/badges/{badge.id}.webp"]}}', timeout=5)
|
2022-05-04 23:09:46 +00:00
|
|
|
except Exception as e:
|
|
|
|
return {"error": str(e)}, 400
|
|
|
|
body += f"\n\n![]({image})"
|
|
|
|
elif file.content_type.startswith('video/'):
|
2022-06-18 16:23:10 +00:00
|
|
|
body += f"\n\n{process_video(file)}"
|
2022-05-22 22:15:29 +00:00
|
|
|
elif file.content_type.startswith('audio/'):
|
2022-05-23 19:00:14 +00:00
|
|
|
body += f"\n\n{process_audio(file)}"
|
2022-06-18 15:53:34 +00:00
|
|
|
else:
|
2022-06-19 16:56:45 +00:00
|
|
|
abort(415)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-09 12:54:46 +00:00
|
|
|
body = body.strip()[:COMMENT_BODY_LENGTH_LIMIT]
|
2022-05-22 10:26:59 +00:00
|
|
|
|
2022-10-06 08:40:21 +00:00
|
|
|
if v.admin_level >= PERMS['SITE_SETTINGS_SNAPPY_QUOTES'] and parent_post.id == SNAPPY_THREAD and level == 1:
|
2022-05-22 10:23:02 +00:00
|
|
|
with open(f"snappy_{SITE_NAME}.txt", "a", encoding="utf-8") as f:
|
|
|
|
f.write('\n{[para]}\n' + body)
|
|
|
|
|
2022-08-27 03:22:57 +00:00
|
|
|
body_for_sanitize = body
|
|
|
|
if v.owoify:
|
2022-08-27 04:48:44 +00:00
|
|
|
body_for_sanitize = owoify(body_for_sanitize)
|
2022-08-27 03:22:57 +00:00
|
|
|
if v.marsify:
|
|
|
|
body_for_sanitize = marsify(body_for_sanitize)
|
|
|
|
|
2022-09-05 20:05:04 +00:00
|
|
|
torture = (v.agendaposter and not v.marseyawarded and parent_post.sub != 'chudrama' and parent_post.id not in ADMIGGERS)
|
|
|
|
|
2022-09-16 16:30:34 +00:00
|
|
|
body_html = sanitize(body_for_sanitize, limit_pings=5, count_marseys=not v.marsify, torture=torture)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
2022-09-04 20:53:34 +00:00
|
|
|
if parent_post.id not in ADMIGGERS and '!wordle' not in body.lower() and AGENDAPOSTER_PHRASE not in body.lower():
|
2022-05-04 23:09: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
|
2022-09-14 08:01:25 +00:00
|
|
|
).first()
|
2022-05-04 23:09:46 +00:00
|
|
|
if existing: return {"error": f"You already made that comment: /comment/{existing.id}"}, 409
|
|
|
|
|
2022-10-06 01:58:43 +00:00
|
|
|
if parent.author.any_block_exists(v) and v.admin_level < PERMS['POST_COMMENT_MODERATION']:
|
2022-10-09 12:54:46 +00:00
|
|
|
return {"error": "You can't reply to users who have blocked you or users that you have blocked."}, 403
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-09 14:03:21 +00:00
|
|
|
is_bot = v.id != BBBB_ID and (bool(request.headers.get("Authorization")) or (SITE == 'pcmemes.net' and v.id == SNAPPY_ID))
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-08-11 10:28:45 +00:00
|
|
|
if len(body) > 50:
|
2022-05-04 23:09:46 +00:00
|
|
|
now = int(time.time())
|
|
|
|
cutoff = now - 60 * 60 * 24
|
|
|
|
|
|
|
|
similar_comments = g.db.query(Comment).filter(
|
|
|
|
Comment.author_id == v.id,
|
2022-08-11 10:28:45 +00:00
|
|
|
Comment.body.op('<->')(body) < COMMENT_SPAM_SIMILAR_THRESHOLD,
|
2022-05-04 23:09:46 +00:00
|
|
|
Comment.created_utc > cutoff
|
|
|
|
).all()
|
|
|
|
|
2022-06-24 15:08:57 +00:00
|
|
|
threshold = COMMENT_SPAM_COUNT_THRESHOLD
|
2022-05-04 23:09:46 +00:00
|
|
|
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 banned for **1 day** for the following reason:\n\n> Too much spam!"
|
|
|
|
send_repeatable_notification(v.id, text)
|
|
|
|
|
|
|
|
v.ban(reason="Spamming.",
|
|
|
|
days=1)
|
|
|
|
|
|
|
|
for comment in similar_comments:
|
|
|
|
comment.is_banned = True
|
|
|
|
comment.ban_reason = "AutoJanny"
|
|
|
|
g.db.add(comment)
|
|
|
|
ma=ModAction(
|
|
|
|
user_id=AUTOJANNY_ID,
|
|
|
|
target_comment_id=comment.id,
|
|
|
|
kind="ban_comment",
|
|
|
|
_note="spam"
|
|
|
|
)
|
|
|
|
g.db.add(ma)
|
|
|
|
|
2022-06-28 03:22:38 +00:00
|
|
|
g.db.commit()
|
2022-05-04 23:09:46 +00:00
|
|
|
return {"error": "Too much spam!"}, 403
|
|
|
|
|
2022-10-09 12:54:46 +00:00
|
|
|
if len(body_html) > COMMENT_BODY_HTML_LENGTH_LIMIT: abort(400)
|
2022-08-15 18:11:41 +00:00
|
|
|
|
2022-05-04 23:09:46 +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,
|
2022-10-09 12:54:46 +00:00
|
|
|
body=body,
|
2022-05-04 23:09:46 +00:00
|
|
|
ghost=parent_post.ghost
|
|
|
|
)
|
|
|
|
|
|
|
|
c.upvotes = 1
|
|
|
|
g.db.add(c)
|
|
|
|
g.db.flush()
|
|
|
|
|
2022-09-27 02:44:06 +00:00
|
|
|
if blackjack and any(i in c.body.lower() for i in blackjack.split()):
|
|
|
|
v.shadowbanned = 'AutoJanny'
|
2022-10-02 11:31:02 +00:00
|
|
|
if not v.is_banned: v.ban_reason = 'Blackjack'
|
2022-09-27 02:44:06 +00:00
|
|
|
notif = Notification(comment_id=c.id, user_id=CARP_ID)
|
|
|
|
g.db.add(notif)
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if c.level == 1: c.top_comment_id = c.id
|
|
|
|
else: c.top_comment_id = parent.top_comment_id
|
|
|
|
|
|
|
|
for option in options:
|
2022-07-02 06:48:04 +00:00
|
|
|
option = CommentOption(
|
|
|
|
comment_id=c.id,
|
2022-05-04 23:09:46 +00:00
|
|
|
body_html=filter_emojis_only(option),
|
2022-08-26 21:53:17 +00:00
|
|
|
exclusive=0
|
2022-07-02 06:48:04 +00:00
|
|
|
)
|
|
|
|
g.db.add(option)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
for choice in choices:
|
2022-07-02 06:48:04 +00:00
|
|
|
choice = CommentOption(
|
|
|
|
comment_id=c.id,
|
2022-05-04 23:09:46 +00:00
|
|
|
body_html=filter_emojis_only(choice),
|
2022-08-26 21:53:17 +00:00
|
|
|
exclusive=1
|
2022-07-02 06:48:04 +00:00
|
|
|
)
|
|
|
|
g.db.add(choice)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-13 18:14:37 +00:00
|
|
|
if SITE == 'pcmemes.net' and c.body.lower().startswith("based"):
|
2022-10-09 13:49:08 +00:00
|
|
|
execute_basedbot(c, level, body, parent_submission, parent_post, v)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-08-19 22:20:25 +00:00
|
|
|
if v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in c.body.lower() and parent_post.sub != 'chudrama':
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
c.is_banned = True
|
|
|
|
c.ban_reason = "AutoJanny"
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
g.db.add(c)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
body = AGENDAPOSTER_MSG.format(username=v.username, type='comment', AGENDAPOSTER_PHRASE=AGENDAPOSTER_PHRASE)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
body_jannied_html = AGENDAPOSTER_MSG_HTML.format(id=v.id, username=v.username, type='comment', AGENDAPOSTER_PHRASE=AGENDAPOSTER_PHRASE)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
c_jannied = Comment(author_id=AUTOJANNY_ID,
|
|
|
|
parent_submission=parent_submission,
|
|
|
|
distinguish_level=6,
|
|
|
|
parent_comment_id=c.id,
|
|
|
|
level=level+1,
|
|
|
|
is_bot=True,
|
|
|
|
body=body,
|
|
|
|
body_html=body_jannied_html,
|
|
|
|
top_comment_id=c.top_comment_id,
|
|
|
|
ghost=c.ghost
|
|
|
|
)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
g.db.add(c_jannied)
|
|
|
|
g.db.flush()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
n = Notification(comment_id=c_jannied.id, user_id=v.id)
|
|
|
|
g.db.add(n)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-09 13:28:18 +00:00
|
|
|
if SITE_NAME == 'rDrama':
|
|
|
|
execute_longpostbot(c, level, body, body_html, parent_submission, v)
|
|
|
|
execute_zozbot(c, level, parent_submission, v)
|
2022-07-13 19:17:17 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
if not v.shadowbanned:
|
|
|
|
notify_users = NOTIFY_USERS(body, v)
|
2022-06-27 19:13:19 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
if c.level == 1:
|
|
|
|
subscribers = g.db.query(Subscription.user_id).filter(Subscription.submission_id == c.parent_submission, Subscription.user_id != v.id).all()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
for x in subscribers:
|
|
|
|
notify_users.add(x[0])
|
|
|
|
|
|
|
|
if parent.author.id != v.id:
|
|
|
|
notify_users.add(parent.author.id)
|
2022-07-13 19:17:17 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
for x in notify_users-bots:
|
|
|
|
n = Notification(comment_id=c.id, user_id=x)
|
|
|
|
g.db.add(n)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
if parent.author.id != v.id and PUSHER_ID != 'blahblahblah' and not v.shadowbanned:
|
|
|
|
interests = f'{SITE}{parent.author.id}'
|
2022-06-27 19:02:24 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
title = f'New reply by @{c.author_name}'
|
2022-06-27 19:02:24 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
if len(c.body) > 500: notifbody = c.body[:500] + '...'
|
|
|
|
else: notifbody = c.body
|
2022-06-27 19:02:24 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
url = f'{SITE_FULL}/comment/{c.id}?context=8&read=true#context'
|
2022-06-27 19:02:24 +00:00
|
|
|
|
2022-07-13 19:21:13 +00:00
|
|
|
gevent.spawn(pusher_thread, interests, title, notifbody, url)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vote = CommentVote(user_id=v.id,
|
|
|
|
comment_id=c.id,
|
|
|
|
vote_type=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
g.db.add(vote)
|
|
|
|
|
|
|
|
|
|
|
|
cache.delete_memoized(comment_idlist)
|
|
|
|
|
2022-07-08 11:07:27 +00:00
|
|
|
v.comment_count = g.db.query(Comment).filter(
|
|
|
|
Comment.author_id == v.id,
|
|
|
|
Comment.parent_submission != None,
|
|
|
|
Comment.deleted_utc == 0
|
|
|
|
).count()
|
2022-05-04 23:09:46 +00:00
|
|
|
g.db.add(v)
|
|
|
|
|
|
|
|
c.voted = 1
|
|
|
|
|
|
|
|
if v.id == PIZZASHILL_ID:
|
|
|
|
for uid in PIZZA_VOTERS:
|
|
|
|
autovote = CommentVote(user_id=uid, comment_id=c.id, vote_type=1)
|
2022-10-02 10:35:04 +00:00
|
|
|
autovote.created_utc += 1
|
2022-05-04 23:09:46 +00:00
|
|
|
g.db.add(autovote)
|
|
|
|
v.coins += 3
|
|
|
|
v.truecoins += 3
|
|
|
|
g.db.add(v)
|
|
|
|
c.upvotes += 3
|
|
|
|
g.db.add(c)
|
|
|
|
|
2022-09-04 20:53:34 +00:00
|
|
|
if v.marseyawarded and parent_post.id not in ADMIGGERS and marseyaward_body_regex.search(body_html):
|
2022-05-04 23:09:46 +00:00
|
|
|
return {"error":"You can only type marseys!"}, 403
|
|
|
|
|
|
|
|
check_for_treasure(body, c)
|
|
|
|
|
2022-07-23 06:22:45 +00:00
|
|
|
if FEATURES['WORDLE'] and "!wordle" in body:
|
2022-05-04 23:09:46 +00:00
|
|
|
answer = random.choice(WORDLE_LIST)
|
|
|
|
c.wordle_result = f'_active_{answer}'
|
|
|
|
|
2022-09-04 20:53:34 +00:00
|
|
|
if not c.wordle_result and not rts:
|
2022-05-04 23:09:46 +00:00
|
|
|
parent_post.comment_count += 1
|
|
|
|
g.db.add(parent_post)
|
|
|
|
|
2022-06-26 22:32:32 +00:00
|
|
|
g.db.flush()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if request.headers.get("Authorization"): return c.json
|
2022-09-10 00:47:48 +00:00
|
|
|
return {"comment": render_template("comments.html", v=v, comments=[c])}
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/edit_comment/<cid>")
|
2022-06-19 17:16:18 +00:00
|
|
|
@limiter.limit("1/second;10/minute;100/hour;200/day")
|
2022-07-13 18:14:37 +00:00
|
|
|
@limiter.limit("1/second;10/minute;100/hour;200/day", key_func=lambda:f'{SITE}-{session.get("lo_user")}')
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
|
|
|
def edit_comment(cid, v):
|
|
|
|
c = get_comment(cid, v=v)
|
|
|
|
|
2022-06-20 11:39:45 +00:00
|
|
|
if time.time() - c.created_utc > 7*24*60*60 and not (c.post and c.post.private):
|
2022-06-18 20:53:10 +00:00
|
|
|
return {"error":"You can't edit comments older than 1 week!"}, 403
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if c.author_id != v.id: abort(403)
|
2022-10-09 12:54:46 +00:00
|
|
|
if not c.post: abort(403)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-09 12:54:46 +00:00
|
|
|
body = sanitize_raw_body(request.values.get("body", ""), False)
|
2022-05-04 23:09:46 +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
|
|
|
|
|
|
|
|
if body != c.body or request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
|
|
|
|
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
|
|
|
|
|
2022-05-09 09:32:55 +00:00
|
|
|
for i in poll_regex.finditer(body):
|
|
|
|
body = body.replace(i.group(0), "")
|
2022-07-02 06:48:04 +00:00
|
|
|
option = CommentOption(
|
|
|
|
comment_id=c.id,
|
2022-05-09 09:32:55 +00:00
|
|
|
body_html=filter_emojis_only(i.group(1)),
|
2022-08-26 21:53:17 +00:00
|
|
|
exclusive = 0
|
2022-07-02 06:48:04 +00:00
|
|
|
)
|
|
|
|
g.db.add(option)
|
2022-05-09 09:32:55 +00:00
|
|
|
|
|
|
|
for i in choice_regex.finditer(body):
|
|
|
|
body = body.replace(i.group(0), "")
|
2022-07-02 06:48:04 +00:00
|
|
|
option = CommentOption(
|
|
|
|
comment_id=c.id,
|
2022-05-09 09:32:55 +00:00
|
|
|
body_html=filter_emojis_only(i.group(1)),
|
2022-08-26 21:53:17 +00:00
|
|
|
exclusive = 1
|
2022-07-02 06:48:04 +00:00
|
|
|
)
|
|
|
|
g.db.add(option)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-08-11 10:28:45 +00:00
|
|
|
if len(body) > 50:
|
2022-05-04 23:09:46 +00:00
|
|
|
now = int(time.time())
|
|
|
|
cutoff = now - 60 * 60 * 24
|
|
|
|
|
|
|
|
similar_comments = g.db.query(Comment
|
|
|
|
).filter(
|
|
|
|
Comment.author_id == v.id,
|
2022-08-11 10:28:45 +00:00
|
|
|
Comment.body.op('<->')(body) < SPAM_SIMILARITY_THRESHOLD,
|
2022-05-04 23:09:46 +00:00
|
|
|
Comment.created_utc > cutoff
|
|
|
|
).all()
|
|
|
|
|
2022-06-24 15:08:57 +00:00
|
|
|
threshold = SPAM_SIMILAR_COUNT_THRESHOLD
|
2022-05-04 23:09:46 +00:00
|
|
|
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:
|
|
|
|
text = "Your account has been banned for **1 day** for the following reason:\n\n> Too much spam!"
|
|
|
|
send_repeatable_notification(v.id, text)
|
|
|
|
|
|
|
|
v.ban(reason="Spamming.",
|
|
|
|
days=1)
|
|
|
|
|
|
|
|
for comment in similar_comments:
|
|
|
|
comment.is_banned = True
|
|
|
|
comment.ban_reason = "AutoJanny"
|
|
|
|
g.db.add(comment)
|
|
|
|
|
2022-06-28 03:22:38 +00:00
|
|
|
g.db.commit()
|
2022-05-04 23:09:46 +00:00
|
|
|
return {"error": "Too much spam!"}, 403
|
|
|
|
|
2022-06-18 15:53:34 +00:00
|
|
|
body += process_files()
|
2022-10-09 12:54:46 +00:00
|
|
|
body = body.strip()[:COMMENT_BODY_LENGTH_LIMIT] # process_files potentially adds characters to the post
|
2022-06-18 15:53:34 +00:00
|
|
|
|
2022-08-27 02:57:19 +00:00
|
|
|
body_for_sanitize = body
|
2022-08-27 03:22:57 +00:00
|
|
|
if v.owoify:
|
2022-08-27 04:48:44 +00:00
|
|
|
body_for_sanitize = owoify(body_for_sanitize)
|
2022-08-27 03:22:57 +00:00
|
|
|
if v.marsify:
|
2022-08-27 02:57:19 +00:00
|
|
|
body_for_sanitize = marsify(body_for_sanitize)
|
|
|
|
|
2022-09-05 20:05:04 +00:00
|
|
|
torture = (v.agendaposter and not v.marseyawarded and c.post.sub != 'chudrama')
|
|
|
|
|
2022-09-16 16:30:34 +00:00
|
|
|
body_html = sanitize(body_for_sanitize, golden=False, limit_pings=5, torture=torture)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-09 12:54:46 +00:00
|
|
|
if len(body_html) > COMMENT_BODY_HTML_LENGTH_LIMIT: abort(400)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if v.marseyawarded and marseyaward_body_regex.search(body_html):
|
|
|
|
return {"error":"You can only type marseys!"}, 403
|
|
|
|
|
2022-10-09 12:54:46 +00:00
|
|
|
c.body = body
|
2022-05-04 23:09:46 +00:00
|
|
|
c.body_html = body_html
|
|
|
|
|
2022-09-27 02:44:06 +00:00
|
|
|
if blackjack and any(i in c.body.lower() for i in blackjack.split()):
|
|
|
|
v.shadowbanned = 'AutoJanny'
|
2022-10-02 11:31:02 +00:00
|
|
|
if not v.is_banned: v.ban_reason = 'Blackjack'
|
2022-09-27 02:44:06 +00:00
|
|
|
g.db.add(v)
|
|
|
|
notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=CARP_ID).one_or_none()
|
|
|
|
if not notif:
|
|
|
|
notif = Notification(comment_id=c.id, user_id=CARP_ID)
|
|
|
|
g.db.add(notif)
|
|
|
|
|
2022-08-19 22:20:25 +00:00
|
|
|
if v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in c.body.lower() and c.post.sub != 'chudrama':
|
2022-08-04 20:57:07 +00:00
|
|
|
return {"error": f'You have to include "{AGENDAPOSTER_PHRASE}" in your comment!'}, 403
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
if int(time.time()) - c.created_utc > 60 * 3: c.edited_utc = int(time.time())
|
|
|
|
|
|
|
|
g.db.add(c)
|
|
|
|
|
|
|
|
notify_users = NOTIFY_USERS(body, v)
|
|
|
|
|
2022-05-28 02:20:31 +00:00
|
|
|
for x in notify_users-bots:
|
2022-05-04 23:09:46 +00:00
|
|
|
notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=x).one_or_none()
|
|
|
|
if not notif:
|
|
|
|
n = Notification(comment_id=c.id, user_id=x)
|
|
|
|
g.db.add(n)
|
|
|
|
|
2022-09-22 20:20:29 +00:00
|
|
|
g.db.commit()
|
2022-05-04 23:09:46 +00:00
|
|
|
return {"comment": c.realbody(v)}
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/delete/comment/<cid>")
|
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
2022-07-13 18:14:37 +00:00
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{SITE}-{session.get("lo_user")}')
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
|
|
|
def delete_comment(cid, v):
|
|
|
|
|
|
|
|
c = get_comment(cid, v=v)
|
|
|
|
|
|
|
|
if not c.deleted_utc:
|
|
|
|
|
|
|
|
if c.author_id != v.id: abort(403)
|
|
|
|
|
|
|
|
c.deleted_utc = int(time.time())
|
|
|
|
|
|
|
|
g.db.add(c)
|
|
|
|
|
|
|
|
cache.delete_memoized(comment_idlist)
|
|
|
|
|
2022-07-08 11:07:27 +00:00
|
|
|
g.db.flush()
|
|
|
|
v.comment_count = g.db.query(Comment).filter(
|
|
|
|
Comment.author_id == v.id,
|
|
|
|
Comment.parent_submission != None,
|
|
|
|
Comment.deleted_utc == 0
|
|
|
|
).count()
|
|
|
|
g.db.add(v)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
return {"message": "Comment deleted!"}
|
|
|
|
|
|
|
|
@app.post("/undelete/comment/<cid>")
|
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
2022-07-13 18:14:37 +00:00
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{SITE}-{session.get("lo_user")}')
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
|
|
|
def undelete_comment(cid, v):
|
|
|
|
|
|
|
|
c = get_comment(cid, v=v)
|
|
|
|
|
|
|
|
if c.deleted_utc:
|
|
|
|
if c.author_id != v.id: abort(403)
|
|
|
|
|
|
|
|
c.deleted_utc = 0
|
|
|
|
|
|
|
|
g.db.add(c)
|
|
|
|
|
|
|
|
cache.delete_memoized(comment_idlist)
|
|
|
|
|
2022-07-08 11:07:27 +00:00
|
|
|
g.db.flush()
|
|
|
|
v.comment_count = g.db.query(Comment).filter(
|
|
|
|
Comment.author_id == v.id,
|
|
|
|
Comment.parent_submission != None,
|
|
|
|
Comment.deleted_utc == 0
|
|
|
|
).count()
|
|
|
|
g.db.add(v)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
return {"message": "Comment undeleted!"}
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/pin_comment/<cid>")
|
|
|
|
@auth_required
|
2022-10-11 07:29:24 +00:00
|
|
|
@feature_required('PINS')
|
2022-05-04 23:09:46 +00:00
|
|
|
def pin_comment(cid, v):
|
2022-10-11 07:29:24 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
comment = get_comment(cid, v=v)
|
|
|
|
|
2022-05-26 23:08:23 +00:00
|
|
|
if not comment.stickied:
|
2022-05-04 23:09:46 +00:00
|
|
|
if v.id != comment.post.author_id: abort(403)
|
|
|
|
|
2022-05-26 23:08:23 +00:00
|
|
|
if comment.post.ghost: comment.stickied = "(OP)"
|
|
|
|
else: comment.stickied = v.username + " (OP)"
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
g.db.add(comment)
|
|
|
|
|
|
|
|
if v.id != comment.author_id:
|
|
|
|
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)
|
|
|
|
|
|
|
|
return {"message": "Comment pinned!"}
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/unpin_comment/<cid>")
|
|
|
|
@auth_required
|
|
|
|
def unpin_comment(cid, v):
|
|
|
|
|
|
|
|
comment = get_comment(cid, v=v)
|
|
|
|
|
2022-05-26 23:08:23 +00:00
|
|
|
if comment.stickied:
|
2022-05-04 23:09:46 +00:00
|
|
|
if v.id != comment.post.author_id: abort(403)
|
|
|
|
|
2022-05-26 23:08:23 +00:00
|
|
|
if not comment.stickied.endswith(" (OP)"):
|
2022-09-12 09:52:07 +00:00
|
|
|
return {"error": "You can only unpin comments you have pinned!"}, 400
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-05-26 23:08:23 +00:00
|
|
|
comment.stickied = None
|
2022-05-04 23:09:46 +00:00
|
|
|
g.db.add(comment)
|
|
|
|
|
|
|
|
if v.id != comment.author_id:
|
|
|
|
message = f"@{v.username} (OP) has unpinned your [comment]({comment.shortlink})!"
|
|
|
|
send_repeatable_notification(comment.author_id, message)
|
|
|
|
return {"message": "Comment unpinned!"}
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/save_comment/<cid>")
|
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
2022-07-13 18:14:37 +00:00
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{SITE}-{session.get("lo_user")}')
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
|
|
|
def save_comment(cid, v):
|
|
|
|
|
|
|
|
comment=get_comment(cid)
|
|
|
|
|
|
|
|
save=g.db.query(CommentSaveRelationship).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
|
|
|
|
|
|
|
|
if not save:
|
|
|
|
new_save=CommentSaveRelationship(user_id=v.id, comment_id=comment.id)
|
|
|
|
g.db.add(new_save)
|
|
|
|
|
|
|
|
|
|
|
|
return {"message": "Comment saved!"}
|
|
|
|
|
|
|
|
@app.post("/unsave_comment/<cid>")
|
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
2022-07-13 18:14:37 +00:00
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{SITE}-{session.get("lo_user")}')
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
|
|
|
def unsave_comment(cid, v):
|
|
|
|
|
|
|
|
comment=get_comment(cid)
|
|
|
|
|
|
|
|
save=g.db.query(CommentSaveRelationship).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
|
|
|
|
|
|
|
|
if save:
|
|
|
|
g.db.delete(save)
|
|
|
|
|
|
|
|
return {"message": "Comment unsaved!"}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/wordle/<cid>")
|
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
2022-07-13 18:14:37 +00:00
|
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{SITE}-{session.get("lo_user")}')
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
|
|
|
def handle_wordle_action(cid, v):
|
|
|
|
|
|
|
|
comment = get_comment(cid)
|
|
|
|
|
2022-08-11 08:15:41 +00:00
|
|
|
if v.id != comment.author_id:
|
|
|
|
abort(403)
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
guesses, status, answer = comment.wordle_result.split("_")
|
|
|
|
count = len(guesses.split(" -> "))
|
|
|
|
|
|
|
|
try: guess = request.values.get("thing").strip().lower()
|
|
|
|
except: abort(400)
|
|
|
|
|
2022-09-07 05:51:05 +00:00
|
|
|
if len(guess) != 5:
|
2022-05-04 23:09:46 +00:00
|
|
|
return {"error": "Not a valid guess!"}, 400
|
|
|
|
|
|
|
|
if status == "active":
|
|
|
|
guesses += "".join(cg + WORDLE_COLOR_MAPPINGS[diff] for cg, diff in zip(guess, diff_words(answer, guess)))
|
|
|
|
|
|
|
|
if (guess == answer): status = "won"
|
|
|
|
elif (count == 6): status = "lost"
|
|
|
|
else: guesses += ' -> '
|
|
|
|
|
|
|
|
comment.wordle_result = f'{guesses}_{status}_{answer}'
|
|
|
|
|
|
|
|
g.db.add(comment)
|
|
|
|
|
2022-06-11 14:39:00 +00:00
|
|
|
return {"response" : comment.wordle_html(v)}
|
2022-10-05 23:24:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.post("/toggle_comment_nsfw/<cid>")
|
|
|
|
@auth_required
|
|
|
|
def toggle_comment_nsfw(cid, v):
|
|
|
|
comment = get_comment(cid)
|
|
|
|
|
2022-10-06 00:57:08 +00:00
|
|
|
if comment.author_id != v.id and not v.admin_level >= PERMS['POST_COMMENT_MODERATION'] and not (comment.post.sub and v.mods(comment.post.sub)):
|
2022-10-05 23:24:54 +00:00
|
|
|
abort(403)
|
|
|
|
|
|
|
|
if comment.over_18 and v.is_suspended_permanently:
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
comment.over_18 = not comment.over_18
|
|
|
|
g.db.add(comment)
|
|
|
|
|
|
|
|
if comment.author_id != v.id:
|
2022-10-06 00:57:08 +00:00
|
|
|
if v.admin_level >= PERMS['POST_COMMENT_MODERATION']:
|
2022-10-05 23:24:54 +00:00
|
|
|
ma = ModAction(
|
|
|
|
kind = "set_nsfw_comment" if comment.over_18 else "unset_nsfw_comment",
|
|
|
|
user_id = v.id,
|
|
|
|
target_comment_id = comment.id,
|
|
|
|
)
|
|
|
|
g.db.add(ma)
|
|
|
|
else:
|
|
|
|
ma = SubAction(
|
|
|
|
sub = comment.post.sub,
|
|
|
|
kind = "set_nsfw_comment" if comment.over_18 else "unset_nsfw_comment",
|
|
|
|
user_id = v.id,
|
|
|
|
target_comment_id = comment.id,
|
|
|
|
)
|
|
|
|
g.db.add(ma)
|
|
|
|
|
|
|
|
if comment.over_18: return {"message": "Comment has been marked as +18!"}
|
|
|
|
else: return {"message": "Comment has been unmarked as +18!"}
|