2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
from sqlalchemy import or_, not_
|
2023-05-01 19:50:32 +00:00
|
|
|
from sqlalchemy.orm import load_only
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
from files.classes.submission import Submission
|
|
|
|
from files.classes.votes import Vote
|
|
|
|
from files.helpers.awards import award_timers
|
2022-12-11 23:44:34 +00:00
|
|
|
from files.helpers.config.const import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.get import *
|
2022-07-09 10:32:49 +00:00
|
|
|
from files.helpers.sorting_and_time import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.routes.wrappers import *
|
2022-05-04 23:09:46 +00:00
|
|
|
from files.__main__ import app, cache, limiter
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
|
@app.get("/h/<sub>")
|
2023-02-26 01:42:39 +00:00
|
|
|
@limiter.limit("30/minute;5000/hour;10000/day")
|
2022-08-05 20:40:48 +00:00
|
|
|
@auth_desired_with_logingate
|
2022-05-04 23:09:46 +00:00
|
|
|
def front_all(v, sub=None, subdomain=None):
|
2022-08-19 21:31:26 +00:00
|
|
|
if sub:
|
2022-10-05 10:30:44 +00:00
|
|
|
sub = get_sub_by_name(sub, graceful=True)
|
2022-12-04 18:39:06 +00:00
|
|
|
if sub and not User.can_see(v, sub):
|
|
|
|
abort(403)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if (request.path.startswith('/h/') or request.path.startswith('/s/')) and not sub: abort(404)
|
|
|
|
|
|
|
|
try: page = max(int(request.values.get("page", 1)), 1)
|
|
|
|
except: abort(400)
|
|
|
|
|
|
|
|
if v:
|
|
|
|
defaultsorting = v.defaultsorting
|
|
|
|
if sub or SITE_NAME != 'rDrama': defaulttime = 'all'
|
|
|
|
else: defaulttime = v.defaulttime
|
|
|
|
else:
|
|
|
|
defaultsorting = "hot"
|
|
|
|
if sub or SITE_NAME != 'rDrama': defaulttime = 'all'
|
2022-07-08 16:21:13 +00:00
|
|
|
else: defaulttime = DEFAULT_TIME_FILTER
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
sort=request.values.get("sort", defaultsorting)
|
|
|
|
t=request.values.get('t', defaulttime)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
try: gt=int(request.values.get("after", 0))
|
|
|
|
except: gt=0
|
|
|
|
|
|
|
|
try: lt=int(request.values.get("before", 0))
|
|
|
|
except: lt=0
|
|
|
|
|
2022-07-13 19:32:28 +00:00
|
|
|
if sort == 'hot': default = True
|
|
|
|
else: default = False
|
|
|
|
|
|
|
|
pins = session.get(sort, default)
|
2022-07-13 17:31:35 +00:00
|
|
|
|
2023-05-01 19:50:32 +00:00
|
|
|
ids, next_exists, size = frontlist(sort=sort,
|
2022-05-04 23:09:46 +00:00
|
|
|
page=page,
|
|
|
|
t=t,
|
|
|
|
v=v,
|
|
|
|
filter_words=v.filter_words if v else [],
|
|
|
|
gt=gt,
|
|
|
|
lt=lt,
|
|
|
|
sub=sub,
|
2022-11-07 00:44:31 +00:00
|
|
|
pins=pins,
|
2022-05-04 23:09:46 +00:00
|
|
|
)
|
|
|
|
|
2022-11-09 14:16:22 +00:00
|
|
|
posts = get_posts(ids, v=v, eager=True)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if v:
|
2023-03-15 06:03:11 +00:00
|
|
|
if v.hidevotedon: posts = [x for x in posts if not hasattr(x, 'voted') or not x.voted]
|
2022-06-10 09:47:41 +00:00
|
|
|
award_timers(v)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
if v and v.client: return {"data": [x.json(g.db) for x in posts], "next_exists": next_exists}
|
2023-05-04 16:48:30 +00:00
|
|
|
return render_template("home.html", v=v, listing=posts, next_exists=next_exists, sort=sort, t=t, page=page, sub=sub, home=True, pins=pins, size=size)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
2023-01-28 10:45:51 +00:00
|
|
|
LIMITED_WPD_HOLES = ('gore', 'aftermath', 'selfharm', 'meta', 'discussion', 'social', 'music', 'request')
|
2023-01-24 05:43:46 +00:00
|
|
|
|
2023-02-02 00:42:47 +00:00
|
|
|
@cache.memoize()
|
2023-05-04 16:48:30 +00:00
|
|
|
def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='', gt=0, lt=0, sub=None, pins=True):
|
2023-03-16 06:27:58 +00:00
|
|
|
posts = g.db.query(Submission)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2023-03-15 06:03:11 +00:00
|
|
|
if v and v.hidevotedon:
|
|
|
|
posts = posts.outerjoin(Vote,
|
|
|
|
and_(Vote.submission_id == Submission.id, Vote.user_id == v.id)
|
|
|
|
).filter(Vote.submission_id == None)
|
|
|
|
|
2022-11-09 11:27:44 +00:00
|
|
|
if sub: posts = posts.filter(Submission.sub == sub.name)
|
2022-05-04 23:09:46 +00:00
|
|
|
elif v: posts = posts.filter(or_(Submission.sub == None, Submission.sub.notin_(v.all_blocks)))
|
|
|
|
|
|
|
|
if gt: posts = posts.filter(Submission.created_utc > gt)
|
|
|
|
if lt: posts = posts.filter(Submission.created_utc < lt)
|
|
|
|
|
|
|
|
if not gt and not lt:
|
2022-07-09 10:32:49 +00:00
|
|
|
posts = apply_time_filter(t, posts, Submission)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-09 11:27:44 +00:00
|
|
|
posts = posts.filter(
|
|
|
|
Submission.is_banned == False,
|
|
|
|
Submission.private == False,
|
|
|
|
Submission.deleted_utc == 0,
|
|
|
|
)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-07 00:44:31 +00:00
|
|
|
if pins and not gt and not lt:
|
2022-11-09 11:27:44 +00:00
|
|
|
if sub: posts = posts.filter(Submission.hole_pinned == None)
|
|
|
|
else: posts = posts.filter(Submission.stickied == None)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-05-24 20:43:49 +00:00
|
|
|
if v:
|
2022-05-04 23:09:46 +00:00
|
|
|
posts = posts.filter(Submission.author_id.notin_(v.userblocks))
|
|
|
|
|
|
|
|
if v and filter_words:
|
|
|
|
for word in filter_words:
|
2022-09-04 23:15:37 +00:00
|
|
|
word = word.replace('\\', '').replace('_', '\_').replace('%', '\%').strip()
|
2022-05-04 23:09:46 +00:00
|
|
|
posts=posts.filter(not_(Submission.title.ilike(f'%{word}%')))
|
|
|
|
|
2023-02-27 15:38:12 +00:00
|
|
|
posts = sort_objects(sort, posts, Submission)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if v: size = v.frontsize or 0
|
2022-10-29 03:20:48 +00:00
|
|
|
else: size = PAGE_SIZE
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-05-01 19:50:32 +00:00
|
|
|
next_exists = posts.count()
|
|
|
|
posts = posts.options(load_only(Submission.id)).offset(size * (page - 1))
|
|
|
|
|
2022-11-06 08:31:28 +00:00
|
|
|
if SITE_NAME == 'WPD' and sort == "hot" and sub == None:
|
2023-05-01 19:50:32 +00:00
|
|
|
posts = posts.limit(200).all()
|
2023-01-24 05:43:46 +00:00
|
|
|
|
|
|
|
to_remove = []
|
|
|
|
for h in LIMITED_WPD_HOLES:
|
|
|
|
to_remove += [x.id for x in posts if x.sub == h][1:]
|
|
|
|
|
2023-05-01 20:15:13 +00:00
|
|
|
posts = [x for x in posts if x.id not in to_remove][:size]
|
2022-11-06 05:18:23 +00:00
|
|
|
else:
|
2023-05-01 19:50:32 +00:00
|
|
|
posts = posts.limit(size).all()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-07 00:44:31 +00:00
|
|
|
if pins and page == 1 and not gt and not lt:
|
2022-07-01 23:11:48 +00:00
|
|
|
if sub:
|
2023-05-01 19:50:32 +00:00
|
|
|
pins = g.db.query(Submission).options(load_only(Submission.id)).filter(Submission.sub == sub.name, Submission.hole_pinned != None)
|
2022-07-01 23:18:41 +00:00
|
|
|
else:
|
2023-05-01 19:50:32 +00:00
|
|
|
pins = g.db.query(Submission).options(load_only(Submission.id)).filter(Submission.stickied != None, Submission.is_banned == False)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-07-01 23:18:41 +00:00
|
|
|
if v:
|
|
|
|
pins = pins.filter(or_(Submission.sub == None, Submission.sub.notin_(v.all_blocks)))
|
|
|
|
for pin in pins:
|
|
|
|
if pin.stickied_utc and int(time.time()) > pin.stickied_utc:
|
|
|
|
pin.stickied = None
|
|
|
|
pin.stickied_utc = None
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(pin)
|
2022-07-01 23:18:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
if v: pins = pins.filter(Submission.author_id.notin_(v.userblocks))
|
2022-10-23 17:01:00 +00:00
|
|
|
if SITE_NAME == 'rDrama':
|
2022-10-23 18:28:51 +00:00
|
|
|
pins = pins.order_by(Submission.author_id != LAWLZ_ID)
|
2022-07-01 23:11:48 +00:00
|
|
|
pins = pins.order_by(Submission.created_utc.desc()).all()
|
2022-05-04 23:09:46 +00:00
|
|
|
posts = pins + posts
|
|
|
|
|
|
|
|
if ids_only: posts = [x.id for x in posts]
|
2023-05-01 19:50:32 +00:00
|
|
|
return posts, next_exists, size
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/random_post")
|
2023-02-26 08:41:04 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT)
|
2023-01-21 04:39:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-11-26 21:00:03 +00:00
|
|
|
def random_post(v:User):
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
p = g.db.query(Submission.id).filter(Submission.deleted_utc == 0, Submission.is_banned == False, Submission.private == False).order_by(func.random()).first()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if p: p = p[0]
|
|
|
|
else: abort(404)
|
|
|
|
|
|
|
|
return redirect(f"/post/{p}")
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/random_user")
|
2023-02-26 08:41:04 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT)
|
2023-01-21 04:39:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2022-11-26 21:00:03 +00:00
|
|
|
def random_user(v:User):
|
2023-03-16 06:27:58 +00:00
|
|
|
u = g.db.query(User.username).filter(User.song != None).order_by(func.random()).first()
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if u: u = u[0]
|
2022-11-28 23:35:23 +00:00
|
|
|
else: abort(404, "No users have set a profile anthem so far!")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
return redirect(f"/@{u}")
|
|
|
|
|
2023-02-02 00:42:47 +00:00
|
|
|
@cache.memoize()
|
2023-03-25 20:57:27 +00:00
|
|
|
def comment_idlist(v=None, page=1, sort="new", t="day", gt=0, lt=0):
|
2023-03-16 06:27:58 +00:00
|
|
|
comments = g.db.query(Comment.id) \
|
2022-12-07 16:51:51 +00:00
|
|
|
.outerjoin(Comment.post) \
|
|
|
|
.filter(
|
|
|
|
or_(Comment.parent_submission != None, Comment.wall_user_id != None),
|
|
|
|
)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-06 02:24:37 +00:00
|
|
|
if v.admin_level < PERMS['POST_COMMENT_MODERATION']:
|
2022-10-13 05:52:54 +00:00
|
|
|
comments = comments.filter(
|
|
|
|
Comment.is_banned == False,
|
|
|
|
Comment.deleted_utc == 0,
|
|
|
|
Comment.author_id.notin_(v.userblocks),
|
2023-04-24 13:28:15 +00:00
|
|
|
or_(Comment.parent_submission == None, Submission.private == False),
|
2022-10-13 05:52:54 +00:00
|
|
|
)
|
2022-05-04 23:09:46 +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:
|
2022-07-09 10:32:49 +00:00
|
|
|
comments = apply_time_filter(t, comments, Comment)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-02-27 15:38:12 +00:00
|
|
|
comments = sort_objects(sort, comments, Comment)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-29 03:20:48 +00:00
|
|
|
comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all()
|
2022-09-29 05:43:29 +00:00
|
|
|
return [x[0] for x in comments]
|
2023-01-20 21:16:09 +00:00
|
|
|
|
2023-01-21 03:59:53 +00:00
|
|
|
@app.get("/comments")
|
2023-02-26 08:41:04 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT)
|
2023-01-21 04:39:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
|
2023-01-21 03:59:53 +00:00
|
|
|
@auth_required
|
|
|
|
def all_comments(v:User):
|
|
|
|
try: page = max(int(request.values.get("page", 1)), 1)
|
|
|
|
except: page = 1
|
|
|
|
|
|
|
|
sort=request.values.get("sort", "new")
|
|
|
|
t=request.values.get("t", "hour")
|
|
|
|
|
|
|
|
try: gt=int(request.values.get("after", 0))
|
|
|
|
except: gt=0
|
|
|
|
|
|
|
|
try: lt=int(request.values.get("before", 0))
|
|
|
|
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) > PAGE_SIZE
|
|
|
|
idlist = idlist[:PAGE_SIZE]
|
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
if v.client: return {"data": [x.json(g.db) for x in comments]}
|
2023-01-21 03:59:53 +00:00
|
|
|
return render_template("home_comments.html", v=v, sort=sort, t=t, page=page, comments=comments, standalone=True, next_exists=next_exists)
|