rDrama/files/routes/front.py

396 lines
12 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-09-04 16:30:39 +00:00
defaulttimefilter = environ.get("DEFAULT_TIME_FILTER", "all").strip()
2021-08-27 22:10:03 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/post/")
2021-07-21 01:12:26 +00:00
def slash_post():
return redirect("/")
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-09-19 13:11:34 +00:00
try: page = int(request.values.get('page', 1))
2021-08-30 16:41:38 +00:00
except: page = 1
2021-09-19 13:11:34 +00:00
messages = request.values.get('messages', False)
modmail = request.values.get('modmail', False)
posts = request.values.get('posts', False)
2021-09-01 16:16:38 +00:00
if modmail and v.admin_level == 6:
2021-09-17 19:31:21 +00:00
comments = g.db.query(Comment).filter(Comment.sentto==0).order_by(Comment.created_utc.desc()).offset(25*(page-1)).limit(26).all()
2021-09-13 14:44:28 +00:00
next_exists = (len(comments) > 25)
comments = comments[:25]
2021-09-01 16:16:38 +00:00
elif messages:
2021-09-17 19:31:21 +00:00
comments = g.db.query(Comment).filter(or_(Comment.author_id==v.id, Comment.sentto==v.id), Comment.parent_submission == None).order_by(Comment.created_utc.desc(), not_(Comment.child_comments.any())).offset(25*(page-1)).limit(26).all()
2021-09-13 14:44:28 +00:00
next_exists = (len(comments) > 25)
comments = comments[:25]
2021-07-21 01:12:26 +00:00
elif posts:
2021-10-13 15:30:50 +00:00
notifications = v.notifications.join(Notification.comment).filter(Comment.author_id == AUTOJANNY_ACCOUNT).order_by(Notification.id.desc()).offset(25 * (page - 1)).limit(101).all()
2021-10-13 14:44:59 +00:00
listing = []
2021-09-18 17:43:03 +00:00
2021-10-13 14:59:36 +00:00
for index, x in enumerate(notifications[:100]):
2021-09-17 08:02:13 +00:00
c = x.comment
2021-10-13 14:44:59 +00:00
if x.read and index > 25: break
2021-09-17 08:02:13 +00:00
elif not x.read:
x.read = True
2021-10-13 14:59:36 +00:00
c.unread = True
2021-09-18 17:57:25 +00:00
g.db.add(x)
2021-10-13 15:28:00 +00:00
listing.append(c)
2021-09-18 17:43:03 +00:00
2021-09-17 08:02:13 +00:00
g.db.commit()
2021-10-13 14:59:36 +00:00
next_exists = (len(notifications) > len(listing))
2021-09-17 08:02:13 +00:00
else:
2021-10-13 14:44:59 +00:00
notifications = v.notifications.join(Notification.comment).filter(Comment.author_id != AUTOJANNY_ACCOUNT).order_by(Notification.id.desc()).offset(25 * (page - 1)).limit(101).all()
listing = []
2021-09-17 08:02:13 +00:00
2021-10-13 14:59:36 +00:00
for index, x in enumerate(notifications[:100]):
2021-10-13 14:44:59 +00:00
c = x.comment
if x.read and index > 25: break
elif not x.read:
x.read = True
g.db.add(x)
listing.append(c.id)
2021-07-21 01:12:26 +00:00
2021-09-16 17:41:48 +00:00
g.db.commit()
2021-10-13 14:44:59 +00:00
comments = get_comments(listing, v=v, load_parent=True)
2021-10-13 14:59:36 +00:00
next_exists = (len(notifications) > len(comments))
2021-10-13 14:44:59 +00:00
2021-09-10 01:47:51 +00:00
if not posts:
2021-09-10 01:44:17 +00:00
listing = []
for c in comments:
2021-09-22 18:36:03 +00:00
c.is_blocked = False
c.is_blocking = False
2021-09-10 01:44:17 +00:00
if c.parent_submission and c.parent_comment and c.parent_comment.author_id == v.id:
c.replies = []
while c.parent_comment and c.parent_comment.author_id == v.id:
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
elif c.parent_submission:
c.replies = []
if c not in listing:
listing.append(c)
else:
if c.parent_comment:
while c.level > 1:
c = c.parent_comment
if c not in listing:
listing.append(c)
2021-07-21 01:12:26 +00:00
2021-09-16 17:15:07 +00:00
2021-07-21 01:12:26 +00:00
return render_template("notifications.html",
2021-09-22 22:54:13 +00:00
v=v,
notifications=listing,
next_exists=next_exists,
page=page,
standalone=True,
render_replies=True
)
2021-09-18 17:52:59 +00:00
2021-07-21 01:12:26 +00:00
2021-09-11 04:11:33 +00:00
2021-09-24 23:34:50 +00:00
@app.get("/")
@app.get("/logged_out")
@auth_desired
def front_all(v):
if not v and request.path == "/" and not request.headers.get("Authorization"): return redirect(f"/logged_out{request.full_path}")
2021-10-02 00:06:10 +00:00
if v and request.path.startswith('/logged_out'): v = None
2021-09-24 23:34:50 +00:00
try: page = int(request.values.get("page") or 1)
except: abort(400)
page = max(page, 1)
if v:
defaultsorting = v.defaultsorting
defaulttime = v.defaulttime
else:
defaultsorting = "hot"
defaulttime = defaulttimefilter
sort=request.values.get("sort", defaultsorting)
t=request.values.get('t', defaulttime)
ids, next_exists = frontlist(sort=sort,
page=page,
t=t,
v=v,
filter_words=v.filter_words if v else [],
)
posts = get_posts(ids, v=v)
if v and v.hidevotedon: posts = [x for x in posts if not hasattr(x, 'voted') or not x.voted]
if request.headers.get("Authorization"): return {"data": [x.json for x in posts], "next_exists": next_exists}
else: return render_template("home.html", v=v, listing=posts, next_exists=next_exists, sort=sort, t=t, page=page)
2021-09-25 01:47:10 +00:00
@cache.memoize(timeout=86400)
2021-10-02 11:34:37 +00:00
def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words=''):
2021-08-14 21:43:26 +00:00
2021-09-25 01:43:21 +00:00
posts = g.db.query(Submission.id).options(lazyload('*'))
2021-08-14 21:43:26 +00:00
2021-10-09 07:22:52 +00:00
if 'rama' in request.host and sort == "hot":
2021-09-22 15:23:57 +00:00
cutoff = int(time.time()) - 86400
posts = posts.filter(Submission.created_utc >= cutoff)
elif t != 'all':
2021-08-14 21:43:26 +00:00
now = int(time.time())
2021-09-27 00:11:59 +00:00
if t == 'hour': cutoff = now - 3600
elif t == 'week': cutoff = now - 604800
elif t == 'month': cutoff = now - 2592000
elif t == 'year': cutoff = now - 31536000
else: cutoff = now - 86400
posts = posts.filter(Submission.created_utc >= cutoff)
2021-08-14 21:43:26 +00:00
2021-09-25 01:44:58 +00:00
posts = posts.filter_by(is_banned=False, stickied=None, private=False, deleted_utc = 0)
2021-07-21 01:12:26 +00:00
if v and v.admin_level == 0:
2021-09-17 12:20:07 +00:00
blocking = [x[0] for x in g.db.query(
2021-07-21 01:12:26 +00:00
UserBlock.target_id).filter_by(
2021-09-17 12:20:07 +00:00
user_id=v.id).all()]
blocked = [x[0] for x in g.db.query(
2021-07-21 01:12:26 +00:00
UserBlock.user_id).filter_by(
2021-09-17 12:20:07 +00:00
target_id=v.id).all()]
2021-07-21 01:12:26 +00:00
posts = posts.filter(
Submission.author_id.notin_(blocking),
Submission.author_id.notin_(blocked)
)
2021-09-12 07:38:25 +00:00
if not (v and v.changelogsub):
2021-09-24 02:00:11 +00:00
posts=posts.filter(not_(Submission.title.ilike(f'[changelog]%')))
2021-07-21 01:12:26 +00:00
if v and filter_words:
for word in filter_words:
2021-09-24 02:00:11 +00:00
posts=posts.filter(not_(Submission.title.ilike(f'%{word}%')))
2021-07-21 01:12:26 +00:00
2021-09-12 07:44:03 +00:00
if not (v and v.shadowbanned):
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
posts = posts.filter(Submission.author_id.notin_(shadowbanned))
2021-09-12 07:41:49 +00:00
2021-07-21 01:12:26 +00:00
if sort == "hot":
2021-09-24 17:49:15 +00:00
ti = int(time.time()) + 3600
2021-10-02 23:00:46 +00:00
posts = posts.order_by(-1000000*(Submission.upvotes - Submission.downvotes + 1 + Submission.comment_count/5)/(func.power(((ti - Submission.created_utc)/1000), 1.35)))
2021-07-21 01:12:26 +00:00
elif sort == "new":
2021-09-23 19:16:44 +00:00
posts = posts.order_by(Submission.created_utc.desc())
2021-07-21 01:12:26 +00:00
elif sort == "old":
2021-09-23 19:16:44 +00:00
posts = posts.order_by(Submission.created_utc.asc())
2021-07-21 01:12:26 +00:00
elif sort == "controversial":
2021-10-12 13:42:10 +00:00
posts = posts.order_by(-1 * Submission.upvotes * Submission.downvotes * Submission.downvotes)
2021-07-21 01:12:26 +00:00
elif sort == "top":
2021-09-23 19:16:44 +00:00
posts = posts.order_by(Submission.downvotes - Submission.upvotes)
2021-07-21 01:12:26 +00:00
elif sort == "bottom":
2021-09-23 19:16:44 +00:00
posts = posts.order_by(Submission.upvotes - Submission.downvotes)
2021-07-21 01:12:26 +00:00
elif sort == "comments":
2021-09-23 19:16:44 +00:00
posts = posts.order_by(Submission.comment_count.desc())
2021-10-02 00:10:28 +00:00
if v:
if v.agendaposter: size = 5
else: size = v.frontsize
2021-10-02 00:06:10 +00:00
else: size = 25
2021-09-23 21:48:43 +00:00
2021-10-02 00:06:10 +00:00
posts = posts.offset(size * (page - 1)).limit(size+1).all()
2021-08-11 17:01:19 +00:00
2021-10-02 00:06:10 +00:00
next_exists = (len(posts) > size)
posts = posts[:size]
2021-08-11 17:01:19 +00:00
2021-10-12 05:39:03 +00:00
pins = g.db.query(Submission.id).options(lazyload('*')).filter(Submission.stickied != None)
if v and v.admin_level == 0:
blocking = [x[0] for x in g.db.query(UserBlock.target_id).filter_by(user_id=v.id).all()]
blocked = [x[0] for x in g.db.query(UserBlock.user_id).filter_by(target_id=v.id).all()]
2021-10-12 05:41:04 +00:00
pins = pins.filter(Submission.author_id.notin_(blocking), Submission.author_id.notin_(blocked))
2021-10-12 05:39:03 +00:00
2021-10-12 05:41:04 +00:00
if page == 1: posts = pins.all() + posts
2021-08-27 21:26:58 +00:00
2021-09-25 01:43:21 +00:00
if ids_only: posts = [x[0] for x in posts]
2021-08-11 17:01:19 +00:00
return posts, next_exists
2021-07-21 01:12:26 +00:00
2021-08-13 01:29:21 +00:00
2021-09-24 23:34:50 +00:00
@app.get("/changelog")
@auth_desired
def changelog(v):
2021-08-13 02:01:54 +00:00
2021-07-21 01:12:26 +00:00
2021-09-24 23:34:50 +00:00
page = int(request.values.get("page") or 1)
2021-07-21 01:12:26 +00:00
page = max(page, 1)
2021-09-24 23:34:50 +00:00
sort=request.values.get("sort", "new")
t=request.values.get('t', "all")
2021-07-21 01:12:26 +00:00
2021-09-24 23:34:50 +00:00
ids = changeloglist(sort=sort,
2021-07-21 01:12:26 +00:00
page=page,
t=t,
v=v,
)
2021-09-24 23:34:50 +00:00
next_exists = (len(ids) > 25)
ids = ids[:25]
2021-07-21 01:12:26 +00:00
2021-09-24 23:34:50 +00:00
posts = get_posts(ids, v=v)
2021-09-01 15:06:07 +00:00
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-09-24 23:34:50 +00:00
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-09-04 00:40:30 +00:00
2021-09-20 14:31:58 +00:00
@cache.memoize(timeout=86400)
2021-10-02 11:34:37 +00:00
def changeloglist(v=None, sort="new", page=1 ,t="all"):
2021-07-21 01:12:26 +00:00
2021-09-23 19:16:44 +00:00
posts = g.db.query(Submission.id).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:
2021-09-17 12:20:07 +00:00
blocking = [x[0] for x in g.db.query(
2021-07-21 01:12:26 +00:00
UserBlock.target_id).filter_by(
2021-09-17 12:20:07 +00:00
user_id=v.id).all()]
blocked = [x[0] for x in g.db.query(
2021-07-21 01:12:26 +00:00
UserBlock.user_id).filter_by(
2021-09-17 12:20:07 +00:00
target_id=v.id).all()]
2021-07-21 01:12:26 +00:00
posts = posts.filter(
Submission.author_id.notin_(blocking),
Submission.author_id.notin_(blocked)
)
2021-09-28 19:39:51 +00:00
admins = [x[0] for x in g.db.query(User.id).options(lazyload('*')).filter(User.admin_level == 6).all()]
posts = posts.filter(Submission.title.ilike('_changelog%'), Submission.author_id.in_(admins))
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)
2021-09-23 19:16:44 +00:00
if sort == "new":
posts = posts.order_by(Submission.created_utc.desc())
2021-07-21 01:12:26 +00:00
elif sort == "old":
2021-09-23 19:16:44 +00:00
posts = posts.order_by(Submission.created_utc.asc())
2021-07-21 01:12:26 +00:00
elif sort == "controversial":
2021-10-12 13:42:10 +00:00
posts = posts.order_by(-1 * Submission.upvotes * Submission.downvotes * Submission.downvotes)
2021-07-21 01:12:26 +00:00
elif sort == "top":
2021-09-23 19:16:44 +00:00
posts = posts.order_by(Submission.downvotes - Submission.upvotes)
2021-07-21 01:12:26 +00:00
elif sort == "bottom":
2021-09-23 19:16:44 +00:00
posts = posts.order_by(Submission.upvotes - Submission.downvotes)
2021-07-21 01:12:26 +00:00
elif sort == "comments":
2021-09-23 19:16:44 +00:00
posts = posts.order_by(Submission.comment_count.desc())
2021-07-21 01:12:26 +00:00
2021-09-23 19:16:44 +00:00
posts = posts.offset(25 * (page - 1)).limit(26).all()
2021-07-21 01:12:26 +00:00
2021-09-23 19:16:44 +00:00
return [x[0] for x in posts]
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-09-17 08:29:05 +00:00
x = g.db.query(Submission).options(lazyload('*')).filter(Submission.deleted_utc == 0, Submission.is_banned == False)
2021-07-21 01:12:26 +00:00
total = x.count()
2021-09-05 09:37:05 +00:00
n = random.randint(1, total - 2)
2021-07-21 01:12:26 +00:00
2021-09-08 20:08:01 +00:00
post = x.offset(n).limit(1).first()
2021-09-03 13:52:37 +00:00
return redirect(f"/post/{post.id}")
2021-07-21 01:12:26 +00:00
2021-09-20 14:31:58 +00:00
@cache.memoize(timeout=86400)
2021-10-02 11:34:37 +00:00
def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all"):
2021-07-21 01:12:26 +00:00
posts = g.db.query(Submission).options(lazyload('*'))
2021-09-17 12:20:07 +00:00
cc_idlist = [x[0] for x in g.db.query(Submission.id).options(lazyload('*')).filter(Submission.club == True).all()]
2021-07-21 01:12:26 +00:00
posts = posts.subquery()
2021-07-21 01:12:26 +00:00
2021-09-23 19:16:44 +00:00
comments = g.db.query(Comment.id).options(lazyload('*')).filter(Comment.parent_submission.notin_(cc_idlist))
2021-07-21 01:12:26 +00:00
if v and v.admin_level <= 3:
2021-09-17 12:20:07 +00:00
blocking = [x[0] for x in g.db.query(
2021-07-21 01:12:26 +00:00
UserBlock.target_id).filter_by(
2021-09-17 12:20:07 +00:00
user_id=v.id).all()]
blocked = [x[0] for x in g.db.query(
2021-07-21 01:12:26 +00:00
UserBlock.user_id).filter_by(
2021-09-17 12:20:07 +00:00
target_id=v.id).all()]
2021-07-21 01:12:26 +00:00
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)
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":
2021-09-23 19:16:44 +00:00
comments = comments.order_by(Comment.created_utc.desc())
2021-07-21 01:12:26 +00:00
elif sort == "old":
2021-09-23 19:16:44 +00:00
comments = comments.order_by(Comment.created_utc.asc())
2021-07-21 01:12:26 +00:00
elif sort == "controversial":
2021-10-12 13:42:10 +00:00
comments = comments.order_by(-1 * Comment.upvotes * Comment.downvotes * Comment.downvotes)
2021-07-21 01:12:26 +00:00
elif sort == "top":
2021-09-23 19:16:44 +00:00
comments = comments.order_by(Comment.downvotes - Comment.upvotes)
2021-07-21 01:12:26 +00:00
elif sort == "bottom":
2021-09-23 19:16:44 +00:00
comments = comments.order_by(Comment.upvotes - Comment.downvotes)
2021-07-21 01:12:26 +00:00
2021-09-23 19:16:44 +00:00
comments = comments.offset(25 * (page - 1)).limit(26).all()
return [x[0] for x in comments]
2021-07-21 01:12:26 +00:00
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
2021-09-19 13:11:34 +00:00
page = int(request.values.get("page", 1))
2021-07-21 01:12:26 +00:00
2021-09-19 13:11:34 +00:00
sort=request.values.get("sort", "new")
t=request.values.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)
2021-09-10 06:04:28 +00:00
next_exists = len(idlist) > 25
2021-07-21 01:12:26 +00:00
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)