2021-10-15 14:08:27 +00:00
|
|
|
from files.classes import *
|
|
|
|
from flask import g
|
|
|
|
|
2021-11-22 23:25:33 +00:00
|
|
|
|
|
|
|
def get_id(username, v=None, graceful=False):
|
2021-11-30 18:22:31 +00:00
|
|
|
|
2022-03-04 22:46:20 +00:00
|
|
|
username = username.replace('\\', '').replace('_', '\_').replace('%', '').strip()
|
2021-11-22 23:25:33 +00:00
|
|
|
|
|
|
|
user = g.db.query(
|
|
|
|
User.id
|
|
|
|
).filter(
|
|
|
|
or_(
|
|
|
|
User.username.ilike(username),
|
|
|
|
User.original_username.ilike(username)
|
|
|
|
)
|
2022-01-02 00:06:46 +00:00
|
|
|
).one_or_none()
|
2021-11-22 23:25:33 +00:00
|
|
|
|
|
|
|
if not user:
|
|
|
|
if not graceful:
|
|
|
|
abort(404)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return user[0]
|
|
|
|
|
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
def get_user(username, v=None, graceful=False):
|
|
|
|
|
2021-11-30 18:22:31 +00:00
|
|
|
if not username:
|
|
|
|
if not graceful: abort(404)
|
|
|
|
else: return None
|
|
|
|
|
2022-02-10 20:35:16 +00:00
|
|
|
username = username.replace('\\', '').replace('_', '\_').replace('%', '').strip()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
user = g.db.query(
|
|
|
|
User
|
2021-11-06 15:52:48 +00:00
|
|
|
).filter(
|
2021-10-15 14:08:27 +00:00
|
|
|
or_(
|
|
|
|
User.username.ilike(username),
|
|
|
|
User.original_username.ilike(username)
|
|
|
|
)
|
2022-01-02 00:06:46 +00:00
|
|
|
).one_or_none()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
if not user:
|
2021-11-30 18:22:31 +00:00
|
|
|
if not graceful: abort(404)
|
|
|
|
else: return None
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
if v:
|
2021-11-06 15:52:48 +00:00
|
|
|
block = g.db.query(UserBlock).filter(
|
2021-10-15 14:08:27 +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
|
|
|
|
)
|
|
|
|
)
|
2022-01-06 01:24:03 +00:00
|
|
|
).first()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
user.is_blocking = block and block.user_id == v.id
|
|
|
|
user.is_blocked = block and block.target_id == v.id
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
def get_account(id, v=None):
|
|
|
|
|
2022-02-26 14:21:07 +00:00
|
|
|
try: id = int(id)
|
|
|
|
except: abort(404)
|
|
|
|
|
2022-01-02 00:06:46 +00:00
|
|
|
user = g.db.query(User).filter_by(id = id).one_or_none()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-02-04 08:59:12 +00:00
|
|
|
if not user: abort(404)
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
if v:
|
2021-11-06 15:52:48 +00:00
|
|
|
block = g.db.query(UserBlock).filter(
|
2021-10-15 14:08:27 +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
|
|
|
|
)
|
|
|
|
)
|
2022-02-26 19:01:32 +00:00
|
|
|
).first()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
user.is_blocking = block and block.user_id == v.id
|
|
|
|
user.is_blocked = block and block.target_id == v.id
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
def get_post(i, v=None, graceful=False):
|
|
|
|
|
|
|
|
if v:
|
2021-11-06 15:52:48 +00:00
|
|
|
vt = g.db.query(Vote).filter_by(
|
2021-10-15 14:08:27 +00:00
|
|
|
user_id=v.id, submission_id=i).subquery()
|
|
|
|
blocking = v.blocking.subquery()
|
|
|
|
|
|
|
|
items = g.db.query(
|
|
|
|
Submission,
|
|
|
|
vt.c.vote_type,
|
2022-02-14 22:50:27 +00:00
|
|
|
blocking.c.target_id,
|
2021-11-06 15:52:48 +00:00
|
|
|
)
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2022-01-02 00:06:46 +00:00
|
|
|
items=items.one_or_none()
|
2022-01-28 18:33:49 +00:00
|
|
|
|
|
|
|
if not items:
|
|
|
|
if graceful: return None
|
|
|
|
else: abort(404)
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
x = items[0]
|
|
|
|
x.voted = items[1] or 0
|
|
|
|
x.is_blocking = items[2] or 0
|
|
|
|
else:
|
|
|
|
items = g.db.query(
|
|
|
|
Submission
|
2022-01-02 00:06:46 +00:00
|
|
|
).filter(Submission.id == i).one_or_none()
|
2022-01-28 18:33:49 +00:00
|
|
|
if not items:
|
|
|
|
if graceful: return None
|
|
|
|
else: abort(404)
|
2021-10-15 14:08:27 +00:00
|
|
|
x=items
|
|
|
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
def get_posts(pids, v=None):
|
|
|
|
|
|
|
|
if not pids:
|
|
|
|
return []
|
|
|
|
|
|
|
|
if v:
|
2021-11-06 15:52:48 +00:00
|
|
|
vt = g.db.query(Vote).filter(
|
2021-10-15 14:08:27 +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,
|
2022-02-14 22:50:27 +00:00
|
|
|
blocking.c.target_id,
|
|
|
|
blocked.c.target_id,
|
2021-11-06 15:52:48 +00:00
|
|
|
).filter(
|
2021-10-15 14:08:27 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
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:
|
2021-11-06 15:52:48 +00:00
|
|
|
output = g.db.query(Submission,).filter(Submission.id.in_(pids)).all()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
return sorted(output, key=lambda x: pids.index(x.id))
|
|
|
|
|
|
|
|
def get_comment(i, v=None, graceful=False):
|
|
|
|
|
|
|
|
if v:
|
|
|
|
|
2021-12-26 01:03:21 +00:00
|
|
|
comment=g.db.query(Comment).filter(Comment.id == i).one_or_none()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
if not comment and not graceful: abort(404)
|
|
|
|
|
2021-11-06 15:52:48 +00:00
|
|
|
block = g.db.query(UserBlock).filter(
|
2021-10-15 14:08:27 +00:00
|
|
|
or_(
|
|
|
|
and_(
|
|
|
|
UserBlock.user_id == v.id,
|
|
|
|
UserBlock.target_id == comment.author_id
|
|
|
|
),
|
2022-02-16 02:15:17 +00:00
|
|
|
and_(
|
|
|
|
UserBlock.user_id == comment.author_id,
|
|
|
|
UserBlock.target_id == v.id
|
|
|
|
)
|
2021-10-15 14:08:27 +00:00
|
|
|
)
|
2022-02-26 19:01:32 +00:00
|
|
|
).first()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-11-06 15:52:48 +00:00
|
|
|
vts = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id)
|
2022-01-02 00:06:46 +00:00
|
|
|
vt = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
|
2021-10-15 14:08:27 +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
|
|
|
|
|
|
|
|
else:
|
2022-01-02 00:06:46 +00:00
|
|
|
comment = g.db.query(Comment).filter(Comment.id == i).one_or_none()
|
2021-10-15 14:08:27 +00:00
|
|
|
if not comment and not graceful:abort(404)
|
|
|
|
|
|
|
|
return comment
|
|
|
|
|
|
|
|
|
2021-12-17 17:55:11 +00:00
|
|
|
def get_comments(cids, v=None, load_parent=False):
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
if not cids: return []
|
|
|
|
|
|
|
|
if v:
|
2021-11-06 15:52:48 +00:00
|
|
|
votes = g.db.query(CommentVote).filter_by(user_id=v.id).subquery()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
blocking = v.blocking.subquery()
|
|
|
|
|
|
|
|
blocked = v.blocked.subquery()
|
|
|
|
|
|
|
|
comments = g.db.query(
|
|
|
|
Comment,
|
|
|
|
votes.c.vote_type,
|
2022-02-14 22:50:27 +00:00
|
|
|
blocking.c.target_id,
|
|
|
|
blocked.c.target_id,
|
2021-11-06 15:52:48 +00:00
|
|
|
).filter(Comment.id.in_(cids))
|
2021-12-17 17:55:11 +00:00
|
|
|
|
2022-04-15 16:28:08 +00:00
|
|
|
if not (v and (v.shadowbanned or v.admin_level > 2)):
|
2021-11-06 00:33:32 +00:00
|
|
|
comments = comments.join(User, User.id == Comment.author_id).filter(User.shadowbanned == None)
|
2021-10-15 14:08:27 +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()
|
|
|
|
|
|
|
|
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:
|
2021-11-06 15:52:48 +00:00
|
|
|
output = g.db.query(Comment).join(User, User.id == Comment.author_id).filter(User.shadowbanned == None, Comment.id.in_(cids)).all()
|
2021-10-15 14:08:27 +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)
|
|
|
|
parents = {x.id: x for x in parents}
|
|
|
|
for c in output: c.sex = parents.get(c.parent_comment_id)
|
|
|
|
|
|
|
|
return sorted(output, key=lambda x: cids.index(x.id))
|
|
|
|
|
|
|
|
|
|
|
|
def get_domain(s):
|
|
|
|
|
|
|
|
parts = s.split(".")
|
2022-01-07 21:03:14 +00:00
|
|
|
domain_list = set()
|
2021-10-15 14:08:27 +00:00
|
|
|
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)
|
|
|
|
|
2021-11-06 15:52:48 +00:00
|
|
|
doms = [x for x in g.db.query(BannedDomain).filter(BannedDomain.domain.in_(domain_list)).all()]
|
2021-10-15 14:08:27 +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]
|