rDrama/files/helpers/get.py

271 lines
6.2 KiB
Python
Raw Normal View History

2021-08-04 15:35:10 +00:00
from files.classes import *
2021-07-21 01:12:26 +00:00
from flask import g
2021-07-25 02:23:11 +00:00
def get_user(username, v=None, graceful=False):
2021-07-21 01:12:26 +00:00
username = username.replace('\\', '')
username = username.replace('_', '\_')
username = username.replace('%', '')
2021-07-25 02:23:11 +00:00
user = g.db.query(
2021-07-21 01:12:26 +00:00
User
).filter(
or_(
User.username.ilike(username),
User.original_username.ilike(username)
)
).first()
if not user:
if not graceful:
abort(404)
else:
return None
if v:
2021-09-17 08:29:05 +00:00
block = g.db.query(UserBlock).options(lazyload('*')).filter(
2021-07-21 01:12:26 +00:00
or_(
and_(
UserBlock.user_id == v.id,
UserBlock.target_id == user.id
),
and_(UserBlock.user_id == user.id,
UserBlock.target_id == v.id
)
)
).first()
2021-09-22 18:36:03 +00:00
user.is_blocking = block and block.user_id == v.id
user.is_blocked = block and block.target_id == v.id
2021-07-21 01:12:26 +00:00
return user
2021-07-30 08:37:21 +00:00
def get_account(id, v=None):
2021-07-21 01:12:26 +00:00
2021-09-17 08:29:05 +00:00
user = g.db.query(User).options(lazyload('*')).filter_by(id = id).first()
2021-07-30 08:42:00 +00:00
2021-07-21 01:12:26 +00:00
if not user:
2021-07-30 08:43:59 +00:00
try: id = int(str(id), 36)
except: abort(404)
2021-09-17 08:29:05 +00:00
user = g.db.query(User).options(lazyload('*')).filter_by(id = id).first()
2021-07-30 08:37:21 +00:00
if not user: abort(404)
2021-07-21 01:12:26 +00:00
if v:
2021-09-17 08:29:05 +00:00
block = g.db.query(UserBlock).options(lazyload('*')).filter(
2021-07-21 01:12:26 +00:00
or_(
and_(
UserBlock.user_id == v.id,
UserBlock.target_id == user.id
),
and_(UserBlock.user_id == user.id,
UserBlock.target_id == v.id
)
)
).first()
2021-09-22 18:36:03 +00:00
user.is_blocking = block and block.user_id == v.id
user.is_blocked = block and block.target_id == v.id
2021-07-21 01:12:26 +00:00
return user
2021-10-02 11:34:37 +00:00
def get_post(i, v=None, graceful=False):
2021-07-21 01:12:26 +00:00
if v:
2021-09-17 08:29:05 +00:00
vt = g.db.query(Vote).options(lazyload('*')).filter_by(
user_id=v.id, submission_id=i).subquery()
blocking = v.blocking.subquery()
2021-07-21 01:12:26 +00:00
2021-07-25 02:23:11 +00:00
items = g.db.query(
2021-07-21 01:12:26 +00:00
Submission,
vt.c.vote_type,
blocking.c.id,
)
items=items.filter(Submission.id == i
).join(
vt,
vt.c.submission_id == Submission.id,
isouter=True
).join(
blocking,
blocking.c.target_id == Submission.author_id,
isouter=True
2021-07-26 03:16:08 +00:00
)
items=items.first()
2021-07-21 01:12:26 +00:00
if not items and not graceful:
abort(404)
x = items[0]
2021-08-07 23:03:15 +00:00
x.voted = items[1] or 0
2021-09-22 18:36:03 +00:00
x.is_blocking = items[2] or 0
2021-07-21 01:12:26 +00:00
else:
2021-07-25 02:23:11 +00:00
items = g.db.query(
Submission
2021-07-21 01:12:26 +00:00
).filter(Submission.id == i).first()
if not items and not graceful:
abort(404)
x=items
return x
2021-07-25 02:35:53 +00:00
def get_posts(pids, v=None):
2021-07-21 01:12:26 +00:00
if not pids:
return []
pids=tuple(pids)
if v:
2021-09-17 08:29:05 +00:00
vt = g.db.query(Vote).options(lazyload('*')).filter(
2021-07-21 01:12:26 +00:00
Vote.submission_id.in_(pids),
Vote.user_id==v.id
).subquery()
2021-07-21 01:12:26 +00:00
blocking = v.blocking.subquery()
blocked = v.blocked.subquery()
2021-07-21 01:12:26 +00:00
query = g.db.query(
Submission,
vt.c.vote_type,
blocking.c.id,
blocked.c.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
2021-07-25 02:53:09 +00:00
).join(
blocked,
blocked.c.user_id == Submission.author_id,
isouter=True
2021-07-25 02:23:11 +00:00
).all()
2021-07-21 01:12:26 +00:00
output = [p[0] for p in query]
for i in range(len(output)):
2021-08-07 23:03:15 +00:00
output[i].voted = query[i][1] or 0
2021-09-22 18:36:03 +00:00
output[i].is_blocking = query[i][2] or 0
output[i].is_blocked = query[i][3] or 0
2021-07-21 01:12:26 +00:00
else:
2021-09-17 08:29:05 +00:00
output = g.db.query(Submission,).options(lazyload('*')).filter(Submission.id.in_(pids)).all()
2021-07-21 01:12:26 +00:00
return sorted(output, key=lambda x: pids.index(x.id))
2021-10-02 11:34:37 +00:00
def get_comment(i, v=None, graceful=False):
2021-07-21 01:12:26 +00:00
if v:
2021-09-17 08:29:05 +00:00
comment=g.db.query(Comment).options(lazyload('*')).filter(Comment.id == i).first()
2021-07-21 01:12:26 +00:00
2021-08-06 12:44:24 +00:00
if not comment and not graceful: abort(404)
2021-07-21 01:12:26 +00:00
2021-09-17 08:29:05 +00:00
block = g.db.query(UserBlock).options(lazyload('*')).filter(
2021-07-21 01:12:26 +00:00
or_(
and_(
UserBlock.user_id == v.id,
2021-08-06 12:44:24 +00:00
UserBlock.target_id == comment.author_id
2021-07-21 01:12:26 +00:00
),
2021-08-06 12:44:24 +00:00
and_(UserBlock.user_id == comment.author_id,
2021-07-21 01:12:26 +00:00
UserBlock.target_id == v.id
)
)
).first()
2021-09-17 08:29:05 +00:00
vts = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment.id)
vt = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment.id).first()
2021-09-22 18:36:03 +00:00
comment.is_blocking = block and block.user_id == v.id
comment.is_blocked = block and block.target_id == v.id
2021-08-07 23:03:15 +00:00
comment.voted = vt.vote_type if vt else 0
2021-07-21 01:12:26 +00:00
else:
2021-09-17 08:29:05 +00:00
comment = g.db.query(Comment).options(lazyload('*')).filter(Comment.id == i).first()
2021-08-06 12:44:24 +00:00
if not comment and not graceful:abort(404)
2021-07-21 01:12:26 +00:00
2021-08-06 12:44:24 +00:00
return comment
2021-07-21 01:12:26 +00:00
2021-09-14 18:18:28 +00:00
def get_comments(cids, v=None, load_parent=False):
2021-07-21 01:12:26 +00:00
2021-07-29 06:57:42 +00:00
if not cids: return []
2021-07-21 01:12:26 +00:00
cids=tuple(cids)
2021-08-06 12:44:24 +00:00
if v:
votes = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id).subquery()
2021-08-06 12:44:24 +00:00
blocking = v.blocking.subquery()
2021-08-06 12:44:24 +00:00
blocked = v.blocked.subquery()
2021-07-21 01:12:26 +00:00
2021-08-06 12:44:24 +00:00
comments = g.db.query(
Comment,
votes.c.vote_type,
blocking.c.id,
blocked.c.id,
).filter(Comment.id.in_(cids))
2021-09-14 18:18:28 +00:00
if not (v and v.shadowbanned) and not (v and v.admin_level == 6):
2021-09-30 19:40:33 +00:00
shadowbanned = [x[0] for x in g.db.query(User.id).options(lazyload('*')).filter(User.shadowbanned != None).all()]
2021-09-28 19:45:17 +00:00
comments = comments.filter(Comment.author_id.notin_(shadowbanned))
2021-09-14 18:18:28 +00:00
2021-08-06 12:44:24 +00:00
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()
2021-07-21 01:12:26 +00:00
2021-08-06 12:44:24 +00:00
output = []
for c in comments:
comment = c[0]
2021-08-07 23:03:15 +00:00
comment.voted = c[1] or 0
2021-09-22 18:36:03 +00:00
comment.is_blocking = c[2] or 0
comment.is_blocked = c[3] or 0
2021-08-06 12:44:24 +00:00
output.append(comment)
2021-07-21 01:12:26 +00:00
2021-08-06 12:44:24 +00:00
else:
2021-09-30 19:40:33 +00:00
shadowbanned = [x[0] for x in g.db.query(User.id).options(lazyload('*')).filter(User.shadowbanned != None).all()]
2021-09-28 19:39:51 +00:00
output = g.db.query(Comment).options(lazyload('*')).filter(Comment.id.in_(cids), Comment.author_id.notin_(shadowbanned)).all()
2021-07-21 01:12:26 +00:00
2021-08-07 15:19:53 +00:00
if load_parent:
parents = [x.parent_comment_id for x in output if x.parent_comment_id]
parents = get_comments(parents, v=v)
2021-08-07 15:25:54 +00:00
parents = {x.id: x for x in parents}
2021-08-07 15:52:02 +00:00
for c in output: c.sex = parents.get(c.parent_comment_id)
2021-08-07 15:19:53 +00:00
2021-08-06 12:44:24 +00:00
return sorted(output, key=lambda x: cids.index(x.id))
2021-07-21 01:12:26 +00:00
def get_domain(s):
parts = s.split(".")
domain_list = set([])
for i in range(len(parts)):
new_domain = parts[i]
for j in range(i + 1, len(parts)):
new_domain += "." + parts[j]
domain_list.add(new_domain)
domain_list = tuple(list(domain_list))
2021-10-02 19:07:30 +00:00
doms = [x for x in g.db.query(BannedDomain).options(lazyload('*')).filter(BannedDomain.domain.in_(domain_list)).all()]
2021-07-21 01:12:26 +00:00
if not doms:
return None
doms = sorted(doms, key=lambda x: len(x.domain), reverse=True)
2021-07-25 14:23:53 +00:00
return doms[0]