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
|
|
|
|
2023-06-07 23:26:32 +00:00
|
|
|
from files.classes.post import Post
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.classes.votes import Vote
|
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 *
|
2023-05-13 02:04:25 +00:00
|
|
|
from files.helpers.useractions import *
|
2023-10-05 10:09:58 +00:00
|
|
|
from files.helpers.can_see import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.routes.wrappers import *
|
2023-06-27 20:03:14 +00:00
|
|
|
from files.__main__ import app, cache, limiter, redis_instance
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@app.get("/")
|
2023-10-07 17:55:50 +00:00
|
|
|
@app.get("/h/<hole>")
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit("30/minute;5000/hour;10000/day", deduct_when=lambda response: response.status_code < 400)
|
2022-08-05 20:40:48 +00:00
|
|
|
@auth_desired_with_logingate
|
2023-10-07 17:55:50 +00:00
|
|
|
def front_all(v, hole=None):
|
|
|
|
if hole:
|
|
|
|
hole = get_hole(hole, graceful=True)
|
|
|
|
if hole and not can_see(v, hole):
|
2022-12-04 18:39:06 +00:00
|
|
|
abort(403)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2023-10-07 17:55:50 +00:00
|
|
|
if request.path.startswith('/h/') and not hole:
|
2023-09-17 19:29:06 +00:00
|
|
|
abort(404)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-05-05 05:23:59 +00:00
|
|
|
page = get_page()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2024-02-07 01:00:17 +00:00
|
|
|
effortposts_only = request.values.get('effortposts_only', False)
|
|
|
|
|
|
|
|
if effortposts_only:
|
|
|
|
defaultsorting = 'new'
|
|
|
|
defaulttime = 'all'
|
|
|
|
elif v:
|
2022-05-04 23:09:46 +00:00
|
|
|
defaultsorting = v.defaultsorting
|
2024-01-29 03:33:06 +00:00
|
|
|
if hole: defaulttime = 'all'
|
2022-05-04 23:09:46 +00:00
|
|
|
else: defaulttime = v.defaulttime
|
|
|
|
else:
|
|
|
|
defaultsorting = "hot"
|
2024-01-29 03:33:06 +00:00
|
|
|
if hole: defaulttime = 'all'
|
2022-07-08 16:21:13 +00:00
|
|
|
else: defaulttime = DEFAULT_TIME_FILTER
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2024-01-29 03:33:26 +00:00
|
|
|
if hole:
|
2024-01-12 01:25:02 +00:00
|
|
|
defaultsorting = "new"
|
2023-07-18 13:06:00 +00:00
|
|
|
|
2023-08-23 21:57:39 +00:00
|
|
|
sort = request.values.get("sort", defaultsorting)
|
|
|
|
t = request.values.get('t', defaulttime)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2023-09-07 15:54:30 +00:00
|
|
|
if SITE == 'rdrama.net' and t == 'all' and sort == 'hot' and page > 1000:
|
2023-08-23 21:58:29 +00:00
|
|
|
sort = 'top'
|
|
|
|
|
2023-08-23 21:57:39 +00:00
|
|
|
try: gt = int(request.values.get("after", 0))
|
|
|
|
except: gt = 0
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-08-23 21:57:39 +00:00
|
|
|
try: lt = int(request.values.get("before", 0))
|
|
|
|
except: lt = 0
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2024-02-06 23:03:39 +00:00
|
|
|
if effortposts_only:
|
|
|
|
pins = False
|
2024-02-07 01:00:17 +00:00
|
|
|
else:
|
2024-02-18 13:25:16 +00:00
|
|
|
if sort == 'hot' or (hole and sort == 'new'):
|
|
|
|
default = True
|
|
|
|
else:
|
|
|
|
default = False
|
2024-02-07 01:00:17 +00:00
|
|
|
pins = session.get(f'{hole}_{sort}', default)
|
2022-07-13 17:31:35 +00:00
|
|
|
|
2023-06-27 20:03:14 +00:00
|
|
|
if not v:
|
2024-02-06 23:02:05 +00:00
|
|
|
result = cache.get(f'frontpage_{sort}_{t}_{page}_{hole}_{pins}_{effortposts_only}')
|
2023-07-07 20:40:16 +00:00
|
|
|
if result:
|
|
|
|
calc_users()
|
2024-02-13 13:41:17 +00:00
|
|
|
return result
|
2023-06-27 20:03:14 +00:00
|
|
|
|
2024-02-12 19:42:36 +00:00
|
|
|
hide_cw = (SITE_NAME == 'WPD' and v and v.hide_cw)
|
|
|
|
|
2023-05-05 21:44:24 +00:00
|
|
|
ids, total, 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,
|
2023-10-07 17:55:50 +00:00
|
|
|
hole=hole,
|
2022-11-07 00:44:31 +00:00
|
|
|
pins=pins,
|
2024-02-06 23:02:05 +00:00
|
|
|
effortposts_only=effortposts_only,
|
2024-02-12 19:42:36 +00:00
|
|
|
hide_cw=hide_cw,
|
2022-05-04 23:09:46 +00:00
|
|
|
)
|
|
|
|
|
2023-07-17 14:49:26 +00:00
|
|
|
posts = get_posts(ids, v=v)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2023-05-13 02:00:54 +00:00
|
|
|
if v and v.hidevotedon:
|
|
|
|
posts = [x for x in posts if not hasattr(x, 'voted') or not x.voted]
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-06-08 00:49:37 +00:00
|
|
|
if v and v.client: return {"data": [x.json for x in posts], "total": total}
|
2023-06-27 20:03:14 +00:00
|
|
|
|
2024-01-30 23:41:44 +00:00
|
|
|
result = render_template("home.html", v=v, listing=posts, total=total, sort=sort, t=t, page=page, hole=hole, home=True, pins=pins, size=size)
|
2023-06-27 20:03:14 +00:00
|
|
|
|
|
|
|
if not v:
|
2024-02-06 23:02:05 +00:00
|
|
|
cache.set(f'frontpage_{sort}_{t}_{page}_{hole}_{pins}_{effortposts_only}', result, timeout=900)
|
2023-06-27 20:03:14 +00:00
|
|
|
|
|
|
|
return result
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
2023-10-08 12:01:40 +00:00
|
|
|
LIMITED_WPD_HOLES = {'aftermath', 'fights', 'gore', 'medical', 'request', 'selfharm',
|
2023-09-15 17:10:26 +00:00
|
|
|
'discussion', 'meta', 'music', 'pets', 'social',
|
2023-12-24 23:05:02 +00:00
|
|
|
'countryclub', 'highrollerclub',
|
|
|
|
'slavshit', 'sandshit'}
|
2023-01-24 05:43:46 +00:00
|
|
|
|
2023-02-02 00:42:47 +00:00
|
|
|
@cache.memoize()
|
2024-02-12 19:42:36 +00:00
|
|
|
def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='', gt=0, lt=0, hole=None, pins=True, effortposts_only=False, hide_cw=False):
|
2023-06-07 23:26:32 +00:00
|
|
|
posts = g.db.query(Post)
|
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,
|
2023-06-07 23:26:32 +00:00
|
|
|
and_(Vote.post_id == Post.id, Vote.user_id == v.id)
|
|
|
|
).filter(Vote.post_id == None)
|
2023-03-15 06:03:11 +00:00
|
|
|
|
2023-10-07 17:55:50 +00:00
|
|
|
if hole:
|
|
|
|
posts = posts.filter(Post.hole == hole.name)
|
2023-06-24 15:49:32 +00:00
|
|
|
elif v:
|
2023-10-07 17:55:50 +00:00
|
|
|
posts = posts.filter(or_(Post.hole == None, Post.hole.notin_(v.hole_blocks)))
|
2023-06-24 15:49:32 +00:00
|
|
|
else:
|
2023-10-07 17:55:50 +00:00
|
|
|
stealth = [x[0] for x in g.db.query(Hole.name).filter_by(stealth=True)]
|
|
|
|
posts = posts.filter(or_(Post.hole == None, Post.hole.notin_(stealth)))
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-06-07 23:26:32 +00:00
|
|
|
if gt: posts = posts.filter(Post.created_utc > gt)
|
|
|
|
if lt: posts = posts.filter(Post.created_utc < lt)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2024-02-06 23:02:05 +00:00
|
|
|
if effortposts_only:
|
2024-02-07 23:59:42 +00:00
|
|
|
posts = posts.filter(Post.effortpost == True)
|
2024-02-06 23:02:05 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if not gt and not lt:
|
2023-06-07 23:26:32 +00:00
|
|
|
posts = apply_time_filter(t, posts, Post)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-11-09 11:27:44 +00:00
|
|
|
posts = posts.filter(
|
2023-06-07 23:26:32 +00:00
|
|
|
Post.is_banned == False,
|
2024-02-18 15:29:53 +00:00
|
|
|
Post.draft == False,
|
2023-06-07 23:26:32 +00:00
|
|
|
Post.deleted_utc == 0,
|
2022-11-09 11:27:44 +00:00
|
|
|
)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2024-02-12 19:42:36 +00:00
|
|
|
if hide_cw:
|
|
|
|
posts = posts.filter(
|
|
|
|
or_(
|
|
|
|
Post.cw == False,
|
|
|
|
Post.author_id == v.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-11-07 00:44:31 +00:00
|
|
|
if pins and not gt and not lt:
|
2023-10-07 17:55:50 +00:00
|
|
|
if hole: posts = posts.filter(Post.hole_pinned == None)
|
2024-02-18 15:29:53 +00:00
|
|
|
else: posts = posts.filter(Post.pinned == None)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-05-24 20:43:49 +00:00
|
|
|
if v:
|
2023-06-07 23:26:32 +00:00
|
|
|
posts = posts.filter(Post.author_id.notin_(v.userblocks))
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if v and filter_words:
|
|
|
|
for word in filter_words:
|
2023-09-26 22:12:01 +00:00
|
|
|
word = escape_for_search(word)
|
|
|
|
posts = posts.filter(not_(Post.title.ilike(f'%{word}%')))
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2024-01-12 07:40:36 +00:00
|
|
|
if v and v.admin_level < PERMS['USER_SHADOWBAN']:
|
2024-01-12 07:15:58 +00:00
|
|
|
posts = posts.join(Post.author).filter(or_(User.id == v.id, User.shadowbanned == None))
|
|
|
|
|
2023-05-05 21:44:24 +00:00
|
|
|
total = posts.count()
|
2023-05-04 22:15:13 +00:00
|
|
|
|
2023-06-07 23:26:32 +00:00
|
|
|
posts = sort_objects(sort, posts, Post)
|
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
|
|
|
|
2024-02-07 22:10:59 +00:00
|
|
|
if SITE_NAME == 'WPD' and sort == "hot" and page == 1 and not hole:
|
2024-01-31 20:01:08 +00:00
|
|
|
posts = posts.filter(Post.hole != 'pets')
|
|
|
|
|
2023-06-07 23:26:32 +00:00
|
|
|
posts = posts.options(load_only(Post.id)).offset(size * (page - 1))
|
2023-05-01 19:50:32 +00:00
|
|
|
|
2024-01-31 20:01:08 +00:00
|
|
|
if SITE_NAME == 'WPD' and sort == "hot" and hole == None:
|
2023-05-01 19:50:32 +00:00
|
|
|
posts = posts.limit(200).all()
|
2023-12-30 17:11:43 +00:00
|
|
|
posts_in_limited_holes = [x for x in posts if x.hole in LIMITED_WPD_HOLES]
|
|
|
|
captured_holes = set()
|
|
|
|
for post in posts_in_limited_holes:
|
|
|
|
if len(captured_holes) < 3 and post.hole not in captured_holes:
|
|
|
|
captured_holes.add(post.hole)
|
|
|
|
else:
|
|
|
|
posts.remove(post)
|
2023-12-31 00:57:09 +00:00
|
|
|
posts = posts[:size]
|
2023-10-07 17:55:50 +00:00
|
|
|
elif SITE_NAME == 'WPD' and not v and hole == None:
|
2023-09-11 17:32:46 +00:00
|
|
|
posts = posts.limit(200).all()
|
2023-10-07 17:55:50 +00:00
|
|
|
posts = [x for x in posts if x.hole not in {'pets','selfharm'}][: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:
|
2023-10-07 17:55:50 +00:00
|
|
|
if hole:
|
|
|
|
pins = g.db.query(Post).options(load_only(Post.id)).filter(Post.hole == hole.name, Post.hole_pinned != None)
|
2022-07-01 23:18:41 +00:00
|
|
|
else:
|
2024-02-18 15:29:53 +00:00
|
|
|
pins = g.db.query(Post).options(load_only(Post.id)).filter(Post.pinned != None, Post.is_banned == False)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-07-01 23:18:41 +00:00
|
|
|
if v:
|
2023-10-07 17:55:50 +00:00
|
|
|
pins = pins.filter(or_(Post.hole == None, Post.hole.notin_(v.hole_blocks)))
|
2022-07-01 23:18:41 +00:00
|
|
|
|
|
|
|
|
2023-06-07 23:26:32 +00:00
|
|
|
if v: pins = pins.filter(Post.author_id.notin_(v.userblocks))
|
|
|
|
pins = pins.order_by(Post.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-05 21:44:24 +00:00
|
|
|
return posts, total, size
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/random_post")
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2023-07-30 00:42:06 +00:00
|
|
|
def random_post(v):
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-08-08 14:51:20 +00:00
|
|
|
p = g.db.query(Post.id).filter(
|
|
|
|
Post.deleted_utc == 0,
|
|
|
|
Post.is_banned == False,
|
2024-02-18 15:29:53 +00:00
|
|
|
Post.draft == False,
|
2023-10-07 17:55:50 +00:00
|
|
|
or_(Post.hole == None, Post.hole.notin_(v.hole_blocks)),
|
2023-08-08 14:51:20 +00:00
|
|
|
).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-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
2022-05-04 23:09:46 +00:00
|
|
|
@auth_required
|
2023-07-30 00:42:06 +00:00
|
|
|
def random_user(v):
|
2023-10-12 21:13:49 +00:00
|
|
|
u = g.db.query(User.username).filter(
|
|
|
|
User.song != None,
|
|
|
|
User.shadowbanned == 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-05-04 22:15:13 +00:00
|
|
|
comments = g.db.query(Comment) \
|
2022-12-07 16:51:51 +00:00
|
|
|
.outerjoin(Comment.post) \
|
2023-05-04 22:15:13 +00:00
|
|
|
.options(load_only(Comment.id)) \
|
2022-12-07 16:51:51 +00:00
|
|
|
.filter(
|
2023-06-23 13:46:42 +00:00
|
|
|
or_(Comment.parent_post != None, Comment.wall_user_id != None),
|
2022-12-07 16:51:51 +00:00
|
|
|
)
|
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),
|
2024-02-18 15:29:53 +00:00
|
|
|
or_(Comment.parent_post == None, Post.draft == 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
|
|
|
|
2024-01-12 07:20:32 +00:00
|
|
|
if v.admin_level < PERMS['USER_SHADOWBAN']:
|
2024-01-12 07:15:58 +00:00
|
|
|
comments = comments.join(Comment.author).filter(or_(User.id == v.id, User.shadowbanned == None))
|
|
|
|
|
2023-05-05 21:44:24 +00:00
|
|
|
total = comments.count()
|
2023-02-27 15:38:12 +00:00
|
|
|
comments = sort_objects(sort, comments, Comment)
|
2023-05-05 21:45:25 +00:00
|
|
|
|
2023-05-04 22:15:13 +00:00
|
|
|
comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all()
|
2023-05-05 21:44:24 +00:00
|
|
|
return [x.id for x in comments], total
|
2023-01-20 21:16:09 +00:00
|
|
|
|
2023-01-21 03:59:53 +00:00
|
|
|
@app.get("/comments")
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
2023-01-21 03:59:53 +00:00
|
|
|
@auth_required
|
2023-07-30 00:42:06 +00:00
|
|
|
def all_comments(v):
|
2023-05-05 05:23:59 +00:00
|
|
|
page = get_page()
|
2023-01-21 03:59:53 +00:00
|
|
|
|
2023-08-23 21:57:39 +00:00
|
|
|
sort = request.values.get("sort", "new")
|
|
|
|
t = request.values.get("t", "hour")
|
2023-01-21 03:59:53 +00:00
|
|
|
|
|
|
|
try: gt=int(request.values.get("after", 0))
|
|
|
|
except: gt=0
|
|
|
|
|
|
|
|
try: lt=int(request.values.get("before", 0))
|
|
|
|
except: lt=0
|
2023-05-05 21:44:24 +00:00
|
|
|
idlist, total = comment_idlist(v=v,
|
2023-01-21 03:59:53 +00:00
|
|
|
page=page,
|
|
|
|
sort=sort,
|
|
|
|
t=t,
|
|
|
|
gt=gt,
|
|
|
|
lt=lt,
|
|
|
|
)
|
|
|
|
|
|
|
|
comments = get_comments(idlist, v=v)
|
|
|
|
|
2023-06-08 00:49:37 +00:00
|
|
|
if v.client: return {"data": [x.json for x in comments]}
|
2023-05-05 21:44:24 +00:00
|
|
|
return render_template("home_comments.html", v=v, sort=sort, t=t, page=page, comments=comments, standalone=True, total=total, size = PAGE_SIZE)
|