Port get.py improvements from upstream.
Generally standardizes the get_* helpers: - Adds type hinting. - Deduplicates block property addition. - Respects `graceful` in more contexts. - More resilient to invalid user input / less boilerplate necessary at call-sites.
This commit is contained in:
parent
6b832aba99
commit
9953c5763c
3 changed files with 193 additions and 160 deletions
|
@ -1,15 +1,19 @@
|
|||
from files.classes import *
|
||||
from files.helpers.strings import sql_ilike_clean
|
||||
from typing import Iterable, List, Optional, Type, Union
|
||||
|
||||
from flask import g
|
||||
from sqlalchemy import and_, any_, or_
|
||||
|
||||
from files.classes import *
|
||||
from files.helpers.const import AUTOJANNY_ID
|
||||
from files.helpers.strings import sql_ilike_clean
|
||||
|
||||
|
||||
def get_id(username, v=None, graceful=False):
|
||||
|
||||
def get_id(
|
||||
username:str,
|
||||
graceful:bool=False) -> Optional[int]:
|
||||
username = sql_ilike_clean(username)
|
||||
|
||||
user = g.db.query(
|
||||
User.id
|
||||
).filter(
|
||||
user = g.db.query(User.id).filter(
|
||||
or_(
|
||||
User.username.ilike(username),
|
||||
User.original_username.ilike(username)
|
||||
|
@ -17,25 +21,23 @@ def get_id(username, v=None, graceful=False):
|
|||
).one_or_none()
|
||||
|
||||
if not user:
|
||||
if not graceful:
|
||||
if graceful: return None
|
||||
abort(404)
|
||||
else:
|
||||
return None
|
||||
|
||||
return user[0]
|
||||
|
||||
|
||||
def get_user(username, v=None, graceful=False):
|
||||
|
||||
if not username:
|
||||
if not graceful: abort(404)
|
||||
else: return None
|
||||
|
||||
def get_user(
|
||||
username:Optional[str],
|
||||
v:Optional[User]=None,
|
||||
graceful:bool=False,
|
||||
include_blocks:bool=False) -> Optional[User]:
|
||||
username = sql_ilike_clean(username)
|
||||
if not username:
|
||||
if graceful: return None
|
||||
abort(404)
|
||||
|
||||
user = g.db.query(
|
||||
User
|
||||
).filter(
|
||||
user = g.db.query(User).filter(
|
||||
or_(
|
||||
User.username.ilike(username),
|
||||
User.original_username.ilike(username)
|
||||
|
@ -43,34 +45,23 @@ def get_user(username, v=None, graceful=False):
|
|||
).one_or_none()
|
||||
|
||||
if not user:
|
||||
if not graceful: abort(404)
|
||||
else: return None
|
||||
if graceful: return None
|
||||
abort(404)
|
||||
|
||||
if v:
|
||||
block = g.db.query(UserBlock).filter(
|
||||
or_(
|
||||
and_(
|
||||
UserBlock.user_id == v.id,
|
||||
UserBlock.target_id == user.id
|
||||
),
|
||||
and_(UserBlock.user_id == user.id,
|
||||
UserBlock.target_id == v.id
|
||||
)
|
||||
)
|
||||
).first()
|
||||
|
||||
user.is_blocking = block and block.user_id == v.id
|
||||
user.is_blocked = block and block.target_id == v.id
|
||||
if v and include_blocks:
|
||||
user = _add_block_props(user, v)
|
||||
|
||||
return user
|
||||
|
||||
def get_users(usernames, v=None, graceful=False):
|
||||
if not usernames:
|
||||
if not graceful: abort(404)
|
||||
else: return []
|
||||
|
||||
def get_users(
|
||||
usernames:Iterable[str],
|
||||
graceful:bool=False) -> List[User]:
|
||||
if not usernames: return []
|
||||
usernames = [ sql_ilike_clean(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_(
|
||||
User.username == any_(usernames),
|
||||
|
@ -78,58 +69,55 @@ def get_users(usernames, v=None, graceful=False):
|
|||
)
|
||||
).all()
|
||||
|
||||
if not users:
|
||||
if not graceful: abort(404)
|
||||
else: return []
|
||||
if len(users) != len(usernames) and not graceful:
|
||||
abort(404)
|
||||
|
||||
return users
|
||||
|
||||
def get_account(id, v=None):
|
||||
|
||||
try: id = int(id)
|
||||
except: abort(404)
|
||||
def get_account(
|
||||
id:Union[str,int],
|
||||
v:Optional[User]=None,
|
||||
graceful:bool=False,
|
||||
include_blocks:bool=False) -> Optional[User]:
|
||||
try:
|
||||
id = int(id)
|
||||
except:
|
||||
if graceful: return None
|
||||
abort(404)
|
||||
|
||||
user = g.db.query(User).filter_by(id = id).one_or_none()
|
||||
user = g.db.get(User, id)
|
||||
if not user:
|
||||
if graceful: return None
|
||||
abort(404)
|
||||
|
||||
if not user: abort(404)
|
||||
|
||||
if v:
|
||||
block = g.db.query(UserBlock).filter(
|
||||
or_(
|
||||
and_(
|
||||
UserBlock.user_id == v.id,
|
||||
UserBlock.target_id == user.id
|
||||
),
|
||||
and_(UserBlock.user_id == user.id,
|
||||
UserBlock.target_id == v.id
|
||||
)
|
||||
)
|
||||
).first()
|
||||
|
||||
user.is_blocking = block and block.user_id == v.id
|
||||
user.is_blocked = block and block.target_id == v.id
|
||||
if v and include_blocks:
|
||||
user = _add_block_props(user, v)
|
||||
|
||||
return user
|
||||
|
||||
|
||||
def get_post(i, v=None, graceful=False):
|
||||
def get_post(
|
||||
i:Union[str,int],
|
||||
v:Optional[User]=None,
|
||||
graceful:bool=False) -> Optional[Submission]:
|
||||
try: i = int(i)
|
||||
except:
|
||||
if graceful: return None
|
||||
else: abort(404)
|
||||
abort(404)
|
||||
|
||||
if v:
|
||||
vt = g.db.query(Vote).filter_by(
|
||||
user_id=v.id, submission_id=i).subquery()
|
||||
blocking = v.blocking.subquery()
|
||||
|
||||
items = g.db.query(
|
||||
post = g.db.query(
|
||||
Submission,
|
||||
vt.c.vote_type,
|
||||
blocking.c.target_id,
|
||||
)
|
||||
|
||||
items=items.filter(Submission.id == i
|
||||
post = post.filter(Submission.id == i
|
||||
).join(
|
||||
vt,
|
||||
vt.c.submission_id == Submission.id,
|
||||
|
@ -139,35 +127,32 @@ def get_post(i, v=None, graceful=False):
|
|||
blocking.c.target_id == Submission.author_id,
|
||||
isouter=True
|
||||
)
|
||||
post = post.one_or_none()
|
||||
|
||||
items=items.one_or_none()
|
||||
|
||||
if not items:
|
||||
if not post:
|
||||
if graceful: return None
|
||||
else: abort(404)
|
||||
|
||||
x = items[0]
|
||||
x.voted = items[1] or 0
|
||||
x.is_blocking = items[2] or 0
|
||||
x = post[0]
|
||||
x.voted = post[1] or 0
|
||||
x.is_blocking = post[2] or 0
|
||||
else:
|
||||
items = g.db.query(
|
||||
Submission
|
||||
).filter(Submission.id == i).one_or_none()
|
||||
if not items:
|
||||
post = g.db.get(Submission, i)
|
||||
if not post:
|
||||
if graceful: return None
|
||||
else: abort(404)
|
||||
x=items
|
||||
x = post
|
||||
|
||||
return x
|
||||
|
||||
|
||||
def get_posts(pids, v=None):
|
||||
|
||||
if not pids:
|
||||
return []
|
||||
def get_posts(
|
||||
pids:Iterable[int],
|
||||
v:Optional[User]=None) -> List[Submission]:
|
||||
if not pids: return []
|
||||
|
||||
if v:
|
||||
vt = g.db.query(Vote).filter(
|
||||
vt = g.db.query(Vote.vote_type, Vote.submission_id).filter(
|
||||
Vote.submission_id.in_(pids),
|
||||
Vote.user_id==v.id
|
||||
).subquery()
|
||||
|
@ -183,67 +168,52 @@ def get_posts(pids, v=None):
|
|||
).filter(
|
||||
Submission.id.in_(pids)
|
||||
).join(
|
||||
vt, vt.c.submission_id==Submission.id, isouter=True
|
||||
vt, vt.c.submission_id == Submission.id, isouter=True
|
||||
).join(
|
||||
blocking,
|
||||
blocking.c.target_id == Submission.author_id,
|
||||
isouter=True
|
||||
blocking, blocking.c.target_id == Submission.author_id, isouter=True
|
||||
).join(
|
||||
blocked,
|
||||
blocked.c.user_id == Submission.author_id,
|
||||
isouter=True
|
||||
).all()
|
||||
|
||||
output = [p[0] for p in query]
|
||||
for i in range(len(output)):
|
||||
output[i].voted = query[i][1] or 0
|
||||
output[i].is_blocking = query[i][2] or 0
|
||||
output[i].is_blocked = query[i][3] or 0
|
||||
blocked, blocked.c.user_id == Submission.author_id, isouter=True
|
||||
)
|
||||
else:
|
||||
output = g.db.query(Submission,).filter(Submission.id.in_(pids)).all()
|
||||
query = g.db.query(Submission).filter(Submission.id.in_(pids))
|
||||
|
||||
results = query.all()
|
||||
|
||||
if v:
|
||||
output = [p[0] for p in results]
|
||||
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
|
||||
else:
|
||||
output = results
|
||||
|
||||
return sorted(output, key=lambda x: pids.index(x.id))
|
||||
|
||||
def get_comment(i, v=None, graceful=False):
|
||||
|
||||
def get_comment(
|
||||
i:Union[str,int],
|
||||
v:Optional[User]=None,
|
||||
graceful:bool=False) -> Optional[Comment]:
|
||||
try: i = int(i)
|
||||
except:
|
||||
if graceful: return None
|
||||
abort(404)
|
||||
if not i:
|
||||
if graceful: return None
|
||||
else: abort(404)
|
||||
|
||||
if v:
|
||||
comment = g.db.get(Comment, i)
|
||||
if not comment:
|
||||
if graceful: return None
|
||||
else: abort(404)
|
||||
|
||||
comment=g.db.query(Comment).filter(Comment.id == i).one_or_none()
|
||||
|
||||
if not comment and not graceful: abort(404)
|
||||
|
||||
block = g.db.query(UserBlock).filter(
|
||||
or_(
|
||||
and_(
|
||||
UserBlock.user_id == v.id,
|
||||
UserBlock.target_id == comment.author_id
|
||||
),
|
||||
and_(
|
||||
UserBlock.user_id == comment.author_id,
|
||||
UserBlock.target_id == v.id
|
||||
)
|
||||
)
|
||||
).first()
|
||||
|
||||
vts = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id)
|
||||
vt = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
|
||||
comment.is_blocking = block and block.user_id == v.id
|
||||
comment.is_blocked = block and block.target_id == v.id
|
||||
comment.voted = vt.vote_type if vt else 0
|
||||
|
||||
else:
|
||||
comment = g.db.query(Comment).filter(Comment.id == i).one_or_none()
|
||||
if not comment and not graceful:abort(404)
|
||||
|
||||
return comment
|
||||
return _add_vote_and_block_props(comment, v, CommentVote)
|
||||
|
||||
|
||||
def get_comments(cids, v=None, load_parent=False):
|
||||
|
||||
def get_comments(
|
||||
cids:Iterable[int],
|
||||
v:Optional[User]=None) -> List[Comment]:
|
||||
if not cids: return []
|
||||
|
||||
if v:
|
||||
|
@ -261,7 +231,8 @@ def get_comments(cids, v=None, load_parent=False):
|
|||
).filter(Comment.id.in_(cids))
|
||||
|
||||
if not (v and (v.shadowbanned or v.admin_level > 1)):
|
||||
comments = comments.join(User, User.id == Comment.author_id).filter(User.shadowbanned == None)
|
||||
comments = comments.join(User, User.id == Comment.author_id) \
|
||||
.filter(User.shadowbanned == None)
|
||||
|
||||
comments = comments.join(
|
||||
votes,
|
||||
|
@ -284,21 +255,18 @@ def get_comments(cids, v=None, load_parent=False):
|
|||
comment.is_blocking = c[2] or 0
|
||||
comment.is_blocked = c[3] or 0
|
||||
output.append(comment)
|
||||
|
||||
else:
|
||||
output = g.db.query(Comment).join(User, User.id == Comment.author_id).filter(User.shadowbanned == None, Comment.id.in_(cids)).all()
|
||||
|
||||
if load_parent:
|
||||
parents = [x.parent_comment_id for x in output if x.parent_comment_id]
|
||||
parents = get_comments(parents, v=v)
|
||||
parents = {x.id: x for x in parents}
|
||||
for c in output: c.sex = parents.get(c.parent_comment_id)
|
||||
output = g.db.query(Comment) \
|
||||
.join(User, User.id == Comment.author_id) \
|
||||
.filter(User.shadowbanned == None, Comment.id.in_(cids)) \
|
||||
.all()
|
||||
|
||||
return sorted(output, key=lambda x: cids.index(x.id))
|
||||
|
||||
|
||||
def get_domain(s):
|
||||
|
||||
# TODO: This function was concisely inlined into posts.py in upstream.
|
||||
# Think it involved adding `tldextract` as a dependency.
|
||||
def get_domain(s:str) -> Optional[BannedDomain]:
|
||||
parts = s.split(".")
|
||||
domain_list = set()
|
||||
for i in range(len(parts)):
|
||||
|
@ -308,7 +276,9 @@ def get_domain(s):
|
|||
|
||||
domain_list.add(new_domain)
|
||||
|
||||
doms = [x for x in g.db.query(BannedDomain).filter(BannedDomain.domain.in_(domain_list)).all()]
|
||||
doms = g.db.query(BannedDomain) \
|
||||
.filter(BannedDomain.domain.in_(domain_list)).all()
|
||||
doms = [x for x in doms]
|
||||
|
||||
if not doms:
|
||||
return None
|
||||
|
@ -316,3 +286,70 @@ def get_domain(s):
|
|||
doms = sorted(doms, key=lambda x: len(x.domain), reverse=True)
|
||||
|
||||
return doms[0]
|
||||
|
||||
|
||||
def _add_block_props(
|
||||
target:Union[Submission, Comment, User],
|
||||
v:Optional[User]):
|
||||
if not v: return target
|
||||
id = None
|
||||
|
||||
if any(isinstance(target, cls) for cls in [Submission, Comment]):
|
||||
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")
|
||||
|
||||
if hasattr(target, 'is_blocking') and hasattr(target, 'is_blocked'):
|
||||
return target
|
||||
|
||||
# users can't block or be blocked by themselves or AutoJanny
|
||||
if v.id == id or id == AUTOJANNY_ID:
|
||||
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:Union[Type[Vote], Type[CommentVote], None]):
|
||||
if hasattr(target, 'voted'): return target
|
||||
|
||||
vt = g.db.query(vote_cls.vote_type).filter_by(user_id=v.id)
|
||||
if vote_cls is Vote:
|
||||
vt = vt.filter_by(submission_id=target.id)
|
||||
elif vote_cls is 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:Union[Type[Vote], Type[CommentVote], None]):
|
||||
if not v: return target
|
||||
target = _add_block_props(target, v)
|
||||
return _add_vote_props(target, v, vote_cls)
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
|
||||
import typing
|
||||
|
||||
# clean strings for searching
|
||||
def sql_ilike_clean(my_str):
|
||||
if my_str is None:
|
||||
return None
|
||||
return my_str.replace(r'\\', '').replace('_', r'\_').replace('%', '').strip()
|
||||
|
||||
# this will also just return a bool verbatim
|
||||
|
|
|
@ -540,7 +540,7 @@ def message2(v, username):
|
|||
return {"error": "You have been permabanned and cannot send messages; " + \
|
||||
"contact modmail if you think this decision was incorrect."}, 403
|
||||
|
||||
user = get_user(username, v=v)
|
||||
user = get_user(username, v=v, include_blocks=True)
|
||||
if hasattr(user, 'is_blocking') and user.is_blocking: return {"error": "You're blocking this user."}, 403
|
||||
|
||||
if v.admin_level <= 1 and hasattr(user, 'is_blocked') and user.is_blocked:
|
||||
|
@ -772,9 +772,7 @@ def visitors(v):
|
|||
@app.get("/@<username>")
|
||||
@auth_desired
|
||||
def u_username(username, v=None):
|
||||
|
||||
|
||||
u = get_user(username, v=v)
|
||||
u = get_user(username, v=v, include_blocks=True)
|
||||
|
||||
|
||||
if username != u.username:
|
||||
|
@ -858,8 +856,7 @@ def u_username(username, v=None):
|
|||
@app.get("/@<username>/comments")
|
||||
@auth_desired
|
||||
def u_username_comments(username, v=None):
|
||||
|
||||
user = get_user(username, v=v)
|
||||
user = get_user(username, v=v, include_blocks=True)
|
||||
|
||||
if username != user.username: return redirect(f'/@{user.username}/comments')
|
||||
|
||||
|
@ -945,8 +942,7 @@ def u_username_comments(username, v=None):
|
|||
@app.get("/@<username>/info")
|
||||
@auth_required
|
||||
def u_username_info(username, v=None):
|
||||
|
||||
user=get_user(username, v=v)
|
||||
user = get_user(username, v=v, include_blocks=True)
|
||||
|
||||
if hasattr(user, 'is_blocking') and user.is_blocking:
|
||||
return {"error": "You're blocking this user."}, 401
|
||||
|
@ -958,8 +954,7 @@ def u_username_info(username, v=None):
|
|||
@app.get("/<id>/info")
|
||||
@auth_required
|
||||
def u_user_id_info(id, v=None):
|
||||
|
||||
user=get_account(id, v=v)
|
||||
user = get_account(id, v=v, include_blocks=True)
|
||||
|
||||
if hasattr(user, 'is_blocking') and user.is_blocking:
|
||||
return {"error": "You're blocking this user."}, 401
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue