rDrama/files/routes/settings.py

910 lines
29 KiB
Python
Raw Normal View History

2021-10-15 14:08:27 +00:00
from __future__ import unicode_literals
from files.helpers.alerts import *
from files.helpers.sanitize import *
from files.helpers.discord import remove_user, set_nick
from files.helpers.const import *
from files.mail import *
from files.__main__ import app, cache, limiter
import youtube_dl
from .front import frontlist
import os
2021-12-07 23:18:06 +00:00
from files.helpers.sanitize import filter_emojis_only
2021-10-15 14:08:27 +00:00
from files.helpers.discord import add_role
2022-01-24 23:40:34 +00:00
from shutil import copyfile
2021-10-15 14:08:27 +00:00
import requests
2022-05-04 03:14:14 +00:00
import tldextract
2021-10-15 14:08:27 +00:00
GUMROAD_TOKEN = environ.get("GUMROAD_TOKEN", "").strip()
GUMROAD_ID = environ.get("GUMROAD_ID", "tfcvri").strip()
2021-10-15 14:08:27 +00:00
tiers={
"(Paypig)": 1,
"(Renthog)": 2,
"(Landchad)": 3,
"(Terminally online turboautist)": 4,
2022-02-26 19:34:10 +00:00
"(Marsey's Sugar Daddy)": 5,
"(JIDF Bankroller)": 6,
"(Rich Bich)": 7,
2022-01-02 14:12:19 +00:00
"(LlamaBean)": 1,
2021-10-15 14:08:27 +00:00
}
@app.post("/settings/removebackground")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def removebackground(v):
v.background = None
g.db.add(v)
g.db.commit()
return {"message": "Background removed!"}
@app.post("/settings/profile")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_profile_post(v):
updated = False
2021-12-27 01:08:06 +00:00
if request.values.get("background", v.background) != v.background:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.background = request.values.get("background")
2021-10-15 14:08:27 +00:00
2022-04-10 18:42:58 +00:00
elif request.values.get("reddit", v.reddit) != v.reddit:
reddit = request.values.get("reddit")
2022-04-11 16:39:04 +00:00
if reddit in {'old.reddit.com', 'reddit.com', 'i.reddit.com', 'teddit.net', 'libredd.it', 'unddit.com'}:
2022-04-10 18:42:58 +00:00
updated = True
v.reddit = reddit
2021-11-07 13:12:31 +00:00
elif request.values.get("slurreplacer", v.slurreplacer) != v.slurreplacer:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.slurreplacer = request.values.get("slurreplacer") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("hidevotedon", v.hidevotedon) != v.hidevotedon:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.hidevotedon = request.values.get("hidevotedon") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("cardview", v.cardview) != v.cardview:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.cardview = request.values.get("cardview") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("highlightcomments", v.highlightcomments) != v.highlightcomments:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.highlightcomments = request.values.get("highlightcomments") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("newtab", v.newtab) != v.newtab:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.newtab = request.values.get("newtab") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("newtabexternal", v.newtabexternal) != v.newtabexternal:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.newtabexternal = request.values.get("newtabexternal") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("nitter", v.nitter) != v.nitter:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.nitter = request.values.get("nitter") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("controversial", v.controversial) != v.controversial:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.controversial = request.values.get("controversial") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("sigs_disabled", v.sigs_disabled) != v.sigs_disabled:
2021-11-04 17:24:43 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.sigs_disabled = request.values.get("sigs_disabled") == 'true'
2021-11-04 17:24:43 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("over18", v.over_18) != v.over_18:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.over_18 = request.values.get("over18") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("private", v.is_private) != v.is_private:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.is_private = request.values.get("private") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("nofollow", v.is_nofollow) != v.is_nofollow:
2021-10-15 14:08:27 +00:00
updated = True
2022-03-04 23:49:38 +00:00
v.is_nofollow = request.values.get("nofollow") == 'true'
2021-10-15 14:08:27 +00:00
2021-11-30 13:09:17 +00:00
elif request.values.get("bio") == "":
v.bio = None
v.bio_html = None
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html", v=v, msg="Your bio has been updated.")
2021-11-30 13:09:17 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("sig") == "":
2021-11-04 19:54:12 +00:00
v.sig = None
v.sig_html = None
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html", v=v, msg="Your sig has been updated.")
2021-11-04 19:54:12 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("friends") == "":
v.friends = None
v.friends_html = None
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html", v=v, msg="Your friends list has been updated.")
2021-11-07 13:12:31 +00:00
elif request.values.get("enemies") == "":
v.enemies = None
v.enemies_html = None
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html", v=v, msg="Your enemies list has been updated.")
2021-11-07 13:12:31 +00:00
2022-01-07 21:44:38 +00:00
elif (v.patron or v.id == MOOSE_ID) and request.values.get("sig"):
2021-11-04 15:20:10 +00:00
sig = request.values.get("sig")[:200]
2021-11-04 15:12:17 +00:00
2022-01-11 19:46:50 +00:00
sig_html = sanitize(sig)
2021-11-04 15:12:17 +00:00
2021-11-04 15:20:10 +00:00
if len(sig_html) > 1000:
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-11-04 15:20:10 +00:00
v=v,
error="Your sig is too long")
2021-11-04 15:12:17 +00:00
2021-11-04 15:20:10 +00:00
v.sig = sig[:200]
2021-11-04 15:12:17 +00:00
v.sig_html=sig_html
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-11-04 15:12:17 +00:00
v=v,
msg="Your sig has been updated.")
2021-11-04 16:15:40 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("friends"):
2021-11-04 16:07:13 +00:00
friends = request.values.get("friends")[:500]
2022-01-11 19:46:50 +00:00
friends_html = sanitize(friends)
2021-11-04 16:07:13 +00:00
2021-11-04 16:15:40 +00:00
if len(friends_html) > 2000:
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-11-04 16:07:13 +00:00
v=v,
2021-11-06 15:21:05 +00:00
error="Your friends list is too long")
2021-11-04 16:07:13 +00:00
2021-11-04 16:44:34 +00:00
2022-02-27 21:57:44 +00:00
notify_users = NOTIFY_USERS(friends, v)
2021-12-20 20:03:59 +00:00
2022-03-01 00:06:50 +00:00
if notify_users:
cid = notif_comment(f"@{v.username} has added you to their friends list!")
for x in notify_users:
add_notif(cid, x)
2021-11-04 16:44:34 +00:00
2021-11-04 16:07:13 +00:00
v.friends = friends[:500]
v.friends_html=friends_html
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-11-04 16:07:13 +00:00
v=v,
2021-11-06 15:21:05 +00:00
msg="Your friends list has been updated.")
2021-11-04 16:07:13 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("enemies"):
2021-11-06 15:21:05 +00:00
enemies = request.values.get("enemies")[:500]
2022-01-11 19:46:50 +00:00
enemies_html = sanitize(enemies)
2021-11-06 15:21:05 +00:00
if len(enemies_html) > 2000:
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-11-06 15:21:05 +00:00
v=v,
error="Your enemies list is too long")
2022-02-27 21:57:44 +00:00
notify_users = NOTIFY_USERS(enemies, v)
2021-12-20 20:03:59 +00:00
2022-03-01 00:06:50 +00:00
if notify_users:
cid = notif_comment(f"@{v.username} has added you to their enemies list!")
for x in notify_users:
add_notif(cid, x)
2021-11-06 15:21:05 +00:00
v.enemies = enemies[:500]
v.enemies_html=enemies_html
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-11-06 15:21:05 +00:00
v=v,
msg="Your enemies list has been updated.")
2021-11-04 16:15:40 +00:00
2021-11-07 13:12:31 +00:00
elif request.values.get("bio") or request.files.get('file') and request.headers.get("cf-ipcountry") != "T1":
2021-11-04 15:12:17 +00:00
bio = request.values.get("bio")[:1500]
if request.files.get('file'):
file = request.files['file']
2021-12-18 02:59:40 +00:00
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)
url = process_image(name)
bio += f"\n\n![]({url})"
2021-12-18 02:59:40 +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']
2022-05-01 21:44:38 +00:00
except:
error = req['error']
if error == 'File exceeds max duration': error += ' (60 seconds)'
return {"error": error}, 400
2022-01-06 17:57:59 +00:00
if url.endswith('.'): url += 'mp4'
2021-12-18 04:48:10 +00:00
bio += f"\n\n{url}"
2021-12-18 02:59:40 +00:00
else:
2022-01-16 06:06:16 +00:00
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "Image/Video files only"}, 400
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html", v=v, error="Image/Video files only."), 400
2021-11-04 15:12:17 +00:00
2022-01-11 19:46:50 +00:00
bio_html = sanitize(bio)
2021-11-04 15:12:17 +00:00
if len(bio_html) > 10000:
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-11-04 15:12:17 +00:00
v=v,
error="Your bio is too long")
if len(bio_html) > 10000: abort(400)
v.bio = bio[:1500]
v.bio_html=bio_html
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-11-04 15:12:17 +00:00
v=v,
msg="Your bio has been updated.")
2021-10-15 14:08:27 +00:00
frontsize = request.values.get("frontsize")
if frontsize:
2022-03-17 07:58:28 +00:00
if frontsize in {"15", "25", "50", "100"}:
2021-10-15 14:08:27 +00:00
v.frontsize = int(frontsize)
updated = True
cache.delete_memoized(frontlist)
else: abort(400)
defaultsortingcomments = request.values.get("defaultsortingcomments")
if defaultsortingcomments:
2022-02-23 05:17:03 +00:00
if defaultsortingcomments in {"new", "old", "controversial", "top", "bottom"}:
2021-10-15 14:08:27 +00:00
v.defaultsortingcomments = defaultsortingcomments
updated = True
else: abort(400)
defaultsorting = request.values.get("defaultsorting")
if defaultsorting:
2022-03-17 10:28:13 +00:00
if defaultsorting in {"hot", "bump", "new", "old", "comments", "controversial", "top", "bottom"}:
2021-10-15 14:08:27 +00:00
v.defaultsorting = defaultsorting
updated = True
else: abort(400)
defaulttime = request.values.get("defaulttime")
if defaulttime:
2022-02-23 05:17:03 +00:00
if defaulttime in {"hour", "day", "week", "month", "year", "all"}:
2021-10-15 14:08:27 +00:00
v.defaulttime = defaulttime
updated = True
else: abort(400)
theme = request.values.get("theme")
if theme:
2022-04-22 22:07:58 +00:00
if theme in {"dramblr", "reddit", "classic", "classic_dark", "transparent", "win98", "dark", "light", "coffee", "tron", "4chan", "midnight"}:
2022-03-26 18:35:44 +00:00
if theme == "transparent" and not v.background:
return {"error": "You need to set a background to use the transparent theme!"}
2022-01-02 13:22:12 +00:00
v.theme = theme
if theme == "win98": v.themecolor = "30409f"
updated = True
else: abort(400)
2021-10-15 14:08:27 +00:00
2022-02-14 23:59:20 +00:00
house = request.values.get("house")
if house and house in ("None","Furry","Femboy","Vampire","Racist"):
2022-02-24 16:23:17 +00:00
if v.house: cost = 2000
else: cost = 500
if v.coins >= cost: v.coins -= cost
elif v.procoins >= cost: v.procoins -= cost
2022-02-14 23:59:20 +00:00
else: abort(403)
if house == "None": house = None
v.house = house
2022-02-16 22:23:44 +00:00
if v.house == "Vampire":
send_repeatable_notification(DAD_ID, f"@{v.username} has joined House Vampire!")
2022-02-14 23:59:20 +00:00
updated = True
2021-10-15 14:08:27 +00:00
if updated:
g.db.add(v)
g.db.commit()
return {"message": "Your settings have been updated."}
else:
return {"error": "You didn't change anything."}, 400
2021-10-16 14:44:42 +00:00
@app.post("/settings/filters")
@auth_required
def filters(v):
filters=request.values.get("filters")[:1000].strip()
2021-12-19 13:01:28 +00:00
if filters == v.custom_filter_list:
2022-01-14 12:04:35 +00:00
return render_template("settings_filters.html", v=v, error="You didn't change anything")
2021-10-16 14:44:42 +00:00
v.custom_filter_list=filters
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_filters.html", v=v, msg="Your custom filters have been updated.")
2021-10-16 14:44:42 +00:00
2021-10-15 14:08:27 +00:00
@app.post("/changelogsub")
@auth_required
def changelogsub(v):
v.changelogsub = not v.changelogsub
g.db.add(v)
cache.delete_memoized(frontlist)
g.db.commit()
if v.changelogsub: return {"message": "You have subscribed to the changelog!"}
else: return {"message": "You have unsubscribed from the changelog!"}
@app.post("/settings/namecolor")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def namecolor(v):
2021-12-19 13:01:28 +00:00
2021-10-15 14:08:27 +00:00
color = str(request.values.get("color", "")).strip()
if color.startswith('#'): color = color[1:]
2022-01-14 12:04:35 +00:00
if len(color) != 6: return render_template("settings_security.html", v=v, error="Invalid color code")
2021-10-15 14:08:27 +00:00
v.namecolor = color
g.db.add(v)
g.db.commit()
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2021-10-15 14:08:27 +00:00
@app.post("/settings/themecolor")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def themecolor(v):
2021-12-19 13:01:28 +00:00
2021-10-15 14:08:27 +00:00
themecolor = str(request.values.get("themecolor", "")).strip()
if themecolor.startswith('#'): themecolor = themecolor[1:]
2022-01-14 12:04:35 +00:00
if len(themecolor) != 6: return render_template("settings_security.html", v=v, error="Invalid color code")
2021-10-15 14:08:27 +00:00
v.themecolor = themecolor
g.db.add(v)
g.db.commit()
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2021-10-15 14:08:27 +00:00
@app.post("/settings/gumroad")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def gumroad(v):
2022-01-06 16:46:09 +00:00
if not (v.email and v.is_activated):
return {"error": f"You must have a verified email to verify {patron} status and claim your rewards"}, 400
2021-10-15 14:08:27 +00:00
2022-01-01 22:06:53 +00:00
data = {'access_token': GUMROAD_TOKEN, 'email': v.email}
2022-02-24 17:48:14 +00:00
response = requests.get('https://api.gumroad.com/v2/sales', data=data, timeout=5).json()["sales"]
2021-10-15 14:08:27 +00:00
2022-01-01 22:06:53 +00:00
if len(response) == 0: return {"error": "Email not found"}, 404
2021-12-03 16:59:00 +00:00
2022-01-01 22:06:53 +00:00
response = response[0]
2021-10-15 14:08:27 +00:00
tier = tiers[response["variants_and_quantity"]]
if v.patron == tier: return {"error": f"{patron} rewards already claimed"}, 400
2022-04-19 19:13:36 +00:00
procoins = procoins_li[tier] - procoins_li[v.patron]
if procoins < 0: return {"error": f"{patron} rewards already claimed"}, 400
2022-04-19 19:14:09 +00:00
existing = g.db.query(User.id).filter(User.email == v.email, User.is_activated == True, User.patron >= tier).one_or_none()
if existing: return {"error": f"{patron} rewards already claimed on another account"}, 400
2021-10-15 14:08:27 +00:00
v.patron = tier
2021-10-21 22:55:48 +00:00
if v.discord_id: add_role(v, f"{tier}")
2021-10-21 22:59:19 +00:00
v.procoins += procoins
2021-12-20 20:03:59 +00:00
send_repeatable_notification(v.id, f"You have received {procoins} Marseybux! You can use them to buy awards in the [shop](/shop).")
2021-10-15 14:08:27 +00:00
2022-01-17 15:03:51 +00:00
if v.patron > 1 and v.verified == None: v.verified = "Verified"
2021-11-11 00:10:48 +00:00
g.db.add(v)
2021-10-15 14:08:27 +00:00
if not v.has_badge(20+tier):
2021-10-21 22:55:48 +00:00
new_badge = Badge(badge_id=20+tier, user_id=v.id)
2021-10-15 14:08:27 +00:00
g.db.add(new_badge)
2022-01-24 23:49:02 +00:00
g.db.flush()
2021-12-28 13:51:26 +00:00
send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}")
2021-10-15 14:08:27 +00:00
g.db.commit()
return {"message": f"{patron} rewards claimed!"}
@app.post("/settings/titlecolor")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def titlecolor(v):
2021-12-19 13:01:28 +00:00
2021-10-15 14:08:27 +00:00
titlecolor = str(request.values.get("titlecolor", "")).strip()
if titlecolor.startswith('#'): titlecolor = titlecolor[1:]
2022-01-14 12:04:35 +00:00
if len(titlecolor) != 6: return render_template("settings_profile.html", v=v, error="Invalid color code")
2021-10-15 14:08:27 +00:00
v.titlecolor = titlecolor
g.db.add(v)
g.db.commit()
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2021-10-15 14:08:27 +00:00
2021-10-27 00:37:34 +00:00
@app.post("/settings/verifiedcolor")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-27 00:37:34 +00:00
@auth_required
def verifiedcolor(v):
verifiedcolor = str(request.values.get("verifiedcolor", "")).strip()
if verifiedcolor.startswith('#'): verifiedcolor = verifiedcolor[1:]
2022-01-14 12:04:35 +00:00
if len(verifiedcolor) != 6: return render_template("settings_profile.html", v=v, error="Invalid color code")
2021-10-27 00:37:34 +00:00
v.verifiedcolor = verifiedcolor
g.db.add(v)
g.db.commit()
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2021-10-15 14:08:27 +00:00
@app.post("/settings/security")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_security_post(v):
2021-12-21 19:48:39 +00:00
if request.values.get("new_password"):
2021-12-21 19:56:38 +00:00
if request.values.get("new_password") != request.values.get("cnf_password"):
return render_template("settings_security.html", v=v, error="Passwords do not match.")
2021-10-15 14:08:27 +00:00
2022-02-26 20:13:34 +00:00
if not valid_password_regex.fullmatch(request.values.get("new_password")):
2021-12-21 19:56:38 +00:00
return render_template("settings_security.html", v=v, error="Password must be between 8 and 100 characters.")
2021-10-15 14:08:27 +00:00
if not v.verifyPass(request.values.get("old_password")):
2021-12-21 19:56:38 +00:00
return render_template("settings_security.html", v=v, error="Incorrect password")
2021-10-15 14:08:27 +00:00
v.passhash = v.hash_password(request.values.get("new_password"))
g.db.add(v)
g.db.commit()
2021-12-31 12:52:04 +00:00
return render_template("settings_security.html", v=v, msg="Your password has been changed.")
2021-10-15 14:08:27 +00:00
if request.values.get("new_email"):
if not v.verifyPass(request.values.get('password')):
2021-12-21 19:56:38 +00:00
return render_template("settings_security.html", v=v, error="Invalid password.")
2021-10-15 14:08:27 +00:00
2021-12-20 14:56:47 +00:00
new_email = request.values.get("new_email","").strip().lower()
2021-11-23 21:03:20 +00:00
2021-10-15 14:08:27 +00:00
if new_email == v.email:
2021-12-21 19:56:38 +00:00
return render_template("settings_security.html", v=v, error="That email is already yours!")
2021-10-15 14:08:27 +00:00
2022-01-28 21:42:09 +00:00
url = f"{SITE_FULL}/activate"
2021-10-15 14:08:27 +00:00
now = int(time.time())
token = generate_hash(f"{new_email}+{v.id}+{now}")
params = f"?email={quote(new_email)}&id={v.id}&time={now}&token={token}"
link = url + params
send_mail(to_address=new_email,
subject="Verify your email address.",
html=render_template("email/email_change.html",
action_url=link,
v=v)
)
2022-01-06 16:46:09 +00:00
return render_template("settings_security.html", v=v, msg="Check your email and click the verification link to complete the email change.")
2021-10-15 14:08:27 +00:00
2022-01-02 13:32:50 +00:00
if request.values.get("2fa_token"):
2021-10-15 14:08:27 +00:00
if not v.verifyPass(request.values.get('password')):
2021-12-21 19:56:38 +00:00
return render_template("settings_security.html", v=v, error="Invalid password or token.")
2021-10-15 14:08:27 +00:00
secret = request.values.get("2fa_secret")
x = pyotp.TOTP(secret)
if not x.verify(request.values.get("2fa_token"), valid_window=1):
2021-12-21 19:56:38 +00:00
return render_template("settings_security.html", v=v, error="Invalid password or token.")
2021-10-15 14:08:27 +00:00
v.mfa_secret = secret
g.db.add(v)
g.db.commit()
2022-01-02 13:22:12 +00:00
return render_template("settings_security.html", v=v, msg="Two-factor authentication enabled.")
2021-10-15 14:08:27 +00:00
2022-01-02 13:32:50 +00:00
if request.values.get("2fa_remove"):
2021-10-15 14:08:27 +00:00
if not v.verifyPass(request.values.get('password')):
2021-12-21 19:56:38 +00:00
return render_template("settings_security.html", v=v, error="Invalid password or token.")
2021-10-15 14:08:27 +00:00
token = request.values.get("2fa_remove")
if not v.validate_2fa(token):
2021-12-21 19:56:38 +00:00
return render_template("settings_security.html", v=v, error="Invalid password or token.")
2021-10-15 14:08:27 +00:00
v.mfa_secret = None
g.db.add(v)
g.db.commit()
2022-01-02 13:22:12 +00:00
return render_template("settings_security.html", v=v, msg="Two-factor authentication disabled.")
2021-10-15 14:08:27 +00:00
@app.post("/settings/log_out_all_others")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_log_out_others(v):
submitted_password = request.values.get("password", "").strip()
2021-12-17 03:25:05 +00:00
if not v.verifyPass(submitted_password):
2022-01-14 12:04:35 +00:00
return render_template("settings_security.html", v=v, error="Incorrect Password"), 401
2021-10-15 14:08:27 +00:00
v.login_nonce += 1
session["login_nonce"] = v.login_nonce
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_security.html", v=v, msg="All other devices have been logged out")
2021-10-15 14:08:27 +00:00
@app.post("/settings/images/profile")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_images_profile(v):
2021-12-29 12:38:54 +00:00
if request.headers.get("cf-ipcountry") == "T1": return {"error":"Image uploads are not allowed through TOR."}, 403
2021-10-15 14:08:27 +00:00
file = request.files["profile"]
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)
highres = process_image(name)
2021-10-15 14:08:27 +00:00
if not highres: abort(400)
2022-01-24 23:40:34 +00:00
name2 = name.replace('.webp', 'r.webp')
copyfile(name, name2)
imageurl = process_image(name2, resize=100)
2021-10-15 14:08:27 +00:00
if not imageurl: abort(400)
2022-01-31 22:20:05 +00:00
if v.highres and '/images/' in v.highres:
fpath = '/images/' + v.highres.split('/images/')[1]
if path.isfile(fpath): os.remove(fpath)
if v.profileurl and '/images/' in v.profileurl:
fpath = '/images/' + v.profileurl.split('/images/')[1]
if path.isfile(fpath): os.remove(fpath)
2021-10-15 14:08:27 +00:00
v.highres = highres
v.profileurl = imageurl
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html", v=v, msg="Profile picture successfully updated.")
2021-10-15 14:08:27 +00:00
@app.post("/settings/images/banner")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_images_banner(v):
2021-12-29 12:38:54 +00:00
if request.headers.get("cf-ipcountry") == "T1": return {"error":"Image uploads are not allowed through TOR."}, 403
2021-10-15 14:08:27 +00:00
file = request.files["banner"]
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)
bannerurl = process_image(name)
2021-10-15 14:08:27 +00:00
2021-10-27 20:12:16 +00:00
if bannerurl:
2022-01-31 22:20:05 +00:00
if v.bannerurl and '/images/' in v.bannerurl:
fpath = '/images/' + v.bannerurl.split('/images/')[1]
if path.isfile(fpath): os.remove(fpath)
2021-10-27 20:12:16 +00:00
v.bannerurl = bannerurl
2021-10-15 14:08:27 +00:00
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html", v=v, msg="Banner successfully updated.")
2021-10-15 14:08:27 +00:00
@app.get("/settings/blocks")
@auth_required
def settings_blockedpage(v):
2022-01-14 12:04:35 +00:00
return render_template("settings_blocks.html", v=v)
2021-10-15 14:08:27 +00:00
@app.get("/settings/css")
@auth_required
def settings_css_get(v):
2022-01-14 12:04:35 +00:00
return render_template("settings_css.html", v=v)
2021-10-15 14:08:27 +00:00
@app.post("/settings/css")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_css(v):
2021-12-28 12:30:59 +00:00
if v.agendaposter: return {"error": "Agendapostered users can't edit css!"}
2021-10-15 14:08:27 +00:00
2021-12-28 12:30:49 +00:00
css = request.values.get("css").strip().replace('\\', '').strip()[:4000]
2021-12-10 17:31:32 +00:00
v.css = css
2021-10-15 14:08:27 +00:00
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_css.html", v=v)
2021-10-15 14:08:27 +00:00
@app.get("/settings/profilecss")
@auth_required
def settings_profilecss_get(v):
2022-01-14 12:04:35 +00:00
return render_template("settings_profilecss.html", v=v)
2021-10-15 14:08:27 +00:00
@app.post("/settings/profilecss")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_profilecss(v):
profilecss = request.values.get("profilecss").strip().replace('\\', '').strip()[:4000]
2022-05-03 19:07:15 +00:00
2022-05-04 03:14:14 +00:00
urls = list(css_regex.finditer(profilecss)) + list(css_regex2.finditer(profilecss))
for i in urls:
url = i.group(1)
if url.startswith('/'): continue
domain = tldextract.extract(url).registered_domain
if domain not in approved_embed_hosts:
error = f"The domain '{domain}' is not allowed, please use one of these domains\n\n{approved_embed_hosts}."
return render_template("settings_profilecss.html", error=error, v=v)
2022-05-03 19:07:15 +00:00
2021-10-15 14:08:27 +00:00
v.profilecss = profilecss
g.db.add(v)
g.db.commit()
2022-01-14 12:04:35 +00:00
return render_template("settings_profilecss.html", v=v)
2021-10-15 14:08:27 +00:00
@app.post("/settings/block")
2022-01-31 23:55:11 +00:00
@limiter.limit("1/second;10/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;10/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_block_user(v):
user = get_user(request.values.get("username"), graceful=True)
2021-11-26 23:46:41 +00:00
if not user: return {"error": "That user doesn't exist."}, 404
2022-04-06 22:37:25 +00:00
if user.unblockable:
2022-04-27 22:38:03 +00:00
send_notification(user.id, f"@{v.username} has tried to block you and failed because of your unblockable status!")
2022-04-06 22:37:25 +00:00
g.db.commit()
return {"error": "This user is unblockable."}, 403
2021-11-23 22:36:38 +00:00
2021-10-15 14:08:27 +00:00
if user.id == v.id:
return {"error": "You can't block yourself."}, 409
2021-11-29 23:07:57 +00:00
if v.is_blocking(user):
2021-10-15 14:08:27 +00:00
return {"error": f"You have already blocked @{user.username}."}, 409
2021-11-18 14:21:19 +00:00
if user.id == NOTIFICATIONS_ID:
2021-10-15 14:08:27 +00:00
return {"error": "You can't block this user."}, 409
new_block = UserBlock(user_id=v.id,
target_id=user.id,
)
g.db.add(new_block)
2021-12-20 20:03:59 +00:00
send_notification(user.id, f"@{v.username} has blocked you!")
2021-10-15 14:08:27 +00:00
cache.delete_memoized(frontlist)
g.db.commit()
2021-11-26 19:28:55 +00:00
return {"message": f"@{user.username} blocked."}
2021-10-15 14:08:27 +00:00
@app.post("/settings/unblock")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_unblock_user(v):
user = get_user(request.values.get("username"))
2021-11-29 23:07:57 +00:00
x = v.is_blocking(user)
2021-10-15 14:08:27 +00:00
if not x: abort(409)
g.db.delete(x)
2021-12-20 20:03:59 +00:00
send_notification(user.id, f"@{v.username} has unblocked you!")
2021-10-15 14:08:27 +00:00
cache.delete_memoized(frontlist)
g.db.commit()
return {"message": f"@{user.username} unblocked."}
@app.get("/settings/apps")
@auth_required
def settings_apps(v):
2022-01-14 12:04:35 +00:00
return render_template("settings_apps.html", v=v)
2021-10-15 14:08:27 +00:00
@app.post("/settings/remove_discord")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_remove_discord(v):
remove_user(v)
v.discord_id=None
g.db.add(v)
g.db.commit()
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2021-10-15 14:08:27 +00:00
@app.get("/settings/content")
@auth_required
def settings_content_get(v):
2022-01-14 12:04:35 +00:00
return render_template("settings_filters.html", v=v)
2021-10-15 14:08:27 +00:00
@app.post("/settings/name_change")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2022-01-06 16:46:09 +00:00
@is_not_permabanned
2021-10-15 14:08:27 +00:00
def settings_name_change(v):
new_name=request.values.get("name").strip()
if new_name==v.username:
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-10-15 14:08:27 +00:00
v=v,
error="You didn't change anything")
2022-02-26 20:13:34 +00:00
if not valid_username_regex.fullmatch(new_name):
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-10-15 14:08:27 +00:00
v=v,
2022-01-07 21:03:14 +00:00
error="This isn't a valid username.")
2021-10-15 14:08:27 +00:00
2022-03-05 00:04:20 +00:00
search_name = new_name.replace('\\', '').replace('_','\_').replace('%','')
2021-10-15 14:08:27 +00:00
2021-11-07 13:37:26 +00:00
x= g.db.query(User).filter(
2021-10-15 14:08:27 +00:00
or_(
2022-03-05 00:04:20 +00:00
User.username.ilike(search_name),
User.original_username.ilike(search_name)
2021-10-15 14:08:27 +00:00
)
2022-01-02 00:06:46 +00:00
).one_or_none()
2021-10-15 14:08:27 +00:00
if x and x.id != v.id:
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-10-15 14:08:27 +00:00
v=v,
error=f"Username `{new_name}` is already in use.")
2022-01-19 15:35:08 +00:00
v=g.db.query(User).filter_by(id=v.id).one_or_none()
2021-10-15 14:08:27 +00:00
v.username=new_name
v.name_changed_utc=int(time.time())
set_nick(v, new_name)
g.db.add(v)
g.db.commit()
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2021-10-15 14:08:27 +00:00
@app.post("/settings/song_change")
2022-02-26 22:00:46 +00:00
@limiter.limit("2/second;10/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("2/second;10/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_song_change(v):
song=request.values.get("song").strip()
2022-01-19 15:35:08 +00:00
if song == "" and v.song:
if path.isfile(f"/songs/{v.song}.mp3") and g.db.query(User.id).filter_by(song=v.song).count() == 1:
os.remove(f"/songs/{v.song}.mp3")
2021-10-15 14:08:27 +00:00
v.song = None
g.db.add(v)
g.db.commit()
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2021-10-15 14:08:27 +00:00
song = song.replace("https://music.youtube.com", "https://youtube.com")
2021-12-25 20:46:49 +00:00
if song.startswith(("https://www.youtube.com/watch?v=", "https://youtube.com/watch?v=", "https://m.youtube.com/watch?v=")):
2021-10-15 14:08:27 +00:00
id = song.split("v=")[1]
elif song.startswith("https://youtu.be/"):
id = song.split("https://youtu.be/")[1]
2021-12-20 12:48:02 +00:00
else:
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html", v=v, error="Not a youtube link.")
2021-10-15 14:08:27 +00:00
if "?" in id: id = id.split("?")[0]
if "&" in id: id = id.split("&")[0]
2021-12-13 01:00:08 +00:00
if path.isfile(f'/songs/{id}.mp3'):
2021-10-15 14:08:27 +00:00
v.song = id
g.db.add(v)
g.db.commit()
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2021-10-15 14:08:27 +00:00
2021-11-14 01:19:32 +00:00
req = requests.get(f"https://www.googleapis.com/youtube/v3/videos?id={id}&key={YOUTUBE_KEY}&part=contentDetails", timeout=5).json()
2021-10-15 14:08:27 +00:00
duration = req['items'][0]['contentDetails']['duration']
2021-12-17 17:55:11 +00:00
if duration == 'P0D':
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html", v=v, error="Can't use a live youtube video!")
2021-12-17 17:55:11 +00:00
2021-10-15 14:08:27 +00:00
if "H" in duration:
2022-04-19 18:28:19 +00:00
return render_template("settings_profile.html", v=v, error="Duration of the video must not exceed 15 minutes.")
2021-10-15 14:08:27 +00:00
if "M" in duration:
duration = int(duration.split("PT")[1].split("M")[0])
2022-04-19 18:28:19 +00:00
if duration > 15:
return render_template("settings_profile.html", v=v, error="Duration of the video must not exceed 15 minutes.")
2021-10-15 14:08:27 +00:00
2021-12-13 01:00:08 +00:00
if v.song and path.isfile(f"/songs/{v.song}.mp3") and g.db.query(User.id).filter_by(song=v.song).count() == 1:
os.remove(f"/songs/{v.song}.mp3")
2021-10-15 14:08:27 +00:00
ydl_opts = {
2021-12-13 01:00:08 +00:00
'outtmpl': '/songs/%(title)s.%(ext)s',
2021-10-15 14:08:27 +00:00
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
2021-12-25 20:46:49 +00:00
try: ydl.download([f"https://youtube.com/watch?v={id}"])
2021-10-15 14:08:27 +00:00
except Exception as e:
print(e)
2022-01-14 12:04:35 +00:00
return render_template("settings_profile.html",
2021-10-15 14:08:27 +00:00
v=v,
2022-01-07 21:03:14 +00:00
error="Age-restricted videos aren't allowed.")
2021-10-15 14:08:27 +00:00
2021-12-13 01:00:08 +00:00
files = os.listdir("/songs/")
paths = [path.join("/songs/", basename) for basename in files]
2021-10-15 14:08:27 +00:00
songfile = max(paths, key=path.getctime)
2021-12-13 01:00:08 +00:00
os.rename(songfile, f"/songs/{id}.mp3")
2021-10-15 14:08:27 +00:00
v.song = id
g.db.add(v)
g.db.commit()
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2021-10-15 14:08:27 +00:00
@app.post("/settings/title_change")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2022-05-03 02:15:35 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{request.host}-{session.get("lo_user")}')
2021-10-15 14:08:27 +00:00
@auth_required
def settings_title_change(v):
if v.flairchanged: abort(403)
new_name=request.values.get("title").strip()[:100].replace("𒐪","")
2022-01-14 12:04:35 +00:00
if new_name==v.customtitle: return render_template("settings_profile.html", v=v, error="You didn't change anything")
2021-10-15 14:08:27 +00:00
v.customtitleplain = new_name
2021-12-07 23:18:06 +00:00
v.customtitle = filter_emojis_only(new_name)
2021-10-15 14:08:27 +00:00
2021-10-19 16:16:34 +00:00
if len(v.customtitle) < 1000:
g.db.add(v)
g.db.commit()
2021-10-15 14:08:27 +00:00
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2022-01-28 02:54:50 +00:00
@app.get("/settings")
@auth_required
def settings(v):
2022-04-02 17:11:35 +00:00
return redirect("/settings/profile")
2022-01-28 02:54:50 +00:00
@app.get("/settings/profile")
@auth_required
def settings_profile(v):
if v.flairchanged: ti = datetime.utcfromtimestamp(v.flairchanged).strftime('%Y-%m-%d %H:%M:%S')
else: ti = ''
return render_template("settings_profile.html", v=v, ti=ti)