rDrama/files/routes/front.py

457 lines
13 KiB
Python
Raw Normal View History

2021-08-04 15:35:10 +00:00
from files.helpers.wrappers import *
from files.helpers.get import *
2021-07-21 01:12:26 +00:00
2021-08-04 15:35:10 +00:00
from files.__main__ import app, cache
from files.classes.submission import Submission
2021-07-21 01:12:26 +00:00
2021-08-27 22:10:03 +00:00
defaulttimefilter = environ.get("DEFAULT_TIME_FILTER", "day").strip()
2021-07-27 22:31:28 +00:00
@app.get("/post/")
2021-07-21 01:12:26 +00:00
def slash_post():
return redirect("/")
# this is a test
2021-07-27 22:31:28 +00:00
@app.get("/notifications")
2021-07-21 01:12:26 +00:00
@auth_required
def notifications(v):
2021-08-30 16:41:38 +00:00
try: page = int(request.args.get('page', 1))
except: page = 1
2021-07-21 01:12:26 +00:00
messages = request.args.get('messages', False)
2021-09-01 16:16:38 +00:00
modmail = request.args.get('modmail', False)
2021-07-21 01:12:26 +00:00
posts = request.args.get('posts', False)
2021-09-01 16:16:38 +00:00
if modmail and v.admin_level == 6:
2021-09-01 16:26:02 +00:00
comments = g.db.query(Comment).filter(Comment.sentto==0).order_by(Comment.created_utc.desc()).all()
firstrange = 25 * (page - 1)
secondrange = firstrange + 26
comments = comments[firstrange:secondrange]
next_exists = (len(comments) == 26)
comments = comments[:25]
2021-09-01 16:16:38 +00:00
elif messages:
2021-08-31 16:23:34 +00:00
cids = v.notification_messages(page=page)
2021-08-31 16:24:25 +00:00
next_exists = (len(cids) == 26)
2021-08-31 16:26:44 +00:00
cids = cids[:25]
2021-08-31 16:26:17 +00:00
comments = get_comments(cids, v=v)
2021-07-21 01:12:26 +00:00
elif posts:
2021-09-01 15:51:27 +00:00
notifications = v.notifications.join(Notification.comment).filter(Comment.author_id == AUTOJANNY_ACCOUNT).order_by(Notification.id.desc()).offset(25 * (page - 1)).limit(26)
comments = []
for x in notifications:
c = x.comment
if not x.read: c.unread = True
x.read = True
g.db.add(x)
comments.append(c)
next_exists = (len(comments) == 26)
comments = comments[:25]
2021-07-21 01:12:26 +00:00
else:
2021-09-01 15:51:27 +00:00
notifications = v.notifications.join(Notification.comment).filter(
Comment.is_banned == False,
Comment.deleted_utc == 0,
Comment.author_id != AUTOJANNY_ACCOUNT,
2021-09-01 19:48:26 +00:00
).order_by(Notification.id.desc()).offset(25 * (page - 1)).limit(26).all()
2021-09-01 15:51:27 +00:00
2021-09-01 19:46:11 +00:00
next_exists = (len(notifications) == 26)
notifications = notifications[:25]
2021-09-01 17:12:48 +00:00
cids = [x.comment_id for x in notifications]
2021-09-01 17:14:33 +00:00
comments = get_comments(cids, v=v, load_parent=True)
2021-09-01 17:12:48 +00:00
i = 0
2021-09-01 15:51:27 +00:00
for x in notifications:
2021-09-01 19:46:11 +00:00
if not x.read: comments[i].unread = True
2021-09-01 15:51:27 +00:00
x.read = True
g.db.add(x)
2021-09-01 17:12:48 +00:00
i += 1
2021-07-21 01:12:26 +00:00
listing = []
for c in comments:
c._is_blocked = False
c._is_blocking = False
2021-07-24 20:37:45 +00:00
if c.parent_submission and c.parent_comment and c.parent_comment.author_id == v.id:
2021-08-13 21:37:22 +00:00
c.replies = []
2021-08-07 21:34:27 +00:00
while c.parent_comment and c.parent_comment.author_id == v.id:
2021-07-21 01:12:26 +00:00
parent = c.parent_comment
if c not in parent.replies2:
parent.replies2 = parent.replies2 + [c]
parent.replies = parent.replies2
c = parent
if c not in listing:
listing.append(c)
c.replies = c.replies2
2021-08-07 15:33:36 +00:00
elif c.parent_submission:
2021-08-13 21:37:22 +00:00
c.replies = []
2021-08-07 15:33:36 +00:00
if c not in listing:
listing.append(c)
2021-07-21 01:12:26 +00:00
else:
if c.parent_comment:
while c.level > 1:
c = c.parent_comment
if c not in listing:
listing.append(c)
return render_template("notifications.html",
v=v,
notifications=listing,
next_exists=next_exists,
page=page,
standalone=True,
render_replies=True,
is_notification_page=True)
2021-09-01 15:06:36 +00:00
@cache.memoize(timeout=86400)
2021-08-14 21:43:26 +00:00
def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='', **kwargs):
posts = g.db.query(Submission).options(lazyload('*'))
if t != 'all':
cutoff = 0
now = int(time.time())
if t == 'hour':
cutoff = now - 3600
elif t == 'day':
cutoff = now - 86400
elif t == 'week':
cutoff = now - 604800
elif t == 'month':
cutoff = now - 2592000
elif t == 'year':
cutoff = now - 31536000
posts = posts.filter(Submission.created_utc >= cutoff)
posts = posts.filter_by(is_banned=False,stickied=False,private=False).filter(Submission.deleted_utc == 0)
2021-07-21 01:12:26 +00:00
if v and v.admin_level == 0:
blocking = g.db.query(
UserBlock.target_id).filter_by(
user_id=v.id).subquery()
blocked = g.db.query(
UserBlock.user_id).filter_by(
target_id=v.id).subquery()
posts = posts.filter(
Submission.author_id.notin_(blocking),
Submission.author_id.notin_(blocked)
)
if not (v and v.changelogsub):
posts=posts.join(Submission.submission_aux)
2021-08-26 13:33:49 +00:00
posts=posts.filter(not_(SubmissionAux.title.ilike(f'[changelog]%')))
2021-07-21 01:12:26 +00:00
if v and filter_words:
for word in filter_words:
posts=posts.filter(not_(SubmissionAux.title.ilike(f'%{word}%')))
gt = kwargs.get("gt")
lt = kwargs.get("lt")
if gt:
posts = posts.filter(Submission.created_utc > gt)
if lt:
posts = posts.filter(Submission.created_utc < lt)
if sort == "hot":
posts = sorted(posts.all(), key=lambda x: x.hotscore, reverse=True)
elif sort == "new":
posts = posts.order_by(Submission.created_utc.desc()).all()
elif sort == "old":
posts = posts.order_by(Submission.created_utc.asc()).all()
elif sort == "controversial":
posts = sorted(posts.all(), key=lambda x: x.score_disputed, reverse=True)
elif sort == "top":
2021-08-06 12:22:29 +00:00
posts = sorted(posts.all(), key=lambda x: x.score, reverse=True)
2021-07-21 01:12:26 +00:00
elif sort == "bottom":
2021-08-06 12:41:26 +00:00
posts = sorted(posts.all(), key=lambda x: x.score)
2021-07-21 01:12:26 +00:00
elif sort == "comments":
2021-08-22 11:33:13 +00:00
posts = posts.order_by(Submission.comment_count.desc()).all()
2021-07-21 01:12:26 +00:00
elif sort == "random":
posts = posts.all()
posts = random.sample(posts, k=len(posts))
else:
abort(400)
2021-08-24 18:10:08 +00:00
firstrange = 50 * (page - 1)
2021-08-27 21:26:58 +00:00
secondrange = firstrange+200
2021-07-21 01:12:26 +00:00
posts = posts[firstrange:secondrange]
2021-07-24 21:54:56 +00:00
words = ['captainmeta4', ' cm4 ', 'dissident001', 'ladine']
2021-07-21 01:12:26 +00:00
for post in posts:
if post.author and post.author.admin_level == 0:
for word in words:
if word in post.title.lower():
posts.remove(post)
break
2021-08-28 11:32:52 +00:00
if random.random() < 0.004:
2021-07-21 01:12:26 +00:00
for post in posts:
if post.author and post.author.shadowbanned:
2021-09-02 19:24:50 +00:00
rand = random.randint(5,20)
if post.score > rand: continue
2021-07-21 01:12:26 +00:00
rand = random.randint(500,1400)
vote = Vote(user_id=rand,
2021-09-02 19:24:50 +00:00
vote_type=random.choice([-1, 1, 1, 1, 1]),
2021-07-21 01:12:26 +00:00
submission_id=post.id)
g.db.add(vote)
try: g.db.flush()
except: g.db.rollback()
2021-07-26 17:00:29 +00:00
post.upvotes = g.db.query(Vote).filter_by(submission_id=post.id, vote_type=1).count()
post.downvotes = g.db.query(Vote).filter_by(submission_id=post.id, vote_type=-1).count()
2021-07-21 01:12:26 +00:00
post.views = post.views + random.randint(7,10)
g.db.add(post)
2021-08-24 18:10:08 +00:00
posts = [x for x in posts if not (x.author and x.author.shadowbanned) or (v and v.id == x.author_id)][:51]
2021-07-28 22:38:58 +00:00
2021-08-24 18:10:08 +00:00
next_exists = (len(posts) == 51)
2021-08-11 17:01:19 +00:00
2021-08-24 18:10:08 +00:00
posts = posts[:50]
2021-08-11 17:01:19 +00:00
2021-08-27 21:26:58 +00:00
if page == 1: posts = g.db.query(Submission).filter_by(stickied=True).all() + posts
2021-08-11 17:01:19 +00:00
if ids_only: posts = [x.id for x in posts]
return posts, next_exists
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/")
2021-08-13 01:29:21 +00:00
@app.get("/logged_out")
2021-07-21 01:12:26 +00:00
@auth_desired
def front_all(v):
2021-08-23 17:48:55 +00:00
2021-08-09 18:58:01 +00:00
2021-08-13 01:29:21 +00:00
if not v and request.path == "/": return redirect("/logged_out")
2021-08-13 18:53:11 +00:00
if v and "logged_out" in request.full_path: v = None
2021-08-13 02:01:54 +00:00
2021-07-27 00:13:17 +00:00
try: page = int(request.args.get("page") or 1)
except: abort(400)
2021-07-21 01:12:26 +00:00
# prevent invalid paging
page = max(page, 1)
if v:
defaultsorting = v.defaultsorting
2021-07-22 19:16:56 +00:00
defaulttime = v.defaulttime
2021-07-21 01:12:26 +00:00
else:
defaultsorting = "hot"
2021-08-27 22:10:03 +00:00
defaulttime = defaulttimefilter
2021-07-21 01:12:26 +00:00
sort=request.args.get("sort", defaultsorting)
t=request.args.get('t', defaulttime)
2021-08-11 17:01:19 +00:00
ids, next_exists = frontlist(sort=sort,
2021-07-21 01:12:26 +00:00
page=page,
t=t,
v=v,
gt=int(request.args.get("utc_greater_than", 0)),
lt=int(request.args.get("utc_less_than", 0)),
filter_words=v.filter_words if v else [],
)
2021-07-28 22:38:58 +00:00
posts = get_posts(ids, v=v)
2021-07-21 01:12:26 +00:00
2021-09-01 15:06:07 +00:00
if v and v.hidevotedon: posts = [x for x in posts if not hasattr(x, 'voted') or not x.voted]
2021-07-31 05:28:05 +00:00
if request.headers.get("Authorization"): return {"data": [x.json for x in posts], "next_exists": next_exists}
2021-07-31 04:32:42 +00:00
else: return render_template("home.html", v=v, listing=posts, next_exists=next_exists, sort=sort, t=t, page=page)
2021-07-21 01:12:26 +00:00
2021-08-31 16:32:04 +00:00
@cache.memoize(timeout=86400)
2021-07-21 01:12:26 +00:00
def changeloglist(v=None, sort="new", page=1 ,t="all", **kwargs):
2021-08-26 13:43:51 +00:00
posts = g.db.query(Submission).options(lazyload('*')).filter_by(is_banned=False, private=False,).filter(Submission.deleted_utc == 0)
2021-07-21 01:12:26 +00:00
if v and v.admin_level == 0:
blocking = g.db.query(
UserBlock.target_id).filter_by(
user_id=v.id).subquery()
blocked = g.db.query(
UserBlock.user_id).filter_by(
target_id=v.id).subquery()
posts = posts.filter(
Submission.author_id.notin_(blocking),
Submission.author_id.notin_(blocked)
)
posts=posts.join(Submission.submission_aux).join(Submission.author)
2021-08-26 13:33:49 +00:00
posts=posts.filter(SubmissionAux.title.ilike(f'_changelog%', User.admin_level == 6))
2021-07-21 01:12:26 +00:00
if t != 'all':
cutoff = 0
now = int(time.time())
if t == 'hour':
cutoff = now - 3600
elif t == 'day':
cutoff = now - 86400
elif t == 'week':
cutoff = now - 604800
elif t == 'month':
cutoff = now - 2592000
elif t == 'year':
cutoff = now - 31536000
posts = posts.filter(Submission.created_utc >= cutoff)
gt = kwargs.get("gt")
lt = kwargs.get("lt")
if gt:
posts = posts.filter(Submission.created_utc > gt)
if lt:
posts = posts.filter(Submission.created_utc < lt)
if sort == "hot":
posts = sorted(posts.all(), key=lambda x: x.hotscore, reverse=True)
elif sort == "new":
posts = posts.order_by(Submission.created_utc.desc()).all()
elif sort == "old":
posts = posts.order_by(Submission.created_utc.asc()).all()
elif sort == "controversial":
posts = sorted(posts.all(), key=lambda x: x.score_disputed, reverse=True)
elif sort == "top":
2021-08-06 12:22:29 +00:00
posts = sorted(posts.all(), key=lambda x: x.score, reverse=True)
2021-07-21 01:12:26 +00:00
elif sort == "bottom":
2021-08-06 12:22:29 +00:00
posts = sorted(posts.all(), key=lambda x: x.score)
2021-07-21 01:12:26 +00:00
elif sort == "comments":
2021-08-22 13:15:10 +00:00
posts = posts.order_by(Submission.comment_count.desc()).all()
2021-07-21 01:12:26 +00:00
elif sort == "random":
posts = posts.all()
posts = random.sample(posts, k=len(posts))
else:
abort(400)
2021-08-24 18:10:08 +00:00
firstrange = 50 * (page - 1)
secondrange = firstrange+51
2021-07-21 01:12:26 +00:00
posts = posts[firstrange:secondrange]
posts = [x.id for x in posts]
return posts
2021-07-27 22:31:28 +00:00
@app.get("/changelog")
2021-07-21 01:12:26 +00:00
@auth_desired
def changelog(v):
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
page = int(request.args.get("page") or 1)
page = max(page, 1)
sort=request.args.get("sort", "new")
t=request.args.get('t', "all")
ids = changeloglist(sort=sort,
page=page,
t=t,
v=v,
gt=int(request.args.get("utc_greater_than", 0)),
lt=int(request.args.get("utc_less_than", 0)),
)
# check existence of next page
2021-08-24 18:10:08 +00:00
next_exists = (len(ids) == 51)
ids = ids[:50]
2021-07-21 01:12:26 +00:00
# check if ids exist
2021-07-25 02:54:07 +00:00
posts = get_posts(ids, v=v)
2021-07-21 01:12:26 +00:00
2021-07-31 05:33:32 +00:00
if request.headers.get("Authorization"): return {"data": [x.json for x in posts], "next_exists": next_exists}
else: return render_template("changelog.html", v=v, listing=posts, next_exists=next_exists, sort=sort, t=t, page=page)
2021-07-21 01:12:26 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/random")
2021-07-21 01:12:26 +00:00
@auth_desired
def random_post(v):
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
2021-08-06 15:36:54 +00:00
x = g.db.query(Submission).filter(Submission.deleted_utc == 0, Submission.is_banned == False)
2021-07-21 01:12:26 +00:00
total = x.count()
n = random.randint(0, total - 1)
2021-09-02 13:20:19 +00:00
post = x.offset(n).limit(1).first()
2021-07-21 01:12:26 +00:00
return redirect(post.permalink)
2021-08-31 16:32:04 +00:00
@cache.memoize(timeout=86400)
2021-07-21 01:12:26 +00:00
def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all", **kwargs):
posts = g.db.query(Submission).options(lazyload('*'))
posts = posts.subquery()
comments = g.db.query(Comment).options(lazyload('*'))
if v and v.admin_level <= 3:
blocking = g.db.query(
UserBlock.target_id).filter_by(
user_id=v.id).subquery()
blocked = g.db.query(
UserBlock.user_id).filter_by(
target_id=v.id).subquery()
comments = comments.filter(
Comment.author_id.notin_(blocking),
Comment.author_id.notin_(blocked)
)
if not v or not v.admin_level >= 3:
comments = comments.filter_by(is_banned=False).filter(Comment.deleted_utc == 0)
comments = comments.join(posts, Comment.parent_submission == posts.c.id)
now = int(time.time())
if t == 'hour':
cutoff = now - 3600
elif t == 'day':
cutoff = now - 86400
elif t == 'week':
cutoff = now - 604800
elif t == 'month':
cutoff = now - 2592000
elif t == 'year':
cutoff = now - 31536000
else:
cutoff = 0
comments = comments.filter(Comment.created_utc >= cutoff)
if sort == "new":
comments = comments.order_by(Comment.created_utc.desc()).all()
elif sort == "old":
comments = comments.order_by(Comment.created_utc.asc()).all()
elif sort == "controversial":
comments = sorted(comments.all(), key=lambda x: x.score_disputed, reverse=True)
elif sort == "top":
2021-08-06 12:22:29 +00:00
comments = sorted(comments.all(), key=lambda x: x.score, reverse=True)
2021-07-21 01:12:26 +00:00
elif sort == "bottom":
2021-08-06 12:22:29 +00:00
comments = sorted(comments.all(), key=lambda x: x.score)
2021-07-21 01:12:26 +00:00
firstrange = 25 * (page - 1)
secondrange = firstrange+100
comments = comments[firstrange:secondrange]
comments = [x.id for x in comments if not (x.author and x.author.shadowbanned) or (v and v.id == x.author_id)]
return comments[:26]
2021-07-27 22:31:28 +00:00
@app.get("/comments")
2021-07-21 01:12:26 +00:00
@auth_desired
def all_comments(v):
2021-08-23 17:48:55 +00:00
2021-07-21 01:12:26 +00:00
page = int(request.args.get("page", 1))
sort=request.args.get("sort", "new")
2021-08-27 22:10:03 +00:00
t=request.args.get("t", defaulttimefilter)
2021-07-21 01:12:26 +00:00
idlist = comment_idlist(v=v,
page=page,
sort=sort,
t=t,
)
comments = get_comments(idlist, v=v)
next_exists = len(idlist) == 26
2021-08-02 07:37:46 +00:00
idlist = idlist[:25]
2021-07-21 01:12:26 +00:00
2021-07-31 06:05:09 +00:00
if request.headers.get("Authorization"): return {"data": [x.json for x in comments]}
2021-08-22 11:33:13 +00:00
else: return render_template("home_comments.html", v=v, sort=sort, t=t, page=page, comments=comments, standalone=True, next_exists=next_exists)