MarseyWorld/files/routes/settings.py

870 lines
24 KiB
Python
Raw Normal View History

2021-07-21 01:12:26 +00:00
from __future__ import unicode_literals
2021-08-04 15:35:10 +00:00
from files.helpers.alerts import *
from files.helpers.sanitize import *
from files.helpers.filters import filter_comment_html
from files.helpers.markdown import *
from files.helpers.discord import remove_user, set_nick
2021-08-21 11:06:28 +00:00
from files.helpers.const import *
2021-08-04 15:35:10 +00:00
from files.mail import *
2021-10-07 06:04:29 +00:00
from files.__main__ import app, cache, limiter
2021-07-21 01:12:26 +00:00
import youtube_dl
2021-07-30 09:42:17 +00:00
from .front import frontlist
2021-08-19 15:11:35 +00:00
import os
from .posts import filter_title
2021-08-22 15:36:28 +00:00
from files.helpers.discord import add_role
2021-10-03 19:34:24 +00:00
from shutil import copyfile
2021-10-07 06:30:43 +00:00
import requests
2021-07-21 01:12:26 +00:00
valid_username_regex = re.compile("^[a-zA-Z0-9_\-]{3,25}$")
valid_password_regex = re.compile("^.{8,100}$")
2021-08-11 15:15:51 +00:00
YOUTUBE_KEY = environ.get("YOUTUBE_KEY", "").strip()
2021-08-05 14:43:54 +00:00
COINS_NAME = environ.get("COINS_NAME").strip()
2021-08-22 20:03:01 +00:00
GUMROAD_TOKEN = environ.get("GUMROAD_TOKEN", "").strip()
2021-08-22 15:36:28 +00:00
tiers={
"(Paypig)": 1,
"(Renthog)": 2,
"(Landchad)": 3,
2021-08-22 18:43:20 +00:00
"(Terminally online turboautist)": 4,
2021-08-30 21:32:09 +00:00
"(Footpig)": 5,
2021-08-22 15:36:28 +00:00
}
2021-07-21 01:12:26 +00:00
2021-08-15 04:18:50 +00:00
@app.post("/settings/removebackground")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-08-15 04:18:50 +00:00
@auth_required
def removebackground(v):
v.background = None
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 11:05:31 +00:00
return {"message": "Background removed!"}
2021-08-15 04:18:50 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/settings/profile")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_profile_post(v):
2021-10-03 19:36:29 +00:00
if request.content_length > 4 * 1024 * 1024: return "Max file size is 4 MB.", 413
2021-09-21 21:54:44 +00:00
2021-07-21 01:12:26 +00:00
updated = False
2021-08-11 20:11:55 +00:00
if request.values.get("background", v.background) != v.background:
updated = True
2021-09-30 19:40:33 +00:00
v.background = request.values.get("background", None)
2021-08-11 20:11:55 +00:00
2021-07-21 01:12:26 +00:00
if request.values.get("slurreplacer", v.slurreplacer) != v.slurreplacer:
updated = True
v.slurreplacer = request.values.get("slurreplacer", None) == 'true'
if request.values.get("hidevotedon", v.hidevotedon) != v.hidevotedon:
updated = True
v.hidevotedon = request.values.get("hidevotedon", None) == 'true'
2021-09-03 20:52:40 +00:00
if request.values.get("cardview", v.cardview) != v.cardview:
updated = True
v.cardview = request.values.get("cardview", None) == 'true'
2021-09-05 19:20:32 +00:00
if request.values.get("highlightcomments", v.highlightcomments) != v.highlightcomments:
updated = True
v.highlightcomments = request.values.get("highlightcomments", None) == 'true'
2021-07-21 01:12:26 +00:00
if request.values.get("newtab", v.newtab) != v.newtab:
updated = True
v.newtab = request.values.get("newtab", None) == 'true'
if request.values.get("newtabexternal", v.newtabexternal) != v.newtabexternal:
updated = True
v.newtabexternal = request.values.get("newtabexternal", None) == 'true'
if request.values.get("oldreddit", v.oldreddit) != v.oldreddit:
updated = True
v.oldreddit = request.values.get("oldreddit", None) == 'true'
2021-09-13 12:08:06 +00:00
if request.values.get("nitter", v.nitter) != v.nitter:
2021-09-13 12:06:22 +00:00
updated = True
v.nitter = request.values.get("nitter", None) == 'true'
2021-08-11 18:57:12 +00:00
if request.values.get("controversial", v.controversial) != v.controversial:
updated = True
v.controversial = request.values.get("controversial", None) == 'true'
2021-07-21 01:12:26 +00:00
if request.values.get("over18", v.over_18) != v.over_18:
updated = True
v.over_18 = request.values.get("over18", None) == 'true'
if request.values.get("private", v.is_private) != v.is_private:
updated = True
v.is_private = request.values.get("private", None) == 'true'
if request.values.get("nofollow", v.is_nofollow) != v.is_nofollow:
updated = True
v.is_nofollow = request.values.get("nofollow", None) == 'true'
2021-07-28 04:17:23 +00:00
if request.values.get("bio"):
2021-08-02 07:37:46 +00:00
bio = request.values.get("bio")[:1500]
2021-07-21 01:12:26 +00:00
2021-10-09 17:15:47 +00:00
for i in re.finditer('^(https:\/\/.*\.(png|jpg|jpeg|gif|webp|PNG|JPG|JPEG|GIF|WEBP|9999))', bio, re.MULTILINE):
2021-09-15 12:43:46 +00:00
if "wikipedia" not in i.group(1): bio = bio.replace(i.group(1), f'![]({i.group(1)})')
2021-09-22 22:27:09 +00:00
bio = re.sub('([^\n])\n([^\n])', r'\1\n\n\2', bio)
2021-09-03 14:35:32 +00:00
if request.files.get('file') and request.headers.get("cf-ipcountry") != "T1":
2021-09-03 14:42:18 +00:00
2021-09-03 14:35:32 +00:00
file = request.files['file']
if not file.content_type.startswith('image/'):
if request.headers.get("Authorization"): return {"error": f"Image files only"}, 400
else: return render_template("settings_profile.html", v=v, error=f"Image files only."), 400
2021-10-13 13:18:32 +00:00
name = f'/images/{int(time.time())}{secrets.token_urlsafe(2)}.gif'
2021-10-03 19:05:59 +00:00
file.save(name)
2021-10-03 21:51:39 +00:00
url = request.host_url[:-1] + process_image(name)
2021-09-03 14:35:32 +00:00
bio += f"\n\n![]({url})"
2021-09-13 11:45:03 +00:00
bio_html = CustomRenderer().render(mistletoe.Document(bio))
2021-08-21 12:57:16 +00:00
bio_html = sanitize(bio_html)
2021-08-31 12:09:51 +00:00
bans = filter_comment_html(bio_html)
2021-08-31 12:08:18 +00:00
if len(bio_html) > 10000:
return render_template("settings_profile.html",
v=v,
error="Your bio is too long")
2021-07-21 01:12:26 +00:00
if bans:
ban = bans[0]
reason = f"Remove the {ban.domain} link from your bio and try again."
if ban.reason:
2021-08-03 12:20:40 +00:00
reason += f" {ban.reason}"
2021-07-21 01:12:26 +00:00
2021-07-31 05:28:05 +00:00
return {"error": reason}, 401
2021-07-21 01:12:26 +00:00
2021-09-02 14:38:33 +00:00
if len(bio_html) > 10000: abort(400)
2021-08-31 12:09:51 +00:00
v.bio = bio[:1500]
2021-09-02 14:38:33 +00:00
v.bio_html=bio_html
2021-07-21 01:12:26 +00:00
g.db.add(v)
2021-09-23 20:15:08 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return render_template("settings_profile.html",
v=v,
msg="Your bio has been updated.")
2021-07-28 04:17:23 +00:00
if request.values.get("filters"):
2021-07-21 01:12:26 +00:00
2021-08-02 07:37:46 +00:00
filters=request.values.get("filters")[:1000].strip()
2021-07-21 01:12:26 +00:00
if filters==v.custom_filter_list:
return render_template("settings_profile.html",
v=v,
error="You didn't change anything")
v.custom_filter_list=filters
g.db.add(v)
2021-09-23 20:15:08 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return render_template("settings_profile.html",
v=v,
msg="Your custom filters have been updated.")
2021-10-01 22:10:12 +00:00
frontsize = request.values.get("frontsize")
if frontsize:
2021-10-02 12:54:11 +00:00
if frontsize in ["25", "50", "100"]:
v.frontsize = int(frontsize)
2021-10-01 22:10:12 +00:00
updated = True
cache.delete_memoized(frontlist)
else: abort(400)
2021-07-21 01:12:26 +00:00
defaultsortingcomments = request.values.get("defaultsortingcomments")
if defaultsortingcomments:
2021-09-23 19:16:44 +00:00
if defaultsortingcomments in ["new", "old", "controversial", "top", "bottom"]:
2021-07-21 01:12:26 +00:00
v.defaultsortingcomments = defaultsortingcomments
updated = True
2021-10-01 22:10:12 +00:00
else: abort(400)
2021-07-21 01:12:26 +00:00
defaultsorting = request.values.get("defaultsorting")
if defaultsorting:
2021-09-23 19:16:44 +00:00
if defaultsorting in ["hot", "new", "old", "comments", "controversial", "top", "bottom"]:
2021-07-21 01:12:26 +00:00
v.defaultsorting = defaultsorting
updated = True
2021-10-01 22:10:12 +00:00
else: abort(400)
2021-07-21 01:12:26 +00:00
defaulttime = request.values.get("defaulttime")
if defaulttime:
if defaulttime in ["hour", "day", "week", "month", "year", "all"]:
v.defaulttime = defaulttime
updated = True
2021-10-01 22:10:12 +00:00
else: abort(400)
2021-07-21 01:12:26 +00:00
theme = request.values.get("theme")
if theme:
v.theme = theme
2021-10-09 13:49:13 +00:00
if theme == "win98": v.themecolor = "30409f"
2021-09-20 13:30:07 +00:00
updated = True
2021-07-21 01:12:26 +00:00
2021-08-27 16:52:57 +00:00
quadrant = request.values.get("quadrant")
2021-09-13 17:03:46 +00:00
if quadrant and 'pcmemes.net' in request.host.lower():
2021-08-27 16:52:57 +00:00
v.quadrant = quadrant
v.customtitle = quadrant
2021-08-27 16:57:41 +00:00
if quadrant=="Centrist":
v.namecolor = "7f8fa6"
v.titlecolor = "7f8fa6"
elif quadrant=="LibLeft":
2021-08-27 16:52:57 +00:00
v.namecolor = "62ca56"
v.titlecolor = "62ca56"
elif quadrant=="LibRight":
v.namecolor = "f8db58"
v.titlecolor = "f8db58"
elif quadrant=="AuthLeft":
v.namecolor = "ff0000"
v.titlecolor = "ff0000"
elif quadrant=="AuthRight":
v.namecolor = "2a96f3"
v.titlecolor = "2a96f3"
elif quadrant=="LibCenter":
v.namecolor = "add357"
v.titlecolor = "add357"
elif quadrant=="AuthCenter":
v.namecolor = "954b7a"
v.titlecolor = "954b7a"
elif quadrant=="Left":
v.namecolor = "b1652b"
v.titlecolor = "b1652b"
elif quadrant=="Right":
v.namecolor = "91b9A6"
v.titlecolor = "91b9A6"
2021-09-20 13:30:07 +00:00
updated = True
2021-08-27 16:52:57 +00:00
2021-07-21 01:12:26 +00:00
if updated:
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
2021-07-31 05:28:05 +00:00
return {"message": "Your settings have been updated."}
2021-07-21 01:12:26 +00:00
else:
2021-07-31 05:28:05 +00:00
return {"error": "You didn't change anything."}, 400
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/changelogsub")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def changelogsub(v):
v.changelogsub = not v.changelogsub
g.db.add(v)
2021-07-30 09:42:17 +00:00
2021-09-14 13:33:10 +00:00
cache.delete_memoized(frontlist)
2021-07-30 09:42:17 +00:00
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 11:05:31 +00:00
if v.changelogsub: return {"message": "You have subscribed to the changelog!"}
else: return {"message": "You have unsubscribed from the changelog!"}
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/settings/namecolor")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def namecolor(v):
2021-09-19 13:11:34 +00:00
color = str(request.values.get("color", "")).strip()
2021-08-11 19:46:57 +00:00
if color.startswith('#'): color = color[1:]
if len(color) != 6: return render_template("settings_security.html", v=v, error="Invalid color code")
2021-07-21 01:12:26 +00:00
v.namecolor = color
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/profile")
2021-07-27 22:31:28 +00:00
@app.post("/settings/themecolor")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def themecolor(v):
2021-09-19 13:11:34 +00:00
themecolor = str(request.values.get("themecolor", "")).strip()
2021-08-11 19:46:57 +00:00
if themecolor.startswith('#'): themecolor = themecolor[1:]
if len(themecolor) != 6: return render_template("settings_security.html", v=v, error="Invalid color code")
2021-07-21 01:12:26 +00:00
v.themecolor = themecolor
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/profile")
2021-08-22 15:50:59 +00:00
@app.post("/settings/gumroad")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-08-22 15:36:28 +00:00
@auth_required
@validate_formkey
def gumroad(v):
2021-10-09 07:22:52 +00:00
if 'rama' in request.host: patron = 'Paypig'
2021-09-15 05:49:36 +00:00
else: patron = 'Patron'
2021-09-21 21:06:48 +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-08-22 15:36:28 +00:00
data = {
'access_token': GUMROAD_TOKEN,
'email': v.email
}
response = requests.get('https://api.gumroad.com/v2/sales', data=data).json()["sales"]
2021-09-21 21:06:48 +00:00
if len(response) == 0: return {"error": "Email not found"}, 404
2021-08-22 15:36:28 +00:00
response = response[0]
tier = tiers[response["variants_and_quantity"]]
2021-09-21 19:18:16 +00:00
if v.patron == tier: return {"error": f"{patron} rewards already claimed"}, 400
2021-08-22 15:50:59 +00:00
2021-08-22 15:36:28 +00:00
v.patron = tier
2021-09-17 17:01:47 +00:00
g.db.add(v)
2021-08-22 15:36:28 +00:00
grant_awards = {}
if tier == 1:
2021-08-22 16:05:30 +00:00
if v.discord_id: add_role(v, "1")
2021-08-22 15:36:28 +00:00
grant_awards["shit"] = 1
2021-09-20 13:11:10 +00:00
grant_awards["fireflies"] = 1
2021-08-22 15:36:28 +00:00
elif tier == 2:
2021-08-22 16:05:30 +00:00
if v.discord_id: add_role(v, "2")
2021-08-22 15:36:28 +00:00
grant_awards["shit"] = 3
2021-09-20 13:11:10 +00:00
grant_awards["fireflies"] = 3
2021-08-22 15:36:28 +00:00
elif tier == 3:
2021-08-22 16:05:30 +00:00
if v.discord_id: add_role(v, "3")
2021-08-22 15:36:28 +00:00
grant_awards["shit"] = 5
2021-09-20 13:11:10 +00:00
grant_awards["fireflies"] = 5
2021-08-22 15:36:28 +00:00
grant_awards["ban"] = 1
2021-08-31 18:47:26 +00:00
elif tier == 4:
2021-08-22 16:05:30 +00:00
if v.discord_id: add_role(v, "4")
2021-08-22 15:36:28 +00:00
grant_awards["shit"] = 10
2021-09-20 13:11:10 +00:00
grant_awards["fireflies"] = 10
2021-08-22 15:36:28 +00:00
grant_awards["ban"] = 3
2021-08-31 18:47:26 +00:00
elif tier == 5 or tier == 8:
2021-08-30 21:19:10 +00:00
if v.discord_id: add_role(v, "5")
2021-08-30 21:25:21 +00:00
grant_awards["shit"] = 20
2021-09-20 13:11:10 +00:00
grant_awards["fireflies"] = 20
2021-08-30 21:19:10 +00:00
grant_awards["ban"] = 6
2021-08-22 15:36:28 +00:00
thing = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).first().id
for name in grant_awards:
for count in range(grant_awards[name]):
thing += 1
2021-09-18 18:04:05 +00:00
award = AwardRelationship(
2021-08-22 15:36:28 +00:00
id=thing,
2021-08-22 16:02:12 +00:00
user_id=v.id,
2021-08-22 15:36:28 +00:00
kind=name
2021-09-18 18:04:05 +00:00
)
2021-08-22 15:36:28 +00:00
2021-09-18 18:04:05 +00:00
g.db.add(award)
2021-08-22 15:36:28 +00:00
2021-09-17 17:01:47 +00:00
if not v.has_badge(20+tier):
new_badge = Badge(badge_id=20+tier,
user_id=v.id,
)
g.db.add(new_badge)
2021-08-22 16:04:38 +00:00
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-15 05:49:36 +00:00
return {"message": f"{patron} rewards claimed!"}
2021-08-22 15:36:28 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/settings/titlecolor")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def titlecolor(v):
2021-09-19 13:11:34 +00:00
titlecolor = str(request.values.get("titlecolor", "")).strip()
2021-08-11 19:46:57 +00:00
if titlecolor.startswith('#'): titlecolor = titlecolor[1:]
if len(titlecolor) != 6: return render_template("settings_security.html", v=v, error="Invalid color code")
2021-07-21 01:12:26 +00:00
v.titlecolor = titlecolor
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/profile")
2021-07-27 22:31:28 +00:00
@app.post("/settings/security")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_security_post(v):
2021-09-19 13:11:34 +00:00
if request.values.get("new_password"):
if request.values.get(
"new_password") != request.values.get("cnf_password"):
2021-07-21 01:12:26 +00:00
return redirect("/settings/security?error=" +
escape("Passwords do not match."))
2021-09-19 13:11:34 +00:00
if not re.match(valid_password_regex, request.values.get("new_password")):
2021-07-21 01:12:26 +00:00
return redirect("/settings/security?error=" +
escape("Password must be between 8 and 100 characters."))
2021-09-19 13:11:34 +00:00
if not v.verifyPass(request.values.get("old_password")):
2021-07-21 01:12:26 +00:00
return render_template(
"settings_security.html", v=v, error="Incorrect password")
2021-09-19 13:11:34 +00:00
v.passhash = v.hash_password(request.values.get("new_password"))
2021-07-21 01:12:26 +00:00
g.db.add(v)
2021-09-16 17:15:07 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/security?msg=" +
escape("Your password has been changed."))
2021-09-19 13:11:34 +00:00
if request.values.get("new_email"):
2021-07-21 01:12:26 +00:00
2021-09-19 13:11:34 +00:00
if not v.verifyPass(request.values.get('password')):
2021-07-21 01:12:26 +00:00
return redirect("/settings/security?error=" +
escape("Invalid password."))
2021-09-19 13:11:34 +00:00
new_email = request.values.get("new_email","").strip()
2021-07-21 01:12:26 +00:00
if new_email == v.email:
2021-08-22 15:11:41 +00:00
return redirect("/settings/security?error=That email is already yours!")
2021-07-21 01:12:26 +00:00
2021-09-17 08:29:05 +00:00
existing = g.db.query(User).options(lazyload('*')).filter(User.id != v.id,
2021-07-21 01:12:26 +00:00
func.lower(User.email) == new_email.lower()).first()
if existing:
return redirect("/settings/security?error=" +
escape("That email address is already in use."))
url = f"https://{app.config['SERVER_NAME']}/activate"
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)
)
return redirect("/settings/security?msg=" + escape(
"Check your email and click the verification link to complete the email change."))
2021-09-19 13:11:34 +00:00
if request.values.get("2fa_token", ""):
2021-07-21 01:12:26 +00:00
2021-09-19 13:11:34 +00:00
if not v.verifyPass(request.values.get('password')):
2021-07-21 01:12:26 +00:00
return redirect("/settings/security?error=" +
escape("Invalid password or token."))
2021-09-19 13:11:34 +00:00
secret = request.values.get("2fa_secret")
2021-07-21 01:12:26 +00:00
x = pyotp.TOTP(secret)
2021-09-19 13:11:34 +00:00
if not x.verify(request.values.get("2fa_token"), valid_window=1):
2021-07-21 01:12:26 +00:00
return redirect("/settings/security?error=" +
escape("Invalid password or token."))
v.mfa_secret = secret
g.db.add(v)
2021-09-16 17:15:07 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/security?msg=" +
escape("Two-factor authentication enabled."))
2021-09-19 13:11:34 +00:00
if request.values.get("2fa_remove", ""):
2021-07-21 01:12:26 +00:00
2021-09-19 13:11:34 +00:00
if not v.verifyPass(request.values.get('password')):
2021-07-21 01:12:26 +00:00
return redirect("/settings/security?error=" +
escape("Invalid password or token."))
2021-09-19 13:11:34 +00:00
token = request.values.get("2fa_remove")
2021-07-21 01:12:26 +00:00
if not v.validate_2fa(token):
return redirect("/settings/security?error=" +
escape("Invalid password or token."))
v.mfa_secret = None
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/security?msg=" +
escape("Two-factor authentication disabled."))
2021-07-27 22:31:28 +00:00
@app.post("/settings/log_out_all_others")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_log_out_others(v):
2021-10-12 05:23:17 +00:00
submitted_password = request.values.get("password", "").strip()
2021-07-21 01:12:26 +00:00
2021-09-23 20:15:08 +00:00
if not v.verifyPass(submitted_password): return render_template("settings_security.html", v=v, error="Incorrect Password"), 401
2021-07-21 01:12:26 +00:00
v.login_nonce += 1
session["login_nonce"] = v.login_nonce
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-23 20:15:08 +00:00
return render_template("settings_security.html", v=v, msg="All other devices have been logged out")
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/settings/images/profile")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_images_profile(v):
2021-10-03 19:36:29 +00:00
if request.content_length > 4 * 1024 * 1024: return "Max file size is 4 MB.", 413
2021-07-21 01:12:26 +00:00
2021-07-31 09:00:56 +00:00
if request.headers.get("cf-ipcountry") == "T1": return "Image uploads are not allowed through TOR.", 403
2021-09-02 14:06:28 +00:00
2021-10-03 19:34:24 +00:00
file = request.files["profile"]
2021-10-13 13:18:32 +00:00
name = f'/images/{int(time.time())}{secrets.token_urlsafe(2)}.gif'
2021-10-03 19:34:24 +00:00
file.save(name)
2021-10-03 21:51:39 +00:00
highres = request.host_url[:-1] + process_image(name)
2021-10-03 19:05:59 +00:00
2021-09-05 10:26:27 +00:00
if not highres: abort(400)
2021-09-02 14:06:28 +00:00
2021-10-07 03:13:38 +00:00
name2 = name.replace('.gif', 'r.gif')
2021-10-03 19:34:24 +00:00
copyfile(name, name2)
2021-10-03 21:51:39 +00:00
imageurl = request.host_url[:-1] + process_image(name2, True)
2021-10-03 19:05:59 +00:00
2021-09-05 10:26:27 +00:00
if not imageurl: abort(400)
2021-09-02 14:06:28 +00:00
2021-07-30 09:31:17 +00:00
v.highres = highres
v.profileurl = imageurl
g.db.add(v)
2021-07-21 01:12:26 +00:00
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-31 09:00:56 +00:00
return render_template("settings_profile.html", v=v, msg="Profile picture successfully updated.")
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/settings/images/banner")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_images_banner(v):
2021-10-03 19:36:29 +00:00
if request.content_length > 4 * 1024 * 1024: return "Max file size is 4 MB.", 413
2021-07-27 21:35:50 +00:00
2021-07-31 09:00:56 +00:00
if request.headers.get("cf-ipcountry") == "T1": return "Image uploads are not allowed through TOR.", 403
2021-09-02 14:06:28 +00:00
2021-09-09 14:11:44 +00:00
file = request.files["banner"]
2021-10-03 19:05:59 +00:00
2021-10-13 13:18:32 +00:00
name = f'/images/{int(time.time())}{secrets.token_urlsafe(2)}.gif'
2021-10-03 19:05:59 +00:00
file.save(name)
2021-10-03 21:53:26 +00:00
imageurl = request.host_url[:-1] + process_image(name)
2021-09-02 14:06:28 +00:00
2021-07-27 21:35:50 +00:00
if imageurl:
v.bannerurl = imageurl
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
2021-07-31 09:18:59 +00:00
return render_template("settings_profile.html", v=v, msg="Banner successfully updated.")
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/settings/delete/profile")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_delete_profile(v):
v.highres = None
2021-07-27 00:05:58 +00:00
v.profileurl = None
g.db.add(v)
2021-09-16 17:15:07 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return render_template("settings_profile.html", v=v,
msg="Profile picture successfully removed.")
2021-07-27 22:31:28 +00:00
@app.post("/settings/delete/banner")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_delete_banner(v):
2021-08-09 04:21:37 +00:00
2021-07-29 13:56:25 +00:00
v.bannerurl = None
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return render_template("settings_profile.html", v=v,
msg="Banner successfully removed.")
2021-07-27 22:31:28 +00:00
@app.get("/settings/blocks")
2021-07-21 01:12:26 +00:00
@auth_required
def settings_blockedpage(v):
2021-08-23 17:48:55 +00:00
2021-09-23 20:15:08 +00:00
return render_template("settings_blocks.html", v=v)
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/settings/css")
2021-07-21 01:12:26 +00:00
@auth_required
def settings_css_get(v):
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
return render_template("settings_css.html", v=v)
2021-07-27 22:31:28 +00:00
@app.post("/settings/css")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
def settings_css(v):
2021-10-12 05:23:17 +00:00
css = request.values.get("css").strip().replace('\\', '').strip()[:4000]
2021-07-21 01:12:26 +00:00
if not v.agendaposter:
v.css = css
else:
v.css = 'body *::before, body *::after { content: "Trans rights are human rights!"; }'
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return render_template("settings_css.html", v=v)
2021-07-27 22:31:28 +00:00
@app.get("/settings/profilecss")
2021-07-21 01:12:26 +00:00
@auth_required
def settings_profilecss_get(v):
2021-08-23 17:48:55 +00:00
2021-09-14 12:30:28 +00:00
if v.coins < 1000 and not v.patron and v.admin_level < 6: return f"You must have +1000 {COINS_NAME} or be a patron to set profile css."
2021-07-21 01:12:26 +00:00
return render_template("settings_profilecss.html", v=v)
2021-07-27 22:31:28 +00:00
@app.post("/settings/profilecss")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
def settings_profilecss(v):
2021-08-05 14:43:54 +00:00
if v.coins < 1000 and not v.patron: return f"You must have +1000 {COINS_NAME} or be a patron to set profile css."
2021-10-12 05:23:17 +00:00
profilecss = request.values.get("profilecss").strip().replace('\\', '').strip()[:4000]
2021-07-21 01:12:26 +00:00
v.profilecss = profilecss
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return render_template("settings_profilecss.html", v=v)
2021-07-27 22:31:28 +00:00
@app.post("/settings/block")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_block_user(v):
user = get_user(request.values.get("username"), graceful=True)
if not user:
2021-07-31 05:28:05 +00:00
return {"error": "That user doesn't exist."}, 404
2021-07-21 01:12:26 +00:00
if user.id == v.id:
2021-07-31 05:28:05 +00:00
return {"error": "You can't block yourself."}, 409
2021-07-21 01:12:26 +00:00
if v.has_block(user):
2021-07-31 05:28:05 +00:00
return {"error": f"You have already blocked @{user.username}."}, 409
2021-07-21 01:12:26 +00:00
2021-08-21 11:06:28 +00:00
if user.id == NOTIFICATIONS_ACCOUNT:
2021-09-23 20:15:08 +00:00
return {"error": "You can't block this user."}, 409
2021-07-21 01:12:26 +00:00
new_block = UserBlock(user_id=v.id,
target_id=user.id,
)
g.db.add(new_block)
2021-07-28 11:00:15 +00:00
2021-07-21 01:12:26 +00:00
2021-09-17 08:29:05 +00:00
existing = g.db.query(Notification).options(lazyload('*')).filter_by(blocksender=v.id, user_id=user.id).first()
2021-07-21 01:12:26 +00:00
if not existing: send_block_notif(v.id, user.id, f"@{v.username} has blocked you!")
2021-09-14 13:33:10 +00:00
cache.delete_memoized(frontlist)
2021-07-30 09:42:17 +00:00
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-20 13:30:07 +00:00
if v.admin_level == 1: return {"message": f"@{user.username} banned!"}
else: return {"message": f"@{user.username} blocked."}
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.post("/settings/unblock")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_unblock_user(v):
user = get_user(request.values.get("username"))
x = v.has_block(user)
if not x: abort(409)
g.db.delete(x)
2021-07-28 11:00:15 +00:00
2021-07-21 01:12:26 +00:00
2021-09-17 08:29:05 +00:00
existing = g.db.query(Notification).options(lazyload('*')).filter_by(unblocksender=v.id, user_id=user.id).first()
2021-07-21 01:12:26 +00:00
if not existing: send_unblock_notif(v.id, user.id, f"@{v.username} has unblocked you!")
2021-09-14 13:33:10 +00:00
cache.delete_memoized(frontlist)
2021-07-30 09:42:17 +00:00
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-20 13:30:07 +00:00
if v.admin_level == 1: return {"message": f"@{user.username} unbanned!"}
2021-07-31 05:28:05 +00:00
return {"message": f"@{user.username} unblocked."}
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/settings/apps")
2021-07-21 01:12:26 +00:00
@auth_required
def settings_apps(v):
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
return render_template("settings_apps.html", v=v)
2021-07-27 22:31:28 +00:00
@app.post("/settings/remove_discord")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_remove_discord(v):
remove_user(v)
v.discord_id=None
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/profile")
2021-07-27 22:31:28 +00:00
@app.get("/settings/content")
2021-07-21 01:12:26 +00:00
@auth_required
def settings_content_get(v):
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
return render_template("settings_filters.html", v=v)
2021-07-27 22:31:28 +00:00
@app.post("/settings/name_change")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_name_change(v):
2021-09-19 13:11:34 +00:00
new_name=request.values.get("name").strip()
2021-07-21 01:12:26 +00:00
if new_name==v.username:
return render_template("settings_profile.html",
v=v,
error="You didn't change anything")
if not re.match(valid_username_regex, new_name):
return render_template("settings_profile.html",
v=v,
error=f"This isn't a valid username.")
name=new_name.replace('_','\_')
x= g.db.query(User).options(
lazyload('*')
).filter(
or_(
User.username.ilike(name),
User.original_username.ilike(name)
)
).first()
if x and x.id != v.id:
return render_template("settings_profile.html",
v=v,
error=f"Username `{new_name}` is already in use.")
2021-09-22 23:41:51 +00:00
v=g.db.query(User).with_for_update().options(lazyload('*')).filter_by(id=v.id).first()
2021-07-21 01:12:26 +00:00
v.username=new_name
v.name_changed_utc=int(time.time())
set_nick(v, new_name)
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/profile")
2021-07-27 22:31:28 +00:00
@app.post("/settings/song_change")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_song_change(v):
2021-09-19 13:11:34 +00:00
song=request.values.get("song").strip()
2021-07-21 01:12:26 +00:00
2021-09-27 21:46:35 +00:00
if song == "" and v.song and path.isfile(f"/songs/{v.song}.mp3") and g.db.query(User.id).options(lazyload('*')).filter_by(song=v.song).count() == 1:
2021-07-21 01:12:26 +00:00
os.remove(f"/songs/{v.song}.mp3")
2021-09-30 19:40:33 +00:00
v.song = None
2021-07-21 01:12:26 +00:00
g.db.add(v)
2021-09-23 20:15:08 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/profile")
song = song.replace("https://music.youtube.com", "https://youtube.com")
if song.startswith(("https://www.youtube.com/watch?v=", "https://youtube.com/watch?v=", "https://m.youtube.com/watch?v=")):
id = song.split("v=")[1]
elif song.startswith("https://youtu.be/"):
id = song.split("https://youtu.be/")[1]
else:
return render_template("settings_profile.html",
v=v,
error=f"Not a youtube link.")
if "?" in id: id = id.split("?")[0]
if "&" in id: id = id.split("&")[0]
2021-07-26 18:47:42 +00:00
if path.isfile(f'/songs/{id}.mp3'):
2021-09-30 19:40:33 +00:00
v.song = id
2021-07-21 01:12:26 +00:00
g.db.add(v)
2021-09-23 20:15:08 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/profile")
2021-08-05 14:43:54 +00:00
req = requests.get(f"https://www.googleapis.com/youtube/v3/videos?id={id}&key={YOUTUBE_KEY}&part=contentDetails").json()
2021-10-03 19:18:02 +00:00
duration = req['items'][0]['contentDetails']['duration']
2021-07-21 01:12:26 +00:00
if "H" in duration:
return render_template("settings_profile.html",
v=v,
error=f"Duration of the video must not exceed 10 minutes.")
if "M" in duration:
duration = int(duration.split("PT")[1].split("M")[0])
if duration > 10:
return render_template("settings_profile.html",
v=v,
error=f"Duration of the video must not exceed 10 minutes.")
2021-09-27 21:46:35 +00:00
if v.song and path.isfile(f"/songs/{v.song}.mp3") and g.db.query(User.id).options(lazyload('*')).filter_by(song=v.song).count() == 1:
2021-07-21 01:12:26 +00:00
os.remove(f"/songs/{v.song}.mp3")
ydl_opts = {
'outtmpl': '/songs/%(title)s.%(ext)s',
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try: ydl.download([f"https://youtube.com/watch?v={id}"])
except Exception as e:
print(e)
return render_template("settings_profile.html",
v=v,
error=f"Age-restricted videos aren't allowed.")
files = os.listdir("/songs/")
2021-07-26 18:47:42 +00:00
paths = [path.join("/songs/", basename) for basename in files]
songfile = max(paths, key=path.getctime)
2021-07-21 01:12:26 +00:00
os.rename(songfile, f"/songs/{id}.mp3")
2021-09-30 19:40:33 +00:00
v.song = id
2021-07-21 01:12:26 +00:00
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
return redirect("/settings/profile")
2021-07-27 22:31:28 +00:00
@app.post("/settings/title_change")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-21 01:12:26 +00:00
@auth_required
@validate_formkey
def settings_title_change(v):
if v.flairchanged: abort(403)
2021-09-19 13:11:34 +00:00
new_name=request.values.get("title").strip()[:100].replace("𒐪","")
2021-07-21 01:12:26 +00:00
if new_name==v.customtitle:
return render_template("settings_profile.html",
v=v,
error="You didn't change anything")
v.customtitleplain = new_name
2021-08-19 15:11:35 +00:00
v.customtitle = filter_title(new_name)
2021-07-21 01:12:26 +00:00
g.db.add(v)
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-11 03:49:59 +00:00
return redirect("/settings/profile")