From 0f2f72f2c3d571918d6b6b1a58b16509cd29160f Mon Sep 17 00:00:00 2001 From: justcool393 Date: Thu, 1 Dec 2022 22:04:10 +0000 Subject: [PATCH] sort by new button (#34) also get rid of megathread logic do the needful and do ```sql UPDATE submissions SET new=true WHERE title LIKE 'Thread' OR title ILIKE 'megathread'; ``` or whatever the proper equivalent is Co-authored-by: justcool393 Reviewed-on: https://fsdfsd.net/rDrama/rDrama/pulls/34 Co-authored-by: justcool393 Co-committed-by: justcool393 --- files/classes/user.py | 11 +- files/routes/posts.py | 19 ++- files/templates/post_actions.html | 124 ++++++++---------- .../templates/post_admin_actions_mobile.html | 4 + files/templates/submission.html | 10 +- 5 files changed, 85 insertions(+), 83 deletions(-) diff --git a/files/classes/user.py b/files/classes/user.py index 0a5573f92..a52d693b4 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -1,12 +1,12 @@ import random from operator import * -from typing import Union +from typing import Any, Union import pyotp from sqlalchemy import Column, ForeignKey from sqlalchemy.orm import aliased, deferred from sqlalchemy.sql import func -from sqlalchemy.sql.expression import not_, and_, or_ +from sqlalchemy.sql.expression import not_, and_, or_, ColumnOperators from sqlalchemy.sql.sqltypes import * from files.classes import Base @@ -414,6 +414,13 @@ class User(Base): def can_view_offsitementions(self): return self.offsitementions or self.admin_level >= PERMS['NOTIFICATIONS_REDDIT'] + @lazy + def can_edit(self, target:Union[Submission, Comment]) -> bool: + if isinstance(target, Comment) and not target.post: return False + if self.id == target.author_id: return True + if not isinstance(target, Submission): return False + return bool(self.admin_level >= PERMS['POST_EDITING']) + @property @lazy def user_awards(self): diff --git a/files/routes/posts.py b/files/routes/posts.py index 1ee1c7fbf..201fd0c4e 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -145,8 +145,7 @@ def post_id(pid, anything=None, v=None, sub=None): if g.is_api_or_xhr: return {"error":"Must be 18+ to view"}, 451 return render_template("errors/nsfw.html", v=v) - if post.new or 'megathread' in post.title.lower() or 'Thread' in post.title: - defaultsortingcomments = 'new' + if post.new: defaultsortingcomments = 'new' elif v: defaultsortingcomments = v.defaultsortingcomments else: defaultsortingcomments = "hot" sort = request.values.get("sort", defaultsortingcomments) @@ -315,8 +314,7 @@ def morecomments(v, cid): @ratelimit_user("1/second;10/minute;100/hour;200/day") def edit_post(pid, v): p = get_post(pid) - if v.id != p.author_id and v.admin_level < PERMS['POST_EDITING']: - abort(403) + if not v.can_edit(p): abort(403) # Disable edits on things older than 1wk unless it's a draft or editor is a jannie if (time.time() - p.created_utc > 7*24*60*60 and not p.private @@ -942,8 +940,7 @@ def submit_post(v:User, sub=None): if v.client: return post.json(g.db) else: post.voted = 1 - if post.new or 'megathread' in post.title.lower() or 'Thread' in post.title: - sort = 'new' + if post.new: sort = 'new' else: sort = v.defaultsortingcomments return render_template('submission.html', v=v, p=post, sort=sort, render_replies=True, offset=0, success=True, sub=post.subr) @@ -1075,6 +1072,16 @@ def pin_post(post_id, v): else: return {"message": "Post unpinned!"} return abort(404, "Post not found!") +@app.route("/post//new", methods=["PUT", "DELETE"]) +@limiter.limit(DEFAULT_RATELIMIT_SLOWER) +@auth_required +def toggle_new_sort(post_id:int, v:User): + post = get_post(post_id) + if not v.can_edit(post): abort(403, "Only the post author can do that!") + post.new = request.method == "PUT" + g.db.add(post) + return {"message": f"Turned {'on' if post.new else 'off'} sort by new"} + extensions = IMAGE_FORMATS + VIDEO_FORMATS + AUDIO_FORMATS diff --git a/files/templates/post_actions.html b/files/templates/post_actions.html index 6ad239f20..eb49f845c 100644 --- a/files/templates/post_actions.html +++ b/files/templates/post_actions.html @@ -17,9 +17,7 @@ {% if v %} -{% endif %} -{% if v %} @@ -35,72 +33,60 @@ - {% endif %} +{% endif %} - {% if v %} - - +{% endif %} diff --git a/files/templates/post_admin_actions_mobile.html b/files/templates/post_admin_actions_mobile.html index d0598e4a2..54959b3c5 100644 --- a/files/templates/post_admin_actions_mobile.html +++ b/files/templates/post_admin_actions_mobile.html @@ -21,6 +21,10 @@ {%- endif %} + {% if v.can_edit(p) %} + + + {% endif %} {% if v.admin_level >= PERMS['POST_COMMENT_DISTINGUISH'] and (v.id == p.author.id or v.admin_level >= PERMS['POST_COMMENT_MODERATION']) %} diff --git a/files/templates/submission.html b/files/templates/submission.html index 2f22bfc44..38bdcc20b 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -178,7 +178,7 @@ {% endif %} - {% if v and (v.id==p.author_id or v.admin_level >= PERMS['POST_EDITING']) and not v.is_suspended %} + {% if v and v.can_edit(p) and not v.is_suspended %} - - {% if v and v.id != p.author_id and p.body and not v_forbid_deleted %} @@ -334,7 +332,7 @@