rDrama/files/routes/front.py

602 lines
20 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-03-29 14:08:28 +00:00
from files.helpers.discord 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-15 14:08:27 +00:00
2021-11-11 00:26:25 +00:00
@app.post("/clear")
@auth_required
def clear(v):
2022-04-04 10:34:49 +00:00
notifs = g.db.query(Notification).join(Comment, Notification.comment_id == Comment.id).filter(Notification.read == False, Notification.user_id == v.id).all()
2022-04-03 23:21:06 +00:00
for n in notifs:
2021-11-11 00:26:25 +00:00
n.read = True
g.db.add(n)
g.db.commit()
return {"message": "Notifications cleared!"}
2022-01-21 23:04:10 +00:00
@app.get("/unread")
@auth_required
def unread(v):
2022-04-03 23:21:06 +00:00
listing = g.db.query(Notification, Comment).join(Comment, Notification.comment_id == Comment.id).filter(
2022-01-21 23:04:10 +00:00
Notification.read == False,
Notification.user_id == v.id,
Comment.is_banned == False,
Comment.deleted_utc == 0,
Comment.author_id != AUTOJANNY_ID,
2022-02-16 01:16:01 +00:00
).order_by(Notification.created_utc.desc()).all()
2022-01-21 23:04:10 +00:00
2022-04-03 23:21:06 +00:00
for n, c in listing:
2022-01-21 23:04:10 +00:00
n.read = True
g.db.add(n)
g.db.commit()
2022-04-03 23:21:06 +00:00
return {"data":[x[1].json for x in listing]}
2022-01-21 23:04:10 +00:00
2022-02-03 10:49:47 +00:00
2021-10-15 14:08:27 +00:00
@app.get("/notifications")
@auth_required
def notifications(v):
2022-04-03 22:26:09 +00:00
t = time.time()
2021-10-15 14:08:27 +00:00
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')
2022-04-03 17:37:20 +00:00
reddit = request.values.get('reddit')
2022-04-02 19:35:27 +00:00
if modmail and v.admin_level > 1:
2022-02-21 03:07:36 +00:00
comments = g.db.query(Comment).filter(Comment.sentto==2).order_by(Comment.id.desc()).offset(25*(page-1)).limit(26).all()
next_exists = (len(comments) > 25)
2022-04-03 23:24:59 +00:00
listing = comments[:25]
2021-10-15 14:08:27 +00:00
elif messages:
2022-04-03 23:05:21 +00:00
comments = g.db.query(Comment).filter(Comment.sentto != None, or_(Comment.author_id==v.id, Comment.sentto==v.id), Comment.parent_submission == None, Comment.level == 1).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-04-03 23:24:59 +00:00
listing = comments[:25]
2021-10-15 14:08:27 +00:00
elif posts:
2022-04-03 23:15:57 +00:00
notifications = g.db.query(Notification, Comment).join(Comment, Notification.comment_id == Comment.id).filter(Notification.user_id == v.id, Comment.author_id == AUTOJANNY_ID).order_by(Notification.created_utc.desc()).offset(25 * (page - 1)).limit(101).all()
2022-02-03 10:49:47 +00:00
listing = []
2021-10-15 14:08:27 +00:00
2022-04-03 23:21:06 +00:00
for index, x in enumerate(notifications[:100]):
n, c = x
2022-04-03 23:15:57 +00:00
if n.read and index > 24: break
elif not n.read:
n.read = True
2022-02-03 10:49:47 +00:00
c.unread = True
2022-04-03 23:15:57 +00:00
g.db.add(n)
if n.created_utc > 1620391248: c.notif_utc = n.created_utc
2022-02-03 10:49:47 +00:00
listing.append(c)
2021-10-15 14:08:27 +00:00
g.db.commit()
next_exists = (len(notifications) > len(listing))
2022-04-03 17:37:20 +00:00
elif reddit:
2022-04-03 23:15:57 +00:00
notifications = g.db.query(Notification, Comment).join(Comment, Notification.comment_id == Comment.id).filter(Notification.user_id == v.id, Comment.body_html.like('<html><body><p>New rdrama mention: <a href="https://old.reddit.com/r/%')).order_by(Notification.created_utc.desc()).offset(25 * (page - 1)).limit(101).all()
2021-10-15 14:08:27 +00:00
2022-04-03 17:37:20 +00:00
listing = []
2022-04-03 23:21:06 +00:00
for index, x in enumerate(notifications[:100]):
n, c = x
2022-04-03 23:15:57 +00:00
if n.read and index > 24: break
elif not n.read:
n.read = True
2022-04-03 17:37:20 +00:00
c.unread = True
2022-04-03 23:15:57 +00:00
g.db.add(n)
if n.created_utc > 1620391248: c.notif_utc = n.created_utc
2022-04-03 17:37:20 +00:00
listing.append(c)
g.db.commit()
next_exists = (len(notifications) > len(listing))
2021-10-15 14:08:27 +00:00
else:
2022-04-03 23:05:21 +00:00
unread = g.db.query(Notification, Comment).join(Comment, Notification.comment_id == Comment.id).filter(
Notification.read == False,
Notification.user_id == v.id,
Comment.author_id != AUTOJANNY_ID,
2022-04-03 23:34:37 +00:00
Comment.body_html.notlike('<html><body><p>New rdrama mention: <a href="https://old.reddit.com/r/%'))
2022-04-03 23:15:57 +00:00
for n, c in unread:
n.read = True
c.unread = True
g.db.add(c)
2022-04-03 23:05:21 +00:00
g.db.commit()
2022-04-04 02:01:06 +00:00
sq = g.db.query(Comment.id, Notification.created_utc).join(Notification).distinct(Comment.top_comment_id).filter(
2022-04-03 23:57:47 +00:00
Notification.user_id == v.id,
Comment.is_banned == False,
2022-04-03 23:05:21 +00:00
Comment.deleted_utc == 0,
Comment.author_id != AUTOJANNY_ID,
Comment.body_html.notlike('<html><body><p>New rdrama mention: <a href="https://old.reddit.com/r/%')
2022-04-04 02:34:35 +00:00
).order_by(Comment.top_comment_id.desc(), Notification.created_utc.desc()).subquery()
2022-04-04 01:10:47 +00:00
2022-04-04 02:01:06 +00:00
comments = g.db.query(Comment).join(sq, sq.c.id == Comment.id).order_by(sq.c.created_utc.desc()).offset(25 * (page - 1)).limit(26).all()
2022-04-04 00:38:00 +00:00
next_exists = (len(comments) > 25)
comments = comments[:25]
2022-04-03 23:05:21 +00:00
2022-04-04 01:00:50 +00:00
cids = set([x[0] for x in g.db.query(Comment.id).join(Notification).filter(
2022-04-04 00:47:47 +00:00
Notification.user_id == v.id,
Comment.is_banned == False,
Comment.deleted_utc == 0,
Comment.author_id != AUTOJANNY_ID,
Comment.body_html.notlike('<html><body><p>New rdrama mention: <a href="https://old.reddit.com/r/%')
2022-04-04 02:01:06 +00:00
).order_by(Notification.created_utc.desc()).offset(25 * (page - 1)).limit(500).all()] + [x.id for x in comments])
2022-04-04 00:54:53 +00:00
2022-04-04 00:56:16 +00:00
comms = get_comments(list(cids), v=v)
2022-04-04 00:54:53 +00:00
listing = []
for c in comments:
if c.parent_submission:
if c.replies2 == None:
2022-04-04 01:00:50 +00:00
c.replies2 = c.child_comments.filter(or_(Comment.author_id == v.id, Comment.id.in_(cids))).all()
2022-04-04 00:54:53 +00:00
for x in c.replies2:
if x.replies2 == None: x.replies2 = []
while c.parent_comment and (c.parent_comment.author_id == v.id or c.parent_comment.id in cids):
c = c.parent_comment
if c.replies2 == None:
2022-04-04 01:00:50 +00:00
c.replies2 = c.child_comments.filter(or_(Comment.author_id == v.id, Comment.id.in_(cids))).all()
2022-04-04 00:54:53 +00:00
for x in c.replies2:
if x.replies2 == None: x.replies2 = []
2022-04-04 00:56:16 +00:00
cids.add(c.id)
2022-04-04 00:54:53 +00:00
else:
while c.parent_comment:
c = c.parent_comment
c.replies2 = g.db.query(Comment).filter_by(parent_comment_id=c.id).order_by(Comment.id).all()
if c not in listing: listing.append(c)
2022-04-04 17:52:14 +00:00
print(time.time() - t)
2022-04-04 00:54:53 +00:00
if request.headers.get("Authorization"): return {"data":[x.json for x in listing]}
return render_template("notifications.html",
v=v,
notifications=listing,
next_exists=next_exists,
page=page,
standalone=True,
render_replies=True
)
2021-10-15 14:08:27 +00:00
2022-03-04 20:58:45 +00:00
@app.get("/")
2022-03-09 02:04:37 +00:00
@app.get("/h/<sub>")
2022-03-09 03:06:51 +00:00
@app.get("/s/<sub>")
2022-02-20 01:09:55 +00:00
@limiter.limit("3/second;30/minute;1000/hour;5000/day")
2022-01-11 22:57:05 +00:00
@auth_desired
2022-03-04 20:42:28 +00:00
def front_all(v, sub=None, subdomain=None):
2022-02-10 20:54:45 +00:00
if sub: sub = g.db.query(Sub).filter_by(name=sub.strip().lower()).one_or_none()
2022-02-05 21:51:00 +00:00
2022-03-09 04:23:35 +00:00
if (request.path.startswith('/h/') or request.path.startswith('/s/')) and not sub: abort(404)
2022-02-05 21:09:17 +00:00
2022-02-04 04:16:52 +00:00
if g.webview and not session.get("session_id"):
2022-01-27 20:15:05 +00:00
session["session_id"] = secrets.token_hex(49)
2021-10-15 14:08:27 +00:00
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
2022-04-02 16:54:27 +00:00
if sub or SITE_NAME != 'rDrama': defaulttime = 'all'
2022-02-08 19:12:06 +00:00
else: defaulttime = v.defaulttime
2021-10-15 14:08:27 +00:00
else:
defaultsorting = "hot"
2022-04-02 16:54:27 +00:00
if sub or SITE_NAME != 'rDrama': defaulttime = 'all'
2022-02-08 20:07:52 +00:00
else: defaulttime = defaulttimefilter
2021-10-15 14:08:27 +00:00
sort=request.values.get("sort", defaultsorting)
t=request.values.get('t', defaulttime)
2022-02-25 17:39:25 +00:00
ccmode=request.values.get('ccmode', "false").lower()
2022-03-05 22:46:56 +00:00
if sort == 'bump': t='all'
2022-03-03 00:22:11 +00:00
2022-03-27 13:39:21 +00:00
try: gt=int(request.values.get("after", 0))
2022-02-08 10:42:45 +00:00
except: gt=0
2022-03-27 13:39:21 +00:00
try: lt=int(request.values.get("before", 0))
2022-02-08 10:42:45 +00:00
except: lt=0
2022-03-21 20:21:22 +00:00
if v: subs = v.subs
2022-04-02 16:54:27 +00:00
elif SITE_NAME == 'rDrama': subs = 1
2022-03-21 20:21:22 +00:00
else: subs = 2
2022-03-05 23:53:43 +00:00
2021-10-15 14:08:27 +00:00
ids, next_exists = frontlist(sort=sort,
page=page,
t=t,
v=v,
2022-01-31 21:32:11 +00:00
ccmode=ccmode,
2021-10-15 14:08:27 +00:00
filter_words=v.filter_words if v else [],
2022-02-08 10:42:45 +00:00
gt=gt,
lt=lt,
2022-02-10 22:21:32 +00:00
sub=sub,
2022-02-25 16:28:19 +00:00
site=SITE,
2022-03-05 23:53:43 +00:00
subs=subs
2021-10-15 14:08:27 +00:00
)
posts = get_posts(ids, v=v)
2022-02-03 07:00:43 +00:00
2022-02-04 04:21:47 +00:00
if v:
if v.hidevotedon: posts = [x for x in posts if not hasattr(x, 'voted') or not x.voted]
if v.patron_utc and v.patron_utc < time.time():
v.patron = 0
v.patron_utc = 0
send_repeatable_notification(v.id, "Your paypig status has expired!")
2022-03-27 13:39:21 +00:00
if v.discord_id: remove_role(v, "1")
2022-02-04 04:21:47 +00:00
g.db.add(v)
g.db.commit()
if v.unban_utc and v.unban_utc < time.time():
v.is_banned = 0
v.unban_utc = 0
v.ban_evade = 0
send_repeatable_notification(v.id, "You have been unbanned!")
g.db.add(v)
g.db.commit()
2022-02-12 23:10:29 +00:00
if v.agendaposter and v.agendaposter < time.time():
v.agendaposter = 0
2022-02-04 04:21:47 +00:00
send_repeatable_notification(v.id, "Your chud theme has expired!")
g.db.add(v)
2022-02-19 21:42:55 +00:00
badge = v.has_badge(28)
2022-02-04 04:21:47 +00:00
if badge: g.db.delete(badge)
g.db.commit()
if v.flairchanged and v.flairchanged < time.time():
v.flairchanged = None
send_repeatable_notification(v.id, "Your flair lock has expired. You can now change your flair!")
g.db.add(v)
badge = v.has_badge(96)
if badge: g.db.delete(badge)
g.db.commit()
if v.marseyawarded and v.marseyawarded < time.time():
v.marseyawarded = None
send_repeatable_notification(v.id, "Your marsey award has expired!")
g.db.add(v)
badge = v.has_badge(98)
if badge: g.db.delete(badge)
g.db.commit()
if v.longpost and v.longpost < time.time():
v.longpost = None
send_repeatable_notification(v.id, "Your pizzashill award has expired!")
g.db.add(v)
badge = v.has_badge(97)
if badge: g.db.delete(badge)
g.db.commit()
if v.bird and v.bird < time.time():
v.bird = None
send_repeatable_notification(v.id, "Your bird site award has expired!")
g.db.add(v)
badge = v.has_badge(95)
if badge: g.db.delete(badge)
g.db.commit()
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)
badge = v.has_badge(94)
if badge: g.db.delete(badge)
g.db.commit()
if v.rehab and v.rehab < time.time():
v.rehab = None
send_repeatable_notification(v.id, "Your rehab has finished!")
g.db.add(v)
badge = v.has_badge(109)
if badge: g.db.delete(badge)
g.db.commit()
2021-10-15 14:08:27 +00:00
2022-03-23 22:42:33 +00:00
if v.deflector and v.deflector < time.time():
v.deflector = None
send_repeatable_notification(v.id, "Your deflector has expired!")
g.db.add(v)
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-03-05 23:53:43 +00:00
return render_template("home.html", v=v, listing=posts, next_exists=next_exists, sort=sort, t=t, page=page, ccmode=ccmode, sub=sub, home=True)
2021-10-15 14:08:27 +00:00
@cache.memoize(timeout=86400)
2022-03-29 17:35:11 +00:00
def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false", filter_words='', gt=0, lt=0, sub=None, site=None, subs=2):
2021-10-15 14:08:27 +00:00
2021-11-11 22:52:26 +00:00
posts = g.db.query(Submission)
2022-02-08 10:42:45 +00:00
2022-03-26 11:47:00 +00:00
if v and v.hidevotedon:
voted = [x[0] for x in g.db.query(Vote.submission_id).filter_by(user_id=v.id).all()]
posts = posts.filter(Submission.id.notin_(voted))
2022-03-05 23:53:43 +00:00
if sub:
posts = posts.filter_by(sub=sub.name)
2022-03-29 17:39:47 +00:00
elif not v:
if subs == 1: posts = posts.filter(Submission.sub == None)
elif v.subs == 1:
2022-03-05 23:53:43 +00:00
posts = posts.filter(or_(Submission.sub == None, Submission.sub.in_(v.subbed_subs)))
2022-03-29 17:39:47 +00:00
elif v.subs == 2:
2022-03-05 23:53:43 +00:00
posts = posts.filter(or_(Submission.sub == None, Submission.sub.notin_(v.all_blocks)))
2022-03-29 17:39:47 +00:00
elif v.subs == 3:
2022-03-05 23:53:43 +00:00
posts = posts.filter(Submission.sub != None, Submission.sub.notin_(v.all_blocks))
2022-02-04 14:49:27 +00:00
2022-02-08 10:42:45 +00:00
if gt: posts = posts.filter(Submission.created_utc > gt)
if lt: posts = posts.filter(Submission.created_utc < lt)
if not gt and not lt:
if t == 'all': cutoff = 0
else:
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-31 21:32:11 +00:00
if (ccmode == "true"):
posts = posts.filter(Submission.club == True)
2022-02-08 13:49:17 +00:00
posts = posts.filter_by(is_banned=False, private=False, deleted_utc = 0)
2022-02-08 13:49:42 +00:00
if (sort == "hot" or (v and v.id == Q_ID)) and ccmode == "false" and not gt and not lt:
2022-02-08 13:49:17 +00:00
posts = posts.filter_by(stickied=None)
2021-10-15 14:08:27 +00:00
2022-03-05 20:53:39 +00:00
if v and v.admin_level < 2:
posts = posts.filter(Submission.author_id.notin_(v.userblocks))
2021-10-15 14:08:27 +00:00
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:
2022-03-04 22:46:20 +00:00
word = word.replace('\\', '').replace('_', '\_').replace('%', '\%').strip()
2021-10-15 14:08:27 +00:00
posts=posts.filter(not_(Submission.title.ilike(f'%{word}%')))
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:44:49 +00:00
if sort == "hot":
2021-10-15 14:08:27 +00:00
ti = int(time.time()) + 3600
2022-03-30 15:56:20 +00:00
posts = posts.order_by(-1000000*(Submission.realupvotes + 1 + Submission.comment_count/5 + (func.length(Submission.body_html)-func.length(func.replace(Submission.body_html,'</a>','')))/4)/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc())
2022-03-05 22:46:56 +00:00
elif sort == "bump":
2022-03-21 21:46:10 +00:00
posts = posts.filter(Submission.comment_count > 1).order_by(Submission.bump_utc.desc(), Submission.created_utc.desc())
2021-10-15 14:08:27 +00:00
elif sort == "new":
posts = posts.order_by(Submission.created_utc.desc())
elif sort == "old":
2022-03-21 21:46:10 +00:00
posts = posts.order_by(Submission.created_utc)
2021-10-15 14:08:27 +00:00
elif sort == "controversial":
2022-03-21 21:46:10 +00:00
posts = posts.order_by((Submission.upvotes+1)/(Submission.downvotes+1) + (Submission.downvotes+1)/(Submission.upvotes+1), Submission.downvotes.desc(), Submission.created_utc.desc())
2021-10-15 14:08:27 +00:00
elif sort == "top":
2022-03-21 21:46:10 +00:00
posts = posts.order_by(Submission.downvotes - Submission.upvotes, Submission.created_utc.desc())
2021-10-15 14:08:27 +00:00
elif sort == "bottom":
2022-03-21 21:46:10 +00:00
posts = posts.order_by(Submission.upvotes - Submission.downvotes, Submission.created_utc.desc())
2021-10-15 14:08:27 +00:00
elif sort == "comments":
2022-03-21 21:46:10 +00:00
posts = posts.order_by(Submission.comment_count.desc(), Submission.created_utc.desc())
2021-10-15 14:08:27 +00:00
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-02-08 13:49:17 +00:00
if (sort == "hot" or (v and v.id == Q_ID)) and page == 1 and ccmode == "false" and not gt and not lt:
2021-12-09 21:21:52 +00:00
pins = g.db.query(Submission).filter(Submission.stickied != None, Submission.is_banned == False)
2022-03-05 23:53:43 +00:00
if sub:
pins = pins.filter_by(sub=sub.name)
2022-03-29 17:38:25 +00:00
elif not v:
2022-03-29 17:39:47 +00:00
if subs == 1: pins = pins.filter(Submission.sub == None)
elif v.subs == 1:
2022-03-05 23:53:43 +00:00
pins = pins.filter(or_(Submission.sub == None, Submission.sub.in_(v.subbed_subs)))
2022-03-29 17:39:47 +00:00
elif v.subs == 2:
2022-03-05 23:53:43 +00:00
pins = pins.filter(or_(Submission.sub == None, Submission.sub.notin_(v.all_blocks)))
2022-03-29 17:39:47 +00:00
elif v.subs == 3:
2022-03-09 01:44:53 +00:00
pins = pins.filter(Submission.sub != None, Submission.sub.notin_(v.all_blocks))
2022-02-08 19:05:06 +00:00
2022-03-05 20:53:39 +00:00
if v and v.admin_level < 2:
pins = pins.filter(Submission.author_id.notin_(v.userblocks))
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
2022-03-05 20:53:39 +00:00
if v.admin_level < 2:
posts = posts.filter(Submission.author_id.notin_(v.userblocks))
2021-10-15 14:08:27 +00:00
2022-02-04 09:32:18 +00:00
admins = [x[0] for x in g.db.query(User.id).filter(User.admin_level > 0).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":
2022-03-21 21:46:10 +00:00
posts = posts.order_by(Submission.created_utc)
2021-10-15 14:08:27 +00:00
elif sort == "controversial":
2022-02-09 23:29:34 +00:00
posts = posts.order_by((Submission.upvotes+1)/(Submission.downvotes+1) + (Submission.downvotes+1)/(Submission.upvotes+1), Submission.downvotes.desc())
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):
2022-03-23 19:33:27 +00:00
x = g.db.query(Submission).filter(Submission.deleted_utc == 0, Submission.is_banned == False, Submission.private == 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()
2022-04-02 17:11:35 +00:00
return redirect(f"/post/{post.id}")
2021-10-15 14:08:27 +00:00
2022-03-26 20:14:53 +00:00
@app.get("/comments")
@auth_required
def all_comments(v):
page = int(request.values.get("page", 1))
sort=request.values.get("sort", "new")
t=request.values.get("t", defaulttimefilter)
2022-03-27 13:39:21 +00:00
try: gt=int(request.values.get("after", 0))
2022-03-26 20:14:53 +00:00
except: gt=0
2022-03-27 13:39:21 +00:00
try: lt=int(request.values.get("before", 0))
2022-03-26 20:14:53 +00:00
except: lt=0
idlist = comment_idlist(v=v,
page=page,
sort=sort,
t=t,
gt=gt,
lt=lt
)
comments = get_comments(idlist, v=v)
next_exists = len(idlist) > 25
idlist = idlist[:25]
if request.headers.get("Authorization"): return {"data": [x.json for x in comments]}
return render_template("home_comments.html", v=v, sort=sort, t=t, page=page, comments=comments, standalone=True, next_exists=next_exists)
2021-10-15 14:08:27 +00:00
@cache.memoize(timeout=86400)
2022-03-26 20:14:53 +00:00
def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all", gt=0, lt=0):
2021-10-15 14:08:27 +00:00
2022-03-05 20:53:39 +00:00
comments = g.db.query(Comment.id).filter(Comment.parent_submission != None)
2021-10-15 14:08:27 +00:00
2022-03-05 20:53:39 +00:00
if v.admin_level < 2:
private = [x[0] for x in g.db.query(Submission.id).filter(Submission.private == True).all()]
2021-10-15 14:08:27 +00:00
2022-03-05 20:53:39 +00:00
comments = comments.filter(Comment.author_id.notin_(v.userblocks), Comment.is_banned==False, Comment.deleted_utc == 0, Comment.parent_submission.notin_(private))
2021-10-15 14:08:27 +00:00
2022-03-05 20:53:39 +00:00
if not v.paid_dues:
club = [x[0] for x in g.db.query(Submission.id).filter(Submission.club == True).all()]
comments = comments.filter(Comment.parent_submission.notin_(club))
2021-10-15 14:08:27 +00:00
2022-03-26 20:14:53 +00:00
if gt: comments = comments.filter(Comment.created_utc > gt)
if lt: comments = comments.filter(Comment.created_utc < lt)
if not gt and not lt:
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)
2021-10-15 14:08:27 +00:00
if sort == "new":
comments = comments.order_by(Comment.created_utc.desc())
elif sort == "old":
2022-03-21 21:46:10 +00:00
comments = comments.order_by(Comment.created_utc)
2021-10-15 14:08:27 +00:00
elif sort == "controversial":
2022-02-09 23:29:34 +00:00
comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc())
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]
2022-01-17 12:06:26 +00:00
@app.get("/transfers")
@auth_required
def transfers(v):
2022-03-04 16:53:28 +00:00
comments = g.db.query(Comment).filter(Comment.author_id == NOTIFICATIONS_ID, Comment.parent_submission == None, Comment.body_html.like("%</a> has transferred %")).order_by(Comment.id.desc())
2022-01-17 12:06:26 +00:00
2022-01-18 10:36:52 +00:00
if request.headers.get("Authorization"): return {"data": [x.json for x in comments.all()]}
2022-01-17 12:06:26 +00:00
2022-01-18 10:36:52 +00:00
page = int(request.values.get("page", 1))
comments = comments.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 14:14:24 +00:00
return render_template("transfers.html", v=v, page=page, comments=comments, standalone=True, next_exists=next_exists)