From b64c34519c10e3aaca753a92f642b61ea0154da4 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Fri, 30 Jul 2021 07:31:38 +0200 Subject: [PATCH] gfgf --- drama/classes/clients.py | 2 +- drama/classes/comment.py | 15 +- drama/classes/mod_logs.py | 12 +- drama/classes/submission.py | 6 +- drama/classes/user.py | 10 +- drama/helpers/base36.py | 31 ---- drama/helpers/get.py | 18 +- drama/helpers/markdown.py | 2 +- drama/routes/admin.py | 16 +- drama/routes/comments.py | 20 +-- drama/routes/login.py | 2 +- drama/routes/oauth.py | 14 +- drama/routes/posts.py | 8 +- drama/routes/static.py | 2 +- drama/routes/votes.py | 4 +- drama/templates/admin/app.html | 6 +- drama/templates/admin/app_data.html | 6 +- drama/templates/admin/apps.html | 6 +- drama/templates/comments.html | 174 ++++++++++---------- drama/templates/embeds/comment.html | 26 +-- drama/templates/embeds/comment_removed.html | 4 +- drama/templates/embeds/submission.html | 24 +-- drama/templates/modlog.html | 2 +- drama/templates/settings_apps.html | 6 +- drama/templates/submission.html | 118 ++++++------- drama/templates/submission_banned.html | 26 +-- drama/templates/submission_listing.html | 110 ++++++------- drama/templates/user_listing.html | 8 +- 28 files changed, 315 insertions(+), 363 deletions(-) diff --git a/drama/classes/clients.py b/drama/classes/clients.py index a04b36c87..8f0d0c26a 100644 --- a/drama/classes/clients.py +++ b/drama/classes/clients.py @@ -29,7 +29,7 @@ class OauthApp(Base, Stndrd): @property def permalink(self): - return f"/admin/app/{self.base36id}" + return f"/admin/app/{self.id}" def idlist(self, page=1, **kwargs): diff --git a/drama/classes/comment.py b/drama/classes/comment.py index 88900c2c2..6842e11fb 100644 --- a/drama/classes/comment.py +++ b/drama/classes/comment.py @@ -90,7 +90,7 @@ class Comment(Base, Age_times, Scores, Stndrd, Fuzzing): @property @lazy def fullname(self): - return f"t3_{self.base36id}" + return f"t3_{self.id}" @property @lazy @@ -162,7 +162,7 @@ class Comment(Base, Age_times, Scores, Stndrd, Fuzzing): 'permalink': self.permalink, 'is_pinned': self.is_pinned, 'distinguish_level': self.distinguish_level, - 'post_id': self.post.base36id, + 'post_id': self.post.id, 'score': self.score_fuzzed, 'upvotes': self.upvotes_fuzzed, 'downvotes': self.downvotes_fuzzed, @@ -182,15 +182,15 @@ class Comment(Base, Age_times, Scores, Stndrd, Fuzzing): if self.is_banned: data= {'is_banned': True, 'ban_reason': self.ban_reason, - 'id': self.base36id, - 'post': self.post.base36id, + 'id': self.id, + 'post': self.post.id, 'level': self.level, 'parent': self.parent_fullname } elif self.deleted_utc > 0: data= {'deleted_utc': self.deleted_utc, - 'id': self.base36id, - 'post': self.post.base36id, + 'id': self.id, + 'post': self.post.id, 'level': self.level, 'parent': self.parent_fullname } @@ -198,8 +198,7 @@ class Comment(Base, Age_times, Scores, Stndrd, Fuzzing): data=self.json_raw - if self.level>=2: - data['parent_comment_id']= base36encode(self.parent_comment_id), + if self.level>=2: data['parent_comment_id']= self.parent_comment_id, if "replies" in self.__dict__: data['replies']=[x.json_core for x in self.replies] diff --git a/drama/classes/mod_logs.py b/drama/classes/mod_logs.py index 02c821d77..b18fc4d81 100644 --- a/drama/classes/mod_logs.py +++ b/drama/classes/mod_logs.py @@ -33,7 +33,7 @@ class ModAction(Base, Stndrd, Age_times): super().__init__(*args, **kwargs) def __repr__(self): - return f"" + return f"" @property def actiontype(self): @@ -80,21 +80,21 @@ class ModAction(Base, Stndrd, Age_times): @property def json(self): data={ - "id":self.base36id, + "id":self.id, "kind": self.kind, "created_utc": self.created_utc, "mod": self.user.username, } if self.target_user_id: - data["target_user_id"]=self.target_user.base36id + data["target_user_id"]=self.target_user.id data["target_user"]=self.target_user.username if self.target_comment_id: - data["target_comment_id"]=self.target_comment.base36id + data["target_comment_id"]=self.target_comment.id if self.target_submission_id: - data["target_submission_id"]=self.target_submission.base36id + data["target_submission_id"]=self.target_submission.id if self._note: data["note"]=self._note @@ -114,7 +114,7 @@ class ModAction(Base, Stndrd, Age_times): @property def permalink(self): - return f"/log/{self.base36id}" + return f"/log/{self.id}" @property def title_text(self): return f"@{self.user.username} {self.actiontype['title'].format(self=self)}" diff --git a/drama/classes/submission.py b/drama/classes/submission.py index 6e5695962..d22a73308 100644 --- a/drama/classes/submission.py +++ b/drama/classes/submission.py @@ -116,7 +116,7 @@ class Submission(Base, Stndrd, Age_times, Scores, Fuzzing): @property @lazy def fullname(self): - return f"t2_{self.base36id}" + return f"t2_{self.id}" @property @lazy @@ -254,14 +254,14 @@ class Submission(Base, Stndrd, Age_times, Scores, Fuzzing): return {'is_banned': True, 'deleted_utc': self.deleted_utc, 'ban_reason': self.ban_reason, - 'id': self.base36id, + 'id': self.id, 'title': self.title, 'permalink': self.permalink, } elif self.deleted_utc: return {'is_banned': bool(self.is_banned), 'deleted_utc': True, - 'id': self.base36id, + 'id': self.id, 'title': self.title, 'permalink': self.permalink, } diff --git a/drama/classes/user.py b/drama/classes/user.py index 26dd91569..82bb17081 100644 --- a/drama/classes/user.py +++ b/drama/classes/user.py @@ -233,13 +233,9 @@ class User(Base, Stndrd, Age_times): secondrange = firstrange + 26 return [x.id for x in comments[firstrange:secondrange]] - @property - def base36id(self): - return base36encode(self.id) - @property def fullname(self): - return f"t1_{self.base36id}" + return f"t1_{self.id}" @property def banned_by(self): @@ -447,7 +443,7 @@ class User(Base, Stndrd, Age_times): 'url': self.url, 'is_banned': bool(self.is_banned), 'created_utc': self.created_utc, - 'id': self.base36id, + 'id': self.id, 'is_private': self.is_private, 'profile_url': self.profile_url, 'banner_url': self.banner_url, @@ -468,7 +464,7 @@ class User(Base, Stndrd, Age_times): 'is_banned': True, 'is_permanent_ban': not bool(self.unban_utc), 'ban_reason': self.ban_reason, - 'id': self.base36id + 'id': self.id } return self.json_raw diff --git a/drama/helpers/base36.py b/drama/helpers/base36.py index a0e00aa70..17acfce9a 100644 --- a/drama/helpers/base36.py +++ b/drama/helpers/base36.py @@ -1,34 +1,3 @@ -from flask import abort - -def base36encode(number, alphabet='0123456789abcdefghijklmnopqrstuvwxyz'): - """Converts an integer to a base36 string.""" - if not isinstance(number, int): - raise TypeError('number must be an integer') - - base36 = '' - sign = '' - - if number < 0: - sign = '-' - number = -number - - if 0 <= number < len(alphabet): - return sign + alphabet[number] - - while number != 0: - number, i = divmod(number, len(alphabet)) - base36 = alphabet[i] + base36 - - return sign + base36 - - -def base36decode(number): - try: - return int(str(number), 36) - except ValueError: - abort(400) - - def hex2bin(hexstr): value = int(hexstr, 16) bindigits = [] diff --git a/drama/helpers/get.py b/drama/helpers/get.py index 838ef6a08..804f7daa2 100644 --- a/drama/helpers/get.py +++ b/drama/helpers/get.py @@ -44,11 +44,7 @@ def get_user(username, v=None, graceful=False): return user -def get_account(base36id, v=None, graceful=False): - - - if isinstance(base36id, str): id = base36decode(base36id) - else: id = base36id +def get_account(id, v=None, graceful=False): user = g.db.query(User ).filter( @@ -80,12 +76,7 @@ def get_account(base36id, v=None, graceful=False): return user -def get_post(pid, v=None, graceful=False, **kwargs): - - if isinstance(pid, str): - i = base36decode(pid) - else: - i = pid +def get_post(i, v=None, graceful=False, **kwargs): if v: vt = g.db.query(Vote).filter_by( @@ -177,10 +168,7 @@ def get_posts(pids, v=None): return sorted(output, key=lambda x: pids.index(x.id)) -def get_comment(cid, v=None, graceful=False, **kwargs): - - if isinstance(cid, str): i = base36decode(cid) - else: i = cid +def get_comment(i, v=None, graceful=False, **kwargs): if v: blocking = v.blocking.subquery() diff --git a/drama/helpers/markdown.py b/drama/helpers/markdown.py index d5b393de4..471265e6e 100644 --- a/drama/helpers/markdown.py +++ b/drama/helpers/markdown.py @@ -87,7 +87,7 @@ class CustomRenderer(HTMLRenderer): if not user: return f"{space}@{target}" - return f'{space}@{user.username}' + return f'{space}@{user.username}' def render_sub_mention(self, token): space = token.target[0] diff --git a/drama/routes/admin.py b/drama/routes/admin.py index 4b8b31ded..169fab9b6 100644 --- a/drama/routes/admin.py +++ b/drama/routes/admin.py @@ -725,7 +725,7 @@ def unban_user(user_id, v): @validate_formkey def ban_post(post_id, v): - post = g.db.query(Submission).filter_by(id=base36decode(post_id)).first() + post = g.db.query(Submission).filter_by(id=post_id).first() if not post: abort(400) @@ -761,7 +761,7 @@ def ban_post(post_id, v): @validate_formkey def unban_post(post_id, v): - post = g.db.query(Submission).filter_by(id=base36decode(post_id)).first() + post = g.db.query(Submission).filter_by(id=post_id).first() if not post: abort(400) @@ -789,7 +789,7 @@ def unban_post(post_id, v): @validate_formkey def api_distinguish_post(post_id, v): - post = g.db.query(Submission).filter_by(id=base36decode(post_id)).first() + post = g.db.query(Submission).filter_by(id=post_id).first() if not post: abort(404) @@ -811,7 +811,7 @@ def api_distinguish_post(post_id, v): @admin_level_required(3) def api_sticky_post(post_id, v): - post = g.db.query(Submission).filter_by(id=base36decode(post_id)).first() + post = g.db.query(Submission).filter_by(id=post_id).first() if post: post.stickied = not (post.stickied) g.db.add(post) @@ -824,7 +824,7 @@ def api_sticky_post(post_id, v): @auth_required def api_pin_post(post_id, v): - post = g.db.query(Submission).filter_by(id=base36decode(post_id)).first() + post = g.db.query(Submission).filter_by(id=post_id).first() if post: post.is_pinned = not (post.is_pinned) g.db.add(post) @@ -835,7 +835,7 @@ def api_pin_post(post_id, v): @admin_level_required(1) def api_ban_comment(c_id, v): - comment = g.db.query(Comment).filter_by(id=base36decode(c_id)).first() + comment = g.db.query(Comment).filter_by(id=c_id).first() if not comment: abort(404) @@ -856,7 +856,7 @@ def api_ban_comment(c_id, v): @admin_level_required(1) def api_unban_comment(c_id, v): - comment = g.db.query(Comment).filter_by(id=base36decode(c_id)).first() + comment = g.db.query(Comment).filter_by(id=c_id).first() if not comment: abort(404) g.db.add(comment) @@ -900,7 +900,7 @@ def admin_distinguish_comment(c_id, v): render_replies=False, ) - html=str(BeautifulSoup(html, features="html.parser").find(id=f"comment-{comment.base36id}-only")) + html=str(BeautifulSoup(html, features="html.parser").find(id=f"comment-{comment.id}-only")) return jsonify({"html":html, "api":html}) diff --git a/drama/routes/comments.py b/drama/routes/comments.py index d1e2abf55..6c581e7f7 100644 --- a/drama/routes/comments.py +++ b/drama/routes/comments.py @@ -237,7 +237,7 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None): @api("create") def api_comment(v): - parent_submission = base36decode(request.form.get("submission")) + parent_submission = request.form.get("submission") parent_fullname = request.form.get("parent_fullname") # get parent item info @@ -247,14 +247,14 @@ def api_comment(v): parent = parent_post parent_comment_id = None level = 1 - parent_submission = base36decode(parent_id) + parent_submission = parent_id elif parent_fullname.startswith("t3"): parent = get_comment(parent_id, v=v) parent_comment_id = parent.id level = parent.level + 1 parent_id = parent.parent_submission parent_submission = parent_id - parent_post = get_post(base36encode(parent_id)) + parent_post = get_post(parent_id) else: abort(400) @@ -389,7 +389,7 @@ def api_comment(v): if not file.content_type.startswith('image/'): return jsonify({"error": "That wasn't an image!"}), 400 - name = f'comment/{c.base36id}/{secrets.token_urlsafe(8)}' + name = f'comment/{c.id}/{secrets.token_urlsafe(8)}' url = upload_file(file) body = request.form.get("body") + f"\n![]({url})" @@ -653,7 +653,7 @@ def edit_comment(cid, v): body = request.form.get("body", "")[0:10000] for i in re.finditer('^(https:\/\/.*\.(png|jpg|jpeg|gif|PNG|JPG|JPEG|GIF))', body, re.MULTILINE): body = body.replace(i.group(1), f'![]({i.group(1)})') body = body.replace("\n", "\n\n").replace("\n\n\n\n\n\n", "\n\n").replace("\n\n\n\n", "\n\n") - with CustomRenderer(post_id=c.post.base36id) as renderer: body_md = renderer.render(mistletoe.Document(body)) + with CustomRenderer(post_id=c.post.id) as renderer: body_md = renderer.render(mistletoe.Document(body)) body_html = sanitize(body_md, linkgen=True) bans = filter_comment_html(body_html) @@ -672,7 +672,7 @@ def edit_comment(cid, v): reason += f" {ban.reason_text}" return {'html': lambda: render_template("comment_failed.html", - action=f"/edit_comment/{c.base36id}", + action=f"/edit_comment/{c.id}", badlinks=[ x.domain for x in bans], body=body, @@ -744,7 +744,7 @@ def edit_comment(cid, v): file=request.files["file"] if not file.content_type.startswith('image/'): return jsonify({"error": "That wasn't an image!"}), 400 - name = f'comment/{c.base36id}/{secrets.token_urlsafe(8)}' + name = f'comment/{c.id}/{secrets.token_urlsafe(8)}' url = upload_file(file) body += f"\n![]({url})" @@ -837,7 +837,7 @@ def edit_comment(cid, v): @api("delete") def delete_comment(cid, v): - c = g.db.query(Comment).filter_by(id=base36decode(cid)).first() + c = g.db.query(Comment).filter_by(id=cid).first() if not c: abort(404) @@ -862,7 +862,7 @@ def delete_comment(cid, v): @api("delete") def undelete_comment(cid, v): - c = g.db.query(Comment).filter_by(id=base36decode(cid)).first() + c = g.db.query(Comment).filter_by(id=cid).first() if not c: abort(404) @@ -927,7 +927,7 @@ def toggle_comment_pin(cid, v): render_replies=False, ) - html=str(BeautifulSoup(html, features="html.parser").find(id=f"comment-{comment.base36id}-only")) + html=str(BeautifulSoup(html, features="html.parser").find(id=f"comment-{comment.id}-only")) return jsonify({"html":html}) diff --git a/drama/routes/login.py b/drama/routes/login.py index cffd90be5..1f9fcaad8 100644 --- a/drama/routes/login.py +++ b/drama/routes/login.py @@ -540,7 +540,7 @@ def request_2fa_disable(): valid=int(time.time()) token=generate_hash(f"{user.id}+{user.username}+disable2fa+{valid}+{user.mfa_secret}+{user.login_nonce}") - action_url=f"https://{app.config['SERVER_NAME']}/reset_2fa?id={user.base36id}&t={valid}&token={token}" + action_url=f"https://{app.config['SERVER_NAME']}/reset_2fa?id={user.id}&t={valid}&token={token}" send_mail(to_address=user.email, subject="Drama - 2FA Removal Request", diff --git a/drama/routes/oauth.py b/drama/routes/oauth.py index cad56bb0d..aa4291198 100644 --- a/drama/routes/oauth.py +++ b/drama/routes/oauth.py @@ -287,7 +287,7 @@ def api_v1_identity(v): @validate_formkey def admin_app_approve(v, aid): - app = g.db.query(OauthApp).filter_by(id=base36decode(aid)).first() + app = g.db.query(OauthApp).filter_by(id=aid).first() app.client_id = secrets.token_urlsafe(64)[0:64] app.client_secret = secrets.token_urlsafe(128)[0:128] @@ -305,7 +305,7 @@ def admin_app_approve(v, aid): @validate_formkey def admin_app_revoke(v, aid): - app = g.db.query(OauthApp).filter_by(id=base36decode(aid)).first() + app = g.db.query(OauthApp).filter_by(id=aid).first() app.client_id = None app.client_secret = None @@ -323,7 +323,7 @@ def admin_app_revoke(v, aid): @validate_formkey def admin_app_reject(v, aid): - app = g.db.query(OauthApp).filter_by(id=base36decode(aid)).first() + app = g.db.query(OauthApp).filter_by(id=aid).first() for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): g.db.delete(auth) @@ -341,7 +341,7 @@ def admin_app_reject(v, aid): @admin_level_required(3) def admin_app_id(v, aid): - aid=base36decode(aid) + aid=aid oauth = g.db.query(OauthApp).options( joinedload( @@ -367,7 +367,7 @@ def admin_app_id(v, aid): @admin_level_required(3) def admin_app_id_comments(v, aid): - aid=base36decode(aid) + aid=aid oauth = g.db.query(OauthApp).options( joinedload( @@ -409,7 +409,7 @@ def admin_apps_list(v): @auth_required def reroll_oauth_tokens(aid, v): - aid = base36decode(aid) + aid = aid a = g.db.query(OauthApp).filter_by(id=aid).first() @@ -433,7 +433,7 @@ def reroll_oauth_tokens(aid, v): @validate_formkey def oauth_rescind_app(aid, v): - aid = base36decode(aid) + aid = aid auth = g.db.query(ClientAuth).filter_by(id=aid).first() if auth.user_id != v.id: diff --git a/drama/routes/posts.py b/drama/routes/posts.py index 7a7d4cd48..1af77ab62 100644 --- a/drama/routes/posts.py +++ b/drama/routes/posts.py @@ -77,7 +77,7 @@ def submit_get(v): @app.get("/api/v1/post/") @auth_desired @api("read") -def post_base36id(pid, anything=None, v=None): +def post_id(pid, anything=None, v=None): try: pid = int(pid) except Exception as e: pass @@ -405,7 +405,7 @@ def get_post_title(v): return jsonify({"error": f"Could not find a title"}), 400 def thumbs(new_post): - pid = new_post.base36id + pid = new_post.id post = get_post(pid, graceful=True, session=g.db) if not post: # account for possible follower lag @@ -921,7 +921,7 @@ def submit_post(v): "api": lambda: ({"error": f"Image files only"}, 400) } - name = f'post/{new_post.base36id}/{secrets.token_urlsafe(8)}' + name = f'post/{new_post.id}/{secrets.token_urlsafe(8)}' new_post.url = upload_file(file) new_post.domain_ref = 1 # id of i.ruqqus.ga domain g.db.add(new_post) @@ -1087,7 +1087,7 @@ def embed_post_pid(pid): @validate_formkey def toggle_comment_nsfw(cid, v): - comment = g.db.query(Comment).filter_by(id=base36decode(cid)).first() + comment = g.db.query(Comment).filter_by(id=cid).first() if not comment.author_id == v.id and not v.admin_level >= 3: abort(403) comment.over_18 = not comment.over_18 g.db.add(comment) diff --git a/drama/routes/static.py b/drama/routes/static.py index 2e571b65b..7f8fc5dce 100644 --- a/drama/routes/static.py +++ b/drama/routes/static.py @@ -44,7 +44,7 @@ def log(v): @auth_desired def log_item(aid, v): - action=g.db.query(ModAction).filter_by(id=base36decode(aid)).first() + action=g.db.query(ModAction).filter_by(id=aid).first() if not action: abort(404) diff --git a/drama/routes/votes.py b/drama/routes/votes.py index 33823ce45..b261bffb8 100644 --- a/drama/routes/votes.py +++ b/drama/routes/votes.py @@ -96,7 +96,7 @@ def api_vote_post(post_id, new, v): g.db.add(post.author) vote = Vote(user_id=v.id, vote_type=new, - submission_id=base36decode(post_id), + submission_id=post_id, app_id=v.client.application.id if v.client else None ) g.db.add(vote) @@ -143,7 +143,7 @@ def api_vote_comment(comment_id, new, v): g.db.add(comment.author) vote = CommentVote(user_id=v.id, vote_type=new, - comment_id=base36decode(comment_id), + comment_id=comment_id, app_id=v.client.application.id if v.client else None ) diff --git a/drama/templates/admin/app.html b/drama/templates/admin/app.html index b9c6d2e0f..ff521ec0c 100644 --- a/drama/templates/admin/app.html +++ b/drama/templates/admin/app.html @@ -34,12 +34,12 @@
{% if not app.client_secret%} - Approve - Reject + Approve + Reject {% else %} - Revoke + Revoke {% endif %}
diff --git a/drama/templates/admin/app_data.html b/drama/templates/admin/app_data.html index c0b93710f..891fa917e 100644 --- a/drama/templates/admin/app_data.html +++ b/drama/templates/admin/app_data.html @@ -52,12 +52,12 @@
{% if not thing.oauth_app.client_secret %} - Approve - Reject + Approve + Reject {% else %} - Revoke + Revoke {% endif %}
diff --git a/drama/templates/admin/apps.html b/drama/templates/admin/apps.html index f289765d3..8afaac3fc 100644 --- a/drama/templates/admin/apps.html +++ b/drama/templates/admin/apps.html @@ -39,12 +39,12 @@
{% if not app.client_secret %} - Approve - Reject + Approve + Reject {% else %} - Revoke + Revoke {% endif %}
diff --git a/drama/templates/comments.html b/drama/templates/comments.html index 39b62ee8c..2a0923d26 100644 --- a/drama/templates/comments.html +++ b/drama/templates/comments.html @@ -17,17 +17,17 @@ -
+
- +
-
+
@@ -42,20 +42,20 @@ {% if render_replies %} {% if level<6 %} -
+
{% set standalone=False %} {% for reply in c.children(v) %} {{single_comment(reply, level=level+1)}} {% endfor %}
{% elif c.children(v) %} -
+
{% set standalone=False %} {% for reply in c.children(v) %} {{single_comment(reply, level=level+1)}} {% endfor %}
-
+ {% endif %} @@ -101,16 +101,16 @@
{% endif %} -
+
- +
-
+