rDrama/files/routes/subs.py

459 lines
11 KiB
Python
Raw Normal View History

2022-02-10 20:35:16 +00:00
from files.__main__ import app, limiter, mail
from files.helpers.alerts import *
from files.helpers.wrappers import *
from files.classes import *
from .front import frontlist
2022-02-14 01:22:26 +00:00
2022-02-16 04:33:13 +00:00
@app.post("/exile/post/<pid>")
@is_not_permabanned
def exile_post(v, pid):
try: pid = int(pid)
except: abort(400)
p = get_post(pid)
sub = p.sub
if not sub: abort(400)
if not v.mods(sub): abort(403)
u = p.author
if u.admin_level < 2 and not u.exiled_from(sub):
2022-02-22 12:39:02 +00:00
exile = Exile(user_id=u.id, sub=sub, exiler_id=v.id)
2022-02-16 04:33:13 +00:00
g.db.add(exile)
2022-02-24 13:20:48 +00:00
send_notification(u.id, f"@{v.username} has exiled you from /s/{sub} for [{p.title}]({p.shortlink})")
2022-02-16 04:33:13 +00:00
g.db.commit()
return {"message": "User exiled successfully!"}
@app.post("/unexile/post/<pid>")
@is_not_permabanned
def unexile_post(v, pid):
try: pid = int(pid)
except: abort(400)
p = get_post(pid)
sub = p.sub
if not sub: abort(400)
if not v.mods(sub): abort(403)
u = p.author
if u.exiled_from(sub):
exile = g.db.query(Exile).filter_by(user_id=u.id, sub=sub).one_or_none()
g.db.delete(exile)
2022-02-22 11:43:38 +00:00
send_notification(u.id, f"@{v.username} has revoked your exile from /s/{sub}")
2022-02-16 04:33:13 +00:00
g.db.commit()
return {"message": "User unexiled successfully!"}
@app.post("/exile/comment/<cid>")
@is_not_permabanned
def exile_comment(v, cid):
try: cid = int(cid)
except: abort(400)
c = get_comment(cid)
sub = c.post.sub
if not sub: abort(400)
if not v.mods(sub): abort(403)
u = c.author
if u.admin_level < 2 and not u.exiled_from(sub):
2022-02-22 12:39:02 +00:00
exile = Exile(user_id=u.id, sub=sub, exiler_id=v.id)
2022-02-16 04:33:13 +00:00
g.db.add(exile)
2022-02-24 13:20:48 +00:00
send_notification(u.id, f"@{v.username} has exiled you from /s/{sub} for [{c.permalink}]({c.shortlink})")
2022-02-16 04:33:13 +00:00
g.db.commit()
return {"message": "User exiled successfully!"}
@app.post("/unexile/comment/<cid>")
@is_not_permabanned
def unexile_comment(v, cid):
try: cid = int(cid)
except: abort(400)
c = get_comment(cid)
sub = c.post.sub
if not sub: abort(400)
if not v.mods(sub): abort(403)
u = c.author
if u.exiled_from(sub):
exile = g.db.query(Exile).filter_by(user_id=u.id, sub=sub).one_or_none()
g.db.delete(exile)
2022-02-22 11:43:38 +00:00
send_notification(u.id, f"@{v.username} has revoked your exile from /s/{sub}")
2022-02-16 04:33:13 +00:00
g.db.commit()
return {"message": "User unexiled successfully!"}
2022-02-14 01:22:26 +00:00
@app.post("/s/<sub>/block")
@auth_required
def block_sub(v, sub):
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
if not sub: abort(404)
sub = sub.name
# if v.mods(sub): return {"error": "You can't block subs you mod!"}
existing = g.db.query(SubBlock).filter_by(user_id=v.id, sub=sub).one_or_none()
if not existing:
block = SubBlock(user_id=v.id, sub=sub)
g.db.add(block)
g.db.commit()
cache.delete_memoized(frontlist)
return {"message": "Sub blocked successfully!"}
@app.post("/s/<sub>/unblock")
@auth_required
def unblock_sub(v, sub):
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
if not sub: abort(404)
sub = sub.name
block = g.db.query(SubBlock).filter_by(user_id=v.id, sub=sub).one_or_none()
if block:
g.db.delete(block)
g.db.commit()
cache.delete_memoized(frontlist)
return {"message": "Sub unblocked successfully!"}
2022-02-10 20:35:16 +00:00
@app.get("/s/<sub>/mods")
2022-02-21 06:45:01 +00:00
@auth_required
2022-02-10 20:35:16 +00:00
def mods(v, sub):
2022-02-11 23:32:14 +00:00
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
2022-02-10 20:35:16 +00:00
if not sub: abort(404)
2022-02-11 23:32:14 +00:00
users = g.db.query(User, Mod).join(Mod, Mod.user_id==User.id).filter_by(sub=sub.name).order_by(Mod.created_utc).all()
2022-02-10 20:35:16 +00:00
return render_template("sub/mods.html", v=v, sub=sub, users=users)
2022-02-21 06:45:01 +00:00
@app.get("/s/<sub>/exilees")
@auth_required
def exilees(v, sub):
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
if not sub: abort(404)
2022-02-22 13:43:17 +00:00
users = g.db.query(User, Exile).join(Exile, Exile.user_id==User.id).filter_by(sub=sub.name).all()
2022-02-21 06:45:01 +00:00
return render_template("sub/exilees.html", v=v, sub=sub, users=users)
@app.get("/s/<sub>/blockers")
@auth_required
def blockers(v, sub):
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
if not sub: abort(404)
users = g.db.query(User).join(SubBlock, SubBlock.user_id==User.id).filter_by(sub=sub.name).all()
return render_template("sub/blockers.html", v=v, sub=sub, users=users)
2022-02-10 20:35:16 +00:00
@app.post("/s/<sub>/add_mod")
2022-02-12 18:21:20 +00:00
@limiter.limit("1/second;5/day")
2022-02-11 15:02:27 +00:00
@is_not_permabanned
2022-02-10 20:35:16 +00:00
def add_mod(v, sub):
2022-02-11 23:32:14 +00:00
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
2022-02-10 20:35:16 +00:00
if not sub: abort(404)
sub = sub.name
if not v.mods(sub): abort(403)
user = request.values.get('user')
if not user: abort(400)
user = get_user(user)
2022-02-11 23:32:14 +00:00
existing = g.db.query(Mod).filter_by(user_id=user.id, sub=sub).one_or_none()
if not existing:
2022-02-14 21:07:31 +00:00
mod = Mod(user_id=user.id, sub=sub)
2022-02-11 23:32:14 +00:00
g.db.add(mod)
2022-02-22 11:43:38 +00:00
send_repeatable_notification(user.id, f"@{v.username} has added you as a mod to /s/{sub}")
2022-02-11 23:32:14 +00:00
g.db.commit()
return redirect(f'/s/{sub}/mods')
@app.post("/s/<sub>/remove_mod")
@is_not_permabanned
def remove_mod(v, sub):
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
if not sub: abort(404)
sub = sub.name
if not v.mods(sub): abort(403)
uid = request.values.get('uid')
if not uid: abort(400)
try: uid = int(uid)
except: abort(400)
2022-02-12 19:33:34 +00:00
user = g.db.query(User).filter_by(id=uid).one_or_none()
if not user: abort(404)
mod = g.db.query(Mod).filter_by(user_id=user.id, sub=sub).one_or_none()
2022-02-11 23:32:14 +00:00
if not mod: abort(400)
2022-02-25 12:46:33 +00:00
if not (v.id == user.id or v.mod_date(sub) and v.mod_date(sub) < mod.created_utc): abort(403)
2022-02-12 19:33:34 +00:00
2022-02-11 23:32:14 +00:00
g.db.delete(mod)
2022-02-10 20:35:16 +00:00
2022-02-22 11:43:38 +00:00
send_repeatable_notification(user.id, f"@{v.username} has removed you as a mod from /s/{sub}")
2022-02-10 20:35:16 +00:00
g.db.commit()
return redirect(f'/s/{sub}/mods')
2022-03-02 04:24:36 +00:00
@app.get("/create_sub")
@is_not_permabanned
def create_sub(v):
if SITE_NAME == 'Drama' and v.id not in (AEVANN_ID, CARP_ID): abort(403)
if v.id == MENTION_ID: cost = 0
else:
num = v.subs_created + 1
for a in v.alts:
num += a.subs_created
cost = num * 100
return render_template("sub/create_sub.html", v=v, cost=cost)
2022-03-01 21:19:51 +00:00
2022-03-02 04:24:36 +00:00
@app.post("/create_sub")
@is_not_permabanned
def create_sub2(v):
if SITE_NAME == 'Drama' and v.id not in (AEVANN_ID, CARP_ID): abort(403)
2022-03-01 21:19:51 +00:00
2022-03-02 04:24:36 +00:00
name = request.values.get('name')
if not name: abort(400)
name = name.strip().lower()
2022-03-01 21:19:51 +00:00
2022-03-02 04:24:36 +00:00
if not valid_sub_regex.fullmatch(name):
return render_template("sub/create_sub.html", v=v, error="Sub name not allowed."), 400
2022-03-01 21:19:51 +00:00
2022-03-02 04:24:36 +00:00
sub = g.db.query(Sub).filter_by(name=name).one_or_none()
if not sub:
if v.id != MENTION_ID:
num = v.subs_created + 1
for a in v.alts:
num += a.subs_created
cost = num * 100
2022-03-01 21:19:51 +00:00
2022-03-02 04:24:36 +00:00
if v.coins < cost:
return render_template("sub/create_sub.html", v=v, error="You don't have enough coins!"), 403
2022-03-01 21:19:51 +00:00
2022-03-02 04:24:36 +00:00
v.coins -= cost
2022-03-01 21:19:51 +00:00
2022-03-02 04:24:36 +00:00
v.subs_created += 1
g.db.add(v)
sub = Sub(name=name)
g.db.add(sub)
g.db.flush()
mod = Mod(user_id=v.id, sub=sub.name)
g.db.add(mod)
g.db.commit()
2022-03-01 21:19:51 +00:00
2022-03-02 04:24:36 +00:00
return redirect(f'/s/{sub.name}')
2022-02-10 20:35:16 +00:00
@app.post("/kick/<pid>")
2022-02-11 15:02:27 +00:00
@is_not_permabanned
2022-02-10 20:35:16 +00:00
def kick(v, pid):
try: pid = int(pid)
except: abort(400)
post = get_post(pid)
if not post.sub: abort(403)
if not v.mods(post.sub): abort(403)
post.sub = 'general'
g.db.add(post)
g.db.commit()
cache.delete_memoized(frontlist)
return {"message": "Post kicked successfully!"}
@app.get('/s/<sub>/settings')
2022-02-11 15:02:27 +00:00
@is_not_permabanned
2022-02-10 20:35:16 +00:00
def sub_settings(v, sub):
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
if not sub: abort(404)
if not v.mods(sub.name): abort(403)
return render_template('sub/settings.html', v=v, sidebar=sub.sidebar, sub=sub)
@app.post('/s/<sub>/sidebar')
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-02-11 15:02:27 +00:00
@is_not_permabanned
2022-02-10 20:35:16 +00:00
def post_sub_sidebar(v, sub):
2022-02-11 23:32:14 +00:00
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
2022-02-10 20:35:16 +00:00
if not sub: abort(404)
if not v.mods(sub.name): abort(403)
2022-02-11 23:32:14 +00:00
sub.sidebar = request.values.get('sidebar', '').strip()[:500]
2022-02-10 20:35:16 +00:00
sub.sidebar_html = sanitize(sub.sidebar)
2022-02-11 23:32:14 +00:00
if len(sub.sidebar_html) > 1000: return "Sidebar is too big!"
2022-02-10 20:35:16 +00:00
g.db.add(sub)
2022-02-11 23:32:14 +00:00
g.db.commit()
return redirect(f'/s/{sub.name}/settings')
@app.post('/s/<sub>/css')
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@is_not_permabanned
def post_sub_css(v, sub):
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
if not sub: abort(404)
if not v.mods(sub.name): abort(403)
sub.css = request.values.get('css', '').strip()
g.db.add(sub)
2022-02-10 20:35:16 +00:00
g.db.commit()
return redirect(f'/s/{sub.name}/settings')
2022-02-11 23:32:14 +00:00
@app.get("/s/<sub>/css")
def get_sub_css(sub):
sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
if not sub: abort(404)
resp=make_response(sub.css or "")
resp.headers.add("Content-Type", "text/css")
return resp
2022-02-21 06:45:01 +00:00
2022-02-10 20:35:16 +00:00
@app.post("/s/<sub>/banner")
2022-02-12 18:21:40 +00:00
@limiter.limit("1/second;10/day")
2022-02-11 15:02:27 +00:00
@is_not_permabanned
2022-02-10 20:35:16 +00:00
def sub_banner(v, sub):
if v and v.patron:
2022-02-24 08:28:13 +00:00
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
2022-02-10 20:35:16 +00:00
if request.headers.get("cf-ipcountry") == "T1": return {"error":"Image uploads are not allowed through TOR."}, 403
sub = g.db.query(Sub).filter_by(name=sub.lower().strip()).one_or_none()
if not sub: abort(404)
if not v.mods(sub.name): abort(403)
file = request.files["banner"]
name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp'
file.save(name)
bannerurl = process_image(name)
if bannerurl:
if sub.bannerurl and '/images/' in sub.bannerurl:
fpath = '/images/' + sub.bannerurl.split('/images/')[1]
if path.isfile(fpath): os.remove(fpath)
sub.bannerurl = bannerurl
g.db.add(sub)
g.db.commit()
return redirect(f'/s/{sub.name}/settings')
@app.post("/s/<sub>/sidebar_image")
2022-02-12 18:21:40 +00:00
@limiter.limit("1/second;10/day")
2022-02-11 15:02:27 +00:00
@is_not_permabanned
2022-02-10 20:35:16 +00:00
def sub_sidebar(v, sub):
if v and v.patron:
2022-02-24 08:28:13 +00:00
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
2022-02-10 20:35:16 +00:00
if request.headers.get("cf-ipcountry") == "T1": return {"error":"Image uploads are not allowed through TOR."}, 403
sub = g.db.query(Sub).filter_by(name=sub.lower().strip()).one_or_none()
if not sub: abort(404)
if not v.mods(sub.name): abort(403)
file = request.files["sidebar"]
name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp'
file.save(name)
sidebarurl = process_image(name)
if sidebarurl:
if sub.sidebarurl and '/images/' in sub.sidebarurl:
fpath = '/images/' + sub.sidebarurl.split('/images/')[1]
if path.isfile(fpath): os.remove(fpath)
sub.sidebarurl = sidebarurl
g.db.add(sub)
g.db.commit()
2022-02-24 12:03:28 +00:00
return redirect(f'/s/{sub.name}/settings')
2022-02-28 00:44:40 +00:00
@app.get("/sub_toggle/<mode>")
2022-02-27 23:28:10 +00:00
def sub_toggle(mode):
2022-02-28 19:07:05 +00:00
if mode in ('Exclude subs', 'Include subs', 'View subs only'): session["sub_toggle"] = mode
2022-03-02 00:05:30 +00:00
if request.referrer and len(request.referrer) > 1 and request.referrer.startswith(SITE_FULL):
return redirect(request.referrer)
2022-02-27 23:28:10 +00:00
return redirect('/')
2022-02-24 12:03:28 +00:00
@app.get("/subs")
@auth_desired
def subs(v):
subs = g.db.query(Submission.sub, func.count(Submission.sub)).group_by(Submission.sub).order_by(func.count(Submission.sub).desc()).all()[:-1]
return render_template('sub/subs.html', v=v, subs=subs)