forked from rDrama/rDrama
get: functions to add blocks and vote properties
get: don't attempt to re-query if blocks or voted properties are already presentmaster
parent
16ba59d8b3
commit
8a8e6f2233
|
@ -47,26 +47,7 @@ def get_user(username:str, v:Optional[User]=None, graceful=False, rendered=False
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
if rendered and v and include_blocks:
|
if rendered and v and include_blocks:
|
||||||
if v.id == user.id:
|
user = add_block_props(user, v)
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
def get_users(usernames:List[str], graceful=False) -> List[User]:
|
def get_users(usernames:List[str], graceful=False) -> List[User]:
|
||||||
|
@ -89,7 +70,7 @@ def get_users(usernames:List[str], graceful=False) -> List[User]:
|
||||||
|
|
||||||
return users
|
return users
|
||||||
|
|
||||||
def get_account(id:Union[str, int], v=None, graceful=False, include_blocks=False, include_shadowbanned=True) -> Optional[User]:
|
def get_account(id:Union[str, int], v:Optional[User]=None, graceful=False, include_blocks=False, include_shadowbanned=True) -> Optional[User]:
|
||||||
try:
|
try:
|
||||||
id = int(id)
|
id = int(id)
|
||||||
except:
|
except:
|
||||||
|
@ -102,26 +83,12 @@ def get_account(id:Union[str, int], v=None, graceful=False, include_blocks=False
|
||||||
if not graceful: abort(404)
|
if not graceful: abort(404)
|
||||||
else: return None
|
else: return None
|
||||||
|
|
||||||
if v and include_blocks:
|
if include_blocks:
|
||||||
block = g.db.query(UserBlock).filter(
|
user = add_block_props(user, v)
|
||||||
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
|
return user
|
||||||
|
|
||||||
|
|
||||||
def get_post(i:Union[str, int], v=None, graceful=False) -> Optional[Submission]:
|
def get_post(i:Union[str, int], v:Optional[User]=None, graceful=False) -> Optional[Submission]:
|
||||||
try: i = int(i)
|
try: i = int(i)
|
||||||
except:
|
except:
|
||||||
if graceful: return None
|
if graceful: return None
|
||||||
|
@ -213,7 +180,7 @@ def get_posts(pids:List[int], v:Optional[User]=None) -> List[Submission]:
|
||||||
|
|
||||||
return sorted(output, key=lambda x: pids.index(x.id))
|
return sorted(output, key=lambda x: pids.index(x.id))
|
||||||
|
|
||||||
def get_comment(i:Union[str, int], v=None, graceful=False) -> Optional[Comment]:
|
def get_comment(i:Union[str, int], v:Optional[User]=None, graceful=False) -> Optional[Comment]:
|
||||||
try: i = int(i)
|
try: i = int(i)
|
||||||
except:
|
except:
|
||||||
if graceful: return None
|
if graceful: return None
|
||||||
|
@ -228,27 +195,61 @@ def get_comment(i:Union[str, int], v=None, graceful=False) -> Optional[Comment]:
|
||||||
if graceful: return None
|
if graceful: return None
|
||||||
else: abort(404)
|
else: abort(404)
|
||||||
|
|
||||||
if v:
|
return add_vote_and_block_props(comment, v, CommentVote)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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(
|
block = g.db.query(UserBlock).filter(
|
||||||
or_(
|
or_(
|
||||||
and_(
|
and_(
|
||||||
UserBlock.user_id == v.id,
|
UserBlock.user_id == v.id,
|
||||||
UserBlock.target_id == comment.author_id
|
UserBlock.target_id == id
|
||||||
),
|
),
|
||||||
and_(
|
and_(
|
||||||
UserBlock.user_id == comment.author_id,
|
UserBlock.user_id == id,
|
||||||
UserBlock.target_id == v.id
|
UserBlock.target_id == v.id
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
).first()
|
).first()
|
||||||
|
target.is_blocking = block and block.user_id == v.id
|
||||||
|
target.is_blocked = block and block.target_id == v.id
|
||||||
|
return target
|
||||||
|
|
||||||
vt = g.db.query(CommentVote.vote_type).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
|
def add_vote_props(target:Union[Submission, Comment], v:Optional[User], vote_cls):
|
||||||
comment.is_blocking = block and block.user_id == v.id
|
if hasattr(target, 'voted'): return target
|
||||||
comment.is_blocked = block and block.target_id == v.id
|
|
||||||
comment.voted = vt.vote_type if vt else 0
|
|
||||||
|
|
||||||
return comment
|
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):
|
||||||
|
if not v: return target
|
||||||
|
target = add_block_props(target, v)
|
||||||
|
return add_vote_props(target, v, vote_cls)
|
||||||
|
|
||||||
def get_comments(cids:List[int], v:Optional[User]=None) -> List[Comment]:
|
def get_comments(cids:List[int], v:Optional[User]=None) -> List[Comment]:
|
||||||
if not cids: return []
|
if not cids: return []
|
||||||
|
|
Loading…
Reference in New Issue