MarseyWorld/files/routes/front.py

480 lines
15 KiB
Python
Raw Normal View History

2021-10-15 14:08:27 +00:00
from files.helpers.wrappers import *
from files.helpers.get import *
2022-01-14 10:13:03 +00:00
from files.__main__ import app, cache, limiter
2021-10-15 14:08:27 +00:00
from files.classes.submission import Submission
2022-01-13 02:59:17 +00:00
defaulttimefilter = environ.get("DEFAULT_TIME_FILTER", "all").strip()
2021-10-21 17:01:25 +00:00
SITE_NAME = environ.get("SITE_NAME", "").strip()
2021-10-15 14:08:27 +00:00
2021-11-11 00:26:25 +00:00
@app.post("/clear")
@auth_required
def clear(v):
for n in v.notifications.filter_by(read=False).all():
n.read = True
g.db.add(n)
g.db.commit()
return {"message": "Notifications cleared!"}
2021-10-15 14:08:27 +00:00
@app.get("/notifications")
@auth_required
def notifications(v):
try: page = int(request.values.get('page', 1))
except: page = 1
2022-01-06 17:21:30 +00:00
messages = request.values.get('messages')
modmail = request.values.get('modmail')
posts = request.values.get('posts')
2021-11-15 22:13:29 +00:00
if modmail and v.admin_level > 1:
2022-01-06 17:37:21 +00:00
comments = g.db.query(Comment).filter(Comment.sentto==0).order_by(Comment.id.desc()).offset(25*(page-1)).limit(26).all()
2021-10-15 14:08:27 +00:00
next_exists = (len(comments) > 25)
comments = comments[:25]
elif messages:
2022-01-06 18:47:11 +00:00
comments = g.db.query(Comment).filter(or_(Comment.author_id==v.id, Comment.sentto==v.id), Comment.parent_submission == None, not_(Comment.child_comments.any())).order_by(Comment.id.desc()).offset(25*(page-1)).limit(26).all()
2021-10-15 14:08:27 +00:00
next_exists = (len(comments) > 25)
2022-01-06 17:29:07 +00:00
comments = comments[:25]
2021-10-15 14:08:27 +00:00
elif posts:
2021-11-18 14:21:19 +00:00
notifications = v.notifications.join(Notification.comment).filter(Comment.author_id == AUTOJANNY_ID).order_by(Notification.id.desc()).offset(25 * (page - 1)).limit(101).all()
2021-10-15 14:08:27 +00:00
listing = []
for index, x in enumerate(notifications[:100]):
c = x.comment
2021-11-07 13:58:32 +00:00
if x.read and index > 24: break
2021-10-15 14:08:27 +00:00
elif not x.read:
x.read = True
c.unread = True
g.db.add(x)
listing.append(c)
g.db.commit()
next_exists = (len(notifications) > len(listing))
else:
2021-11-14 02:27:55 +00:00
notifications = v.notifications.join(Notification.comment).filter(
Comment.is_banned == False,
Comment.deleted_utc == 0,
2021-11-18 14:21:19 +00:00
Comment.author_id != AUTOJANNY_ID,
2021-11-14 02:27:55 +00:00
).order_by(Notification.id.desc()).offset(25 * (page - 1)).limit(26).all()
2021-10-15 14:08:27 +00:00
2021-11-14 02:27:55 +00:00
next_exists = (len(notifications) > 25)
notifications = notifications[:25]
cids = [x.comment_id for x in notifications]
comments = get_comments(cids, v=v, load_parent=True)
2021-10-15 14:08:27 +00:00
2021-11-14 02:27:55 +00:00
i = 0
for x in notifications:
try:
if not x.read: comments[i].unread = True
except: continue
x.read = True
g.db.add(x)
i += 1
2021-11-14 02:28:08 +00:00
g.db.commit()
2021-10-15 14:08:27 +00:00
if not posts:
listing = []
for c in comments:
c.is_blocked = False
c.is_blocking = False
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-12-14 18:35:56 +00:00
if request.headers.get("Authorization"): return {"data":[x.json for x in listing]}
2021-10-15 14:08:27 +00:00
2022-01-14 12:04:35 +00:00
return render_template("notifications.html",
2021-10-15 14:08:27 +00:00
v=v,
notifications=listing,
next_exists=next_exists,
page=page,
standalone=True,
render_replies=True
)
@app.get("/")
@app.get("/logged_out")
2022-01-14 10:13:03 +00:00
@limiter.limit("3/second;30/minute;400/hour;2000/day")
2022-01-11 22:57:05 +00:00
@auth_desired
2021-10-15 14:08:27 +00:00
def front_all(v):
2022-01-17 11:06:12 +00:00
if not v and request.path == "/" and not request.headers.get("Authorization"):
return redirect(f"/logged_out{request.full_path}")
2021-10-15 14:08:27 +00:00
if v and request.path.startswith('/logged_out'): v = None
2021-10-16 10:07:32 +00:00
try: page = max(int(request.values.get("page", 1)), 1)
2021-10-15 14:08:27 +00:00
except: abort(400)
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 [],
2021-10-15 17:13:37 +00:00
gt=int(request.values.get("utc_greater_than", 0)),
lt=int(request.values.get("utc_less_than", 0)),
2021-10-15 14:08:27 +00:00
)
posts = get_posts(ids, v=v)
2021-11-07 14:12:20 +00:00
if v:
if v.hidevotedon: posts = [x for x in posts if not hasattr(x, 'voted') or not x.voted]
if v.agendaposter_expires_utc and v.agendaposter_expires_utc < time.time():
v.agendaposter_expires_utc = 0
2021-12-03 18:10:24 +00:00
v.agendaposter = None
2021-12-20 20:03:59 +00:00
send_repeatable_notification(v.id, "Your agendaposter theme has expired!")
2021-11-07 14:12:20 +00:00
g.db.add(v)
2021-12-03 18:10:24 +00:00
badge = v.has_badge(26)
if badge: g.db.delete(badge)
2021-11-07 14:12:20 +00:00
g.db.commit()
if v.flairchanged and v.flairchanged < time.time():
v.flairchanged = None
2021-12-20 20:03:59 +00:00
send_repeatable_notification(v.id, "Your flair lock has expired. You can now change your flair!")
2021-11-07 14:12:20 +00:00
g.db.add(v)
2022-01-06 22:28:27 +00:00
badge = v.has_badge(96)
if badge: g.db.delete(badge)
2021-11-07 14:12:20 +00:00
g.db.commit()
2021-11-07 14:09:20 +00:00
2021-11-30 13:09:17 +00:00
if v.marseyawarded and v.marseyawarded < time.time():
v.marseyawarded = None
2021-12-20 20:03:59 +00:00
send_repeatable_notification(v.id, "Your marsey award has expired!")
2021-11-30 13:09:17 +00:00
g.db.add(v)
2022-01-06 22:28:27 +00:00
badge = v.has_badge(98)
if badge: g.db.delete(badge)
2021-11-30 13:09:17 +00:00
g.db.commit()
if v.longpost and v.longpost < time.time():
v.longpost = None
2021-12-20 20:03:59 +00:00
send_repeatable_notification(v.id, "Your pizzashill award has expired!")
2021-11-30 13:09:17 +00:00
g.db.add(v)
2022-01-06 22:28:27 +00:00
badge = v.has_badge(97)
if badge: g.db.delete(badge)
2021-11-30 13:09:17 +00:00
g.db.commit()
if v.bird and v.bird < time.time():
v.bird = None
2021-12-20 20:03:59 +00:00
send_repeatable_notification(v.id, "Your bird site award has expired!")
2021-11-30 13:09:17 +00:00
g.db.add(v)
2022-01-06 22:28:27 +00:00
badge = v.has_badge(95)
if badge: g.db.delete(badge)
2021-11-30 13:09:17 +00:00
g.db.commit()
2022-01-01 22:51:39 +00:00
if v.progressivestack and v.progressivestack < time.time():
v.progressivestack = None
send_repeatable_notification(v.id, "Your progressive stack has expired!")
g.db.add(v)
2022-01-06 22:28:27 +00:00
badge = v.has_badge(94)
if badge: g.db.delete(badge)
2022-01-01 22:51:39 +00:00
g.db.commit()
2021-10-15 14:08:27 +00:00
if request.headers.get("Authorization"): return {"data": [x.json for x in posts], "next_exists": next_exists}
2022-01-14 12:04:35 +00:00
return render_template("home.html", v=v, listing=posts, next_exists=next_exists, sort=sort, t=t, page=page)
2021-10-15 14:08:27 +00:00
@cache.memoize(timeout=86400)
2021-10-15 17:14:26 +00:00
def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='', gt=None, lt=None):
2021-10-15 14:08:27 +00:00
2021-11-11 22:52:26 +00:00
posts = g.db.query(Submission)
2021-10-15 14:08:27 +00:00
2021-12-17 04:44:25 +00:00
if t == 'all': cutoff = 0
else:
2021-10-15 14:08:27 +00:00
now = int(time.time())
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-11-11 20:04:49 +00:00
2022-01-17 20:29:06 +00:00
if sort == "hot" or v.id == Q_ID: posts = posts.filter_by(is_banned=False, stickied=None, private=False, deleted_utc = 0)
else: posts = posts.filter_by(is_banned=False, private=False, deleted_utc = 0)
2021-10-15 14:08:27 +00:00
if v and v.admin_level == 0:
blocking = [x[0] for x in g.db.query(
2021-11-06 15:52:48 +00:00
UserBlock.target_id).filter_by(
2021-10-15 14:08:27 +00:00
user_id=v.id).all()]
blocked = [x[0] for x in g.db.query(
2021-11-06 15:52:48 +00:00
UserBlock.user_id).filter_by(
2021-10-15 14:08:27 +00:00
target_id=v.id).all()]
posts = posts.filter(
Submission.author_id.notin_(blocking),
Submission.author_id.notin_(blocked)
)
if not (v and v.changelogsub):
2022-01-07 21:03:14 +00:00
posts=posts.filter(not_(Submission.title.ilike('[changelog]%')))
2021-10-15 14:08:27 +00:00
if v and filter_words:
for word in filter_words:
posts=posts.filter(not_(Submission.title.ilike(f'%{word}%')))
2021-10-15 17:13:37 +00:00
if gt: posts = posts.filter(Submission.created_utc > gt)
if lt: posts = posts.filter(Submission.created_utc < lt)
2021-10-15 14:08:27 +00:00
if not (v and v.shadowbanned):
2021-11-06 00:33:32 +00:00
posts = posts.join(User, User.id == Submission.author_id).filter(User.shadowbanned == None)
2021-10-15 14:08:27 +00:00
2022-01-17 20:29:06 +00:00
if sort == "hot" or v.id == Q_ID:
2021-10-15 14:08:27 +00:00
ti = int(time.time()) + 3600
2022-01-13 21:15:36 +00:00
posts = posts.order_by(-1000000*(Submission.realupvotes + 1 + Submission.comment_count/5)/(func.power(((ti - Submission.created_utc)/1000), 1.23)))
2021-10-15 14:08:27 +00:00
elif sort == "new":
posts = posts.order_by(Submission.created_utc.desc())
elif sort == "old":
posts = posts.order_by(Submission.created_utc.asc())
elif sort == "controversial":
2021-11-30 23:21:29 +00:00
posts = posts.order_by(-1 * Submission.upvotes * Submission.downvotes * Submission.downvotes)
2021-10-15 14:08:27 +00:00
elif sort == "top":
2021-11-30 23:21:29 +00:00
posts = posts.order_by(Submission.downvotes - Submission.upvotes)
2021-10-15 14:08:27 +00:00
elif sort == "bottom":
2021-11-30 23:21:29 +00:00
posts = posts.order_by(Submission.upvotes - Submission.downvotes)
2021-10-15 14:08:27 +00:00
elif sort == "comments":
posts = posts.order_by(Submission.comment_count.desc())
2021-12-11 05:02:58 +00:00
if v: size = v.frontsize or 0
2021-10-15 14:08:27 +00:00
else: size = 25
posts = posts.offset(size * (page - 1)).limit(size+1).all()
next_exists = (len(posts) > size)
posts = posts[:size]
2022-01-17 20:29:06 +00:00
if (sort == "hot" or v.id == Q_ID) and page == 1:
2021-12-09 21:21:52 +00:00
pins = g.db.query(Submission).filter(Submission.stickied != None, Submission.is_banned == False)
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()]
pins = pins.filter(Submission.author_id.notin_(blocking), Submission.author_id.notin_(blocked))
2021-10-15 14:08:27 +00:00
2021-12-24 03:59:07 +00:00
pins = pins.all()
for pin in pins:
2021-12-26 01:03:21 +00:00
if pin.stickied_utc and int(time.time()) > pin.stickied_utc:
2021-12-24 03:59:07 +00:00
pin.stickied = None
2021-12-26 01:03:21 +00:00
pin.stickied_utc = None
2021-12-24 03:59:07 +00:00
g.db.add(pin)
pins.remove(pin)
posts = pins + posts
2021-10-15 14:08:27 +00:00
2021-11-11 22:52:26 +00:00
if ids_only: posts = [x.id for x in posts]
2021-10-15 14:08:27 +00:00
2021-12-24 03:59:07 +00:00
g.db.commit()
2021-10-15 14:08:27 +00:00
return posts, next_exists
@app.get("/changelog")
2022-01-11 21:54:41 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def changelog(v):
page = int(request.values.get("page") or 1)
page = max(page, 1)
sort=request.values.get("sort", "new")
t=request.values.get('t', "all")
ids = changeloglist(sort=sort,
page=page,
t=t,
v=v,
)
next_exists = (len(ids) > 25)
ids = ids[:25]
posts = get_posts(ids, v=v)
if request.headers.get("Authorization"): return {"data": [x.json for x in posts], "next_exists": next_exists}
2022-01-14 12:04:35 +00:00
return render_template("changelog.html", v=v, listing=posts, next_exists=next_exists, sort=sort, t=t, page=page)
2021-10-15 14:08:27 +00:00
@cache.memoize(timeout=86400)
def changeloglist(v=None, sort="new", page=1 ,t="all"):
2021-11-06 15:52:48 +00:00
posts = g.db.query(Submission.id).filter_by(is_banned=False, private=False,).filter(Submission.deleted_utc == 0)
2021-10-15 14:08:27 +00:00
if v and v.admin_level == 0:
blocking = [x[0] for x in g.db.query(
2021-11-06 15:52:48 +00:00
UserBlock.target_id).filter_by(
2021-10-15 14:08:27 +00:00
user_id=v.id).all()]
blocked = [x[0] for x in g.db.query(
2021-11-06 15:52:48 +00:00
UserBlock.user_id).filter_by(
2021-10-15 14:08:27 +00:00
target_id=v.id).all()]
posts = posts.filter(
Submission.author_id.notin_(blocking),
Submission.author_id.notin_(blocked)
)
2021-11-15 22:13:29 +00:00
admins = [x[0] for x in g.db.query(User.id).filter(User.admin_level > 1).all()]
2021-10-15 14:08:27 +00:00
posts = posts.filter(Submission.title.ilike('_changelog%'), Submission.author_id.in_(admins))
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)
if sort == "new":
posts = posts.order_by(Submission.created_utc.desc())
elif sort == "old":
posts = posts.order_by(Submission.created_utc.asc())
elif sort == "controversial":
2021-11-30 23:21:29 +00:00
posts = posts.order_by(-1 * Submission.upvotes * Submission.downvotes * Submission.downvotes)
2021-10-15 14:08:27 +00:00
elif sort == "top":
2021-11-30 23:21:29 +00:00
posts = posts.order_by(Submission.downvotes - Submission.upvotes)
2021-10-15 14:08:27 +00:00
elif sort == "bottom":
2021-11-30 23:21:29 +00:00
posts = posts.order_by(Submission.upvotes - Submission.downvotes)
2021-10-15 14:08:27 +00:00
elif sort == "comments":
posts = posts.order_by(Submission.comment_count.desc())
posts = posts.offset(25 * (page - 1)).limit(26).all()
return [x[0] for x in posts]
@app.get("/random")
2022-01-11 21:54:41 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def random_post(v):
2021-11-06 15:52:48 +00:00
x = g.db.query(Submission).filter(Submission.deleted_utc == 0, Submission.is_banned == False)
2021-10-15 14:08:27 +00:00
total = x.count()
n = random.randint(1, total - 2)
2022-01-02 00:06:46 +00:00
post = x.offset(n).limit(1).one_or_none()
2021-10-15 14:08:27 +00:00
return redirect(f"/post/{post.id}")
@cache.memoize(timeout=86400)
2021-12-17 17:55:11 +00:00
def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all"):
2021-10-15 14:08:27 +00:00
2021-11-06 15:52:48 +00:00
posts = g.db.query(Submission)
cc_idlist = [x[0] for x in g.db.query(Submission.id).filter(Submission.club == True).all()]
2021-10-15 14:08:27 +00:00
posts = posts.subquery()
2021-11-06 15:52:48 +00:00
comments = g.db.query(Comment.id).filter(Comment.parent_submission.notin_(cc_idlist))
2021-10-15 14:08:27 +00:00
if v and v.admin_level <= 3:
blocking = [x[0] for x in g.db.query(
2021-11-06 15:52:48 +00:00
UserBlock.target_id).filter_by(
2021-10-15 14:08:27 +00:00
user_id=v.id).all()]
blocked = [x[0] for x in g.db.query(
2021-11-06 15:52:48 +00:00
UserBlock.user_id).filter_by(
2021-10-15 14:08:27 +00:00
target_id=v.id).all()]
2021-11-16 03:34:37 +00:00
comments = comments.filter(Comment.author_id.notin_(blocking), Comment.author_id.notin_(blocked))
2021-10-15 14:08:27 +00:00
2021-11-15 22:19:59 +00:00
if not v or not v.admin_level > 1:
2021-11-16 03:34:37 +00:00
comments = comments.filter(Comment.is_banned==False, Comment.deleted_utc == 0)
2021-10-15 14:08:27 +00:00
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())
elif sort == "old":
comments = comments.order_by(Comment.created_utc.asc())
elif sort == "controversial":
2021-11-30 23:21:29 +00:00
comments = comments.order_by(-1 * Comment.upvotes * Comment.downvotes * Comment.downvotes)
2021-10-15 14:08:27 +00:00
elif sort == "top":
2022-01-17 11:06:12 +00:00
comments = comments.order_by(Comment.downvotes - Comment.upvotes)
2021-10-15 14:08:27 +00:00
elif sort == "bottom":
2021-11-30 23:21:29 +00:00
comments = comments.order_by(Comment.upvotes - Comment.downvotes)
2021-10-15 14:08:27 +00:00
comments = comments.offset(25 * (page - 1)).limit(26).all()
return [x[0] for x in comments]
@app.get("/comments")
2022-01-11 21:54:41 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def all_comments(v):
page = int(request.values.get("page", 1))
sort=request.values.get("sort", "new")
t=request.values.get("t", defaulttimefilter)
2021-12-05 18:58:46 +00:00
idlist = comment_idlist(v=v,
page=page,
sort=sort,
t=t,
2021-12-17 17:55:11 +00:00
)
comments = get_comments(idlist, v=v)
2021-10-15 14:08:27 +00:00
next_exists = len(idlist) > 25
idlist = idlist[:25]
if request.headers.get("Authorization"): return {"data": [x.json for x in comments]}
2022-01-17 12:06:26 +00:00
return render_template("home_comments.html", v=v, sort=sort, t=t, page=page, comments=comments, standalone=True, next_exists=next_exists)
@app.get("/transfers")
@auth_required
def transfers(v):
page = int(request.values.get("page", 1))
2022-01-17 17:30:39 +00:00
comments = g.db.query(Comment).filter(Comment.author_id == NOTIFICATIONS_ID, Comment.parent_submission == None, Comment.distinguish_level == 6, Comment.body_html.like("%</a> has transferred %"), Comment.created_utc == 0).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all()
2022-01-17 12:06:26 +00:00
next_exists = len(comments) > 25
comments = comments[:25]
2022-01-17 17:28:28 +00:00
if request.headers.get("Authorization"): return {"data": [x.json for x in comments]}
2022-01-17 14:14:24 +00:00
return render_template("transfers.html", v=v, page=page, comments=comments, standalone=True, next_exists=next_exists)