rDrama/files/helpers/get.py

309 lines
7.0 KiB
Python
Raw Normal View History

2022-10-28 00:44:34 +00:00
from typing import List, Optional, Union
2022-05-04 23:09:46 +00:00
from files.classes import *
from flask import g
2022-10-28 00:44:34 +00:00
def get_id(username:str, graceful=False) -> Optional[int]:
2022-05-04 23:09:46 +00:00
username = username.replace('\\', '').replace('_', '\_').replace('%', '').strip()
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]
2022-10-28 00:44:34 +00:00
def get_user(username:str, v:Optional[User]=None, graceful=False, rendered=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 = username.replace('\\', '').replace('_', '\_').replace('%', '').replace('(', '').replace(')', '').strip()
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
if rendered and v and include_blocks:
2022-07-03 12:12:33 +00:00
if v.id == user.id:
user.is_blocked = False
user.is_blocking = False
else:
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
2022-05-04 23:09:46 +00:00
return user
2022-10-28 00:44:34 +00:00
def get_users(usernames:List[str], graceful=False) -> List[User]:
def clean(n):
return n.replace('\\', '').replace('_', '\_').replace('%', '').strip()
2022-08-17 19:25:57 +00:00
usernames = [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_(
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
2022-10-28 00:44:34 +00:00
def get_account(id:Union[str, int], v=None, graceful=False, include_blocks=False, include_shadowbanned=True) -> Optional[User]:
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 v and include_blocks:
2022-05-04 23:09:46 +00:00
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
return user
2022-10-28 00:44:34 +00:00
def get_post(i:Union[str, int], v=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
2022-05-04 23:09:46 +00:00
).join(
vt,
vt.c.submission_id == Submission.id,
isouter=True
).join(
blocking,
blocking.c.target_id == Submission.author_id,
isouter=True
)
2022-06-24 13:19:53 +00:00
post=post.one_or_none()
2022-05-04 23:09:46 +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-10-28 00:44:34 +00:00
def get_posts(pids:List[int], v:Optional[User]=None) -> List[Submission]:
2022-05-04 23:09:46 +00:00
if not pids:
return []
if v:
vt = g.db.query(Vote.vote_type, Vote.submission_id).filter(
2022-05-04 23:09:46 +00:00
Vote.submission_id.in_(pids),
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)
).join(
vt, vt.c.submission_id==Submission.id, isouter=True
).join(
blocking,
blocking.c.target_id == Submission.author_id,
isouter=True
).join(
blocked,
blocked.c.user_id == Submission.author_id,
isouter=True
).all()
2022-05-04 23:09:46 +00:00
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
else:
output = g.db.query(Submission,).filter(Submission.id.in_(pids)).all()
return sorted(output, key=lambda x: pids.index(x.id))
2022-10-28 00:44:34 +00:00
def get_comment(i:Union[str, int], v=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
2022-06-24 13:19:53 +00:00
if v:
2022-05-04 23:09:46 +00:00
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()
vt = g.db.query(CommentVote.vote_type).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
2022-05-04 23:09:46 +00:00
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
return comment
2022-10-28 00:44:34 +00:00
def get_comments(cids:List[int], v:Optional[User]=None) -> List[Comment]:
2022-05-04 23:09:46 +00:00
if not cids: return []
if v:
votes = g.db.query(CommentVote.vote_type, CommentVote.comment_id).filter_by(user_id=v.id).subquery()
2022-05-04 23:09:46 +00:00
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,
).filter(Comment.id.in_(cids))
comments = comments.join(
votes,
votes.c.comment_id == Comment.id,
isouter=True
).join(
blocking,
blocking.c.target_id == Comment.author_id,
isouter=True
).join(
blocked,
blocked.c.user_id == Comment.author_id,
isouter=True
).all()
output = []
for c in comments:
comment = c[0]
comment.voted = c[1] or 0
comment.is_blocking = c[2] or 0
comment.is_blocked = c[3] or 0
output.append(comment)
else:
2022-07-03 06:12:53 +00:00
output = g.db.query(Comment).join(Comment.author).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))
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