2022-11-15 09:19:08 +00:00
|
|
|
import os
|
2022-05-04 23:09:46 +00:00
|
|
|
import time
|
2022-11-26 06:11:00 +00:00
|
|
|
import html
|
2022-11-15 09:19:08 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from os import path
|
|
|
|
from shutil import copyfile
|
|
|
|
from sys import stdout
|
|
|
|
from urllib.parse import ParseResult, quote, unquote, urlparse, urlunparse
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
import gevent
|
|
|
|
import requests
|
2022-11-15 09:19:08 +00:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
from files.__main__ import app, cache, limiter
|
|
|
|
from files.classes import *
|
|
|
|
from files.helpers.actions import *
|
2022-05-04 23:09:46 +00:00
|
|
|
from files.helpers.alerts import *
|
|
|
|
from files.helpers.const import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.discord import *
|
|
|
|
from files.helpers.get import *
|
2022-06-24 14:30:59 +00:00
|
|
|
from files.helpers.regex import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.sanitize import *
|
|
|
|
from files.helpers.settings import get_setting
|
2022-05-04 23:09:46 +00:00
|
|
|
from files.helpers.slots import *
|
2022-07-09 10:32:49 +00:00
|
|
|
from files.helpers.sorting_and_time import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.routes.routehelpers import execute_shadowban_viewers_and_voters
|
|
|
|
from files.routes.wrappers import *
|
|
|
|
|
2022-07-13 21:03:11 +00:00
|
|
|
from .front import frontlist
|
2022-11-15 09:19:08 +00:00
|
|
|
from .users import userpagelisting
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.__main__ import app, limiter
|
2022-05-04 23:09:46 +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-10-07 02:48:31 +00:00
|
|
|
@app.post("/club_post/<pid>")
|
2022-10-11 07:29:24 +00:00
|
|
|
@feature_required('COUNTRY_CLUB')
|
2022-11-14 15:11:05 +00:00
|
|
|
@auth_required
|
2022-10-07 02:48:31 +00:00
|
|
|
def club_post(pid, v):
|
2022-05-04 23:09:46 +00:00
|
|
|
post = get_post(pid)
|
2022-10-06 00:57:08 +00:00
|
|
|
if post.author_id != v.id and v.admin_level < PERMS['POST_COMMENT_MODERATION']: abort(403)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-07 02:48:31 +00:00
|
|
|
if not post.club:
|
|
|
|
post.club = True
|
|
|
|
g.db.add(post)
|
|
|
|
|
|
|
|
if post.author_id != v.id:
|
|
|
|
ma = ModAction(
|
|
|
|
kind = "club_post",
|
|
|
|
user_id = v.id,
|
|
|
|
target_submission_id = post.id,
|
|
|
|
)
|
|
|
|
g.db.add(ma)
|
|
|
|
|
2022-11-14 05:49:14 +00:00
|
|
|
message = f"@{v.username} (Admin) has marked [{post.title}]({post.shortlink}) as {CC_TITLE}!"
|
2022-10-07 02:48:31 +00:00
|
|
|
send_repeatable_notification(post.author_id, message)
|
|
|
|
|
2022-11-14 05:49:14 +00:00
|
|
|
return {"message": f"Post has been marked as {CC_TITLE}!"}
|
2022-10-07 02:48:31 +00:00
|
|
|
|
|
|
|
@app.post("/unclub_post/<pid>")
|
2022-10-11 07:29:24 +00:00
|
|
|
@feature_required('COUNTRY_CLUB')
|
2022-11-14 15:11:05 +00:00
|
|
|
@auth_required
|
2022-10-07 02:48:31 +00:00
|
|
|
def unclub_post(pid, v):
|
|
|
|
post = get_post(pid)
|
2022-10-16 10:41:54 +00:00
|
|
|
if post.author_id != v.id and v.admin_level < PERMS['POST_COMMENT_MODERATION']: abort(403)
|
2022-10-07 02:48:31 +00:00
|
|
|
|
|
|
|
if post.club:
|
|
|
|
post.club = False
|
|
|
|
g.db.add(post)
|
|
|
|
|
|
|
|
if post.author_id != v.id:
|
|
|
|
ma = ModAction(
|
|
|
|
kind = "unclub_post",
|
|
|
|
user_id = v.id,
|
|
|
|
target_submission_id = post.id,
|
|
|
|
)
|
|
|
|
g.db.add(ma)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-14 05:49:14 +00:00
|
|
|
message = f"@{v.username} (Admin) has unmarked [{post.title}]({post.shortlink}) as {CC_TITLE}!"
|
2022-10-07 02:48:31 +00:00
|
|
|
send_repeatable_notification(post.author_id, message)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-14 05:49:14 +00:00
|
|
|
return {"message": f"Post has been unmarked as {CC_TITLE}!"}
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.post("/publish/<pid>")
|
2022-11-13 06:43:35 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT_SLOWER)
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-11-13 10:18:52 +00:00
|
|
|
@ratelimit_user()
|
2022-05-04 23:09:46 +00:00
|
|
|
def publish(pid, v):
|
|
|
|
post = get_post(pid)
|
|
|
|
if not post.private: return {"message": "Post published!"}
|
|
|
|
|
|
|
|
if post.author_id != v.id: abort(403)
|
|
|
|
post.private = False
|
|
|
|
post.created_utc = int(time.time())
|
|
|
|
g.db.add(post)
|
|
|
|
|
|
|
|
if not post.ghost:
|
|
|
|
notify_users = NOTIFY_USERS(f'{post.title} {post.body}', v)
|
|
|
|
|
|
|
|
if notify_users:
|
|
|
|
cid = notif_comment2(post)
|
|
|
|
for x in notify_users:
|
|
|
|
add_notif(cid, x)
|
|
|
|
|
|
|
|
|
|
|
|
cache.delete_memoized(frontlist)
|
2022-11-15 09:19:08 +00:00
|
|
|
cache.delete_memoized(userpagelisting)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-16 17:49:24 +00:00
|
|
|
if post.sub == 'changelog':
|
|
|
|
send_changelog_message(post.permalink)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-17 19:13:33 +00:00
|
|
|
if SITE == 'watchpeopledie.tv':
|
2022-07-10 14:25:42 +00:00
|
|
|
send_wpd_message(post.permalink)
|
|
|
|
|
2022-07-08 11:44:17 +00:00
|
|
|
execute_snappy(post, v)
|
|
|
|
|
2022-07-10 14:21:16 +00:00
|
|
|
if is_site_url(request.referrer):
|
2022-06-27 02:34:15 +00:00
|
|
|
return redirect(request.referrer)
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
return redirect(post.permalink)
|
|
|
|
|
|
|
|
@app.get("/submit")
|
|
|
|
@app.get("/h/<sub>/submit")
|
|
|
|
@auth_required
|
2022-11-26 21:00:03 +00:00
|
|
|
def submit_get(v:User, sub=None):
|
2022-10-05 10:30:44 +00:00
|
|
|
sub = get_sub_by_name(sub, graceful=True)
|
2022-05-04 23:09:46 +00:00
|
|
|
if request.path.startswith('/h/') and not sub: abort(404)
|
|
|
|
|
|
|
|
SUBS = [x[0] for x in g.db.query(Sub.name).order_by(Sub.name).all()]
|
|
|
|
|
|
|
|
return render_template("submit.html", SUBS=SUBS, v=v, sub=sub)
|
|
|
|
|
|
|
|
@app.get("/post/<pid>")
|
|
|
|
@app.get("/post/<pid>/<anything>")
|
|
|
|
@app.get("/h/<sub>/post/<pid>")
|
|
|
|
@app.get("/h/<sub>/post/<pid>/<anything>")
|
2022-08-05 20:40:48 +00:00
|
|
|
@auth_desired_with_logingate
|
2022-05-04 23:09:46 +00:00
|
|
|
def post_id(pid, anything=None, v=None, sub=None):
|
2022-07-08 18:28:56 +00:00
|
|
|
post = get_post(pid, v=v)
|
2022-11-17 21:02:08 +00:00
|
|
|
if not User.can_see(v, post): abort(403)
|
|
|
|
if not User.can_see_content(v, post) and post.club: abort(403)
|
2022-08-19 22:36:28 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if post.over_18 and not (v and v.over_18) and session.get('over_18', 0) < int(time.time()):
|
2022-10-15 09:11:36 +00:00
|
|
|
if g.is_api_or_xhr: return {"error":"Must be 18+ to view"}, 451
|
2022-05-04 23:09:46 +00:00
|
|
|
return render_template("errors/nsfw.html", v=v)
|
|
|
|
|
2022-08-30 18:22:15 +00:00
|
|
|
if post.new or 'megathread' in post.title.lower(): defaultsortingcomments = 'new'
|
2022-05-04 23:09:46 +00:00
|
|
|
elif v: defaultsortingcomments = v.defaultsortingcomments
|
2022-10-11 16:41:09 +00:00
|
|
|
else: defaultsortingcomments = "hot"
|
2022-05-04 23:09:46 +00:00
|
|
|
sort = request.values.get("sort", defaultsortingcomments)
|
|
|
|
|
|
|
|
if v:
|
2022-11-15 09:19:08 +00:00
|
|
|
execute_shadowban_viewers_and_voters(v, post)
|
2022-10-29 00:13:37 +00:00
|
|
|
# shadowban check is done in sort_objects
|
|
|
|
# output is needed: see comments.py
|
|
|
|
comments, output = get_comments_v_properties(v, True, None, Comment.parent_submission == post.id, Comment.level < 10)
|
2022-11-29 00:04:06 +00:00
|
|
|
pinned = [c[0] for c in comments.filter(Comment.stickied != None).order_by(Comment.created_utc.desc()).all()]
|
2022-06-26 01:12:31 +00:00
|
|
|
comments = comments.filter(Comment.level == 1, Comment.stickied == None)
|
2022-10-12 08:05:26 +00:00
|
|
|
comments = sort_objects(sort, comments, Comment,
|
2022-10-13 04:10:34 +00:00
|
|
|
include_shadowbanned=(v and v.can_see_shadowbanned))
|
2022-09-04 23:21:48 +00:00
|
|
|
comments = [c[0] for c in comments.all()]
|
2022-05-04 23:09:46 +00:00
|
|
|
else:
|
2022-11-29 00:04:06 +00:00
|
|
|
pinned = g.db.query(Comment).filter(Comment.parent_submission == post.id, Comment.stickied != None).order_by(Comment.created_utc.desc()).all()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-13 10:47:55 +00:00
|
|
|
comments = g.db.query(Comment).filter(
|
|
|
|
Comment.parent_submission == post.id,
|
|
|
|
Comment.level == 1,
|
|
|
|
Comment.stickied == None
|
|
|
|
)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-12 08:05:26 +00:00
|
|
|
comments = sort_objects(sort, comments, Comment,
|
2022-10-13 04:10:34 +00:00
|
|
|
include_shadowbanned=False)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-09-04 23:21:48 +00:00
|
|
|
comments = comments.all()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-04 03:37:48 +00:00
|
|
|
offset = 0
|
|
|
|
ids = set()
|
|
|
|
|
2022-10-25 18:20:43 +00:00
|
|
|
threshold = 100
|
2022-06-27 19:08:05 +00:00
|
|
|
|
2022-10-15 09:11:36 +00:00
|
|
|
if post.comment_count > threshold+25 and not (v and v.client) and not request.values.get("all"):
|
2022-05-04 23:09:46 +00:00
|
|
|
comments2 = []
|
|
|
|
count = 0
|
2022-11-15 10:43:19 +00:00
|
|
|
if post.created_utc > 1638672040: # TODO: migrate old comments to use top_comment_id
|
2022-07-04 03:37:48 +00:00
|
|
|
for comment in comments:
|
|
|
|
comments2.append(comment)
|
|
|
|
ids.add(comment.id)
|
|
|
|
count += g.db.query(Comment).filter_by(parent_submission=post.id, top_comment_id=comment.id).count() + 1
|
|
|
|
if count > threshold: break
|
|
|
|
else:
|
|
|
|
for comment in comments:
|
|
|
|
comments2.append(comment)
|
|
|
|
ids.add(comment.id)
|
|
|
|
count += g.db.query(Comment).filter_by(parent_submission=post.id, parent_comment_id=comment.id).count() + 1
|
|
|
|
if count > 20: break
|
|
|
|
|
|
|
|
if len(comments) == len(comments2): offset = 0
|
|
|
|
else: offset = 1
|
2022-05-04 23:09:46 +00:00
|
|
|
comments = comments2
|
|
|
|
|
2022-11-29 00:04:06 +00:00
|
|
|
pinned2 = {}
|
2022-05-04 23:09:46 +00:00
|
|
|
for pin in pinned:
|
2022-05-26 23:08:23 +00:00
|
|
|
if pin.stickied_utc and int(time.time()) > pin.stickied_utc:
|
|
|
|
pin.stickied = None
|
|
|
|
pin.stickied_utc = None
|
2022-05-04 23:09:46 +00:00
|
|
|
g.db.add(pin)
|
2022-09-02 00:46:17 +00:00
|
|
|
elif pin.level > 1:
|
2022-11-29 00:04:06 +00:00
|
|
|
pinned2[pin.top_comment(g.db)] = ''
|
2022-11-18 21:11:03 +00:00
|
|
|
if pin.top_comment(g.db) in comments:
|
2022-11-15 10:29:47 +00:00
|
|
|
comments.remove(pin.top_comment(g.db))
|
2022-11-12 11:33:18 +00:00
|
|
|
else:
|
2022-11-29 00:04:06 +00:00
|
|
|
pinned2[pin] = ''
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-29 00:04:06 +00:00
|
|
|
post.replies = list(pinned2.keys()) + comments
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
post.views += 1
|
|
|
|
g.db.add(post)
|
2022-08-08 22:21:59 +00:00
|
|
|
|
2022-10-15 09:11:36 +00:00
|
|
|
if v and v.client:
|
2022-11-15 09:19:08 +00:00
|
|
|
return post.json(g.db)
|
2022-08-08 22:21:59 +00:00
|
|
|
|
|
|
|
template = "submission.html"
|
|
|
|
if (post.is_banned or post.author.shadowbanned) \
|
2022-10-06 00:57:08 +00:00
|
|
|
and not (v and (v.admin_level >= PERMS['POST_COMMENT_MODERATION'] or post.author_id == v.id)):
|
2022-08-08 22:21:59 +00:00
|
|
|
template = "submission_banned.html"
|
|
|
|
|
|
|
|
return render_template(template, v=v, p=post, ids=list(ids),
|
|
|
|
sort=sort, render_replies=True, offset=offset, sub=post.subr,
|
2022-11-30 18:26:07 +00:00
|
|
|
fart=get_setting('fart_mode'))
|
2022-07-04 03:37:48 +00:00
|
|
|
|
|
|
|
@app.get("/viewmore/<pid>/<sort>/<offset>")
|
2022-11-13 06:43:35 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT_SLOWER)
|
2022-08-05 20:40:48 +00:00
|
|
|
@auth_desired_with_logingate
|
2022-07-04 03:37:48 +00:00
|
|
|
def viewmore(v, pid, sort, offset):
|
|
|
|
post = get_post(pid, v=v)
|
|
|
|
if post.club and not (v and (v.paid_dues or v.id == post.author_id)): abort(403)
|
2022-10-16 09:51:42 +00:00
|
|
|
try:
|
|
|
|
offset = int(offset)
|
|
|
|
except: abort(400)
|
2022-07-04 03:37:48 +00:00
|
|
|
try: ids = set(int(x) for x in request.values.get("ids").split(','))
|
|
|
|
except: abort(400)
|
|
|
|
|
|
|
|
if v:
|
2022-10-29 00:13:37 +00:00
|
|
|
# shadowban check is done in sort_objects
|
|
|
|
# output is needed: see comments.py
|
|
|
|
comments, output = get_comments_v_properties(v, True, None, Comment.parent_submission == pid, Comment.stickied == None, Comment.id.notin_(ids), Comment.level < 10)
|
2022-07-04 03:37:48 +00:00
|
|
|
comments = comments.filter(Comment.level == 1)
|
2022-10-12 08:05:26 +00:00
|
|
|
comments = sort_objects(sort, comments, Comment,
|
2022-10-13 04:10:34 +00:00
|
|
|
include_shadowbanned=(v and v.can_see_shadowbanned))
|
2022-07-04 03:37:48 +00:00
|
|
|
|
2022-09-04 23:21:48 +00:00
|
|
|
comments = [c[0] for c in comments.all()]
|
2022-07-04 03:37:48 +00:00
|
|
|
else:
|
2022-10-13 10:47:55 +00:00
|
|
|
comments = g.db.query(Comment).filter(
|
|
|
|
Comment.parent_submission == pid,
|
|
|
|
Comment.level == 1,
|
|
|
|
Comment.stickied == None,
|
|
|
|
Comment.id.notin_(ids)
|
|
|
|
)
|
2022-07-04 03:37:48 +00:00
|
|
|
|
2022-10-12 08:05:26 +00:00
|
|
|
comments = sort_objects(sort, comments, Comment,
|
2022-10-13 04:10:34 +00:00
|
|
|
include_shadowbanned=False)
|
2022-07-04 03:37:48 +00:00
|
|
|
|
2022-09-04 23:21:48 +00:00
|
|
|
comments = comments.offset(offset).all()
|
2022-07-04 03:37:48 +00:00
|
|
|
|
|
|
|
comments2 = []
|
|
|
|
count = 0
|
2022-11-15 10:43:19 +00:00
|
|
|
if post.created_utc > 1638672040: # TODO: migrate old comments to use top_comment_id
|
2022-07-04 03:37:48 +00:00
|
|
|
for comment in comments:
|
|
|
|
comments2.append(comment)
|
|
|
|
ids.add(comment.id)
|
|
|
|
count += g.db.query(Comment).filter_by(parent_submission=post.id, top_comment_id=comment.id).count() + 1
|
|
|
|
if count > 100: break
|
|
|
|
else:
|
|
|
|
for comment in comments:
|
|
|
|
comments2.append(comment)
|
|
|
|
ids.add(comment.id)
|
|
|
|
count += g.db.query(Comment).filter_by(parent_submission=post.id, parent_comment_id=comment.id).count() + 1
|
|
|
|
if count > 20: break
|
|
|
|
|
|
|
|
if len(comments) == len(comments2): offset = 0
|
|
|
|
else: offset += 1
|
|
|
|
comments = comments2
|
|
|
|
|
2022-08-30 05:26:13 +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)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/morecomments/<cid>")
|
2022-11-13 06:43:35 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT_SLOWER)
|
2022-08-05 20:40:48 +00:00
|
|
|
@auth_desired_with_logingate
|
2022-05-04 23:09:46 +00:00
|
|
|
def morecomments(v, cid):
|
|
|
|
try: cid = int(cid)
|
2022-10-09 08:25:21 +00:00
|
|
|
except: abort(404)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
tcid = g.db.query(Comment.top_comment_id).filter_by(id=cid).one_or_none()[0]
|
|
|
|
|
|
|
|
if v:
|
2022-10-29 00:13:37 +00:00
|
|
|
# shadowban check is done in sort_objects i think
|
|
|
|
# output is needed: see comments.py
|
|
|
|
comments, output = get_comments_v_properties(v, True, lambda c:bool(c.parent_comment_id == int(cid)), Comment.top_comment_id == tcid, Comment.level > 9)
|
2022-05-04 23:09:46 +00:00
|
|
|
comments = output
|
|
|
|
else:
|
2022-06-24 13:19:53 +00:00
|
|
|
c = get_comment(cid)
|
2022-11-15 09:19:08 +00:00
|
|
|
comments = c.replies(sort=request.values.get('sort'), v=v, db=g.db)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if comments: p = comments[0].post
|
|
|
|
else: p = None
|
|
|
|
|
2022-08-30 05:26:13 +00:00
|
|
|
return render_template("comments.html", v=v, comments=comments, p=p, render_replies=True)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@app.post("/edit_post/<pid>")
|
2022-06-09 18:09:18 +00:00
|
|
|
@limiter.limit("1/second;10/minute;100/hour;200/day")
|
2022-11-14 00:00:55 +00:00
|
|
|
@is_not_permabanned
|
2022-11-25 23:37:04 +00:00
|
|
|
@ratelimit_user("1/second;10/minute;100/hour;200/day")
|
2022-05-04 23:09:46 +00:00
|
|
|
def edit_post(pid, v):
|
2022-07-08 18:28:56 +00:00
|
|
|
p = get_post(pid)
|
2022-10-06 01:24:37 +00:00
|
|
|
if v.id != p.author_id and v.admin_level < PERMS['POST_EDITING']:
|
|
|
|
abort(403)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-11 03:40:08 +00:00
|
|
|
# Disable edits on things older than 1wk unless it's a draft or editor is a jannie
|
|
|
|
if (time.time() - p.created_utc > 7*24*60*60 and not p.private
|
|
|
|
and not v.admin_level >= PERMS['POST_EDITING']):
|
2022-10-11 13:01:39 +00:00
|
|
|
abort(403, "You can't edit posts older than 1 week!")
|
2022-10-11 03:40:08 +00:00
|
|
|
|
2022-10-05 08:04:32 +00:00
|
|
|
title = sanitize_raw_title(request.values.get("title", ""))
|
2022-10-09 12:54:46 +00:00
|
|
|
body = sanitize_raw_body(request.values.get("body", ""), True)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if v.id == p.author_id:
|
|
|
|
if v.longpost and (len(body) < 280 or ' [](' in body or body.startswith('[](')):
|
2022-10-11 13:01:39 +00:00
|
|
|
abort(403, "You have to type more than 280 characters!")
|
2022-05-04 23:09:46 +00:00
|
|
|
elif v.bird and len(body) > 140:
|
2022-10-11 13:01:39 +00:00
|
|
|
abort(403, "You have to type less than 140 characters!")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-05 08:04:32 +00:00
|
|
|
if not title:
|
2022-10-11 13:01:39 +00:00
|
|
|
abort(400, "Please enter a better title.")
|
2022-05-04 23:09:46 +00:00
|
|
|
if title != p.title:
|
2022-09-05 20:05:04 +00:00
|
|
|
torture = (v.agendaposter and not v.marseyawarded and p.sub != 'chudrama' and v.id == p.author_id)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-09-16 16:30:34 +00:00
|
|
|
title_html = filter_emojis_only(title, golden=False, torture=torture)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if v.id == p.author_id and v.marseyawarded and not marseyaward_title_regex.fullmatch(title_html):
|
2022-10-11 13:01:39 +00:00
|
|
|
abort(403, "You can only type marseys!")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-05 08:04:32 +00:00
|
|
|
p.title = title
|
2022-05-04 23:09:46 +00:00
|
|
|
p.title_html = title_html
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
body += process_files(request.files, v)
|
2022-10-05 09:54:44 +00:00
|
|
|
body = body.strip()[:POST_BODY_LENGTH_LIMIT] # process_files() may be adding stuff to the body
|
2022-05-22 10:26:59 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if body != p.body:
|
2022-11-12 10:30:02 +00:00
|
|
|
if v and v.admin_level >= PERMS['POST_BETS']:
|
|
|
|
for i in bet_regex.finditer(body):
|
|
|
|
body = body.replace(i.group(0), "")
|
|
|
|
body_html = filter_emojis_only(i.group(1))
|
|
|
|
if len(body_html) > 500: abort(400, "Bet option too long!")
|
|
|
|
bet = SubmissionOption(
|
|
|
|
submission_id=p.id,
|
|
|
|
body_html=body_html,
|
|
|
|
exclusive = 2
|
|
|
|
)
|
|
|
|
g.db.add(bet)
|
|
|
|
|
2022-11-25 21:33:38 +00:00
|
|
|
for i in list(poll_regex.finditer(body))[:10]:
|
2022-05-09 09:32:55 +00:00
|
|
|
body = body.replace(i.group(0), "")
|
2022-11-12 10:27:01 +00:00
|
|
|
body_html = filter_emojis_only(i.group(1))
|
|
|
|
if len(body_html) > 500: abort(400, "Poll option too long!")
|
2022-07-02 06:48:04 +00:00
|
|
|
option = SubmissionOption(
|
|
|
|
submission_id=p.id,
|
2022-11-12 10:27:01 +00:00
|
|
|
body_html=body_html,
|
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
|
|
|
|
2022-11-25 21:33:38 +00:00
|
|
|
for i in list(choice_regex.finditer(body))[:10]:
|
2022-05-09 09:32:55 +00:00
|
|
|
body = body.replace(i.group(0), "")
|
2022-11-12 10:27:01 +00:00
|
|
|
body_html = filter_emojis_only(i.group(1))
|
|
|
|
if len(body_html) > 500: abort(400, "Poll option too long!")
|
2022-11-12 10:30:02 +00:00
|
|
|
choice = SubmissionOption(
|
2022-07-02 06:48:04 +00:00
|
|
|
submission_id=p.id,
|
2022-11-12 10:27:01 +00:00
|
|
|
body_html=body_html,
|
2022-08-26 21:53:17 +00:00
|
|
|
exclusive = 1
|
2022-07-02 06:48:04 +00:00
|
|
|
)
|
2022-11-12 10:30:02 +00:00
|
|
|
g.db.add(choice)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-09-05 20:05:04 +00:00
|
|
|
torture = (v.agendaposter and not v.marseyawarded and p.sub != 'chudrama' and v.id == p.author_id)
|
|
|
|
|
2022-09-16 16:30:34 +00:00
|
|
|
body_html = sanitize(body, golden=False, limit_pings=100, showmore=False, torture=torture)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if v.id == p.author_id and v.marseyawarded and marseyaward_body_regex.search(body_html):
|
2022-10-11 13:01:39 +00:00
|
|
|
abort(403, "You can only type marseys!")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
p.body = body
|
|
|
|
|
2022-11-30 17:37:35 +00:00
|
|
|
execute_under_siege(v, p, p.body, 'submission')
|
|
|
|
|
|
|
|
for text in [p.body, p.title, p.url]:
|
2022-10-21 00:28:05 +00:00
|
|
|
if not execute_blackjack(v, p, text, 'submission'): break
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-11 13:01:39 +00:00
|
|
|
if len(body_html) > POST_BODY_HTML_LENGTH_LIMIT:
|
|
|
|
abort(400, f"Submission body_html too long! (max {POST_BODY_HTML_LENGTH_LIMIT} characters)")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
p.body_html = body_html
|
|
|
|
|
2022-08-19 22:20:25 +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() and p.sub != 'chudrama':
|
2022-10-11 13:01:39 +00:00
|
|
|
abort(403, f'You have to include "{AGENDAPOSTER_PHRASE}" in your post!')
|
2022-05-04 23:09:46 +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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
return redirect(p.permalink)
|
|
|
|
|
|
|
|
|
2022-11-15 10:57:49 +00:00
|
|
|
def thumbnail_thread(pid:int, vid:int):
|
|
|
|
db = db_session()
|
2022-05-04 23:09:46 +00:00
|
|
|
def expand_url(post_url, fragment_url):
|
|
|
|
if fragment_url.startswith("https://"):
|
|
|
|
return fragment_url
|
|
|
|
elif fragment_url.startswith("https://"):
|
|
|
|
return f"https://{fragment_url.split('https://')[1]}"
|
|
|
|
elif fragment_url.startswith('//'):
|
|
|
|
return f"https:{fragment_url}"
|
2022-05-25 19:45:34 +00:00
|
|
|
elif fragment_url.startswith('/') and '\\' not in fragment_url:
|
2022-05-04 23:09:46 +00:00
|
|
|
parsed_url = urlparse(post_url)
|
|
|
|
return f"https://{parsed_url.netloc}{fragment_url}"
|
|
|
|
else:
|
2022-07-08 18:06:54 +00:00
|
|
|
return f"{post_url}/{fragment_url}"
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-08-25 15:26:27 +00:00
|
|
|
post = db.get(Submission, pid)
|
|
|
|
|
|
|
|
if not post or not post.url:
|
|
|
|
time.sleep(5)
|
|
|
|
post = db.get(Submission, pid)
|
|
|
|
|
|
|
|
if not post or not post.url: return
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
fetch_url = post.url
|
|
|
|
|
2022-05-25 19:45:34 +00:00
|
|
|
if fetch_url.startswith('/') and '\\' not in fetch_url:
|
2022-10-27 17:53:08 +00:00
|
|
|
fetch_url = f"{SITE_FULL}{fetch_url}"
|
2022-05-04 23:09:46 +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"}
|
|
|
|
|
2022-08-25 15:26:27 +00:00
|
|
|
try:
|
|
|
|
x=requests.get(fetch_url, headers=headers, timeout=5, proxies=proxies)
|
|
|
|
except:
|
|
|
|
db.close()
|
|
|
|
return
|
|
|
|
|
|
|
|
if x.status_code != 200:
|
|
|
|
db.close()
|
|
|
|
return
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if x.headers.get("Content-Type","").startswith("text/html"):
|
|
|
|
soup=BeautifulSoup(x.content, 'lxml')
|
|
|
|
|
|
|
|
thumb_candidate_urls=[]
|
|
|
|
|
|
|
|
meta_tags = [
|
|
|
|
"drama:thumbnail",
|
|
|
|
"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:
|
|
|
|
image_req=requests.get(url, headers=headers, timeout=5, proxies=proxies)
|
|
|
|
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-10-25 15:39:57 +00:00
|
|
|
with Image.open(BytesIO(image_req.content)) as i:
|
|
|
|
if i.width < 30 or i.height < 30:
|
|
|
|
continue
|
2022-05-04 23:09:46 +00:00
|
|
|
break
|
2022-08-25 15:26:27 +00:00
|
|
|
else:
|
|
|
|
db.close()
|
|
|
|
return
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
elif x.headers.get("Content-Type","").startswith("image/"):
|
|
|
|
image_req=x
|
2022-10-25 15:39:57 +00:00
|
|
|
with Image.open(BytesIO(x.content)) as i:
|
|
|
|
size = len(i.fp.read())
|
2022-10-28 17:13:21 +00:00
|
|
|
if size > 8 * 1024 * 1024:
|
|
|
|
db.close()
|
|
|
|
return
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-08-25 15:26:27 +00:00
|
|
|
else:
|
|
|
|
db.close()
|
|
|
|
return
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
name = f'/images/{time.time()}'.replace('.','') + '.webp'
|
|
|
|
|
|
|
|
with open(name, "wb") as file:
|
|
|
|
for chunk in image_req.iter_content(1024):
|
|
|
|
file.write(chunk)
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
v = db.get(User, vid)
|
|
|
|
url = process_image(name, v, resize=100, uploader_id=post.author_id, db=db)
|
|
|
|
if url:
|
|
|
|
post.thumburl = url
|
|
|
|
db.add(post)
|
|
|
|
db.commit()
|
2022-08-25 15:26:27 +00:00
|
|
|
db.close()
|
2022-05-04 23:09:46 +00:00
|
|
|
stdout.flush()
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/is_repost")
|
2022-08-11 04:05:23 +00:00
|
|
|
def is_repost():
|
2022-10-14 10:26:48 +00:00
|
|
|
not_a_repost = {'permalink': ''}
|
2022-08-04 04:07:17 +00:00
|
|
|
if not FEATURES['REPOST_DETECTION']:
|
2022-10-14 10:26:48 +00:00
|
|
|
return not_a_repost
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
url = request.values.get('url')
|
2022-10-14 10:26:48 +00:00
|
|
|
if not url or len(url) < MIN_REPOST_CHECK_URL_LENGTH: abort(400)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-05-25 08:43:16 +00:00
|
|
|
url = normalize_url(url)
|
2022-05-04 23:09:46 +00:00
|
|
|
parsed_url = urlparse(url)
|
|
|
|
|
|
|
|
domain = parsed_url.netloc
|
2022-06-29 01:50:55 +00:00
|
|
|
if domain in ('old.reddit.com','twitter.com','instagram.com','tiktok.com') and '/search' not in url:
|
2022-05-04 23:09:46 +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:
|
2022-09-01 20:46:57 +00:00
|
|
|
qd = parse_qs(parsed_url.query, keep_blank_values=True)
|
2022-05-04 23:09:46 +00:00
|
|
|
filtered = {k: val for k, val in qd.items() if not k.startswith('utm_') and not k.startswith('ref_')}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
url = urlunparse(new_url)
|
2022-09-05 01:44:38 +00:00
|
|
|
url = url.rstrip('/')
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
search_url = url.replace('%', '').replace('\\', '').replace('_', '\_').strip()
|
|
|
|
repost = g.db.query(Submission).filter(
|
|
|
|
Submission.url.ilike(search_url),
|
|
|
|
Submission.deleted_utc == 0,
|
|
|
|
Submission.is_banned == False
|
|
|
|
).first()
|
|
|
|
if repost: return {'permalink': repost.permalink}
|
2022-10-14 10:26:48 +00:00
|
|
|
else: return not_a_repost
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@app.post("/submit")
|
|
|
|
@app.post("/h/<sub>/submit")
|
2022-06-03 02:10:59 +00:00
|
|
|
@limiter.limit(POST_RATE_LIMIT)
|
2022-07-13 18:14:37 +00:00
|
|
|
@limiter.limit(POST_RATE_LIMIT, key_func=lambda:f'{SITE}-{session.get("lo_user")}')
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-11-26 21:00:03 +00:00
|
|
|
def submit_post(v:User, sub=None):
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
url = request.values.get("url", "").strip()
|
2022-05-25 18:29:22 +00:00
|
|
|
|
|
|
|
if '\\' in url: abort(400)
|
|
|
|
|
2022-10-05 08:04:32 +00:00
|
|
|
title = sanitize_raw_title(request.values.get("title", ""))
|
2022-10-09 12:54:46 +00:00
|
|
|
body = sanitize_raw_body(request.values.get("body", ""), True)
|
2022-06-29 01:13:11 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
def error(error):
|
2022-10-15 09:11:36 +00:00
|
|
|
if g.is_api_or_xhr: abort(400, error)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
SUBS = [x[0] for x in g.db.query(Sub.name).order_by(Sub.name).all()]
|
|
|
|
return render_template("submit.html", SUBS=SUBS, v=v, error=error, title=title, url=url, body=body), 400
|
|
|
|
|
2022-10-05 08:04:32 +00:00
|
|
|
if not title:
|
|
|
|
return error("Please enter a better title.")
|
|
|
|
torture = (v.agendaposter and not v.marseyawarded and sub != 'chudrama')
|
|
|
|
title_html = filter_emojis_only(title, graceful=True, count_marseys=True, torture=torture)
|
|
|
|
if v.marseyawarded and not marseyaward_title_regex.fullmatch(title_html):
|
|
|
|
return error("You can only type marseys!")
|
2022-10-05 08:35:35 +00:00
|
|
|
if len(title_html) > POST_TITLE_HTML_LENGTH_LIMIT:
|
2022-10-05 08:19:45 +00:00
|
|
|
return error("Rendered title is too big!")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-13 21:12:07 +00:00
|
|
|
sub = request.values.get("sub", "").lower().replace('/h/','').strip()
|
|
|
|
|
2022-10-06 09:09:46 +00:00
|
|
|
if sub == 'changelog' and not v.admin_level >= PERMS['POST_TO_CHANGELOG']:
|
|
|
|
# we also allow 'code contributor' badgeholders to post to the changelog hole
|
|
|
|
allowed = g.db.query(Badge.user_id).filter_by(badge_id=3).all()
|
2022-07-13 21:12:07 +00:00
|
|
|
allowed = [x[0] for x in allowed]
|
2022-11-17 16:00:49 +00:00
|
|
|
if v.id not in allowed: return error("You don't have sufficient permissions to post in /h/changelog")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
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-08-21 15:24:16 +00:00
|
|
|
return error(f"You need to be a member of House {sub.capitalize()} to post in /h/{sub}")
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if sub and sub != 'none':
|
|
|
|
sname = sub.strip().lower()
|
|
|
|
sub = g.db.query(Sub.name).filter_by(name=sname).one_or_none()
|
|
|
|
if not sub: return error(f"/h/{sname} not found!")
|
|
|
|
sub = sub[0]
|
|
|
|
if v.exiled_from(sub): return error(f"You're exiled from /h/{sub}")
|
|
|
|
else: sub = None
|
|
|
|
|
2022-06-26 01:01:21 +00:00
|
|
|
if not sub and HOLE_REQUIRED:
|
|
|
|
return error(f"You must choose a {HOLE_NAME} for your post!")
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if v.is_suspended: return error("You can't perform this action while banned.")
|
|
|
|
|
|
|
|
if v.longpost and (len(body) < 280 or ' [](' in body or body.startswith('[](')):
|
|
|
|
return error("You have to type more than 280 characters!")
|
|
|
|
elif v.bird and len(body) > 140:
|
|
|
|
return error("You have to type less than 140 characters!")
|
|
|
|
|
|
|
|
|
|
|
|
embed = None
|
|
|
|
|
|
|
|
if url:
|
2022-05-25 08:43:16 +00:00
|
|
|
url = normalize_url(url)
|
2022-05-04 23:09:46 +00:00
|
|
|
parsed_url = urlparse(url)
|
|
|
|
|
|
|
|
domain = parsed_url.netloc
|
2022-07-01 18:29:12 +00:00
|
|
|
if domain in ('old.reddit.com','twitter.com','instagram.com','tiktok.com') and '/search' not in url:
|
|
|
|
new_url = ParseResult(scheme="https",
|
|
|
|
netloc=parsed_url.netloc,
|
|
|
|
path=parsed_url.path,
|
|
|
|
params=parsed_url.params,
|
|
|
|
query=None,
|
|
|
|
fragment=parsed_url.fragment)
|
|
|
|
else:
|
2022-09-01 20:46:57 +00:00
|
|
|
qd = parse_qs(parsed_url.query, keep_blank_values=True)
|
2022-07-01 18:29:12 +00:00
|
|
|
filtered = {k: val for k, val in qd.items() if not k.startswith('utm_') and not k.startswith('ref_')}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
url = urlunparse(new_url)
|
|
|
|
|
2022-09-05 01:44:38 +00:00
|
|
|
url = url.rstrip('/')
|
2022-07-01 18:29:12 +00:00
|
|
|
|
|
|
|
search_url = url.replace('%', '').replace('\\', '').replace('_', '\_').strip()
|
|
|
|
repost = g.db.query(Submission).filter(
|
|
|
|
Submission.url.ilike(search_url),
|
|
|
|
Submission.deleted_utc == 0,
|
|
|
|
Submission.is_banned == False
|
|
|
|
).first()
|
2022-10-20 22:51:29 +00:00
|
|
|
if repost and FEATURES['REPOST_DETECTION'] and not v.admin_level >= PERMS['POST_BYPASS_REPOST_CHECKING']:
|
2022-08-04 04:07:17 +00:00
|
|
|
return redirect(repost.permalink)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-28 18:12:37 +00:00
|
|
|
y = tldextract.extract(url).registered_domain + parsed_url.path
|
2022-10-31 14:33:11 +00:00
|
|
|
y = y.lower()
|
2022-10-27 22:37:24 +00:00
|
|
|
banned_domains = g.db.query(BannedDomain).all()
|
|
|
|
for x in banned_domains:
|
|
|
|
if y.startswith(x.domain):
|
|
|
|
return error(f'Remove the banned link "{x.domain}" and try again!<br>Reason for link ban: "{x.reason}"')
|
|
|
|
|
2022-10-27 22:42:32 +00:00
|
|
|
if "twitter.com" == domain:
|
2022-06-11 09:53:53 +00:00
|
|
|
try:
|
2022-09-26 04:01:25 +00:00
|
|
|
embed = requests.get("https://publish.twitter.com/oembed", params={"url":url, "omit_script":"t"}, timeout=5).json()["html"]
|
2022-10-29 21:46:30 +00:00
|
|
|
embed = embed.replace('<a href', '<a rel="nofollow noopener" href')
|
2022-05-04 23:09:46 +00:00
|
|
|
except: pass
|
|
|
|
elif url.startswith('https://youtube.com/watch?v='):
|
|
|
|
url = unquote(url).replace('?t', '&t')
|
|
|
|
yt_id = url.split('https://youtube.com/watch?v=')[1].split('&')[0].split('%')[0]
|
|
|
|
|
|
|
|
if yt_id_regex.fullmatch(yt_id):
|
2022-11-19 12:34:03 +00:00
|
|
|
params = parse_qs(urlparse(url).query, keep_blank_values=True)
|
|
|
|
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'
|
|
|
|
if t:
|
|
|
|
try: embed += f'&start={int(t)}'
|
|
|
|
except: pass
|
|
|
|
embed += '"></lite-youtube>'
|
|
|
|
|
2022-07-11 11:55:15 +00:00
|
|
|
elif SITE in domain and "/post/" in url and "context" not in url and url.count('/') < 6:
|
2022-05-04 23:09:46 +00:00
|
|
|
id = url.split("/post/")[1]
|
|
|
|
if "/" in id: id = id.split("/")[0]
|
|
|
|
embed = str(int(id))
|
|
|
|
|
|
|
|
|
2022-10-05 08:16:56 +00:00
|
|
|
if not url and not body and not request.files.get("file") and not request.files.get("file-url"):
|
2022-05-04 23:09:46 +00:00
|
|
|
return error("Please enter a url or some text.")
|
|
|
|
|
2022-11-20 22:16:49 +00:00
|
|
|
if not IS_LOCALHOST:
|
2022-11-06 23:48:37 +00:00
|
|
|
dup = g.db.query(Submission).filter(
|
|
|
|
Submission.author_id == v.id,
|
|
|
|
Submission.deleted_utc == 0,
|
|
|
|
Submission.title == title,
|
|
|
|
Submission.url == url,
|
|
|
|
Submission.body == body
|
|
|
|
).one_or_none()
|
|
|
|
if dup: return redirect(dup.permalink)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-12 05:11:20 +00:00
|
|
|
if not execute_antispam_submission_check(title, v, url):
|
2022-05-04 23:09:46 +00:00
|
|
|
return redirect("/notifications")
|
|
|
|
|
|
|
|
if len(url) > 2048:
|
|
|
|
return error("There's a 2048 character limit for URLs.")
|
|
|
|
|
2022-10-15 11:02:02 +00:00
|
|
|
bets = []
|
2022-10-06 08:40:21 +00:00
|
|
|
if v and v.admin_level >= PERMS['POST_BETS']:
|
2022-08-26 21:53:17 +00:00
|
|
|
for i in bet_regex.finditer(body):
|
|
|
|
bets.append(i.group(1))
|
|
|
|
body = body.replace(i.group(0), "")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
options = []
|
2022-11-25 21:33:38 +00:00
|
|
|
for i in list(poll_regex.finditer(body))[:10]:
|
2022-05-04 23:09:46 +00:00
|
|
|
options.append(i.group(1))
|
|
|
|
body = body.replace(i.group(0), "")
|
|
|
|
|
|
|
|
choices = []
|
2022-11-25 21:33:38 +00:00
|
|
|
for i in list(choice_regex.finditer(body))[:10]:
|
2022-05-04 23:09:46 +00:00
|
|
|
choices.append(i.group(1))
|
|
|
|
body = body.replace(i.group(0), "")
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
body += process_files(request.files, v)
|
2022-10-05 09:14:12 +00:00
|
|
|
body = body.strip()[:POST_BODY_LENGTH_LIMIT] # process_files() adds content to the body, so we need to re-strip
|
2022-05-22 10:26:59 +00:00
|
|
|
|
2022-09-05 20:05:04 +00:00
|
|
|
torture = (v.agendaposter and not v.marseyawarded and sub != 'chudrama')
|
|
|
|
|
2022-09-16 16:30:34 +00:00
|
|
|
body_html = sanitize(body, count_marseys=True, limit_pings=100, showmore=False, torture=torture)
|
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!")
|
|
|
|
|
2022-10-05 09:14:12 +00:00
|
|
|
if len(body_html) > POST_BODY_HTML_LENGTH_LIMIT: return error(f"Submission body_html too long! (max {POST_BODY_HTML_LENGTH_LIMIT} characters)")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-11 05:21:18 +00:00
|
|
|
flag_notify = (request.values.get("notify", "on") == "on")
|
|
|
|
flag_new = request.values.get("new", False, bool)
|
|
|
|
flag_over_18 = request.values.get("over_18", False, bool)
|
|
|
|
flag_private = request.values.get("private", False, bool)
|
|
|
|
flag_club = (request.values.get("club", False, bool) and FEATURES['COUNTRY_CLUB'])
|
2022-11-14 17:17:29 +00:00
|
|
|
flag_ghost = request.values.get("ghost", False, bool) and v.can_post_in_ghost_threads
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-11 05:21:18 +00:00
|
|
|
if embed and len(embed) > 1500: embed = None
|
2022-07-09 07:41:05 +00:00
|
|
|
if embed: embed = embed.strip()
|
|
|
|
|
2022-10-18 11:09:53 +00:00
|
|
|
if url and url.startswith(SITE_FULL):
|
|
|
|
url = url.split(SITE_FULL)[1]
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
post = Submission(
|
2022-11-11 05:21:18 +00:00
|
|
|
private=flag_private,
|
|
|
|
notify=flag_notify,
|
|
|
|
club=flag_club,
|
2022-05-04 23:09:46 +00:00
|
|
|
author_id=v.id,
|
2022-11-11 05:21:18 +00:00
|
|
|
over_18=flag_over_18,
|
|
|
|
new=flag_new,
|
2022-05-04 23:09:46 +00:00
|
|
|
app_id=v.client.application.id if v.client else None,
|
2022-10-15 14:11:14 +00:00
|
|
|
is_bot=(v.client is not None),
|
2022-05-04 23:09:46 +00:00
|
|
|
url=url,
|
2022-10-05 08:16:56 +00:00
|
|
|
body=body,
|
2022-05-04 23:09:46 +00:00
|
|
|
body_html=body_html,
|
|
|
|
embed_url=embed,
|
2022-10-05 08:04:32 +00:00
|
|
|
title=title,
|
2022-05-04 23:09:46 +00:00
|
|
|
title_html=title_html,
|
|
|
|
sub=sub,
|
2022-11-11 05:21:18 +00:00
|
|
|
ghost=flag_ghost
|
2022-05-04 23:09:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
g.db.add(post)
|
|
|
|
g.db.flush()
|
|
|
|
|
2022-11-26 04:52:47 +00:00
|
|
|
for text in {post.body, post.title, post.url}:
|
2022-10-21 11:30:32 +00:00
|
|
|
if not execute_blackjack(v, post, text, 'submission'): break
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-12 10:30:02 +00:00
|
|
|
if v and v.admin_level >= PERMS['POST_BETS']:
|
|
|
|
for bet in bets:
|
|
|
|
body_html = filter_emojis_only(bet)
|
|
|
|
if len(body_html) > 500: abort(400, "Bet option too long!")
|
|
|
|
bet = SubmissionOption(
|
|
|
|
submission_id=post.id,
|
|
|
|
body_html=body_html,
|
|
|
|
exclusive=2
|
|
|
|
)
|
|
|
|
g.db.add(bet)
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
for option in options:
|
2022-11-12 10:27:01 +00:00
|
|
|
body_html = filter_emojis_only(option)
|
|
|
|
if len(body_html) > 500: abort(400, "Poll option too long!")
|
2022-07-02 06:48:04 +00:00
|
|
|
option = SubmissionOption(
|
|
|
|
submission_id=post.id,
|
2022-11-12 10:27:01 +00:00
|
|
|
body_html=body_html,
|
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-11-12 10:27:01 +00:00
|
|
|
body_html = filter_emojis_only(choice)
|
|
|
|
if len(body_html) > 500: abort(400, "Poll option too long!")
|
2022-07-02 06:48:04 +00:00
|
|
|
choice = SubmissionOption(
|
|
|
|
submission_id=post.id,
|
2022-11-12 10:27:01 +00:00
|
|
|
body_html=body_html,
|
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
|
|
|
|
|
|
|
vote = Vote(user_id=v.id,
|
|
|
|
vote_type=1,
|
|
|
|
submission_id=post.id
|
|
|
|
)
|
|
|
|
g.db.add(vote)
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
if request.files.get('file-url') and not g.is_tor:
|
2022-06-18 15:53:34 +00:00
|
|
|
file = request.files['file-url']
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if file.content_type.startswith('image/'):
|
|
|
|
name = f'/images/{time.time()}'.replace('.','') + '.webp'
|
|
|
|
file.save(name)
|
2022-11-15 09:19:08 +00:00
|
|
|
post.url = process_image(name, v)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
name2 = name.replace('.webp', 'r.webp')
|
|
|
|
copyfile(name, name2)
|
2022-11-15 09:19:08 +00:00
|
|
|
post.thumburl = process_image(name2, v, resize=100)
|
2022-05-04 23:09:46 +00:00
|
|
|
elif file.content_type.startswith('video/'):
|
2022-11-15 09:19:08 +00:00
|
|
|
post.url = process_video(file, v)
|
2022-05-22 22:15:29 +00:00
|
|
|
elif file.content_type.startswith('audio/'):
|
2022-11-15 09:19:08 +00:00
|
|
|
post.url = process_audio(file, v)
|
2022-05-04 23:09:46 +00:00
|
|
|
else:
|
2022-06-19 16:56:45 +00:00
|
|
|
abort(415)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if not post.thumburl and post.url:
|
2022-11-15 10:57:49 +00:00
|
|
|
gevent.spawn(thumbnail_thread, post.id, v.id)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if not post.private and not post.ghost:
|
|
|
|
notify_users = NOTIFY_USERS(f'{title} {body}', v)
|
|
|
|
|
|
|
|
if notify_users:
|
|
|
|
cid = notif_comment2(post)
|
|
|
|
for x in notify_users:
|
|
|
|
add_notif(cid, x)
|
|
|
|
|
2022-08-19 22:20:25 +00:00
|
|
|
if v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in f'{post.body}{post.title}'.lower() and sub != 'chudrama':
|
2022-05-04 23:09:46 +00:00
|
|
|
post.is_banned = True
|
|
|
|
post.ban_reason = "AutoJanny"
|
|
|
|
|
|
|
|
body = AGENDAPOSTER_MSG.format(username=v.username, type='post', AGENDAPOSTER_PHRASE=AGENDAPOSTER_PHRASE)
|
|
|
|
|
2022-06-18 15:37:01 +00:00
|
|
|
body_jannied_html = AGENDAPOSTER_MSG_HTML.format(id=v.id, username=v.username, type='post', AGENDAPOSTER_PHRASE=AGENDAPOSTER_PHRASE)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
2022-07-08 19:03:04 +00:00
|
|
|
c_jannied = Comment(author_id=AUTOJANNY_ID,
|
2022-05-04 23:09:46 +00:00
|
|
|
parent_submission=post.id,
|
|
|
|
level=1,
|
|
|
|
over_18=False,
|
|
|
|
is_bot=True,
|
|
|
|
app_id=None,
|
2022-05-26 23:08:23 +00:00
|
|
|
stickied='AutoJanny',
|
2022-05-04 23:09:46 +00:00
|
|
|
distinguish_level=6,
|
2022-06-18 15:37:01 +00:00
|
|
|
body=body,
|
2022-05-04 23:09:46 +00:00
|
|
|
body_html=body_jannied_html,
|
2022-05-30 03:26:52 +00:00
|
|
|
ghost=post.ghost
|
2022-05-04 23:09:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
g.db.add(c_jannied)
|
|
|
|
g.db.flush()
|
|
|
|
|
|
|
|
c_jannied.top_comment_id = c_jannied.id
|
|
|
|
|
|
|
|
n = Notification(comment_id=c_jannied.id, user_id=v.id)
|
|
|
|
g.db.add(n)
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-07-08 11:44:17 +00:00
|
|
|
if not post.private and not (post.sub and g.db.query(Exile.user_id).filter_by(user_id=SNAPPY_ID, sub=post.sub).one_or_none()):
|
|
|
|
execute_snappy(post, v)
|
2022-05-10 07:20:49 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-07-08 11:07:27 +00:00
|
|
|
v.post_count = g.db.query(Submission).filter_by(author_id=v.id, deleted_utc=0).count()
|
2022-05-04 23:09:46 +00:00
|
|
|
g.db.add(v)
|
|
|
|
|
2022-10-23 17:01:00 +00:00
|
|
|
execute_lawlz_actions(v, post)
|
2022-08-21 17:14:03 +00:00
|
|
|
|
2022-11-28 02:07:06 +00:00
|
|
|
if SITE == 'rdrama.net' and v.id in (IMPASSIONATA_ID, PIZZASHILL_ID, 2008):
|
2022-11-29 01:47:54 +00:00
|
|
|
post.stickied_utc = int(time.time()) + 3600
|
|
|
|
post.stickied = AUTOJANNY_ID
|
2022-11-28 02:07:06 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
cache.delete_memoized(frontlist)
|
2022-11-15 09:19:08 +00:00
|
|
|
cache.delete_memoized(userpagelisting)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-06 02:45:05 +00:00
|
|
|
if post.sub == 'changelog' and not post.private:
|
2022-08-30 02:03:48 +00:00
|
|
|
send_changelog_message(post.permalink)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-17 19:13:33 +00:00
|
|
|
if not post.private and SITE == 'watchpeopledie.tv':
|
2022-07-10 14:25:42 +00:00
|
|
|
send_wpd_message(post.permalink)
|
|
|
|
|
2022-09-22 20:20:29 +00:00
|
|
|
g.db.commit()
|
2022-11-15 09:19:08 +00:00
|
|
|
if v.client: return post.json(g.db)
|
2022-05-04 23:09:46 +00:00
|
|
|
else:
|
|
|
|
post.voted = 1
|
2022-08-30 18:22:15 +00:00
|
|
|
if post.new or 'megathread' in post.title.lower(): sort = 'new'
|
2022-05-04 23:09:46 +00:00
|
|
|
else: sort = v.defaultsortingcomments
|
|
|
|
return render_template('submission.html', v=v, p=post, sort=sort, render_replies=True, offset=0, success=True, sub=post.subr)
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/delete_post/<pid>")
|
2022-11-13 06:43:35 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT_SLOWER)
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-11-13 10:18:52 +00:00
|
|
|
@ratelimit_user()
|
2022-05-04 23:09:46 +00:00
|
|
|
def delete_post_pid(pid, v):
|
|
|
|
post = get_post(pid)
|
2022-07-08 11:07:27 +00:00
|
|
|
if post.author_id != v.id: abort(403)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-11 03:47:39 +00:00
|
|
|
# Temporary special logic by Carp request for events of 2022-10-10
|
|
|
|
if SITE_NAME == 'rDrama' and post.author_id == 3161: abort(403)
|
|
|
|
|
2022-07-08 11:07:27 +00:00
|
|
|
if not post.deleted_utc:
|
|
|
|
post.deleted_utc = int(time.time())
|
|
|
|
post.is_pinned = False
|
|
|
|
post.stickied = None
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-08 11:07:27 +00:00
|
|
|
g.db.add(post)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-08 11:07:27 +00:00
|
|
|
cache.delete_memoized(frontlist)
|
2022-11-15 09:19:08 +00:00
|
|
|
cache.delete_memoized(userpagelisting)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-08 11:07:27 +00:00
|
|
|
g.db.flush()
|
|
|
|
v.post_count = g.db.query(Submission).filter_by(author_id=v.id, deleted_utc=0).count()
|
|
|
|
g.db.add(v)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
return {"message": "Post deleted!"}
|
|
|
|
|
|
|
|
@app.post("/undelete_post/<pid>")
|
2022-11-13 06:43:35 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT_SLOWER)
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-11-13 10:18:52 +00:00
|
|
|
@ratelimit_user()
|
2022-05-04 23:09:46 +00:00
|
|
|
def undelete_post_pid(pid, v):
|
|
|
|
post = get_post(pid)
|
|
|
|
if post.author_id != v.id: abort(403)
|
|
|
|
|
2022-07-08 11:07:27 +00:00
|
|
|
if post.deleted_utc:
|
|
|
|
post.deleted_utc = 0
|
|
|
|
g.db.add(post)
|
|
|
|
|
|
|
|
cache.delete_memoized(frontlist)
|
2022-11-15 09:19:08 +00:00
|
|
|
cache.delete_memoized(userpagelisting)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-08 11:07:27 +00:00
|
|
|
g.db.flush()
|
|
|
|
v.post_count = g.db.query(Submission).filter_by(author_id=v.id, deleted_utc=0).count()
|
|
|
|
g.db.add(v)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
return {"message": "Post undeleted!"}
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/toggle_post_nsfw/<pid>")
|
|
|
|
@auth_required
|
|
|
|
def toggle_post_nsfw(pid, v):
|
|
|
|
post = get_post(pid)
|
|
|
|
|
2022-10-06 00:57:08 +00:00
|
|
|
if post.author_id != v.id and not v.admin_level >= PERMS['POST_COMMENT_MODERATION'] and not (post.sub and v.mods(post.sub)):
|
2022-05-04 23:09:46 +00:00
|
|
|
abort(403)
|
2022-10-04 21:09:25 +00:00
|
|
|
|
|
|
|
if post.over_18 and v.is_suspended_permanently:
|
|
|
|
abort(403)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
post.over_18 = not post.over_18
|
|
|
|
g.db.add(post)
|
|
|
|
|
2022-05-30 12:00:16 +00:00
|
|
|
if post.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" if post.over_18 else "unset_nsfw",
|
|
|
|
user_id = v.id,
|
|
|
|
target_submission_id = post.id,
|
2022-05-30 12:00:16 +00:00
|
|
|
)
|
2022-10-05 23:24:54 +00:00
|
|
|
g.db.add(ma)
|
|
|
|
else:
|
|
|
|
ma = SubAction(
|
|
|
|
sub = post.sub,
|
|
|
|
kind = "set_nsfw" if post.over_18 else "unset_nsfw",
|
|
|
|
user_id = v.id,
|
|
|
|
target_submission_id = post.id,
|
|
|
|
)
|
|
|
|
g.db.add(ma)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
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-11-13 06:43:35 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT_SLOWER)
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-11-13 10:18:52 +00:00
|
|
|
@ratelimit_user()
|
2022-05-04 23:09:46 +00:00
|
|
|
def save_post(pid, v):
|
|
|
|
|
|
|
|
post=get_post(pid)
|
|
|
|
|
|
|
|
save = g.db.query(SaveRelationship).filter_by(user_id=v.id, submission_id=post.id).one_or_none()
|
|
|
|
|
|
|
|
if not save:
|
|
|
|
new_save=SaveRelationship(user_id=v.id, submission_id=post.id)
|
|
|
|
g.db.add(new_save)
|
|
|
|
|
|
|
|
return {"message": "Post saved!"}
|
|
|
|
|
|
|
|
@app.post("/unsave_post/<pid>")
|
2022-11-13 06:43:35 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT_SLOWER)
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-11-13 10:18:52 +00:00
|
|
|
@ratelimit_user()
|
2022-05-04 23:09:46 +00:00
|
|
|
def unsave_post(pid, v):
|
|
|
|
|
|
|
|
post=get_post(pid)
|
|
|
|
|
|
|
|
save = g.db.query(SaveRelationship).filter_by(user_id=v.id, submission_id=post.id).one_or_none()
|
|
|
|
|
|
|
|
if save:
|
|
|
|
g.db.delete(save)
|
|
|
|
|
|
|
|
return {"message": "Post unsaved!"}
|
|
|
|
|
|
|
|
@app.post("/pin/<post_id>")
|
|
|
|
@auth_required
|
2022-08-11 04:05:23 +00:00
|
|
|
def pin_post(post_id, v):
|
2022-06-24 13:19:53 +00:00
|
|
|
post = get_post(post_id)
|
2022-05-04 23:09:46 +00:00
|
|
|
if post:
|
2022-11-03 07:15:49 +00:00
|
|
|
if v.id != post.author_id: abort(403, "Only the post author can do that!")
|
2022-05-04 23:09:46 +00:00
|
|
|
post.is_pinned = not post.is_pinned
|
|
|
|
g.db.add(post)
|
2022-11-15 09:19:08 +00:00
|
|
|
cache.delete_memoized(userpagelisting)
|
2022-05-04 23:09:46 +00:00
|
|
|
if post.is_pinned: return {"message": "Post pinned!"}
|
|
|
|
else: return {"message": "Post unpinned!"}
|
2022-10-11 13:01:39 +00:00
|
|
|
return abort(404, "Post not found!")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
2022-10-30 14:55:43 +00:00
|
|
|
extensions = IMAGE_FORMATS + VIDEO_FORMATS + AUDIO_FORMATS
|
2022-08-30 01:51:09 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
@app.get("/submit/title")
|
2022-09-30 12:13:06 +00:00
|
|
|
@limiter.limit("3/minute")
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-11-25 23:37:04 +00:00
|
|
|
@ratelimit_user("3/minute")
|
2022-05-04 23:09:46 +00:00
|
|
|
def get_post_title(v):
|
2022-11-25 13:10:05 +00:00
|
|
|
POST_TITLE_TIMEOUT = 5
|
2022-05-04 23:09:46 +00:00
|
|
|
url = request.values.get("url")
|
2022-08-30 01:51:09 +00:00
|
|
|
if not url or '\\' in url: abort(400)
|
2022-11-12 05:40:17 +00:00
|
|
|
url = url.strip()
|
|
|
|
if not url.startswith('http'): abort(400)
|
2022-08-30 01:51:09 +00:00
|
|
|
|
2022-10-30 14:55:43 +00:00
|
|
|
checking_url = url.lower().split('?')[0].split('%3F')[0]
|
|
|
|
if any((checking_url.endswith(f'.{x}') for x in extensions)):
|
2022-08-26 22:03:15 +00:00
|
|
|
abort(400)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-25 13:10:05 +00:00
|
|
|
try:
|
|
|
|
x = gevent.with_timeout(POST_TITLE_TIMEOUT, requests.get, url, headers=titleheaders, timeout=POST_TITLE_TIMEOUT, proxies=proxies)
|
2022-05-04 23:09:46 +00:00
|
|
|
except: abort(400)
|
2022-09-30 12:13:06 +00:00
|
|
|
|
|
|
|
content_type = x.headers.get("Content-Type")
|
|
|
|
if not content_type or "text/html" not in content_type: abort(400)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-11 09:24:54 +00:00
|
|
|
# no you can't just parse html with reeeeeeeegex
|
2022-11-11 10:17:59 +00:00
|
|
|
match = html_title_regex.search(x.text)
|
2022-11-11 09:24:54 +00:00
|
|
|
if match and match.lastindex >= 1:
|
|
|
|
title = match.group(1)
|
2022-11-11 09:49:43 +00:00
|
|
|
else: abort(400)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-26 06:11:00 +00:00
|
|
|
title = html.unescape(title)
|
|
|
|
|
2022-11-11 10:17:59 +00:00
|
|
|
return {"url": url, "title": title}
|