rDrama/files/routes/posts.py

1476 lines
46 KiB
Python
Raw Normal View History

2021-10-15 14:08:27 +00:00
import time
import gevent
import requests
from files.helpers.wrappers import *
from files.helpers.sanitize import *
from files.helpers.filters import *
2021-12-20 20:03:59 +00:00
from files.helpers.alerts import *
2021-12-31 14:09:44 +00:00
from files.helpers.discord import send_discord_message
2021-10-15 14:08:27 +00:00
from files.helpers.const import *
2022-02-14 20:29:36 +00:00
from files.helpers.slots import *
2021-10-15 14:08:27 +00:00
from files.classes import *
from flask import *
from io import BytesIO
from files.__main__ import app, limiter, cache, db_session
from PIL import Image as PILimage
from .front import frontlist, changeloglist
2022-01-07 19:13:01 +00:00
from urllib.parse import ParseResult, urlunparse, urlparse, quote, unquote
2021-11-29 23:35:43 +00:00
from os import path
2021-12-18 02:59:40 +00:00
import requests
2022-01-24 23:40:34 +00:00
from shutil import copyfile
2022-01-28 21:21:12 +00:00
from sys import stdout
2021-10-15 14:08:27 +00:00
2022-01-23 23:06:34 +00:00
2022-03-17 18:31:09 +00:00
if SITE_NAME == 'PCM': snappyquotes = []
else: snappyquotes = [f':#{x}:' for x in marseys_const2]
2022-03-17 17:45:09 +00:00
if path.exists(f'snappy_{SITE_NAME}.txt'):
with open(f'snappy_{SITE_NAME}.txt', "r", encoding="utf-8") as f:
2022-03-09 02:54:39 +00:00
snappyquotes += f.read().split("\n{[para]}\n")
2022-01-23 23:06:34 +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-02-15 00:18:08 +00:00
discounts = {
69: 0.02,
70: 0.04,
71: 0.06,
72: 0.08,
73: 0.10,
}
2022-02-18 19:12:14 +00:00
titleheaders = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36"}
2022-02-16 02:15:17 +00:00
2021-10-15 14:08:27 +00:00
@app.post("/toggle_club/<pid>")
@auth_required
def toggle_club(pid, v):
2021-12-21 15:07:28 +00:00
if v.club_allowed == False: abort(403)
2021-10-15 14:08:27 +00:00
post = get_post(pid)
2022-03-05 20:53:39 +00:00
if post.author_id != v.id and v.admin_level < 2: abort(403)
2021-10-15 14:08:27 +00:00
post.club = not post.club
g.db.add(post)
2021-11-26 19:31:36 +00:00
g.db.commit()
2021-10-15 14:08:27 +00:00
if post.club: return {"message": "Post has been marked as club-only!"}
else: return {"message": "Post has been unmarked as club-only!"}
@app.post("/publish/<pid>")
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 publish(pid, v):
post = get_post(pid)
2022-03-06 04:23:12 +00:00
if not post.private: return {"message": "Post published!"}
2022-01-22 09:58:22 +00:00
if post.author_id != v.id: abort(403)
2021-10-15 14:08:27 +00:00
post.private = False
2021-12-13 21:27:25 +00:00
post.created_utc = int(time.time())
2021-10-15 14:08:27 +00:00
g.db.add(post)
2022-02-14 23:36:26 +00:00
if not post.ghost:
2022-02-27 21:57:44 +00:00
notify_users = NOTIFY_USERS(f'{post.title} {post.body}', v)
2021-11-28 14:51:31 +00:00
2022-03-01 00:06:50 +00:00
if notify_users:
2022-03-04 22:46:20 +00:00
cid = notif_comment2(post)
2022-03-01 00:06:50 +00:00
for x in notify_users:
add_notif(cid, x)
2021-10-15 14:08:27 +00:00
2022-03-01 00:06:50 +00:00
if v.followers:
text = f"@{v.username} has made a new post: [{post.title}]({post.shortlink})"
2022-03-09 02:04:37 +00:00
if post.sub: text += f" in <a href='/h/{post.sub}'>/h/{post.sub}"
2022-02-24 12:32:11 +00:00
2022-03-01 00:06:50 +00:00
cid = notif_comment(text, autojanny=True)
for follow in v.followers:
user = get_account(follow.user_id)
if post.club and not user.paid_dues: continue
add_notif(cid, user.id)
2021-12-20 20:03:59 +00:00
2021-10-15 14:08:27 +00:00
2021-10-25 13:55:56 +00:00
cache.delete_memoized(frontlist)
2021-12-31 14:12:26 +00:00
cache.delete_memoized(User.userpagelisting)
2022-01-24 16:21:54 +00:00
if v.admin_level > 0 and ("[changelog]" in post.title.lower() or "(changelog)" in post.title.lower()):
2022-01-29 04:58:13 +00:00
send_discord_message(post.permalink)
2021-12-31 14:12:26 +00:00
cache.delete_memoized(changeloglist)
2021-10-25 13:55:56 +00:00
2021-10-15 14:08:27 +00:00
g.db.commit()
2022-03-18 19:05:54 +00:00
return redirect(post.permalink)
2021-10-15 14:08:27 +00:00
@app.get("/submit")
2022-03-09 02:04:37 +00:00
@app.get("/h/<sub>/submit")
2021-10-15 14:08:27 +00:00
@auth_required
2022-02-04 18:35:39 +00:00
def submit_get(v, sub=None):
2022-02-10 20:35:16 +00:00
if sub: sub = g.db.query(Sub.name).filter_by(name=sub.strip().lower()).one_or_none()
2022-02-05 21:51:00 +00:00
2022-03-09 02:04:37 +00:00
if request.path.startswith('/h/') and not sub: abort(404)
2022-02-05 21:09:17 +00:00
2022-02-24 12:32:11 +00:00
SUBS = [x[0] for x in g.db.query(Sub.name).order_by(Sub.name).all()]
2022-02-16 22:23:44 +00:00
2022-03-09 01:49:57 +00:00
return render_template("submit.html", SUBS=SUBS, v=v, sub=sub)
2021-10-15 14:08:27 +00:00
@app.get("/post/<pid>")
@app.get("/post/<pid>/<anything>")
2022-03-09 02:04:37 +00:00
@app.get("/h/<sub>/post/<pid>")
@app.get("/h/<sub>/post/<pid>/<anything>")
2022-01-11 22:57:05 +00:00
@auth_desired
2022-02-04 18:35:39 +00:00
def post_id(pid, anything=None, v=None, sub=None):
2022-03-03 20:22:23 +00:00
2021-10-15 14:08:27 +00:00
try: pid = int(pid)
except Exception as e: pass
2021-11-26 00:34:52 +00:00
2021-10-15 14:08:27 +00:00
try: pid = int(pid)
2022-02-04 08:59:12 +00:00
except: abort(404)
2021-10-15 14:08:27 +00:00
post = get_post(pid, v=v)
2022-02-28 19:36:30 +00:00
if post.new or 'megathread' in post.title.lower(): defaultsortingcomments = 'new'
2021-12-31 14:01:50 +00:00
elif v: defaultsortingcomments = v.defaultsortingcomments
else: defaultsortingcomments = "top"
2021-12-31 14:03:11 +00:00
sort = request.values.get("sort", defaultsortingcomments)
2021-12-31 14:01:50 +00:00
2021-12-19 03:01:21 +00:00
if post.club and not (v and (v.paid_dues or v.id == post.author_id)): abort(403)
2021-10-15 14:08:27 +00:00
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
2022-02-07 11:39:26 +00:00
comments=comments.filter(Comment.parent_submission == post.id, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID))).join(
2021-10-15 14:08:27 +00:00
votes,
votes.c.comment_id == Comment.id,
isouter=True
).join(
blocking,
blocking.c.target_id == Comment.author_id,
isouter=True
).join(
blocked,
blocked.c.user_id == Comment.author_id,
isouter=True
)
2021-12-05 02:51:14 +00:00
output = []
for c in comments.all():
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-12-05 16:44:09 +00:00
pinned = [c[0] for c in comments.filter(Comment.is_pinned != None).all()]
comments = comments.filter(Comment.level == 1, Comment.is_pinned == None)
2021-12-05 02:51:14 +00:00
2021-10-15 14:08:27 +00:00
if sort == "new":
comments = comments.order_by(Comment.created_utc.desc())
elif sort == "old":
2022-03-21 21:46:10 +00:00
comments = comments.order_by(Comment.created_utc)
2021-10-15 14:08:27 +00:00
elif sort == "controversial":
2022-02-09 23:29:34 +00:00
comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc())
2021-10-15 14:08:27 +00:00
elif sort == "top":
2022-01-17 11:06:12 +00:00
comments = comments.order_by(Comment.realupvotes.desc())
2021-10-15 14:08:27 +00:00
elif sort == "bottom":
2021-11-30 23:21:29 +00:00
comments = comments.order_by(Comment.upvotes - Comment.downvotes)
2021-10-15 14:08:27 +00:00
2022-02-18 09:19:58 +00:00
first = [c[0] for c in comments.filter(or_(and_(Comment.slots_result == None, Comment.blackjack_result == None, Comment.wordle_result == None), func.length(Comment.body) > 50)).all()]
second = [c[0] for c in comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body) <= 50).all()]
2022-01-31 00:58:08 +00:00
comments = first + second
2021-10-15 14:08:27 +00:00
else:
2021-12-05 16:44:09 +00:00
pinned = g.db.query(Comment).filter(Comment.parent_submission == post.id, Comment.is_pinned != None).all()
2022-02-07 11:39:26 +00:00
comments = g.db.query(Comment).join(User, User.id == Comment.author_id).filter(User.shadowbanned == None, Comment.parent_submission == post.id, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID)), Comment.level == 1, Comment.is_pinned == None)
2021-10-15 14:08:27 +00:00
if sort == "new":
comments = comments.order_by(Comment.created_utc.desc())
elif sort == "old":
2022-03-21 21:46:10 +00:00
comments = comments.order_by(Comment.created_utc)
2021-10-15 14:08:27 +00:00
elif sort == "controversial":
2022-02-09 23:29:34 +00:00
comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc())
2021-10-15 14:08:27 +00:00
elif sort == "top":
2022-01-17 11:06:12 +00:00
comments = comments.order_by(Comment.realupvotes.desc())
2021-10-15 14:08:27 +00:00
elif sort == "bottom":
2021-11-30 23:21:29 +00:00
comments = comments.order_by(Comment.upvotes - Comment.downvotes)
2021-10-15 14:08:27 +00:00
2022-02-18 09:19:58 +00:00
first = comments.filter(or_(and_(Comment.slots_result == None, Comment.blackjack_result == None, Comment.wordle_result == None), func.length(Comment.body) > 50)).all()
second = comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body) <= 50).all()
2022-01-31 00:58:08 +00:00
comments = first + second
2021-10-15 14:08:27 +00:00
2021-12-05 18:33:16 +00:00
offset = 0
2022-02-01 01:17:38 +00:00
ids = set()
2021-12-05 18:33:16 +00:00
2022-01-11 22:39:17 +00:00
if post.comment_count > 60 and not request.headers.get("Authorization") and not request.values.get("all"):
2021-12-05 18:33:16 +00:00
comments2 = []
count = 0
if post.created_utc > 1638672040:
for comment in comments:
comments2.append(comment)
2022-02-01 01:17:38 +00:00
ids.add(comment.id)
2021-12-05 18:33:16 +00:00
count += g.db.query(Comment.id).filter_by(parent_submission=post.id, top_comment_id=comment.id).count() + 1
if count > 50: break
else:
for comment in comments:
comments2.append(comment)
2022-02-01 01:17:38 +00:00
ids.add(comment.id)
2021-12-05 18:33:16 +00:00
count += g.db.query(Comment.id).filter_by(parent_submission=post.id, parent_comment_id=comment.id).count() + 1
if count > 10: break
2022-02-01 01:17:38 +00:00
if len(comments) == len(comments2): offset = 0
else: offset = 1
2021-12-05 18:33:16 +00:00
comments = comments2
2021-12-05 02:23:39 +00:00
2021-12-24 03:59:07 +00:00
for pin in pinned:
2021-12-26 01:03:21 +00:00
if pin.is_pinned_utc and int(time.time()) > pin.is_pinned_utc:
2021-12-24 03:59:07 +00:00
pin.is_pinned = None
2021-12-26 01:03:21 +00:00
pin.is_pinned_utc = None
2021-12-24 03:59:07 +00:00
g.db.add(pin)
pinned.remove(pin)
2021-12-05 02:58:30 +00:00
post.replies = pinned + comments
2021-12-05 02:21:35 +00:00
2021-10-15 14:08:27 +00:00
post.views += 1
g.db.add(post)
2022-02-07 15:07:46 +00:00
if post.over_18 and not (v and v.over_18) and session.get('over_18', 0) < int(time.time()):
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error":"Must be 18+ to view"}, 451
2022-01-14 12:09:05 +00:00
return render_template("errors/nsfw.html", v=v)
2021-10-15 14:08:27 +00:00
g.db.commit()
if request.headers.get("Authorization"): return post.json
2021-10-22 00:19:23 +00:00
else:
2022-01-14 12:09:05 +00:00
if post.is_banned and not (v and (v.admin_level > 1 or post.author_id == v.id)): template = "submission_banned.html"
else: template = "submission.html"
2022-02-05 21:09:17 +00:00
return render_template(template, v=v, p=post, ids=list(ids), sort=sort, render_replies=True, offset=offset, sub=post.subr)
2021-10-15 14:08:27 +00:00
2022-02-06 15:52:31 +00:00
@app.get("/viewmore/<pid>/<sort>/<offset>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-01-29 00:03:40 +00:00
@auth_desired
2021-12-05 17:30:32 +00:00
def viewmore(v, pid, sort, offset):
2021-12-05 22:52:39 +00:00
offset = int(offset)
2022-02-07 13:21:42 +00:00
try: ids = set(int(x) for x in request.values.get("ids").split(','))
except: abort(400)
2021-12-05 17:30:32 +00:00
if v:
votes = g.db.query(CommentVote).filter_by(user_id=v.id).subquery()
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,
2022-02-07 11:39:26 +00:00
).filter(Comment.parent_submission == pid, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID)), Comment.is_pinned == None, Comment.id.notin_(ids))
2021-12-05 17:30:32 +00:00
if not (v and v.shadowbanned) and not (v and v.admin_level > 1):
comments = comments.join(User, User.id == Comment.author_id).filter(User.shadowbanned == None)
2022-02-01 01:17:38 +00:00
comments=comments.join(
2021-12-05 17:30:32 +00:00
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.all():
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)
2022-02-01 01:17:38 +00:00
2021-12-06 17:12:34 +00:00
comments = comments.filter(Comment.level == 1)
2021-12-05 17:30:32 +00:00
if sort == "new":
comments = comments.order_by(Comment.created_utc.desc())
elif sort == "old":
2022-03-21 21:46:10 +00:00
comments = comments.order_by(Comment.created_utc)
2021-12-05 17:30:32 +00:00
elif sort == "controversial":
2022-02-09 23:29:34 +00:00
comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc())
2021-12-05 17:30:32 +00:00
elif sort == "top":
2022-01-17 11:06:12 +00:00
comments = comments.order_by(Comment.realupvotes.desc())
2021-12-05 17:30:32 +00:00
elif sort == "bottom":
comments = comments.order_by(Comment.upvotes - Comment.downvotes)
2022-02-18 09:19:58 +00:00
first = [c[0] for c in comments.filter(or_(and_(Comment.slots_result == None, Comment.blackjack_result == None, Comment.wordle_result == None), func.length(Comment.body) > 50)).all()]
second = [c[0] for c in comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body) <= 50).all()]
2022-01-31 00:58:08 +00:00
comments = first + second
2021-12-05 17:30:32 +00:00
else:
2022-02-07 11:39:26 +00:00
comments = g.db.query(Comment).join(User, User.id == Comment.author_id).filter(User.shadowbanned == None, Comment.parent_submission == pid, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID)), Comment.level == 1, Comment.is_pinned == None, Comment.id.notin_(ids))
2021-12-05 17:30:32 +00:00
if sort == "new":
comments = comments.order_by(Comment.created_utc.desc())
elif sort == "old":
2022-03-21 21:46:10 +00:00
comments = comments.order_by(Comment.created_utc)
2021-12-05 17:30:32 +00:00
elif sort == "controversial":
2022-02-09 23:29:34 +00:00
comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc())
2021-12-05 17:30:32 +00:00
elif sort == "top":
2022-01-17 11:06:12 +00:00
comments = comments.order_by(Comment.realupvotes.desc())
2021-12-05 17:30:32 +00:00
elif sort == "bottom":
comments = comments.order_by(Comment.upvotes - Comment.downvotes)
2021-12-05 22:52:39 +00:00
2022-02-18 09:19:58 +00:00
first = comments.filter(or_(and_(Comment.slots_result == None, Comment.blackjack_result == None, Comment.wordle_result == None), func.length(Comment.body) > 50)).all()
second = comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body) <= 50).all()
2022-01-31 00:58:08 +00:00
comments = first + second
comments = comments[offset:]
2021-12-05 17:30:32 +00:00
2021-12-06 17:12:34 +00:00
comments2 = []
count = 0
post = get_post(pid, v=v)
if post.created_utc > 1638672040:
for comment in comments:
comments2.append(comment)
2022-02-01 01:17:38 +00:00
ids.add(comment.id)
2021-12-06 17:12:34 +00:00
count += g.db.query(Comment.id).filter_by(parent_submission=post.id, top_comment_id=comment.id).count() + 1
if count > 50: break
else:
for comment in comments:
comments2.append(comment)
2022-02-01 01:17:38 +00:00
ids.add(comment.id)
2021-12-06 17:12:34 +00:00
count += g.db.query(Comment.id).filter_by(parent_submission=post.id, parent_comment_id=comment.id).count() + 1
if count > 10: break
2022-02-01 01:17:38 +00:00
if len(comments) == len(comments2): offset = 0
else: offset += 1
2021-12-06 17:12:34 +00:00
comments = comments2
2021-12-05 22:52:39 +00:00
2022-03-04 17:22:40 +00:00
return render_template("comments.html", v=v, comments=comments, p=post, ids=list(ids), render_replies=True, pid=pid, sort=sort, offset=offset, ajax=True)
2021-12-05 17:30:32 +00:00
2021-10-15 14:08:27 +00:00
2022-02-06 15:52:31 +00:00
@app.get("/morecomments/<cid>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-01-29 00:03:40 +00:00
@auth_desired
2021-12-11 00:05:01 +00:00
def morecomments(v, cid):
2022-03-03 20:24:02 +00:00
tcid = g.db.query(Comment.top_comment_id).filter_by(id=cid).one_or_none()[0]
2021-12-31 17:38:11 +00:00
2021-12-11 00:23:01 +00:00
if v:
votes = g.db.query(CommentVote).filter_by(user_id=v.id).subquery()
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,
2022-01-30 21:05:29 +00:00
).filter(Comment.top_comment_id == tcid, Comment.level > 9).join(
2021-12-11 00:23:01 +00:00
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 = []
2021-12-31 17:59:24 +00:00
dump = []
2021-12-11 00:23:01 +00:00
for c in comments.all():
comment = c[0]
comment.voted = c[1] or 0
comment.is_blocking = c[2] or 0
comment.is_blocked = c[3] or 0
2021-12-31 17:59:24 +00:00
if c[0].parent_comment_id == int(cid): output.append(comment)
else: dump.append(comment)
comments = output
2021-12-11 00:23:01 +00:00
else:
2022-01-02 00:06:46 +00:00
c = g.db.query(Comment).filter_by(id=cid).one_or_none()
2021-12-11 00:23:01 +00:00
comments = c.replies
2022-03-17 10:28:13 +00:00
if comments: p = comments[0].post
else: p = None
2022-03-04 17:22:40 +00:00
return render_template("comments.html", v=v, comments=comments, p=p, render_replies=True, ajax=True)
2021-12-11 00:05:01 +00:00
2021-10-15 14:08:27 +00:00
@app.post("/edit_post/<pid>")
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_post(pid, v):
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
p = get_post(pid)
2021-11-15 22:13:29 +00:00
if p.author_id != v.id and not (v.admin_level > 1 and v.admin_level > 2): abort(403)
2021-10-15 14:08:27 +00:00
2022-01-10 22:43:00 +00:00
title = request.values.get("title", "").strip().replace('‎','')
2021-12-10 19:53:16 +00:00
2022-01-10 22:43:00 +00:00
body = request.values.get("body", "").strip().replace('‎','')
2021-10-15 14:08:27 +00:00
2022-01-19 09:19:38 +00:00
if len(body) > 20000: return {"error":"Character limit is 20000!"}, 403
2021-10-29 16:57:50 +00:00
2022-03-18 23:21:14 +00:00
if v.id == p.author_id:
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 title != p.title:
2022-03-18 23:21:14 +00:00
if v.id == p.author_id and v.agendaposter and not v.marseyawarded: title = torture_ap(title, v.username)
2021-11-24 17:49:17 +00:00
2022-01-21 20:56:56 +00:00
title_html = filter_emojis_only(title, edit=True)
2022-03-09 01:44:53 +00:00
2022-03-18 23:21:14 +00:00
if v.id == p.author_id and v.marseyawarded and not marseyaward_title_regex.fullmatch(title_html):
2022-03-09 01:44:53 +00:00
return {"error":"You can only type marseys!"}, 403
2021-12-21 19:56:38 +00:00
p.title = title[:500]
2021-10-27 20:12:16 +00:00
p.title_html = title_html
2021-10-15 14:08:27 +00:00
2021-11-30 18:15:33 +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-11-30 18:15:33 +00:00
2021-10-15 14:08:27 +00:00
if body != p.body:
2022-02-27 21:57:44 +00:00
body = image_regex.sub(r'![](\1)', body)
2021-11-24 17:33:44 +00:00
2022-03-18 23:21:14 +00:00
if v.id == p.author_id and v.agendaposter and not v.marseyawarded: body = torture_ap(body, v.username)
2021-11-24 17:33:44 +00:00
2021-12-03 23:34:03 +00:00
if not p.options.count():
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 = Comment(author_id=AUTOPOLLER_ID,
parent_submission=p.id,
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)
2022-02-07 11:39:26 +00:00
if not p.choices.count():
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 = Comment(author_id=AUTOCHOICE_ID,
parent_submission=p.id,
level=1,
body_html=filter_emojis_only(i.group(1)),
upvotes=0,
is_bot=True
)
g.db.add(c)
2022-01-19 06:20:05 +00:00
body_html = sanitize(body, edit=True)
2021-10-15 14:08:27 +00:00
2022-03-18 23:21:14 +00:00
if v.id == p.author_id and v.marseyawarded and marseyaward_body_regex.search(body_html):
2022-03-09 01:44:53 +00:00
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 post and try again."
if ban.reason:
reason += f" {ban.reason}"
return {"error": reason}, 403
p.body = body
2021-11-18 20:50:03 +00:00
2022-01-19 09:19:38 +00:00
if len(body_html) > 40000: return {"error":"Submission body too long!"}, 400
2022-01-04 21:11:29 +00:00
2021-10-15 14:08:27 +00:00
p.body_html = body_html
2022-03-18 23:21:14 +00:00
if v.id == p.author_id and v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in f'{p.body}{p.title}'.lower():
2021-10-15 14:08:27 +00:00
p.is_banned = True
2021-11-03 16:15:06 +00:00
p.ban_reason = "AutoJanny"
2021-10-15 14:08:27 +00:00
g.db.add(p)
2022-01-18 11:19:32 +00:00
body = AGENDAPOSTER_MSG.format(username=v.username, type='post', 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=p.id,
level=1,
over_18=False,
is_bot=True,
app_id=None,
2021-12-28 05:06:24 +00:00
is_pinned='AutoJanny',
2021-10-15 14:08:27 +00:00
distinguish_level=6,
body_html=body_jannied_html,
2022-02-01 04:37:10 +00:00
ghost=p.ghost
2021-10-15 14:08:27 +00:00
)
g.db.add(c_jannied)
g.db.flush()
2022-04-04 01:41:15 +00:00
c_jannied.top_comment_id = c_jannied.id
2021-10-15 14:08:27 +00:00
n = Notification(comment_id=c_jannied.id, user_id=v.id)
g.db.add(n)
2022-02-01 04:37:10 +00:00
2022-03-04 23:13:57 +00:00
if not p.private and not p.ghost:
notify_users = NOTIFY_USERS(f'{p.title} {p.body}', v)
if notify_users:
cid = notif_comment2(p)
for x in notify_users:
add_notif(cid, x)
2022-03-04 22:47:25 +00:00
2022-03-19 19:15:13 +00:00
if v.id == p.author_id:
if int(time.time()) - p.created_utc > 60 * 3: p.edited_utc = int(time.time())
g.db.add(p)
else:
ma=ModAction(
kind="edit_post",
user_id=v.id,
target_submission_id=p.id
)
g.db.add(ma)
2022-03-04 23:13:57 +00:00
g.db.commit()
2021-10-15 14:08:27 +00:00
return redirect(p.permalink)
def archiveorg(url):
try: requests.get(f'https://web.archive.org/save/{url}', headers={'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}, timeout=100)
2022-03-18 18:31:24 +00:00
except: pass
2021-10-15 14:08:27 +00:00
def thumbnail_thread(pid):
2022-01-11 03:02:59 +00:00
db = db_session()
2021-10-15 14:08:27 +00:00
def expand_url(post_url, fragment_url):
if fragment_url.startswith("https://"):
return fragment_url
2021-12-05 19:45:08 +00:00
elif fragment_url.startswith("https://"):
return f"https://{fragment_url.split('https://')[1]}"
2021-10-15 14:08:27 +00:00
elif fragment_url.startswith('//'):
return f"https:{fragment_url}"
elif fragment_url.startswith('/'):
parsed_url = urlparse(post_url)
return f"https://{parsed_url.netloc}{fragment_url}"
else:
return f"{post_url}{'/' if not post_url.endswith('/') else ''}{fragment_url}"
2022-01-02 00:06:46 +00:00
post = db.query(Submission).filter_by(id=pid).one_or_none()
2021-10-15 14:08:27 +00:00
2021-12-30 05:27:22 +00:00
if not post or not post.url:
2021-10-15 14:08:27 +00:00
time.sleep(5)
2022-01-02 00:06:46 +00:00
post = db.query(Submission).filter_by(id=pid).one_or_none()
2021-10-15 14:08:27 +00:00
2021-12-30 05:27:22 +00:00
if not post or not post.url: return
2021-12-16 17:59:30 +00:00
fetch_url = post.url
2022-01-24 17:37:37 +00:00
if fetch_url.startswith('/'): fetch_url = f"{SITE_FULL}{fetch_url}"
2021-10-15 14:08:27 +00:00
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36"}
try:
2021-11-14 01:19:32 +00:00
x=requests.get(fetch_url, headers=headers, timeout=5)
2021-10-15 14:08:27 +00:00
except:
db.close()
return
if x.status_code != 200:
db.close()
return
if x.headers.get("Content-Type","").startswith("text/html"):
2022-02-24 08:28:13 +00:00
soup=BeautifulSoup(x.content, 'lxml')
2021-10-15 14:08:27 +00:00
thumb_candidate_urls=[]
meta_tags = [
2021-10-27 20:12:16 +00:00
"drama:thumbnail",
2021-10-15 14:08:27 +00:00
"twitter:image",
"og:image",
"thumbnail"
]
for tag_name in meta_tags:
tag = soup.find(
'meta',
attrs={
"name": tag_name,
"content": True
}
)
if not tag:
tag = soup.find(
'meta',
attrs={
'property': tag_name,
'content': True
}
)
if tag:
thumb_candidate_urls.append(expand_url(post.url, tag['content']))
for tag in soup.find_all("img", attrs={'src':True}):
thumb_candidate_urls.append(expand_url(post.url, tag['src']))
for url in thumb_candidate_urls:
try:
2021-11-14 01:19:32 +00:00
image_req=requests.get(url, headers=headers, timeout=5)
2021-10-15 14:08:27 +00:00
except:
continue
if image_req.status_code >= 400:
continue
if not image_req.headers.get("Content-Type","").startswith("image/"):
continue
if image_req.headers.get("Content-Type","").startswith("image/svg"):
continue
2022-01-22 00:41:42 +00:00
image = PILimage.open(BytesIO(image_req.content))
if image.width < 30 or image.height < 30:
2021-10-15 14:08:27 +00:00
continue
2022-01-22 00:41:42 +00:00
2021-10-15 14:08:27 +00:00
break
else:
db.close()
return
elif x.headers.get("Content-Type","").startswith("image/"):
image_req=x
2022-01-22 00:46:30 +00:00
image = PILimage.open(BytesIO(x.content))
2021-10-15 14:08:27 +00:00
else:
db.close()
return
2022-03-25 22:30:15 +00:00
name = f'/images/{time.time()}'.replace('.','') + '.webp'
2021-10-15 14:08:27 +00:00
2022-01-22 00:41:42 +00:00
with open(name, "wb") as file:
for chunk in image_req.iter_content(1024):
file.write(chunk)
2022-01-24 23:40:34 +00:00
post.thumburl = process_image(name, resize=100)
2021-10-15 14:08:27 +00:00
db.add(post)
db.commit()
2022-01-28 20:17:17 +00:00
2022-04-02 16:54:27 +00:00
if SITE_NAME == 'rDrama':
2022-01-28 23:50:49 +00:00
for t in ("submission","comment"):
2022-03-17 14:22:44 +00:00
word = random.choice(('rdrama','marsey'))
2022-02-04 04:57:54 +00:00
2022-02-12 00:05:05 +00:00
try:
2022-02-24 18:31:28 +00:00
data = requests.get(f'https://api.pushshift.io/reddit/{t}/search?html_decode=true&q={word}&size=1', timeout=5).json()["data"]
2022-02-11 15:06:19 +00:00
except: break
for i in data:
2022-02-07 11:54:19 +00:00
2022-03-22 13:50:41 +00:00
if i["subreddit"] == 'PokemonGoRaids': continue
2022-03-02 00:05:30 +00:00
2022-04-02 17:22:19 +00:00
body_html = sanitize(f'New rdrama mention: https://old.reddit.com{i["permalink"]}?context=89', noimages=True)
2022-02-04 04:57:54 +00:00
2022-03-01 00:20:11 +00:00
existing_comment = db.query(Comment.id).filter_by(author_id=NOTIFICATIONS_ID, parent_submission=None, body_html=body_html).one_or_none()
2022-02-04 04:57:54 +00:00
if existing_comment: break
new_comment = Comment(author_id=NOTIFICATIONS_ID,
parent_submission=None,
body_html=body_html,
2022-03-31 15:00:57 +00:00
distinguish_level=6
2022-02-04 04:57:54 +00:00
)
db.add(new_comment)
db.flush()
2022-04-04 01:41:15 +00:00
new_comment.top_comment_id = new_comment.id
2022-02-04 04:57:54 +00:00
admins = db.query(User).filter(User.admin_level > 0).all()
for admin in admins:
notif = Notification(comment_id=new_comment.id, user_id=admin.id)
2022-01-28 23:50:49 +00:00
db.add(notif)
2022-01-28 20:55:59 +00:00
2022-02-24 09:24:54 +00:00
k,val = random.choice(tuple(REDDIT_NOTIFS.items()))
2022-02-24 10:48:11 +00:00
2022-02-24 18:31:28 +00:00
try:
data = requests.get(f'https://api.pushshift.io/reddit/{t}/search?html_decode=true&q={k}&size=1', timeout=5).json()["data"]
except: break
for i in data:
2022-02-24 10:48:11 +00:00
body_html = sanitize(f'New mention of you: https://old.reddit.com{i["permalink"]}?context=89', noimages=True)
2022-03-01 00:20:11 +00:00
existing_comment = db.query(Comment.id).filter_by(author_id=NOTIFICATIONS_ID, parent_submission=None,body_html=body_html).one_or_none()
2022-02-04 04:57:54 +00:00
if existing_comment: break
2022-02-04 05:03:49 +00:00
2022-02-04 04:57:54 +00:00
new_comment = Comment(author_id=NOTIFICATIONS_ID,
parent_submission=None,
2022-03-31 15:00:57 +00:00
body_html=body_html,
distinguish_level=6
2022-02-04 04:57:54 +00:00
)
db.add(new_comment)
db.flush()
2022-04-04 01:41:15 +00:00
new_comment.top_comment_id = new_comment.id
2022-02-07 11:54:19 +00:00
notif = Notification(comment_id=new_comment.id, user_id=val)
2022-02-04 04:57:54 +00:00
db.add(notif)
2022-01-28 20:55:59 +00:00
2022-02-07 11:54:19 +00:00
if SITE == 'pcmemes.net':
for t in ("submission","comment"):
2022-02-12 00:05:05 +00:00
try:
2022-02-24 17:48:14 +00:00
data = requests.get(f'https://api.pushshift.io/reddit/{t}/search?html_decode=true&q=pcmemes.net&size=1', timeout=5).json()["data"]
2022-02-11 15:06:19 +00:00
except: break
for i in data:
2022-02-07 12:03:30 +00:00
body_html = sanitize(f'New pcmemes mention: https://old.reddit.com{i["permalink"]}?context=89', noimages=True)
2022-02-07 11:54:19 +00:00
2022-03-01 00:20:11 +00:00
existing_comment = db.query(Comment.id).filter_by(author_id=NOTIFICATIONS_ID, parent_submission=None, body_html=body_html).one_or_none()
2022-02-07 11:54:19 +00:00
if existing_comment: break
new_comment = Comment(author_id=NOTIFICATIONS_ID,
parent_submission=None,
body_html=body_html,
2022-03-31 15:00:57 +00:00
distinguish_level=6
2022-02-07 11:54:19 +00:00
)
db.add(new_comment)
db.flush()
2022-04-04 01:41:15 +00:00
new_comment.top_comment_id = new_comment.id
2022-02-07 11:54:19 +00:00
admins = db.query(User).filter(User.admin_level > 2).all()
for admin in admins:
notif = Notification(comment_id=new_comment.id, user_id=admin.id)
db.add(notif)
2022-01-28 20:55:59 +00:00
db.commit()
2021-10-15 14:08:27 +00:00
db.close()
2022-01-28 20:55:59 +00:00
stdout.flush()
2021-10-15 14:08:27 +00:00
return
@app.post("/submit")
2022-03-09 02:04:37 +00:00
@app.post("/h/<sub>/submit")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;6/minute;200/hour;1000/day")
2022-01-06 16:46:09 +00:00
@auth_required
2022-02-04 18:35:39 +00:00
def submit_post(v, sub=None):
2022-02-05 18:47:21 +00:00
2022-01-10 22:43:00 +00:00
title = request.values.get("title", "").strip()[:500].replace('‎','')
2021-12-10 19:53:16 +00:00
2021-10-15 14:08:27 +00:00
url = request.values.get("url", "").strip()
2022-03-19 11:37:43 +00:00
2022-01-10 22:43:00 +00:00
body = request.values.get("body", "").strip().replace('‎','')
2021-11-18 20:50:03 +00:00
2022-02-14 02:33:27 +00:00
def error(error):
2022-02-16 04:33:13 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": error}, 403
2022-02-16 22:23:44 +00:00
2022-02-24 12:32:11 +00:00
SUBS = [x[0] for x in g.db.query(Sub.name).order_by(Sub.name).all()]
2022-03-09 01:49:57 +00:00
return render_template("submit.html", SUBS=SUBS, v=v, error=error, title=title, url=url, body=body), 400
2022-02-16 04:33:13 +00:00
2022-02-22 10:59:33 +00:00
sub = request.values.get("sub")
2022-03-09 02:04:37 +00:00
if sub: sub = sub.replace('/h/','').replace('s/','')
2022-02-16 04:33:13 +00:00
2022-02-17 07:02:44 +00:00
if sub and sub != 'none':
2022-02-22 11:55:40 +00:00
sname = sub.strip().lower()
sub = g.db.query(Sub.name).filter_by(name=sname).one_or_none()
2022-03-09 02:04:37 +00:00
if not sub: return error(f"/h/{sname} not found!")
2022-02-16 04:33:13 +00:00
sub = sub[0]
2022-03-09 02:04:37 +00:00
if v.exiled_from(sub): return error(f"You're exiled from /h/{sub}")
2022-02-16 04:33:13 +00:00
else: sub = None
2022-02-14 02:33:27 +00:00
2022-02-16 04:33:13 +00:00
if v.is_suspended: return error("You can't perform this action while banned.")
2022-02-14 16:30:48 +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).")
elif request.content_length > 4 * 1024 * 1024: return error( "Max file size is 4 MB (8 MB for paypigs).")
2022-02-14 16:30:48 +00:00
if v.agendaposter and not v.marseyawarded: title = torture_ap(title, v.username)
2022-02-14 02:33:27 +00:00
title_html = filter_emojis_only(title, graceful=True)
2022-03-09 01:44:53 +00:00
if v.marseyawarded and not marseyaward_title_regex.fullmatch(title_html):
return {"error":"You can only type marseys!"}, 403
2022-02-14 02:33:27 +00:00
if len(title_html) > 1500: return error("Rendered title is too big!")
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-03-02 00:13:13 +00:00
embed = None
2021-10-15 14:08:27 +00:00
if url:
2022-03-19 11:37:43 +00:00
for rd in ("://reddit.com", "://new.reddit.com", "://www.reddit.com", "://redd.it", "://libredd.it", "://teddit.net"):
url = url.replace(rd, "://old.reddit.com")
url = url.replace("nitter.net", "twitter.com").replace("old.reddit.com/gallery", "reddit.com/gallery").replace("https://youtu.be/", "https://youtube.com/watch?v=").replace("https://music.youtube.com/watch?v=", "https://youtube.com/watch?v=").replace("https://streamable.com/", "https://streamable.com/e/").replace("https://youtube.com/shorts/", "https://youtube.com/watch?v=").replace("https://mobile.twitter", "https://twitter").replace("https://m.facebook", "https://facebook").replace("m.wikipedia.org", "wikipedia.org").replace("https://m.youtube", "https://youtube").replace("https://www.youtube", "https://youtube").replace("https://www.twitter", "https://twitter").replace("https://www.instagram", "https://instagram").replace("https://www.tiktok", "https://tiktok")
2022-03-18 19:05:54 +00:00
2021-10-15 14:08:27 +00:00
if "/i.imgur.com/" in url: url = url.replace(".png", ".webp").replace(".jpg", ".webp").replace(".jpeg", ".webp")
elif "/media.giphy.com/" in url or "/c.tenor.com/" in url: url = url.replace(".gif", ".webp")
elif "/i.ibb.com/" in url: url = url.replace(".png", ".webp").replace(".jpg", ".webp").replace(".jpeg", ".webp").replace(".gif", ".webp")
if url.startswith("https://streamable.com/") and not url.startswith("https://streamable.com/e/"): url = url.replace("https://streamable.com/", "https://streamable.com/e/")
parsed_url = urlparse(url)
domain = parsed_url.netloc
2022-03-19 11:37:43 +00:00
if domain in ('old.reddit.com','twitter.com','instagram.com','tiktok.com'):
2021-12-28 07:33:55 +00:00
new_url = ParseResult(scheme="https",
netloc=parsed_url.netloc,
path=parsed_url.path,
params=parsed_url.params,
query=None,
fragment=parsed_url.fragment)
else:
qd = parse_qs(parsed_url.query)
2022-01-07 21:03:14 +00:00
filtered = {k: val for k, val in qd.items() if not k.startswith('utm_') and not k.startswith('ref_')}
2021-12-28 07:33:55 +00:00
new_url = ParseResult(scheme="https",
netloc=parsed_url.netloc,
path=parsed_url.path,
params=parsed_url.params,
query=urlencode(filtered, doseq=True),
fragment=parsed_url.fragment)
2021-10-27 20:12:16 +00:00
url = urlunparse(new_url)
2021-10-15 14:08:27 +00:00
2022-03-17 16:12:59 +00:00
search_url = url.replace('%', '')
search_url = reddit_post_regex.sub(r'\1%', search_url)
search_url = search_url.replace('\\', '').replace('_', '\_').strip()
2022-03-04 22:46:20 +00:00
2022-02-24 18:02:10 +00:00
repost = g.db.query(Submission).filter(
2022-03-05 00:04:20 +00:00
Submission.url.ilike(search_url),
2022-02-24 18:02:10 +00:00
Submission.deleted_utc == 0,
Submission.is_banned == False
2022-03-18 18:31:24 +00:00
).first()
2022-02-24 18:02:10 +00:00
if repost: return redirect(repost.permalink)
2021-10-15 14:08:27 +00:00
2022-03-02 00:05:30 +00:00
2021-10-15 14:08:27 +00:00
domain_obj = get_domain(domain)
2022-02-03 06:39:02 +00:00
if not domain_obj: domain_obj = get_domain(domain+parsed_url.path)
2022-03-02 00:05:30 +00:00
2021-10-15 14:08:27 +00:00
if domain_obj:
2022-02-01 05:35:05 +00:00
reason = f"Remove the {domain_obj.domain} link from your post and try again. {domain_obj.reason}"
2022-02-14 02:33:27 +00:00
return error(reason)
2021-12-01 17:25:28 +00:00
elif "twitter.com" == domain:
2022-03-03 00:00:14 +00:00
try: embed = requests.get("https://publish.twitter.com/oembed", params={"url":url, "omit_script":"t"}, timeout=5).json()["html"]
except: pass
2021-12-25 20:46:49 +00:00
elif url.startswith('https://youtube.com/watch?v='):
2022-01-07 19:13:01 +00:00
url = unquote(url).replace('?t', '&t')
2021-12-25 20:46:49 +00:00
yt_id = url.split('https://youtube.com/watch?v=')[1].split('&')[0].split('%')[0]
2022-03-02 00:05:30 +00:00
req = requests.get(f"https://www.googleapis.com/youtube/v3/videos?id={yt_id}&key={YOUTUBE_KEY}&part=contentDetails", timeout=5).json()
if req.get('items'):
params = parse_qs(urlparse(url).query)
t = params.get('t', params.get('start', [0]))[0]
if isinstance(t, str): t = t.replace('s','')
embed = f'<lite-youtube videoid="{yt_id}" params="autoplay=1&modestbranding=1'
2022-04-04 10:34:49 +00:00
if t:
try: embed += f'&start={int(t)}'
except: pass
2022-03-02 00:05:30 +00:00
embed += '"></lite-youtube>'
2021-10-15 14:08:27 +00:00
elif app.config['SERVER_NAME'] in domain and "/post/" in url and "context" not in url:
id = url.split("/post/")[1]
if "/" in id: id = id.split("/")[0]
2022-03-03 00:14:47 +00:00
embed = str(int(id))
2022-03-02 00:05:30 +00:00
2021-10-15 14:08:27 +00:00
2022-02-26 13:14:30 +00:00
if not url and not request.values.get("body") and not request.files.get("file") and not request.files.get("file2"):
2022-02-14 02:33:27 +00:00
return error("Please enter a url or some text.")
2021-10-15 14:08:27 +00:00
if not title:
2022-02-14 02:33:27 +00:00
return error("Please enter a better title.")
2021-10-15 14:08:27 +00:00
elif len(title) > 500:
2022-02-14 02:33:27 +00:00
return error("There's a 500 character limit for titles.")
2021-10-27 00:37:34 +00:00
2021-11-06 15:52:48 +00:00
dup = g.db.query(Submission).filter(
2021-10-15 14:08:27 +00:00
Submission.author_id == v.id,
Submission.deleted_utc == 0,
Submission.title == title,
Submission.url == url,
Submission.body == body
2022-01-02 00:06:46 +00:00
).one_or_none()
2021-10-15 14:08:27 +00:00
if dup: return redirect(dup.permalink)
now = int(time.time())
cutoff = now - 60 * 60 * 24
2021-11-07 13:37:26 +00:00
similar_posts = g.db.query(Submission).filter(
2021-10-15 14:08:27 +00:00
Submission.author_id == v.id,
Submission.title.op('<->')(title) < app.config["SPAM_SIMILARITY_THRESHOLD"],
Submission.created_utc > cutoff
).all()
if url:
2021-11-07 13:37:26 +00:00
similar_urls = g.db.query(Submission).filter(
2021-10-15 14:08:27 +00:00
Submission.author_id == v.id,
Submission.url.op('<->')(url) < app.config["SPAM_URL_SIMILARITY_THRESHOLD"],
Submission.created_utc > cutoff
).all()
2021-10-27 20:12:16 +00:00
else: similar_urls = []
2021-10-15 14:08:27 +00:00
threshold = app.config["SPAM_SIMILAR_COUNT_THRESHOLD"]
2021-10-27 20:12:16 +00:00
if v.age >= (60 * 60 * 24 * 7): threshold *= 3
elif v.age >= (60 * 60 * 24): threshold *= 2
2021-10-15 14:08:27 +00:00
if max(len(similar_urls), len(similar_posts)) >= 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 post in similar_posts + similar_urls:
post.is_banned = True
post.is_pinned = False
2021-11-05 14:40:14 +00:00
post.ban_reason = "AutoJanny"
2021-10-15 14:08:27 +00:00
g.db.add(post)
ma=ModAction(
2021-11-18 14:21:19 +00:00
user_id=AUTOJANNY_ID,
2021-10-15 14:08:27 +00:00
target_submission_id=post.id,
kind="ban_post",
2021-10-25 18:08:03 +00:00
_note="spam"
2021-10-15 14:08:27 +00:00
)
g.db.add(ma)
2022-04-02 17:11:35 +00:00
return redirect("/notifications")
2021-10-15 14:08:27 +00:00
2022-01-19 09:19:38 +00:00
if len(str(body)) > 20000:
2022-02-14 02:33:27 +00:00
return error("There's a 20000 character limit for text body.")
2021-10-15 14:08:27 +00:00
if len(url) > 2048:
2022-02-14 02:33:27 +00:00
return error("There's a 2048 character limit for URLs.")
2021-10-15 14:08:27 +00:00
2022-02-27 21:57:44 +00:00
body = image_regex.sub(r'![](\1)', body)
2021-10-15 14:08:27 +00:00
2021-12-11 03:36:05 +00:00
if v and v.admin_level > 2:
2021-12-11 02:35:21 +00:00
bet_options = []
2022-02-27 22:05:51 +00:00
for i in bet_regex.finditer(body):
2021-12-11 02:35:21 +00:00
bet_options.append(i.group(1))
body = body.replace(i.group(0), "")
2021-12-11 02:27:46 +00:00
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))
body = body.replace(i.group(0), "")
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-12-29 06:43:20 +00:00
if v.agendaposter and not v.marseyawarded: body = torture_ap(body, v.username)
2021-11-24 17:33:44 +00:00
2021-11-30 18:25:41 +00:00
if request.files.get("file2") and request.headers.get("cf-ipcountry") != "T1":
2022-02-24 19:05:04 +00:00
files = request.files.getlist('file2')[: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)
body += f"\n\n![]({process_image(name)})"
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.")
2021-11-30 18:25:41 +00:00
2022-01-11 19:46:50 +00:00
body_html = sanitize(body)
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
2022-02-14 02:33:27 +00:00
if len(body_html) > 40000: return error("Submission body too long!")
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 post and try again."
if ban.reason: reason += f" {ban.reason}"
2022-02-14 02:33:27 +00:00
return error(reason)
2021-10-15 14:08:27 +00:00
2022-04-04 17:52:14 +00:00
if request.host == 'rdrama.net' and v.admin_level < 2: club = False
2021-12-21 15:07:28 +00:00
else: club = bool(request.values.get("club",""))
2021-11-27 18:23:27 +00:00
if embed and len(embed) > 1500: embed = None
2022-03-31 15:49:12 +00:00
is_bot = bool(request.headers.get("Authorization")) or (SITE == 'pcmemes.net' and v.id == SNAPPY_ID)
2022-02-24 12:32:11 +00:00
post = Submission(
2021-10-15 14:08:27 +00:00
private=bool(request.values.get("private","")),
2022-04-04 17:29:52 +00:00
club=club,
2021-10-15 14:08:27 +00:00
author_id=v.id,
2022-01-07 21:44:38 +00:00
over_18=bool(request.values.get("over_18","")),
2022-02-27 22:49:34 +00:00
new=bool(request.values.get("new","")),
2021-10-15 14:08:27 +00:00
app_id=v.client.application.id if v.client else None,
2022-03-31 15:49:12 +00:00
is_bot = is_bot,
2021-10-15 14:08:27 +00:00
url=url,
2022-01-19 09:19:38 +00:00
body=body[:20000],
2021-10-15 14:08:27 +00:00
body_html=body_html,
embed_url=embed,
2021-12-01 17:44:24 +00:00
title=title[:500],
2021-12-13 21:27:25 +00:00
title_html=title_html,
2022-02-14 23:36:26 +00:00
sub=sub,
2022-03-09 01:49:57 +00:00
ghost=bool(request.values.get("ghost",""))
2021-10-15 14:08:27 +00:00
)
2022-02-24 12:32:11 +00:00
g.db.add(post)
2021-10-15 14:08:27 +00:00
g.db.flush()
2021-12-11 02:27:46 +00:00
2022-02-16 02:15:17 +00:00
2021-12-11 03:36:05 +00:00
if v and v.admin_level > 2:
2021-12-11 02:35:21 +00:00
for option in bet_options:
bet_option = Comment(author_id=AUTOBETTER_ID,
2022-02-24 12:32:11 +00:00
parent_submission=post.id,
2021-12-11 02:35:21 +00:00
level=1,
body_html=filter_emojis_only(option),
2022-01-22 09:58:22 +00:00
upvotes=0,
is_bot=True
2021-12-11 02:35:21 +00:00
)
2021-12-11 02:27:46 +00:00
2021-12-11 02:35:21 +00:00
g.db.add(bet_option)
2021-12-11 02:27:46 +00:00
2021-10-15 14:08:27 +00:00
for option in options:
2021-11-18 14:21:19 +00:00
c = Comment(author_id=AUTOPOLLER_ID,
2022-02-24 12:32:11 +00:00
parent_submission=post.id,
2021-10-15 14:08:27 +00:00
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
)
2022-02-07 11:39:26 +00:00
g.db.add(c)
2021-10-15 14:08:27 +00:00
2022-02-07 11:39:26 +00:00
for choice in choices:
c = Comment(author_id=AUTOCHOICE_ID,
2022-02-24 12:32:11 +00:00
parent_submission=post.id,
2022-02-07 11:39:26 +00:00
level=1,
body_html=filter_emojis_only(choice),
upvotes=0,
is_bot=True
)
2021-10-15 14:08:27 +00:00
g.db.add(c)
vote = Vote(user_id=v.id,
vote_type=1,
2022-02-24 12:32:11 +00:00
submission_id=post.id
2021-10-15 14:08:27 +00:00
)
g.db.add(vote)
2021-12-26 05:30:19 +00:00
2021-12-26 05:44:09 +00:00
if request.files.get('file') and request.headers.get("cf-ipcountry") != "T1":
file = request.files['file']
if file.content_type.startswith('image/'):
2022-03-25 22:30:15 +00:00
name = f'/images/{time.time()}'.replace('.','') + '.webp'
2022-01-24 23:40:34 +00:00
file.save(name)
2022-02-24 12:32:11 +00:00
post.url = process_image(name)
2022-01-24 23:40:34 +00:00
name2 = name.replace('.webp', 'r.webp')
copyfile(name, name2)
2022-02-24 12:32:11 +00:00
post.thumburl = process_image(name2, resize=100)
2021-12-26 05:44:09 +00:00
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-01-06 17:57:59 +00:00
if url.endswith('.'): url += 'mp4'
2022-02-24 12:32:11 +00:00
post.url = url
2021-12-29 12:38:54 +00:00
else:
2022-02-14 02:33:27 +00:00
return error("Image/Video files only.")
2022-01-13 21:15:36 +00:00
2022-02-24 12:32:11 +00:00
if not post.thumburl and post.url:
2022-02-27 15:52:24 +00:00
gevent.spawn(thumbnail_thread, post.id)
2021-10-15 14:08:27 +00:00
2022-02-14 23:36:26 +00:00
2022-02-24 12:32:11 +00:00
if not post.private and not post.ghost:
2021-10-25 13:55:56 +00:00
2022-02-27 21:57:44 +00:00
notify_users = NOTIFY_USERS(f'{title} {body}', v)
2021-11-25 22:38:37 +00:00
2022-03-01 00:06:50 +00:00
if notify_users:
2022-03-04 22:46:20 +00:00
cid = notif_comment2(post)
2022-03-01 00:06:50 +00:00
for x in notify_users:
add_notif(cid, x)
2021-12-20 20:03:59 +00:00
2022-03-01 00:06:50 +00:00
if request.values.get('followers') and v.followers:
2022-02-27 22:42:13 +00:00
text = f"@{v.username} has made a new post: [{post.title}]({post.shortlink})"
2022-03-09 02:04:37 +00:00
if post.sub: text += f" in <a href='/h/{post.sub}'>/h/{post.sub}"
2022-02-27 22:42:13 +00:00
cid = notif_comment(text, autojanny=True)
for follow in v.followers:
user = get_account(follow.user_id)
if post.club and not user.paid_dues: continue
add_notif(cid, user.id)
2021-10-15 14:08:27 +00:00
2022-02-14 23:36:26 +00:00
2022-02-24 12:32:11 +00:00
if v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in f'{post.body}{post.title}'.lower():
post.is_banned = True
post.ban_reason = "AutoJanny"
2021-10-15 14:08:27 +00:00
2022-01-18 11:19:32 +00:00
body = AGENDAPOSTER_MSG.format(username=v.username, type='post', 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,
2022-02-24 12:32:11 +00:00
parent_submission=post.id,
2021-10-15 14:08:27 +00:00
level=1,
over_18=False,
is_bot=True,
app_id=None,
2021-12-28 05:06:24 +00:00
is_pinned='AutoJanny',
2021-10-15 14:08:27 +00:00
distinguish_level=6,
body_html=body_jannied_html,
)
g.db.add(c_jannied)
g.db.flush()
2022-04-04 01:41:15 +00:00
c_jannied.top_comment_id = c_jannied.id
2021-10-15 14:08:27 +00:00
n = Notification(comment_id=c_jannied.id, user_id=v.id)
g.db.add(n)
2022-02-01 04:37:10 +00:00
2022-02-26 13:46:53 +00:00
2022-02-28 20:23:54 +00:00
if not (post.sub and g.db.query(Exile.user_id).filter_by(user_id=SNAPPY_ID, sub=post.sub).one_or_none()):
2022-02-26 13:46:53 +00:00
if post.sub == 'dankchristianmemes':
body = random.choice(christian_emojis)
elif v.id == CARP_ID:
if random.random() < 0.02: body = "i love you carp"
else: body = ":#marseyfuckoffcarp:"
elif v.id == LAWLZ_ID:
if random.random() < 0.5: body = "wow, this lawlzpost sucks!"
else: body = "wow, a good lawlzpost for once!"
else:
body = random.choice(snappyquotes)
if body.startswith('â–¼'):
body = body[1:]
vote = Vote(user_id=SNAPPY_ID,
vote_type=-1,
submission_id=post.id,
real = True
)
g.db.add(vote)
post.downvotes += 1
if body.startswith('OP is a Trump supporter'):
flag = Flag(post_id=post.id, user_id=SNAPPY_ID, reason='Trump supporter')
g.db.add(flag)
elif body.startswith('â–²'):
body = body[1:]
vote = Vote(user_id=SNAPPY_ID,
vote_type=1,
submission_id=post.id,
real = True
)
g.db.add(vote)
post.upvotes += 1
body += "\n\n"
if post.url:
if post.url.startswith('https://old.reddit.com/r/'):
rev = post.url.replace('https://old.reddit.com/', '')
rev = f"* [unddit.com](https://unddit.com/{rev})\n"
else: rev = ''
2022-03-26 11:36:25 +00:00
2022-02-26 13:46:53 +00:00
newposturl = post.url
if newposturl.startswith('/'): newposturl = f"{SITE_FULL}{newposturl}"
body += f"Snapshots:\n\n{rev}* [archive.org](https://web.archive.org/{newposturl})\n* [archive.ph](https://archive.ph/?url={quote(newposturl)}&run=1) (click to archive)\n\n"
gevent.spawn(archiveorg, newposturl)
2022-02-28 23:30:44 +00:00
captured = []
for i in list(snappy_url_regex.finditer(post.body_html))[:20]:
if i.group(0) in captured: continue
captured.append(i.group(0))
href = i.group(1)
2022-02-26 13:46:53 +00:00
if not href: continue
2022-02-28 23:30:44 +00:00
title = i.group(2)
2022-02-26 13:46:53 +00:00
if "Snapshots:\n\n" not in body: body += "Snapshots:\n\n"
if f'**[{title}]({href})**:\n\n' not in body:
body += f'**[{title}]({href})**:\n\n'
if href.startswith('https://old.reddit.com/'):
body += f'* [unddit.com](https://unddit.com/{href.replace("https://old.reddit.com/", "")})\n'
body += f'* [archive.org](https://web.archive.org/{href})\n'
body += f'* [archive.ph](https://archive.ph/?url={quote(href)}&run=1) (click to archive)\n\n'
gevent.spawn(archiveorg, href)
body_html = sanitize(body)
if len(body_html) < 40000:
c = Comment(author_id=SNAPPY_ID,
distinguish_level=6,
parent_submission=post.id,
level=1,
over_18=False,
is_bot=True,
app_id=None,
body_html=body_html
)
2022-02-01 04:37:10 +00:00
2022-02-26 13:46:53 +00:00
g.db.add(c)
2021-10-27 20:38:10 +00:00
2022-02-26 14:21:07 +00:00
snappy = g.db.query(User).filter_by(id = SNAPPY_ID).one_or_none()
2022-02-26 13:46:53 +00:00
snappy.comment_count += 1
snappy.coins += 1
g.db.add(snappy)
if body.startswith('!slots1000'):
check_for_slots_command(body, snappy, c)
2022-01-25 03:11:27 +00:00
2022-04-03 20:16:54 +00:00
g.db.flush()
2022-04-04 01:41:15 +00:00
c.top_comment_id = c.id
2022-02-26 13:46:53 +00:00
post.comment_count += 1
2022-04-03 20:16:54 +00:00
post.replies = [c]
2021-10-15 14:08:27 +00:00
2021-11-06 15:52:48 +00:00
v.post_count = g.db.query(Submission.id).filter_by(author_id=v.id, is_banned=False, deleted_utc=0).count()
2021-10-15 14:08:27 +00:00
g.db.add(v)
cache.delete_memoized(frontlist)
cache.delete_memoized(User.userpagelisting)
2022-02-24 12:32:11 +00:00
if v.admin_level > 0 and ("[changelog]" in post.title.lower() or "(changelog)" in post.title.lower()) and not post.private:
send_discord_message(post.permalink)
2021-10-15 14:08:27 +00:00
cache.delete_memoized(changeloglist)
2022-03-18 18:31:24 +00:00
if v.id == PIZZASHILL_ID:
2022-03-23 16:01:10 +00:00
for uid in PIZZA_VOTERS:
autovote = Vote(user_id=uid, submission_id=post.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-02-24 12:32:11 +00:00
post.upvotes += 3
g.db.add(post)
2021-10-15 14:08:27 +00:00
2022-01-11 19:46:50 +00:00
g.db.commit()
2021-11-23 19:54:40 +00:00
2022-02-24 12:32:11 +00:00
if request.headers.get("Authorization"): return post.json
2022-01-13 21:15:36 +00:00
else:
2022-02-24 12:32:11 +00:00
post.voted = 1
2022-02-28 19:36:30 +00:00
if post.new or 'megathread' in post.title.lower(): sort = 'new'
2022-01-13 21:15:36 +00:00
else: sort = v.defaultsortingcomments
2022-02-24 12:32:11 +00:00
return render_template('submission.html', v=v, p=post, sort=sort, render_replies=True, offset=0, success=True, sub=post.subr)
2021-10-15 14:08:27 +00:00
@app.post("/delete_post/<pid>")
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_post_pid(pid, v):
post = get_post(pid)
2022-01-22 09:58:22 +00:00
if post.author_id != v.id:
2021-10-15 14:08:27 +00:00
abort(403)
post.deleted_utc = int(time.time())
post.is_pinned = False
post.stickied = None
g.db.add(post)
cache.delete_memoized(frontlist)
g.db.commit()
return {"message": "Post deleted!"}
@app.post("/undelete_post/<pid>")
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_post_pid(pid, v):
post = get_post(pid)
2022-01-22 09:58:22 +00:00
if post.author_id != v.id: abort(403)
2021-10-15 14:08:27 +00:00
post.deleted_utc =0
g.db.add(post)
cache.delete_memoized(frontlist)
g.db.commit()
return {"message": "Post undeleted!"}
@app.post("/toggle_comment_nsfw/<cid>")
@auth_required
def toggle_comment_nsfw(cid, v):
2022-01-02 00:06:46 +00:00
comment = g.db.query(Comment).filter_by(id=cid).one_or_none()
2022-01-22 09:58:22 +00:00
if comment.author_id != v.id and not v.admin_level > 1: abort(403)
2021-10-15 14:08:27 +00:00
comment.over_18 = not comment.over_18
g.db.add(comment)
g.db.commit()
if comment.over_18: return {"message": "Comment has been marked as +18!"}
else: return {"message": "Comment has been unmarked as +18!"}
@app.post("/toggle_post_nsfw/<pid>")
@auth_required
def toggle_post_nsfw(pid, v):
post = get_post(pid)
2022-01-22 09:58:22 +00:00
if post.author_id != v.id and not v.admin_level > 1:
2021-10-15 14:08:27 +00:00
abort(403)
post.over_18 = not post.over_18
g.db.add(post)
if post.author_id!=v.id:
ma=ModAction(
kind="set_nsfw" if post.over_18 else "unset_nsfw",
user_id=v.id,
target_submission_id=post.id,
)
g.db.add(ma)
g.db.commit()
if post.over_18: return {"message": "Post has been marked as +18!"}
else: return {"message": "Post has been unmarked as +18!"}
@app.post("/save_post/<pid>")
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_post(pid, v):
post=get_post(pid)
2022-01-29 13:43:29 +00:00
save = g.db.query(SaveRelationship).filter_by(user_id=v.id, submission_id=post.id).one_or_none()
2021-10-15 14:08:27 +00:00
if not save:
2022-01-29 13:43:29 +00:00
new_save=SaveRelationship(user_id=v.id, submission_id=post.id)
2021-10-15 14:08:27 +00:00
g.db.add(new_save)
g.db.commit()
return {"message": "Post saved!"}
@app.post("/unsave_post/<pid>")
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_post(pid, v):
post=get_post(pid)
2022-01-29 13:43:29 +00:00
save = g.db.query(SaveRelationship).filter_by(user_id=v.id, submission_id=post.id).one_or_none()
2021-10-15 14:08:27 +00:00
if save:
g.db.delete(save)
g.db.commit()
return {"message": "Post unsaved!"}
2021-10-21 14:47:27 +00:00
@app.post("/pin/<post_id>")
@auth_required
def api_pin_post(post_id, v):
2022-01-02 00:06:46 +00:00
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
2021-10-21 14:47:27 +00:00
if post:
2021-12-27 05:09:22 +00:00
if v.id != post.author_id: return {"error": "Only the post author's can do that!"}
2021-10-21 14:47:27 +00:00
post.is_pinned = not post.is_pinned
g.db.add(post)
2021-12-29 06:43:20 +00:00
cache.delete_memoized(User.userpagelisting)
g.db.commit()
2021-10-21 14:47:27 +00:00
if post.is_pinned: return {"message": "Post pinned!"}
2022-02-18 19:12:14 +00:00
else: return {"message": "Post unpinned!"}
2022-02-27 19:27:03 +00:00
return {"error": "Post not found!"}
2022-02-18 19:12:14 +00:00
@app.get("/submit/title")
@limiter.limit("6/minute")
@auth_required
def get_post_title(v):
2022-03-04 23:49:38 +00:00
url = request.values.get("url")
2022-02-18 19:12:14 +00:00
if not url: abort(400)
try: x = requests.get(url, headers=titleheaders, timeout=5)
except: abort(400)
2022-02-28 19:36:30 +00:00
soup = BeautifulSoup(x.content, 'lxml')
2022-02-18 19:12:14 +00:00
title = soup.find('title')
if not title: abort(400)
return {"url": url, "title": title.string}