MarseyWorld/files/helpers/get.py

372 lines
8.9 KiB
Python
Raw Normal View History

[DO NOT MERGE] import detanglation (#442) * move Base definition to files.classes.__init__.py * fix ImportError * move userpage listing to users.py * don't import the app from classes * consts: set default values to avoid crashes consts: warn if the secret key is the default config value * card view: sneed (user db schema) * cloudflare: use DEFAULT_CONFIG_VALUE * const: set default values * decouple media.py from __main__ * pass database to avoid imports * import cleanup and import request not in const, but in the requests mega import * move asset_submissions site check to __init__ * asset submissions feature flag * flag * g.is_tor * don't import request where it's not needed * i think this is fine * mail: move to own routes and helper * wrappers * required wrappers move * unfuck wrappers a bit * move snappy quotes and marseys to stateful consts * marsify * :pepodrool: * fix missing import * import cache * ...and settings.py * and static.py * static needs cache * route * lmao all of the jinja shit was in feeds.py amazing * classes should only import what they need from flask * import Response * hdjbjdhbhjf * ... * dfdfdfdf * make get a non-required import * isort imports (mostly) * but actually * configs * reload config on import * fgfgfgfg * config * config * initialize snappy and test * cookie of doom debug * edfjnkf * xikscdfd * debug config * set session cookie domain, i think this fixes the can't login bug * sdfbgnhvfdsghbnjfbdvvfghnn * hrsfxgf * dump the entire config on a request * kyskyskyskyskyskyskyskyskys * duifhdskfjdfd * dfdfdfdfdfdfdfdfdfdfdfdf * dfdfdfdf * imoprt all of the consts beacuse fuck it * 😭 * dfdfdfdfdfdfsdasdf * print the entire session * rffdfdfjkfksj * fgbhffh * not the secret keys * minor bug fixes * be helpful in the warning * gfgfgfg * move warning lower * isort main imports (i hope this doesn't fuck something up) * test * session cookie domain redux * dfdfdfd * try only importing Flask * formkeys fix * y * :pepodrool: * route helper * remove before flight * dfdfdfdfdf * isort classes * isort helpers * move check_for_alts to routehelpers and also sort imports and get rid of unused ones * that previous commit but actkally * readd the cache in a dozen places they were implicitly imported * use g.is_tor instead of request.headers. bla bla bla * upgrade streamers to their own route file * get rid of unused imports in __main__ * fgfgf * don't pull in the entire ORM where we don't need it * features * explicit imports for the get helper * explicit imports for the get helper redux * testing allroutes * remove unused import * decouple flask from classes * syntax fix also remember these have side fx for some reason (why?) * move side effects out of the class * posts * testing on devrama * settings * reloading * settingssdsdsds * streamer features * site settings * testing settings on devrama * import * fix modlog * remove debug stuff * revert commit 67275b21ab6e2f2520819e84d10bfc1c746a15b6 * archiveorg to _archiveorg * skhudkfkjfd * fix cron for PCM * fix bugs that snekky wants me to * Fix call to realbody passing db, standardize kwarg * test * import check_for_alts from the right place * cloudflare * testing on devrama * fix cron i think * shadow properly * tasks * Remove print which will surely be annoying in prod. * v and create new session * use files.classes * make errors import little and fix rare 500 in /allow_nsfw * Revert "use files.classes" This reverts commit 98c10b876cf86ce058b7fb955cf1ec0bfb9996c6. * pass v to media functions rather than using g * fix * dfdfdfdfd * cleanup, py type checking is dumb so don't use it where it causes issues * Fix some merge bugs, add DEFAULT_RATELIMIT to main. * Fix imports on sqlalchemy expressions. * `from random import random` is an error. * Fix replies db param. * errors: fix missing import * fix rare 500: only send to GIFT_NOTIF_ID if it exists, and send them the right text * Fix signup formkey. * fix 2 500s * propagate db to submissions * fix replies * dfdfdfdf * Fix verifiedcolor. * is_manual * can't use getters outside of an app context * don't attempt to do gumroad on sites where it's not enabled * don't attempt to do gumraod on sites's where it's unnecessary * Revert "don't attempt to do gumroad on sites where it's not enabled" This reverts commit 6f8a6331878655492dfaf1907b27f8be513c14d3. * fix 500 * validate media type Co-authored-by: TLSM <duolsm@outlook.com>
2022-11-15 09:19:08 +00:00
from flask import *
from sqlalchemy import and_, any_, or_
from sqlalchemy.orm import joinedload, Query, load_only
[DO NOT MERGE] import detanglation (#442) * move Base definition to files.classes.__init__.py * fix ImportError * move userpage listing to users.py * don't import the app from classes * consts: set default values to avoid crashes consts: warn if the secret key is the default config value * card view: sneed (user db schema) * cloudflare: use DEFAULT_CONFIG_VALUE * const: set default values * decouple media.py from __main__ * pass database to avoid imports * import cleanup and import request not in const, but in the requests mega import * move asset_submissions site check to __init__ * asset submissions feature flag * flag * g.is_tor * don't import request where it's not needed * i think this is fine * mail: move to own routes and helper * wrappers * required wrappers move * unfuck wrappers a bit * move snappy quotes and marseys to stateful consts * marsify * :pepodrool: * fix missing import * import cache * ...and settings.py * and static.py * static needs cache * route * lmao all of the jinja shit was in feeds.py amazing * classes should only import what they need from flask * import Response * hdjbjdhbhjf * ... * dfdfdfdf * make get a non-required import * isort imports (mostly) * but actually * configs * reload config on import * fgfgfgfg * config * config * initialize snappy and test * cookie of doom debug * edfjnkf * xikscdfd * debug config * set session cookie domain, i think this fixes the can't login bug * sdfbgnhvfdsghbnjfbdvvfghnn * hrsfxgf * dump the entire config on a request * kyskyskyskyskyskyskyskyskys * duifhdskfjdfd * dfdfdfdfdfdfdfdfdfdfdfdf * dfdfdfdf * imoprt all of the consts beacuse fuck it * 😭 * dfdfdfdfdfdfsdasdf * print the entire session * rffdfdfjkfksj * fgbhffh * not the secret keys * minor bug fixes * be helpful in the warning * gfgfgfg * move warning lower * isort main imports (i hope this doesn't fuck something up) * test * session cookie domain redux * dfdfdfd * try only importing Flask * formkeys fix * y * :pepodrool: * route helper * remove before flight * dfdfdfdfdf * isort classes * isort helpers * move check_for_alts to routehelpers and also sort imports and get rid of unused ones * that previous commit but actkally * readd the cache in a dozen places they were implicitly imported * use g.is_tor instead of request.headers. bla bla bla * upgrade streamers to their own route file * get rid of unused imports in __main__ * fgfgf * don't pull in the entire ORM where we don't need it * features * explicit imports for the get helper * explicit imports for the get helper redux * testing allroutes * remove unused import * decouple flask from classes * syntax fix also remember these have side fx for some reason (why?) * move side effects out of the class * posts * testing on devrama * settings * reloading * settingssdsdsds * streamer features * site settings * testing settings on devrama * import * fix modlog * remove debug stuff * revert commit 67275b21ab6e2f2520819e84d10bfc1c746a15b6 * archiveorg to _archiveorg * skhudkfkjfd * fix cron for PCM * fix bugs that snekky wants me to * Fix call to realbody passing db, standardize kwarg * test * import check_for_alts from the right place * cloudflare * testing on devrama * fix cron i think * shadow properly * tasks * Remove print which will surely be annoying in prod. * v and create new session * use files.classes * make errors import little and fix rare 500 in /allow_nsfw * Revert "use files.classes" This reverts commit 98c10b876cf86ce058b7fb955cf1ec0bfb9996c6. * pass v to media functions rather than using g * fix * dfdfdfdfd * cleanup, py type checking is dumb so don't use it where it causes issues * Fix some merge bugs, add DEFAULT_RATELIMIT to main. * Fix imports on sqlalchemy expressions. * `from random import random` is an error. * Fix replies db param. * errors: fix missing import * fix rare 500: only send to GIFT_NOTIF_ID if it exists, and send them the right text * Fix signup formkey. * fix 2 500s * propagate db to submissions * fix replies * dfdfdfdf * Fix verifiedcolor. * is_manual * can't use getters outside of an app context * don't attempt to do gumroad on sites where it's not enabled * don't attempt to do gumraod on sites's where it's unnecessary * Revert "don't attempt to do gumroad on sites where it's not enabled" This reverts commit 6f8a6331878655492dfaf1907b27f8be513c14d3. * fix 500 * validate media type Co-authored-by: TLSM <duolsm@outlook.com>
2022-11-15 09:19:08 +00:00
2023-10-07 17:55:50 +00:00
from files.classes import Comment, CommentVote, Hat, Hole, Post, User, UserBlock, Vote
from files.helpers.config.const import *
2023-01-25 03:18:17 +00:00
from files.__main__ import cache
2022-05-04 23:09:46 +00:00
2023-09-26 22:12:01 +00:00
# Escape SQL pattern-matching special characters
def escape_for_search(string):
return string.replace('\\', '').replace('_', '\_').replace('%', '\%').strip()
2023-07-30 00:42:06 +00:00
def sanitize_username(username):
2023-09-26 22:12:01 +00:00
username = username.lstrip('@').replace('(', '').replace(')', '')
username = escape_for_search(username)
return username
def get_user(username, v=None, graceful=False, include_blocks=False, attributes=None):
2022-05-04 23:09:46 +00:00
if not username:
if graceful: return None
2023-08-23 10:08:02 +00:00
abort(400, "Empty username.")
2022-05-04 23:09:46 +00:00
2023-10-16 18:09:35 +00:00
search_name = sanitize_username(username)
if not search_name:
if graceful: return None
2023-08-23 10:08:02 +00:00
abort(400, "Empty username.")
2023-03-16 06:27:58 +00:00
user = g.db.query(
2022-05-04 23:09:46 +00:00
User
).filter(
or_(
2023-10-16 18:09:35 +00:00
User.username.ilike(search_name),
User.original_username.ilike(search_name),
2023-11-25 21:57:15 +00:00
User.extra_username.ilike(search_name),
2023-10-16 18:09:35 +00:00
User.prelock_username.ilike(search_name),
2022-05-04 23:09:46 +00:00
)
2022-07-03 11:56:40 +00:00
)
if attributes:
user = user.options(load_only(*attributes))
2022-07-03 11:56:40 +00:00
user = user.one_or_none()
2022-05-04 23:09:46 +00:00
if not user:
if graceful: return None
abort(404, f"A user with the name '{username}' was not found!")
2022-05-04 23:09:46 +00:00
2022-11-02 20:22:23 +00:00
if v and include_blocks:
user = add_block_props(user, v)
2022-05-04 23:09:46 +00:00
return user
2023-07-30 00:42:06 +00:00
def get_users(usernames, ids_only=False, graceful=False):
if not usernames: return []
usernames = [sanitize_username(n) for n in usernames]
if not any(usernames):
if graceful and len(usernames) == 0: return []
2023-08-23 10:08:02 +00:00
abort(400, "Empty usernames.")
2023-03-01 20:28:34 +00:00
if ids_only:
2023-03-16 06:27:58 +00:00
users = g.db.query(User.id)
2023-03-01 20:28:34 +00:00
else:
2023-03-16 06:27:58 +00:00
users = g.db.query(User)
2023-03-01 20:28:34 +00:00
users = users.filter(
or_(
2022-06-17 22:03:23 +00:00
User.username.ilike(any_(usernames)),
2023-05-13 04:53:14 +00:00
User.original_username.ilike(any_(usernames)),
2023-11-25 21:57:15 +00:00
User.extra_username.ilike(any_(usernames)),
2023-05-13 04:53:14 +00:00
User.prelock_username.ilike(any_(usernames)),
2023-11-25 21:57:15 +00:00
)
).all()
if len(users) != len(usernames) and not graceful:
2023-08-23 10:08:02 +00:00
abort(404, "Users not found.")
2023-03-01 20:28:34 +00:00
if ids_only:
users = [x[0] for x in users]
return users
2023-07-30 00:42:06 +00:00
def get_account(id, v=None, graceful=False, include_blocks=False):
2023-01-01 11:36:20 +00:00
try:
id = int(id)
except:
if graceful: return None
2023-08-23 10:08:02 +00:00
abort(400, "User ID needs to be an integer.")
2022-05-04 23:09:46 +00:00
2023-03-16 06:27:58 +00:00
user = g.db.get(User, id)
2022-09-12 07:17:57 +00:00
if not user:
2023-08-23 10:08:02 +00:00
if not graceful: abort(404, "User not found.")
2022-09-12 07:17:57 +00:00
else: return None
2022-05-04 23:09:46 +00:00
if include_blocks:
user = add_block_props(user, v)
2022-05-04 23:09:46 +00:00
return user
2023-07-30 00:42:06 +00:00
def get_accounts_dict(ids, v=None, graceful=False):
if not ids: return {}
2023-01-01 11:36:20 +00:00
try:
ids = set(int(id) for id in ids)
except:
if graceful: return None
2023-08-23 10:08:02 +00:00
abort(400, "User IDs need to be an integer.")
2023-03-16 06:27:58 +00:00
users = g.db.query(User).filter(User.id.in_(ids))
users = users.all()
2023-08-23 10:08:02 +00:00
if len(users) != len(ids) and not graceful:
abort(404, "Users not found.")
return {u.id:u for u in users}
2023-07-30 00:42:06 +00:00
def get_post(i, v=None, graceful=False):
2022-07-01 23:11:48 +00:00
try: i = int(i)
except:
if graceful: return None
2023-08-23 10:08:02 +00:00
else: abort(400, "Post ID needs to be an integer.")
2022-07-01 23:11:48 +00:00
2022-06-24 13:19:53 +00:00
if not i:
if graceful: return None
2023-08-23 10:08:02 +00:00
else: abort(400, "Empty post ID.")
2022-06-24 13:19:53 +00:00
2022-05-04 23:09:46 +00:00
if v:
2023-06-07 23:26:32 +00:00
vt = g.db.query(Vote).filter_by(user_id=v.id, post_id=i).subquery()
2022-05-04 23:09:46 +00:00
blocking = v.blocking.subquery()
2023-03-16 06:27:58 +00:00
post = g.db.query(
2023-06-07 23:26:32 +00:00
Post,
2022-05-04 23:09:46 +00:00
vt.c.vote_type,
blocking.c.target_id,
)
2023-10-29 14:30:30 +00:00
post = post.filter(Post.id == i
).outerjoin(
2023-01-01 11:36:20 +00:00
vt,
2023-06-07 23:26:32 +00:00
vt.c.post_id == Post.id,
).outerjoin(
2023-01-01 11:36:20 +00:00
blocking,
2023-06-07 23:26:32 +00:00
blocking.c.target_id == Post.author_id,
2022-05-04 23:09:46 +00:00
)
2023-10-29 14:30:30 +00:00
post = post.one_or_none()
2023-01-01 11:36:20 +00:00
2022-06-24 13:19:53 +00:00
if not post:
2022-05-04 23:09:46 +00:00
if graceful: return None
2023-08-23 10:08:02 +00:00
else: abort(404, "Post not found.")
2022-05-04 23:09:46 +00:00
2022-06-24 13:19:53 +00:00
x = post[0]
x.voted = post[1] or 0
x.is_blocking = post[2] or 0
2022-05-04 23:09:46 +00:00
else:
2023-06-07 23:26:32 +00:00
post = g.db.get(Post, i)
2022-06-24 13:19:53 +00:00
if not post:
2022-05-04 23:09:46 +00:00
if graceful: return None
2023-08-23 10:08:02 +00:00
else: abort(404, "Post not found.")
2022-06-24 13:19:53 +00:00
x=post
2022-05-04 23:09:46 +00:00
return x
2023-07-30 00:42:06 +00:00
def get_posts(pids, v=None, extra=None):
if not pids: return []
2022-05-04 23:09:46 +00:00
if v:
2023-06-07 23:26:32 +00:00
vt = g.db.query(Vote.vote_type, Vote.post_id).filter(
Vote.post_id.in_(pids),
2022-05-04 23:09:46 +00:00
Vote.user_id==v.id
).subquery()
blocking = v.blocking.subquery()
blocked = v.blocked.subquery()
2023-03-16 06:27:58 +00:00
query = g.db.query(
2023-06-07 23:26:32 +00:00
Post,
2022-05-04 23:09:46 +00:00
vt.c.vote_type,
blocking.c.target_id,
blocked.c.target_id,
).filter(
2023-06-07 23:26:32 +00:00
Post.id.in_(pids)
).outerjoin(
2023-06-07 23:26:32 +00:00
vt, vt.c.post_id==Post.id
).outerjoin(
2023-01-01 11:36:20 +00:00
blocking,
2023-06-07 23:26:32 +00:00
blocking.c.target_id == Post.author_id,
).outerjoin(
2023-01-01 11:36:20 +00:00
blocked,
2023-06-07 23:26:32 +00:00
blocked.c.user_id == Post.author_id,
)
else:
2023-06-07 23:26:32 +00:00
query = g.db.query(Post).filter(Post.id.in_(pids))
2022-11-25 21:43:35 +00:00
if extra: query = extra(query)
results = query.all()
2022-05-04 23:09:46 +00:00
if v:
output = [p[0] for p in results]
2022-05-04 23:09:46 +00:00
for i in range(len(output)):
output[i].voted = results[i][1] or 0
output[i].is_blocking = results[i][2] or 0
output[i].is_blocked = results[i][3] or 0
2022-05-04 23:09:46 +00:00
else:
output = results
2022-05-04 23:09:46 +00:00
return sorted(output, key=lambda x: pids.index(x.id))
2023-07-30 00:42:06 +00:00
def get_comment(i, v=None, graceful=False):
2022-07-01 23:11:48 +00:00
try: i = int(i)
except:
if graceful: return None
2023-08-23 10:08:02 +00:00
abort(404, "Comment ID needs to be an integer.")
2022-07-01 23:11:48 +00:00
2022-06-24 13:19:53 +00:00
if not i:
if graceful: return None
2023-08-23 10:08:02 +00:00
else: abort(404, "Empty comment ID.")
2022-05-04 23:09:46 +00:00
2023-08-23 21:57:39 +00:00
comment = g.db.get(Comment, i)
2022-06-24 13:19:53 +00:00
if not comment:
if graceful: return None
2023-08-23 10:08:02 +00:00
else: abort(404, "Comment not found.")
2022-05-04 23:09:46 +00:00
return add_vote_and_block_props(comment, v, CommentVote)
2023-07-30 00:42:06 +00:00
def add_block_props(target, v):
2022-10-28 23:39:31 +00:00
if not v: return target
id = None
2023-06-07 23:26:32 +00:00
if any(isinstance(target, cls) for cls in {Post, Comment}):
2022-10-28 23:39:31 +00:00
id = target.author_id
elif isinstance(target, User):
id = target.id
else:
2023-06-07 23:26:32 +00:00
raise TypeError("add_block_props only supports non-None posts, comments, and users")
2023-01-01 11:36:20 +00:00
2022-10-28 23:39:31 +00:00
if hasattr(target, 'is_blocking') and hasattr(target, 'is_blocked'):
return target
if v.id == id or id == AUTOJANNY_ID: # users can't block or be blocked by themselves or AutoJanny
target.is_blocking = False
target.is_blocked = False
return target
2023-03-16 06:27:58 +00:00
block = g.db.query(UserBlock).filter(
2022-10-28 23:39:31 +00:00
or_(
and_(
UserBlock.user_id == v.id,
UserBlock.target_id == id
),
and_(
UserBlock.user_id == id,
UserBlock.target_id == v.id
)
)
).first()
target.is_blocking = block and block.user_id == v.id
target.is_blocked = block and block.target_id == v.id
return target
2023-07-30 00:42:06 +00:00
def add_vote_props(target, v, vote_cls):
2022-10-28 23:39:31 +00:00
if hasattr(target, 'voted'): return target
2023-03-16 06:27:58 +00:00
vt = g.db.query(vote_cls.vote_type).filter_by(user_id=v.id)
2022-10-28 23:39:31 +00:00
if vote_cls == Vote:
2023-06-07 23:26:32 +00:00
vt = vt.filter_by(post_id=target.id)
2022-10-28 23:39:31 +00:00
elif vote_cls == CommentVote:
vt = vt.filter_by(comment_id=target.id)
else:
vt = None
if vt: vt = vt.one_or_none()
target.voted = vt.vote_type if vt else 0
return target
2023-07-30 00:42:06 +00:00
def add_vote_and_block_props(target, v, vote_cls):
2022-10-28 23:39:31 +00:00
if not v: return target
target = add_block_props(target, v)
return add_vote_props(target, v, vote_cls)
2022-05-04 23:09:46 +00:00
2023-07-30 00:42:06 +00:00
def get_comments(cids, v=None, extra=None):
2022-05-04 23:09:46 +00:00
if not cids: return []
if v:
2023-02-27 16:16:12 +00:00
output = get_comments_v_properties(v, None, Comment.id.in_(cids))[1]
2022-05-04 23:09:46 +00:00
else:
2023-03-16 06:27:58 +00:00
output = g.db.query(Comment).join(Comment.author)
2022-11-25 21:43:35 +00:00
if extra: output = extra(output)
output = output.filter(Comment.id.in_(cids)).all()
2022-05-04 23:09:46 +00:00
return sorted(output, key=lambda x: cids.index(x.id))
2023-07-30 00:42:06 +00:00
def get_comments_v_properties(v, should_keep_func=None, *criterion):
if not v:
raise TypeError("A user is required")
2023-03-16 06:27:58 +00:00
votes = g.db.query(CommentVote.vote_type, CommentVote.comment_id).filter_by(user_id=v.id).subquery()
blocking = v.blocking.subquery()
blocked = v.blocked.subquery()
2023-03-16 06:27:58 +00:00
comments = g.db.query(
Comment,
votes.c.vote_type,
blocking.c.target_id,
blocked.c.target_id,
)
comments = comments.filter(*criterion)
comments = comments.outerjoin(
votes,
votes.c.comment_id == Comment.id,
).outerjoin(
blocking,
blocking.c.target_id == Comment.author_id,
).outerjoin(
blocked,
blocked.c.user_id == Comment.author_id,
)
queried = comments.all()
output = []
dump = []
for c in queried:
comment = c[0]
comment.voted = c[1] or 0
comment.is_blocking = c[2] or 0
comment.is_blocked = c[3] or 0
if not should_keep_func or should_keep_func(c[0]): output.append(comment)
else: dump.append(comment)
return (comments, output)
2023-10-07 17:55:50 +00:00
def get_hole(hole, v=None, graceful=False):
if not hole:
2022-10-05 10:30:44 +00:00
if graceful: return None
2024-02-16 13:23:48 +00:00
else: abort(404, f"/h/{hole} was not found.")
2023-10-07 17:55:50 +00:00
hole = hole.replace('/h/', '').replace('h/', '').strip().lower()
if not hole:
2022-10-05 10:30:44 +00:00
if graceful: return None
2024-02-16 13:23:48 +00:00
else: abort(404, f"/h/{hole} was not found.")
2023-10-07 17:55:50 +00:00
hole = g.db.get(Hole, hole)
if not hole:
2022-10-05 10:30:44 +00:00
if graceful: return None
2024-02-16 13:23:48 +00:00
else: abort(404, f"/h/{hole} was not found.")
2023-10-07 18:05:52 +00:00
return hole
2023-01-25 03:18:17 +00:00
@cache.memoize(timeout=3600)
2023-07-30 00:42:06 +00:00
def get_profile_picture(identifier):
2023-01-25 03:18:17 +00:00
if isinstance(identifier, int):
x = get_account(identifier, graceful=True)
else:
x = get_user(identifier, graceful=True)
return x.profile_url if x else 'not_found'
2023-01-27 09:41:32 +00:00
2023-09-17 20:14:41 +00:00
def get_msg():
if request.referrer and request.referrer.split('?')[0] == request.base_url:
return request.values.get("msg")
else:
return None
2023-01-27 11:19:56 +00:00
def get_error():
if request.referrer and request.referrer.split('?')[0] == request.base_url:
return request.values.get("error")
else:
return None
2023-01-27 09:41:32 +00:00
2023-05-05 05:23:59 +00:00
def get_page():
try: return max(int(request.values.get("page", 1)), 1)
except: return 1
def get_obj_hole(obj):
if isinstance(obj, Comment):
if obj.parent_post:
obj.hole = g.db.query(Post.hole).filter_by(id=obj.parent_post).one()[0]
else:
obj.hole = None
return obj.hole