
* prepare codebase to create scheduled tasks
there is some prep work involved with this. the scheduler would be happy
if this work was done. simply, we extract out the `created_utc`
interface from *everything* that uses it such that we don't have to
repeat ourselves a bunch. all fun stuff.
next commit is the meat of it.
* cron: basic backend work for scheduler
* avoid ipmort loop
* attempt 2 at fixing import loops
* parathensize because operator precedence
* delete file that came back for some reason.
* does NOPing the oauth apps work?
* import late and undo clients.py change
* stringify column names.
* reorder imports.
* remove task reference
* fix missing mapper object
* make coupled to repeatabletask i guess
* sanitize: fix sanitize imports
* import shadowing crap
* re-shadow shadowed variable
* fix regexes
* use the correct not operator
* readd missing commit
* scheduler: SQLA only allows concrete relations
* implement submission scheduler
* fix import loop with db_session
* get rid of import loop in submission.py and comment.py
* remove import loops by deferring import until function clal
* i give up.
* awful.
* ...
* fix another app import loop
* fix missing import in route handler
* fix import error in wrappers.py
* fix wrapper error
* call update wrapper in the admin_level_required case
* :marseyshrug:
* fix issue with wrapper
* some cleanup and some fixes
* some more cleanup
let's avoid polluting scopes where we can.
* ...
* add SCHEDULED_POSTS permission.
* move const.py into config like the other files.
* style fixes.
* lock table for concurrency improvements
* don't attempt to commit on errors
* Refactor code, create `TaskRunContext`, create python callable task type.
* use import contextlib
* testing stuff i guess.
* handle repeatable tasks properly.
* Attempt another fix at fighting the mapper
* do it right ig
* SQLA1.4 doesn't support nested polymorphism ig
* fix errenous class import
* fix mapper errors
* import app in wrappers.py
* fix import failures and stuff like that.
* embed and import fixes
* minor formatting changes.
* Add running state enum and don't attempt to check for currently running tasks.
* isort
* documentation, style, and commit after each task.
* Add completion time and more docs, rename, etc
* document `CRON_SLEEP_SECONDS` better.
* add note about making LiteralString
* filter out tasks that have been run in the future
* reference RepeatableTask's `__tablename__` directly
* use a master/slave configuration for tasks
the master periodically checks to see if the slave is alive, healthy,
and not taking too many resources, and if applicable kills its
child and restarts it.
only one relation is supported at the moment.
* don't duplicate process unnecessarily
* note impl detail, add comments
* fix imports.
* getting imports to stop being stupid.
* environment notes.
* syntax derp
* *sigh*
* stupid environment stuff
* add UI for submitting a scheduled post
* stupid things i need to fix the user class
* ...
* fix template
* add formkey
* pass v
* add hour and minute field
* bleh
* remove concrete
* the sqlalchemy docs are wrong
* fix me being dumb and not understanding error messages
* missing author attribute for display
* author_name property
* it's a property
* with_polymorphic i think fixes this
* dsfavgnhmjk
* *sigh*
* okay try this again
* try getting rid of the comment section
* include -> extends
* put the div outside of the thing.
* fix user page listings :/
* mhm
* i hate this why isn't this working
* this should fix it
* Fix posts being set as disabled by default
* form UI imrpovements
* label
* <textarea>s should have their closing tag
* UI fixes.
* and fix errenous spinner thing.
* don't abort(415) when browsers send 0 length files for some reason
* UI improvements
* line break.
* CSS :S
* better explainer
* don't show moderation buttons for scheduled posts
* ...
* meh
* add edit form
* include forms on default page.
* fix hour minute selectino.
* improve ui i guess and add api
* Show previous postings on scheduled task page
* create task id
* sqla
* posts -> submissions
* fix OTM relationship
* edit URL
* use common formkey control
* Idk why this isn't working
* Revert "Idk why this isn't working"
This reverts commit 3b93f741df
.
* does removing viewonly fix it?
* don't import routes on db migrations
* apparently this has to be a string
* UI improvements redux
* margins and stuff
* add cron to supervisord
* remove stupid duplication
* typo fix
* postgres syntax error
* better lock and error handling
* add relationship between task and runs
* fix some ui stuff
* fix incorrect timestamp comparison
* ...
* Fix logic errors blocking scheduled posts
Two bugs here:
- RepeatableTask.run_time_last <= now: run_time_last is NULL by
default. NULL is not greater than, less than, or equal to any
value. We use NULL to signify a never-run task; check for that
condition when building the task list.
- `6 <= weekday <= 0`: there is no integer that is both gte 6 and
lte 0. This was always false.
* pasthrough worker process STDOUT and STDERR
* Add scheduler to admin panel
* scheduler
* fix listing and admin home
* date formatting ixes
* fix ages
* task user interface
* fix some more import crap i have to deal with
* fix typing
* avoid import loop
* UI fixes
* fix incorrect type
* task type
* Scheduled task UI improvements (add runs and stuff)
* make the width a lil bit smaller
* task runs.
* fix submit page
* add alembic migration
* log on startup
* Fix showing edit button
* Fix logic for `can_edit` (accidentally did `author_id` instead of `id`)
* Broad review pass
Review:
- Call `invalidate_cache` with `is_html=` explicitly for clarity,
rather than a bare boolean in the call args.
- Remove `marseys_const*` and associated stateful const system:
the implementation was good if we needed them, but TheMotte
doesn't use emoji, and a greenfield emoji system would likely
not keep those darned lists floating in thread-local scope.
Also they were only needed for goldens and random emoji, which
are fairly non-central features.
- Get `os.environ` fully out of the templates by using the new
constants we already have in files.helpers.config.environment.
- Given files.routes.posts cleanup,get rid of shop discount dict.
It's already a mapping of badge IDs to discounts for badges that
likely won't continue to exist (if they even do at present).
- RepeatableTaskRun.exception: use `@property.setter` instead of
overriding `__setattr__`.
Fix:
- Welcome message literal contained an indented Markdown code block.
- Condition to show "View source" button changed to show source to
logged out. This may well be a desirable change, but it's not
clearly intended here.
* Fix couple of routing issues
* fix 400 with post body editing
* Add error handler for HTTP 415
* fix router giving wrong arg name to handler
* Use supervisord to monitor memory rather than DIY
Also means we're using pip for getting supervisord now, so we don't rely
on the Debian image base for any packages.
* fix task run elapsed time display
* formatting and removing redundant code
* Fix missing ModAction import
* dates and times fixes
* Having to modify imports here anyway, might as
well change it.
* correct documentation.
* don't use urlunparse
* validators: import sanitize instead of from syntax
* cron: prevent races on task running
RepeatableTask.run_state_enum acts as the mutex on repeatable tasks.
Previously, the list of tasks to run was acquired before individually
locking each task. However, there was a period where the table is both
unlocked and the tasks are in state WAITING between those points.
This could potentially have led to two 'cron' processes each running the
same task simultaneously. Instead, we check for runnability both when
building the preliminary list and when mutexing the task via run state
in the database.
Also:
- g.db and the cron db object are both instances of `Session`, not
`scoped_session` because they are obtained from
`scoped_session.__call__`, which acts as a `Session` factory.
Propagate this to the type hints.
- Sort order of task run submissions so /tasks/scheduled_posts/<id>
"Previous Task Runs" listings are useful.
* Notify followers on post publication
This was old behavior lost in the refactoring of the submit endpoint.
Also fix an AttributeError in `Follow.__repr__` which carried over
from all the repr copypasta.
* Fix image attachment
Any check for `file.content_length` relies on browsers sending
Content-Length headers with the request. It seems that few actually do.
The pre-refactor approach was to check for truthiness, which excludes
both None and the strange empty strings that we seem to get in absence
of a file upload. We return to doing so.
---------
Co-authored-by: TLSM <duolsm@outlook.com>
645 lines
18 KiB
Python
645 lines
18 KiB
Python
import os
|
|
from shutil import copyfile
|
|
|
|
from files.__main__ import app, limiter
|
|
from files.helpers.alerts import *
|
|
from files.helpers.caching import invalidate_cache
|
|
from files.helpers.config.const import *
|
|
from files.helpers.media import process_image
|
|
from files.helpers.sanitize import *
|
|
from files.helpers.sanitize import filter_emojis_only
|
|
from files.mail import *
|
|
|
|
tiers={
|
|
"(Paypig)": 1,
|
|
"(Renthog)": 2,
|
|
"(Landchad)": 3,
|
|
"(Terminally online turboautist)": 4,
|
|
"(Marsey's Sugar Daddy)": 5,
|
|
"(JIDF Bankroller)": 6,
|
|
"(Rich Bich)": 7,
|
|
"(LlamaBean)": 1,
|
|
}
|
|
|
|
@app.post("/settings/profile")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def settings_profile_post(v):
|
|
updated = False
|
|
|
|
if request.values.get("reddit", v.reddit) != v.reddit:
|
|
reddit = request.values.get("reddit")
|
|
if reddit in {'old.reddit.com', 'reddit.com', 'i.reddit.com', 'teddit.net', 'libredd.it', 'unddit.com'}:
|
|
updated = True
|
|
v.reddit = reddit
|
|
|
|
elif request.values.get("hidevotedon", v.hidevotedon) != v.hidevotedon:
|
|
updated = True
|
|
v.hidevotedon = request.values.get("hidevotedon") == 'true'
|
|
|
|
elif request.values.get("cardview", v.cardview) != v.cardview:
|
|
updated = True
|
|
v.cardview = request.values.get("cardview") == 'true'
|
|
|
|
elif request.values.get("highlightcomments", v.highlightcomments) != v.highlightcomments:
|
|
updated = True
|
|
v.highlightcomments = request.values.get("highlightcomments") == 'true'
|
|
|
|
elif request.values.get("newtab", v.newtab) != v.newtab:
|
|
updated = True
|
|
v.newtab = request.values.get("newtab") == 'true'
|
|
|
|
elif request.values.get("newtabexternal", v.newtabexternal) != v.newtabexternal:
|
|
updated = True
|
|
v.newtabexternal = request.values.get("newtabexternal") == 'true'
|
|
|
|
elif request.values.get("nitter", v.nitter) != v.nitter:
|
|
updated = True
|
|
v.nitter = request.values.get("nitter") == 'true'
|
|
|
|
elif request.values.get("controversial", v.controversial) != v.controversial:
|
|
updated = True
|
|
v.controversial = request.values.get("controversial") == 'true'
|
|
|
|
elif request.values.get("over18", v.over_18) != v.over_18:
|
|
updated = True
|
|
v.over_18 = request.values.get("over18") == 'true'
|
|
|
|
elif request.values.get("private", v.is_private) != v.is_private:
|
|
updated = True
|
|
v.is_private = request.values.get("private") == 'true'
|
|
|
|
elif request.values.get("nofollow", v.is_nofollow) != v.is_nofollow:
|
|
updated = True
|
|
v.is_nofollow = request.values.get("nofollow") == 'true'
|
|
|
|
elif request.values.get("bio") == "":
|
|
v.bio = None
|
|
v.bio_html = None
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return render_template("settings_profile.html", v=v, msg="Your bio has been updated.")
|
|
|
|
elif request.values.get("friends") == "":
|
|
v.friends = None
|
|
v.friends_html = None
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return render_template("settings_profile.html", v=v, msg="Your friends list has been updated.")
|
|
|
|
elif request.values.get("enemies") == "":
|
|
v.enemies = None
|
|
v.enemies_html = None
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return render_template("settings_profile.html", v=v, msg="Your enemies list has been updated.")
|
|
|
|
elif request.values.get("friends"):
|
|
friends = request.values.get("friends")[:500]
|
|
|
|
friends_html = sanitize(friends)
|
|
|
|
if len(friends_html) > 2000:
|
|
return render_template("settings_profile.html",
|
|
v=v,
|
|
error="Your friends list is too long")
|
|
|
|
|
|
notify_users = NOTIFY_USERS(friends, v)
|
|
|
|
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)
|
|
|
|
v.friends = friends[:500]
|
|
v.friends_html=friends_html
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return render_template("settings_profile.html",
|
|
v=v,
|
|
msg="Your friends list has been updated.")
|
|
|
|
|
|
elif request.values.get("enemies"):
|
|
enemies = request.values.get("enemies")[:500]
|
|
|
|
enemies_html = sanitize(enemies)
|
|
|
|
if len(enemies_html) > 2000:
|
|
return render_template("settings_profile.html",
|
|
v=v,
|
|
error="Your enemies list is too long")
|
|
|
|
|
|
notify_users = NOTIFY_USERS(enemies, v)
|
|
|
|
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)
|
|
|
|
v.enemies = enemies[:500]
|
|
v.enemies_html=enemies_html
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return render_template("settings_profile.html",
|
|
v=v,
|
|
msg="Your enemies list has been updated.")
|
|
|
|
|
|
elif request.values.get("bio") or request.files.get('file') and request.headers.get("cf-ipcountry") != "T1":
|
|
bio = request.values.get("bio")[:1500]
|
|
|
|
if request.files.get('file'):
|
|
file = request.files['file']
|
|
if file.content_type.startswith('image/'):
|
|
name = f'/images/{time.time()}'.replace('.','') + '.webp'
|
|
file.save(name)
|
|
url = process_image(name)
|
|
bio += f"\n\n"
|
|
else:
|
|
if request.headers.get("Authorization") or request.headers.get("xhr"): abort(400, "Image files only")
|
|
return render_template("settings_profile.html", v=v, error="Image files only"), 400
|
|
|
|
bio_html = sanitize(bio)
|
|
|
|
if len(bio_html) > 10000:
|
|
return render_template("settings_profile.html",
|
|
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()
|
|
return render_template("settings_profile.html",
|
|
v=v,
|
|
msg="Your bio has been updated.")
|
|
|
|
|
|
frontsize = request.values.get("frontsize")
|
|
if frontsize:
|
|
if frontsize in {"15", "25", "50", "100"}:
|
|
v.frontsize = int(frontsize)
|
|
updated = True
|
|
invalidate_cache(frontlist=True)
|
|
else: abort(400)
|
|
|
|
defaultsortingcomments = request.values.get("defaultsortingcomments")
|
|
if defaultsortingcomments:
|
|
if defaultsortingcomments in SORTS_COMMENTS:
|
|
v.defaultsortingcomments = defaultsortingcomments
|
|
updated = True
|
|
else: abort(400)
|
|
|
|
defaultsorting = request.values.get("defaultsorting")
|
|
if defaultsorting:
|
|
if defaultsorting in SORTS_POSTS:
|
|
v.defaultsorting = defaultsorting
|
|
updated = True
|
|
else: abort(400)
|
|
|
|
defaulttime = request.values.get("defaulttime")
|
|
if defaulttime:
|
|
if defaulttime in {"hour", "day", "week", "month", "year", "all"}:
|
|
v.defaulttime = defaulttime
|
|
updated = True
|
|
else: abort(400)
|
|
|
|
theme = request.values.get("theme")
|
|
if theme:
|
|
if theme in THEMES:
|
|
v.theme = theme
|
|
if theme == "win98":
|
|
v.themecolor = "30409f"
|
|
else:
|
|
v.themecolor = "fff"
|
|
updated = True
|
|
else: abort(400)
|
|
|
|
house = request.values.get("house")
|
|
if house and house in ("None","Furry","Femboy","Vampire","Racist"):
|
|
if v.house: cost = 2000
|
|
else: cost = 500
|
|
|
|
if v.coins >= cost: v.coins -= cost
|
|
elif v.procoins >= cost: v.procoins -= cost
|
|
else: abort(403)
|
|
|
|
if house == "None": house = None
|
|
v.house = house
|
|
updated = True
|
|
|
|
if updated:
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
|
|
return {"message": "Your settings have been updated."}
|
|
|
|
else:
|
|
abort(400, "You didn't change anything.")
|
|
|
|
|
|
@app.post("/settings/filters")
|
|
@auth_required
|
|
def filters(v):
|
|
filters=request.values.get("filters")[:1000].strip()
|
|
|
|
if filters == v.custom_filter_list:
|
|
return render_template("settings_filters.html", v=v, error="You didn't change anything")
|
|
|
|
v.custom_filter_list=filters
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return render_template("settings_filters.html", v=v, msg="Your custom filters have been updated.")
|
|
|
|
@app.post("/changelogsub")
|
|
@auth_required
|
|
def changelogsub(v):
|
|
v.changelogsub = not v.changelogsub
|
|
g.db.add(v)
|
|
|
|
invalidate_cache(frontlist=True)
|
|
|
|
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")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def namecolor(v):
|
|
color = str(request.values.get("color", "")).strip()
|
|
if color.startswith('#'): color = color[1:]
|
|
if len(color) != 6: return render_template("settings_security.html", v=v, error="Invalid color code")
|
|
v.namecolor = color
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return redirect("/settings/profile")
|
|
|
|
@app.post("/settings/themecolor")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def themecolor(v):
|
|
themecolor = str(request.values.get("themecolor", "")).strip()
|
|
if themecolor.startswith('#'): themecolor = themecolor[1:]
|
|
if len(themecolor) != 6: return render_template("settings_security.html", v=v, error="Invalid color code")
|
|
v.themecolor = themecolor
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return redirect("/settings/profile")
|
|
|
|
@app.post("/settings/titlecolor")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def titlecolor(v):
|
|
titlecolor = str(request.values.get("titlecolor", "")).strip()
|
|
if titlecolor.startswith('#'): titlecolor = titlecolor[1:]
|
|
if len(titlecolor) != 6: return render_template("settings_profile.html", v=v, error="Invalid color code")
|
|
v.titlecolor = titlecolor
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return redirect("/settings/profile")
|
|
|
|
@app.post("/settings/verifiedcolor")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def verifiedcolor(v):
|
|
verifiedcolor = str(request.values.get("verifiedcolor", "")).strip()
|
|
if verifiedcolor.startswith('#'): verifiedcolor = verifiedcolor[1:]
|
|
if len(verifiedcolor) != 6: return render_template("settings_profile.html", v=v, error="Invalid color code")
|
|
v.verifiedcolor = verifiedcolor
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return redirect("/settings/profile")
|
|
|
|
@app.post("/settings/security")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def settings_security_post(v):
|
|
if request.values.get("new_password"):
|
|
if request.values.get("new_password") != request.values.get("cnf_password"):
|
|
return render_template("settings_security.html", v=v, error="Passwords do not match.")
|
|
|
|
if not valid_password_regex.fullmatch(request.values.get("new_password")):
|
|
return render_template("settings_security.html", v=v, error="Password must be between 8 and 100 characters.")
|
|
|
|
if not v.verifyPass(request.values.get("old_password")):
|
|
return render_template("settings_security.html", v=v, error="Incorrect password")
|
|
|
|
v.passhash = v.hash_password(request.values.get("new_password"))
|
|
|
|
g.db.add(v)
|
|
|
|
g.db.commit()
|
|
|
|
return render_template("settings_security.html", v=v, msg="Your password has been changed.")
|
|
|
|
if request.values.get("new_email"):
|
|
|
|
if not v.verifyPass(request.values.get('password')):
|
|
return render_template("settings_security.html", v=v, error="Invalid password.")
|
|
|
|
new_email = request.values.get("new_email","").strip().lower()
|
|
|
|
if new_email == v.email:
|
|
return render_template("settings_security.html", v=v, error="That email is already yours!")
|
|
|
|
url = f"{SITE_FULL}/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 render_template("settings_security.html", v=v, msg="Check your email and click the verification link to complete the email change.")
|
|
|
|
if request.values.get("2fa_token"):
|
|
if not v.verifyPass(request.values.get('password')):
|
|
return render_template("settings_security.html", v=v, error="Invalid password or token.")
|
|
|
|
secret = request.values.get("2fa_secret")
|
|
x = pyotp.TOTP(secret)
|
|
if not x.verify(request.values.get("2fa_token"), valid_window=1):
|
|
return render_template("settings_security.html", v=v, error="Invalid password or token.")
|
|
|
|
v.mfa_secret = secret
|
|
g.db.add(v)
|
|
|
|
g.db.commit()
|
|
|
|
return render_template("settings_security.html", v=v, msg="Two-factor authentication enabled.")
|
|
|
|
if request.values.get("2fa_remove"):
|
|
|
|
if not v.verifyPass(request.values.get('password')):
|
|
return render_template("settings_security.html", v=v, error="Invalid password or token.")
|
|
|
|
token = request.values.get("2fa_remove")
|
|
|
|
if not v.validate_2fa(token):
|
|
return render_template("settings_security.html", v=v, error="Invalid password or token.")
|
|
|
|
v.mfa_secret = None
|
|
g.db.add(v)
|
|
|
|
g.db.commit()
|
|
|
|
return render_template("settings_security.html", v=v, msg="Two-factor authentication disabled.")
|
|
|
|
@app.post("/settings/log_out_all_others")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def settings_log_out_others(v):
|
|
|
|
submitted_password = request.values.get("password", "").strip()
|
|
|
|
if not v.verifyPass(submitted_password):
|
|
return render_template("settings_security.html", v=v, error="Incorrect Password"), 401
|
|
|
|
v.login_nonce += 1
|
|
|
|
session["login_nonce"] = v.login_nonce
|
|
|
|
g.db.add(v)
|
|
|
|
g.db.commit()
|
|
|
|
return render_template("settings_security.html", v=v, msg="All other devices have been logged out")
|
|
|
|
|
|
@app.post("/settings/images/profile")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def settings_images_profile(v):
|
|
if request.headers.get("cf-ipcountry") == "T1": abort(403, "Image uploads are not allowed through TOR.")
|
|
|
|
file = request.files["profile"]
|
|
|
|
name = f'/images/{time.time()}'.replace('.','') + '.webp'
|
|
file.save(name)
|
|
highres = process_image(name)
|
|
|
|
if not highres: abort(400)
|
|
|
|
name2 = name.replace('.webp', 'r.webp')
|
|
copyfile(name, name2)
|
|
imageurl = process_image(name2, resize=100)
|
|
|
|
if not imageurl: abort(400)
|
|
|
|
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)
|
|
v.highres = highres
|
|
v.profileurl = imageurl
|
|
g.db.add(v)
|
|
|
|
g.db.commit()
|
|
|
|
return render_template("settings_profile.html", v=v, msg="Profile picture successfully updated.")
|
|
|
|
|
|
@app.post("/settings/images/banner")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def settings_images_banner(v):
|
|
if request.headers.get("cf-ipcountry") == "T1": abort(403, "Image uploads are not allowed through TOR.")
|
|
|
|
file = request.files["banner"]
|
|
|
|
name = f'/images/{time.time()}'.replace('.','') + '.webp'
|
|
file.save(name)
|
|
bannerurl = process_image(name)
|
|
|
|
if bannerurl:
|
|
if v.bannerurl and '/images/' in v.bannerurl:
|
|
fpath = '/images/' + v.bannerurl.split('/images/')[1]
|
|
if path.isfile(fpath): os.remove(fpath)
|
|
v.bannerurl = bannerurl
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
|
|
return render_template("settings_profile.html", v=v, msg="Banner successfully updated.")
|
|
|
|
|
|
@app.get("/settings/blocks")
|
|
@auth_required
|
|
def settings_blockedpage(v):
|
|
return render_template("settings_blocks.html", v=v)
|
|
|
|
@app.get("/settings/css")
|
|
@auth_required
|
|
def settings_css_get(v):
|
|
return render_template("settings_css.html", v=v)
|
|
|
|
@app.post("/settings/css")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def settings_css(v):
|
|
css = sanitize_raw(request.values.get("css", "").replace('\\', ''), allow_newlines=True, length_limit=CSS_LENGTH_MAXIMUM)
|
|
ok, err = validate_css(css)
|
|
if not ok:
|
|
abort(400, err)
|
|
v.css = css
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
|
|
return render_template("settings_css.html", v=v)
|
|
|
|
@app.get("/settings/profilecss")
|
|
@auth_required
|
|
def settings_profilecss_get(v):
|
|
return render_template("settings_profilecss.html", v=v)
|
|
|
|
@app.post("/settings/profilecss")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def settings_profilecss(v):
|
|
profilecss = sanitize_raw(request.values.get("profilecss", "").replace('\\', ''), allow_newlines=True, length_limit=CSS_LENGTH_MAXIMUM)
|
|
ok, err = validate_css(profilecss)
|
|
if not ok:
|
|
abort(400, err)
|
|
v.profilecss = profilecss
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
return render_template("settings_profilecss.html", v=v)
|
|
|
|
@app.post("/settings/block")
|
|
@limiter.limit("1/second;10/day")
|
|
@auth_required
|
|
def settings_block_user(v):
|
|
user = get_user(request.values.get("username"), graceful=True)
|
|
|
|
if not user: abort(404, "That user doesn't exist.")
|
|
|
|
if user.id == v.id:
|
|
abort(409, "You can't block yourself.")
|
|
|
|
if v.is_blocking(user):
|
|
abort(409, f"You have already blocked @{user.username}.")
|
|
|
|
if user.id == NOTIFICATIONS_ID:
|
|
abort(409, "You can't block this user.")
|
|
|
|
new_block = UserBlock(user_id=v.id,
|
|
target_id=user.id,
|
|
)
|
|
g.db.add(new_block)
|
|
invalidate_cache(frontlist=True)
|
|
g.db.commit()
|
|
|
|
return {"message": f"@{user.username} blocked."}
|
|
|
|
|
|
@app.post("/settings/unblock")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def settings_unblock_user(v):
|
|
user = get_user(request.values.get("username"))
|
|
x = v.is_blocking(user)
|
|
if not x: abort(409)
|
|
g.db.delete(x)
|
|
invalidate_cache(frontlist=True)
|
|
g.db.commit()
|
|
|
|
return {"message": f"@{user.username} unblocked."}
|
|
|
|
|
|
@app.get("/settings/apps")
|
|
@auth_required
|
|
def settings_apps(v):
|
|
return render_template("settings_apps.html", v=v)
|
|
|
|
@app.get("/settings/content")
|
|
@auth_required
|
|
def settings_content_get(v):
|
|
return render_template("settings_filters.html", v=v)
|
|
|
|
@app.post("/settings/name_change")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@is_not_permabanned
|
|
def settings_name_change(v):
|
|
|
|
new_name=request.values.get("name").strip()
|
|
|
|
if new_name==v.username:
|
|
return render_template("settings_profile.html",
|
|
v=v,
|
|
error="You didn't change anything")
|
|
|
|
if not valid_username_regex.fullmatch(new_name):
|
|
return render_template("settings_profile.html",
|
|
v=v,
|
|
error="This isn't a valid username.")
|
|
|
|
x = g.db.query(User).filter(
|
|
or_(
|
|
func.lower(User.username) == new_name.lower(),
|
|
func.lower(User.original_username) == new_name.lower()
|
|
)
|
|
).one_or_none()
|
|
|
|
if x and x.id != v.id:
|
|
return render_template("settings_profile.html",
|
|
v=v,
|
|
error=f"Username `{new_name}` is already in use.")
|
|
|
|
v=g.db.query(User).filter_by(id=v.id).one_or_none()
|
|
|
|
v.username=new_name
|
|
v.name_changed_utc=int(time.time())
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
|
|
return redirect("/settings/profile")
|
|
|
|
@app.post("/settings/title_change")
|
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
|
@auth_required
|
|
def settings_title_change(v):
|
|
|
|
if v.flairchanged: abort(403)
|
|
|
|
new_name=request.values.get("title").strip()[:100].replace("𒐪","")
|
|
|
|
if new_name==v.customtitle: return render_template("settings_profile.html", v=v, error="You didn't change anything")
|
|
|
|
v.customtitleplain = new_name
|
|
|
|
v.customtitle = filter_emojis_only(new_name)
|
|
|
|
if len(v.customtitle) < 1000:
|
|
g.db.add(v)
|
|
g.db.commit()
|
|
|
|
return redirect("/settings/profile")
|
|
|
|
|
|
@app.get("/settings")
|
|
@auth_required
|
|
def settings(v):
|
|
return redirect("/settings/profile")
|
|
|
|
|
|
@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)
|