rDrama/files/helpers/get.py

364 lines
9.6 KiB
Python
Raw Normal View History

from typing import Callable, Iterable, List, Optional, Union
[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_
2022-11-25 21:43:35 +00:00
from sqlalchemy.orm import joinedload, selectinload, Query
[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 files.classes import Comment, CommentVote, Hat, Sub, Submission, User, UserBlock, Vote
from files.helpers.config.const import AUTOJANNY_ID
2023-01-25 03:18:17 +00:00
from files.__main__ import cache
2022-05-04 23:09:46 +00:00
def sanitize_username(username:str) -> str:
if not username: return username
return username.replace('\\', '').replace('_', '\_').replace('%', '').replace('(', '').replace(')', '').strip()
2022-10-28 00:44:34 +00:00
def get_id(username:str, graceful=False) -> Optional[int]:
username = sanitize_username(username)
if not username:
if graceful: return None
abort(404)
2022-05-04 23:09:46 +00:00
user = g.db.query(
User.id
).filter(
or_(
User.username.ilike(username),
User.original_username.ilike(username)
)
).one_or_none()
2022-10-01 05:15:56 +00:00
if not user:
if graceful: return None
abort(404)
2022-05-04 23:09:46 +00:00
return user[0]
account linking improvements (#448) currently account delinking is very messy and can sometimes just not work we do codey stuff so it's not as bad also we create a pretty page for mops to mop up borked account links * alts: allow proper delinking * fix prev commit * url fix * fix 500 * fixes * :pepodrool: * flag * :pepodrool: redux * sdsdsdsds * correct endpoint * fix html page * alts: only adjust session history if flag is set * fix 500 * allow relinking * fsdsds * :pepodrool: redux * alts: don't fail if an alt isn't history * use postToastSwitch + some API changes * remove unnecessary variables * d-none * delink accounts mod action * fa-link-slash * alts: add form to create alt * remove copied and pasted template * rounded section * UI improvement + fix * \n * fix status * admin: remove duplicate route admin: do a permissions check on 2 pages that need it admin: set the manual flag for manually flagged alts * variable change * fix 500 * alts * add shadowban icon to alt link tool * shadowbanned tooltip * add user info section * fix 500, remove unnecessary form, and add alt votes button * trans and also link to page * margin * sdsdsd * stop the count * fix prev commit * with ctx * plural * alts * don't show shadowbanned users to those who can't see them this is... extremely rare and won't ever be seen in production however if perms were ever rearranged in the future, this keeps permissions correct * shadowban check in alt list * let shadow realm enthusiasts see shadowban alts * sdsdsds * test * be graceful where needed * sdsdsdsds * alts: don't allow adding the same account alts: clarify wording * rename and reorder on admin panel * EOL * remove frankly unnecessary check * try with a set * test * Revert "try with a set" This reverts commit 72be353fba5ffa39b37590cc5d3bf584c94ee06e. * Revert "Revert "try with a set"" This reverts commit 81e41890a192e8b46d0463477998e905fddf56ba. * Revert "Revert "Revert "try with a set""" This reverts commit be51592135a3c09848f993f0154bd2ac862ae505. * clean up test
2022-11-14 17:32:13 +00:00
def get_user(username:Optional[str], v:Optional[User]=None, graceful=False, include_blocks=False, include_shadowbanned=True) -> Optional[User]:
2022-05-04 23:09:46 +00:00
if not username:
if graceful: return None
abort(404)
2022-05-04 23:09:46 +00:00
username = sanitize_username(username)
if not username:
if graceful: return None
abort(404)
2022-05-04 23:09:46 +00:00
user = g.db.query(
User
).filter(
or_(
User.username.ilike(username),
User.original_username.ilike(username)
)
2022-07-03 11:56:40 +00:00
)
user = user.one_or_none()
2022-05-04 23:09:46 +00:00
2022-10-12 06:53:32 +00:00
if not user or (user.shadowbanned and not (include_shadowbanned or (v and v.can_see_shadowbanned))):
if graceful: return None
abort(404)
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
def get_users(usernames:Iterable[str], graceful=False) -> List[User]:
if not usernames: return []
usernames = [sanitize_username(n) for n in usernames]
if not any(usernames):
if graceful and len(usernames) == 0: return []
abort(404)
users = g.db.query(User).filter(
or_(
2022-06-17 22:03:23 +00:00
User.username.ilike(any_(usernames)),
User.original_username.ilike(any_(usernames))
)
).all()
if len(users) != len(usernames) and not graceful:
abort(404)
return users
def get_account(id:Union[str, int], v:Optional[User]=None, graceful=False, include_blocks=False, include_shadowbanned=True) -> Optional[User]:
2023-01-01 11:36:20 +00:00
try:
id = int(id)
except:
if graceful: return None
abort(404)
2022-05-04 23:09:46 +00:00
user = g.db.get(User, id)
2022-09-12 07:17:57 +00:00
2022-10-12 06:53:32 +00:00
if not user or (user.shadowbanned and not (include_shadowbanned or (v and v.can_see_shadowbanned))):
2022-09-12 07:17:57 +00:00
if not graceful: abort(404)
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
def get_accounts_dict(ids:Union[Iterable[str], Iterable[int]], v:Optional[User]=None, graceful=False, include_shadowbanned=True) -> Optional[dict[int, User]]:
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
abort(404)
users = g.db.query(User).filter(User.id.in_(ids))
if not (include_shadowbanned or (v and v.can_see_shadowbanned)):
users = users.filter(User.shadowbanned == None)
users = users.all()
if len(users) != len(ids) and not graceful: abort(404)
return {u.id:u for u in users}
def get_post(i:Union[str, int], v:Optional[User]=None, graceful=False) -> Optional[Submission]:
2022-07-01 23:11:48 +00:00
try: i = int(i)
except:
if graceful: return None
else: abort(404)
2022-07-01 23:11:48 +00:00
2022-06-24 13:19:53 +00:00
if not i:
if graceful: return None
else: abort(404)
2022-05-04 23:09:46 +00:00
if v:
vt = g.db.query(Vote).filter_by(user_id=v.id, submission_id=i).subquery()
2022-05-04 23:09:46 +00:00
blocking = v.blocking.subquery()
2022-06-24 13:19:53 +00:00
post = g.db.query(
2022-05-04 23:09:46 +00:00
Submission,
vt.c.vote_type,
blocking.c.target_id,
)
2022-06-24 13:19:53 +00:00
post=post.filter(Submission.id == i
).outerjoin(
2023-01-01 11:36:20 +00:00
vt,
vt.c.submission_id == Submission.id,
).outerjoin(
2023-01-01 11:36:20 +00:00
blocking,
blocking.c.target_id == Submission.author_id,
2022-05-04 23:09:46 +00:00
)
2022-06-24 13:19:53 +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
else: abort(404)
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:
2022-06-24 13:19:53 +00:00
post = g.db.get(Submission, i)
if not post:
2022-05-04 23:09:46 +00:00
if graceful: return None
else: abort(404)
2022-06-24 13:19:53 +00:00
x=post
2022-05-04 23:09:46 +00:00
return x
2022-11-25 21:43:35 +00:00
def get_posts(pids:Iterable[int], v:Optional[User]=None, eager:bool=False, extra:Optional[Callable[[Query], Query]]=None) -> List[Submission]:
if not pids: return []
2022-05-04 23:09:46 +00:00
if v:
vt = g.db.query(Vote.vote_type, Vote.submission_id).filter(
2023-01-01 11:36:20 +00:00
Vote.submission_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()
query = g.db.query(
Submission,
vt.c.vote_type,
blocking.c.target_id,
blocked.c.target_id,
).filter(
Submission.id.in_(pids)
).outerjoin(
vt, vt.c.submission_id==Submission.id
).outerjoin(
2023-01-01 11:36:20 +00:00
blocking,
blocking.c.target_id == Submission.author_id,
).outerjoin(
2023-01-01 11:36:20 +00:00
blocked,
blocked.c.user_id == Submission.author_id,
)
else:
query = g.db.query(Submission).filter(Submission.id.in_(pids))
2022-11-25 21:43:35 +00:00
if extra: query = extra(query)
if eager:
query = query.options(
selectinload(Submission.author).options(
selectinload(User.hats_equipped.and_(Hat.equipped == True)) \
.joinedload(Hat.hat_def, innerjoin=True),
2022-11-11 18:01:23 +00:00
selectinload(User.badges),
selectinload(User.sub_mods),
selectinload(User.sub_exiles),
),
selectinload(Submission.flags),
selectinload(Submission.awards),
selectinload(Submission.options),
)
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))
def get_comment(i:Union[str, int], v:Optional[User]=None, graceful=False) -> Optional[Comment]:
2022-07-01 23:11:48 +00:00
try: i = int(i)
except:
if graceful: return None
abort(404)
2022-07-01 23:11:48 +00:00
2022-06-24 13:19:53 +00:00
if not i:
if graceful: return None
else: abort(404)
2022-05-04 23:09:46 +00:00
2022-06-24 13:19:53 +00:00
comment=g.db.get(Comment, i)
if not comment:
if graceful: return None
else: abort(404)
2022-05-04 23:09:46 +00:00
return add_vote_and_block_props(comment, v, CommentVote)
def add_block_props(target:Union[Submission, Comment, User], v:Optional[User]):
2022-10-28 23:39:31 +00:00
if not v: return target
id = None
if any(isinstance(target, cls) for cls in {Submission, Comment}):
2022-10-28 23:39:31 +00:00
id = target.author_id
elif isinstance(target, User):
id = target.id
else:
raise TypeError("add_block_props only supports non-None submissions, 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
block = g.db.query(UserBlock).filter(
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
def add_vote_props(target:Union[Submission, Comment], v:Optional[User], vote_cls):
2022-10-28 23:39:31 +00:00
if hasattr(target, 'voted'): return target
vt = g.db.query(vote_cls.vote_type).filter_by(user_id=v.id)
if vote_cls == Vote:
vt = vt.filter_by(submission_id=target.id)
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
def add_vote_and_block_props(target:Union[Submission, Comment], v:Optional[User], 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
2022-11-25 21:43:35 +00:00
def get_comments(cids:Iterable[int], v:Optional[User]=None, extra:Optional[Callable[[Query], Query]]=None) -> List[Comment]:
2022-05-04 23:09:46 +00:00
if not cids: return []
if v:
2022-11-25 21:43:35 +00:00
output = get_comments_v_properties(v, True, None, Comment.id.in_(cids))[1] # TODO: support 'extra' for get_comments_v_properties
2022-05-04 23:09:46 +00:00
else:
2022-11-25 21:43:35 +00:00
output = g.db.query(Comment).join(Comment.author)
if extra: output = extra(output)
output = output.filter(User.shadowbanned == None, Comment.id.in_(cids)).all()
2022-05-04 23:09:46 +00:00
return sorted(output, key=lambda x: cids.index(x.id))
def get_comments_v_properties(v:User, include_shadowbanned=True, should_keep_func:Optional[Callable[[Comment], bool]]=None, *criterion):
if not v:
raise TypeError("A user is required")
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()
comments = g.db.query(
Comment,
votes.c.vote_type,
blocking.c.target_id,
blocked.c.target_id,
)
if not include_shadowbanned and not v.can_see_shadowbanned:
comments = comments.join(Comment.author).filter(User.shadowbanned == None)
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)
2022-10-28 00:44:34 +00:00
def get_sub_by_name(sub:str, v:Optional[User]=None, graceful=False) -> Optional[Sub]:
2022-10-05 10:30:44 +00:00
if not sub:
if graceful: return None
else: abort(404)
sub = sub.replace('/h/', '').strip().lower()
if not sub:
if graceful: return None
else: abort(404)
sub = g.db.get(Sub, sub)
if not sub:
if graceful: return None
else: abort(404)
return sub
2023-01-25 03:18:17 +00:00
@cache.memoize(timeout=8640000)
def get_profile_picture(identifier:Union[int, str]) -> str:
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'