2021-07-22 19:19:49 +00:00
|
|
|
from drama.classes import *
|
2021-07-21 01:12:26 +00:00
|
|
|
from flask import g
|
|
|
|
from sqlalchemy.orm import joinedload, aliased
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
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-07-25 02:23:11 +00:00
|
|
|
block = g.db.query(UserBlock).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()
|
|
|
|
|
|
|
|
user._is_blocking = block and block.user_id == v.id
|
|
|
|
user._is_blocked = block and block.target_id == v.id
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
2021-07-30 05:31:38 +00:00
|
|
|
def get_account(id, v=None, graceful=False):
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-25 02:23:11 +00:00
|
|
|
user = g.db.query(User
|
2021-07-21 01:12:26 +00:00
|
|
|
).filter(
|
|
|
|
User.id == id
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if not user:
|
|
|
|
if not graceful:
|
|
|
|
abort(404)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if v:
|
2021-07-25 02:23:11 +00:00
|
|
|
block = g.db.query(UserBlock).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()
|
|
|
|
|
|
|
|
user._is_blocking = block and block.user_id == v.id
|
|
|
|
user._is_blocked = block and block.target_id == v.id
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
2021-07-30 05:31:38 +00:00
|
|
|
def get_post(i, v=None, graceful=False, **kwargs):
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
if v:
|
2021-07-25 02:23:11 +00:00
|
|
|
vt = g.db.query(Vote).filter_by(
|
2021-07-21 01:12:26 +00:00
|
|
|
user_id=v.id, submission_id=i).subquery()
|
|
|
|
blocking = v.blocking.subquery()
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
if v.admin_level>=4:
|
|
|
|
items=items.options(joinedload(Submission.oauth_app))
|
|
|
|
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]
|
|
|
|
x._voted = items[1] or 0
|
2021-07-25 02:24:32 +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:
|
|
|
|
vt = g.db.query(Vote).filter(
|
|
|
|
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.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-07-25 02:23:11 +00:00
|
|
|
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
|
2021-07-21 01:12:26 +00:00
|
|
|
else:
|
2021-07-25 02:23:11 +00:00
|
|
|
output = g.db.query(
|
2021-07-21 01:12:26 +00:00
|
|
|
Submission,
|
|
|
|
).filter(Submission.id.in_(pids)
|
2021-07-25 02:23:11 +00:00
|
|
|
).all()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
return sorted(output, key=lambda x: pids.index(x.id))
|
|
|
|
|
2021-07-30 05:31:38 +00:00
|
|
|
def get_comment(i, v=None, graceful=False, **kwargs):
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
if v:
|
|
|
|
blocking = v.blocking.subquery()
|
|
|
|
blocked = v.blocked.subquery()
|
|
|
|
|
2021-07-29 06:57:42 +00:00
|
|
|
items = g.db.query(Comment)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-29 06:57:42 +00:00
|
|
|
if v.admin_level >=4: items=items.options(joinedload(Comment.oauth_app))
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-29 06:57:42 +00:00
|
|
|
x=items.filter(Comment.id == i).first()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-29 06:57:42 +00:00
|
|
|
if not x and not graceful: abort(404)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-25 02:23:11 +00:00
|
|
|
block = g.db.query(UserBlock).filter(
|
2021-07-21 01:12:26 +00:00
|
|
|
or_(
|
|
|
|
and_(
|
|
|
|
UserBlock.user_id == v.id,
|
|
|
|
UserBlock.target_id == x.author_id
|
|
|
|
),
|
|
|
|
and_(UserBlock.user_id == x.author_id,
|
|
|
|
UserBlock.target_id == v.id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).first()
|
|
|
|
|
|
|
|
x._is_blocking = block and block.user_id == v.id
|
|
|
|
x._is_blocked = block and block.target_id == v.id
|
|
|
|
|
|
|
|
else:
|
2021-07-29 06:57:42 +00:00
|
|
|
x = g.db.query(Comment).filter(Comment.id == i).first()
|
|
|
|
if not x and not graceful:abort(404)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
2021-07-29 06:57:58 +00:00
|
|
|
def get_comments(cids, v=None):
|
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-07-29 06:57:42 +00:00
|
|
|
output = g.db.query(Comment)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-29 06:57:42 +00:00
|
|
|
if v and v.admin_level >=4: output=output.options(joinedload(Comment.oauth_app))
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-29 06:57:42 +00:00
|
|
|
output = output.options(joinedload(Comment.parent_comment)).filter(Comment.id.in_(cids)).all()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
output = sorted(output, key=lambda x: cids.index(x.id))
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
def get_domain(s):
|
|
|
|
|
|
|
|
# parse domain into all possible subdomains
|
|
|
|
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))
|
|
|
|
|
|
|
|
doms = [x for x in g.db.query(Domain).filter(
|
|
|
|
Domain.domain.in_(domain_list)).all()]
|
|
|
|
|
|
|
|
if not doms:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# return the most specific domain - the one with the longest domain
|
|
|
|
# property
|
|
|
|
doms = sorted(doms, key=lambda x: len(x.domain), reverse=True)
|
|
|
|
|
2021-07-25 14:23:53 +00:00
|
|
|
return doms[0]
|