From 8d00745dc3549412de88e2c6710f779e558cf4ec Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Wed, 20 Oct 2021 16:37:46 +0200 Subject: [PATCH] fd --- files/classes/comment.py | 15 +- files/classes/submission.py | 15 +- files/helpers/const.py | 47 ++--- files/helpers/word_censor.py | 77 -------- files/routes/awards.py | 13 +- files/routes/comments.py | 9 +- files/routes/posts.py | 4 +- files/templates/admin/admin_home.html | 2 +- .../admin/{user_award.html => awards.html} | 2 +- files/templates/expanded_image_modal.html | 2 +- files/templates/submission.html | 2 +- files/templates/submission_listing.html | 8 +- requirements.txt | 1 - setup | 4 - test/__init__.py | 0 test/files/__init__.py | 0 test/files/helpers/__init__.py | 0 test/files/helpers/test_word_censor.py | 186 ------------------ 18 files changed, 56 insertions(+), 331 deletions(-) delete mode 100644 files/helpers/word_censor.py rename files/templates/admin/{user_award.html => awards.html} (93%) delete mode 100644 test/__init__.py delete mode 100644 test/files/__init__.py delete mode 100644 test/files/helpers/__init__.py delete mode 100644 test/files/helpers/test_word_censor.py diff --git a/files/classes/comment.py b/files/classes/comment.py index 84b4678c5..3cb35b363 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -9,10 +9,9 @@ from sqlalchemy.orm import relationship, deferred, lazyload from files.__main__ import Base from files.classes.votes import CommentVote -from files.helpers.const import AUTOPOLLER_ACCOUNT +from files.helpers.const import AUTOPOLLER_ACCOUNT, censor_slurs from files.helpers.lazy import lazy from .flags import CommentFlag -from ..helpers.word_censor import censor_slurs site = environ.get("DOMAIN").strip() @@ -53,8 +52,10 @@ class Comment(Base): senttouser = relationship("User", primaryjoin="User.id==Comment.sentto", viewonly=True) parent_comment = relationship("Comment", remote_side=[id], viewonly=True) child_comments = relationship("Comment", remote_side=[parent_comment_id], viewonly=True) - awards = relationship("AwardRelationship", viewonly=True) + #awards = relationship("AwardRelationship", viewonly=True) + awards = None + def __init__(self, *args, **kwargs): if "created_utc" not in kwargs: @@ -294,7 +295,9 @@ class Comment(Base): return data def realbody(self, v): - if self.post and self.post.club and not (v and v.paid_dues): return "

COUNTRY CLUB ONLY

" + if self.post and self.post.club and not (v and v.paid_dues): + if v: return f"

{v.username} dox/p>" + return "

COUNTRY CLUB ONLY

" body = self.body_html @@ -320,7 +323,9 @@ class Comment(Base): return body def plainbody(self, v): - if self.post and self.post.club and not (v and v.paid_dues): return "

COUNTRY CLUB ONLY

" + if self.post and self.post.club and not (v and v.paid_dues): + if v: return f"

{v.username} dox/p>" + return "

COUNTRY CLUB ONLY

" body = self.body diff --git a/files/classes/submission.py b/files/classes/submission.py index 49f07369d..5ce8e3497 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -9,10 +9,9 @@ from sqlalchemy import * from sqlalchemy.orm import relationship, deferred from files.__main__ import Base -from files.helpers.const import AUTOPOLLER_ACCOUNT +from files.helpers.const import AUTOPOLLER_ACCOUNT, censor_slurs from files.helpers.lazy import lazy from .flags import Flag -from ..helpers.word_censor import censor_slurs site = environ.get("DOMAIN").strip() site_name = environ.get("SITE_NAME").strip() @@ -340,9 +339,11 @@ class Submission(Base): else: return "" def realbody(self, v): - if self.club and not (v and v.paid_dues): return "COUNTRY CLUB ONLY" - body = self.body_html + if self.club and not (v and v.paid_dues): + if v: return f"

{v.username} dox/p>" + return "

COUNTRY CLUB ONLY

" + body = self.body_html body = censor_slurs(body, v) if v and not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com") @@ -350,9 +351,11 @@ class Submission(Base): return body def plainbody(self, v): - if self.club and not (v and v.paid_dues): return "COUNTRY CLUB ONLY" - body = self.body + if self.club and not (v and v.paid_dues): + if v: return f"

{v.username} dox/p>" + return "

COUNTRY CLUB ONLY

" + body = self.body body = censor_slurs(body, v) if v and not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com") diff --git a/files/helpers/const.py b/files/helpers/const.py index 35e2971bb..c841d9cf8 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -1,39 +1,8 @@ from os import environ +import re site = environ.get("DOMAIN", '').strip() -##################### -# Formatting rules: # -##################### -# If all letters are lowercase then it will match lowercase, first letter up in first or all the words and all letters up -# "dancing israelis" will match: -# - "dancing israelis" -# - "Dancing israelis" -# - "Dancing Israelis" -# - "DANCING ISRAELIS" -# -# If some letters are Uppercase, the same, but with the additional option of the original casing, and respecting already existing uppercase -# "NoNewNormal" will match: -# - "NoNewNormal" -# - "nonewnormal" -# - "Nonewnormal" -# - "NONEWNORMAL" -# -# Now on the replacement side, The replacement will have the same capitalization as the slur if the replacement is lowercase -# "kill yourself" -> "keep yourself safe" -# "Kill yourself" -> "Keep yourself safe" -# "Kill Yourself" -> "Keep Yourself Safe" -# "KILL YOURSELF" -> "KEEP YOURSELF SAFE" -# -# If the replacement side has some capitalization, then that capitalization will always be maintained -# for the pair: <"pajeet": "sexy Indian dude"> it will replace: -# "pajeet" -> "sexy Indian dude" -# "Pajeet" -> "Sexy Indian dude" -# "PAJEET" -> "SEXY INDIAN DUDE" -# -# There is a super special case that if the replacer starts with "http" then it never changes capitalization -# -# TL;DR: Just read the above once, or don't, and try to guess! SLURS = { "faggot": "cute twink", "fag": "cute twink", @@ -146,4 +115,16 @@ else: AUTOPOLLER_ACCOUNT = 6 PUSHER_INSTANCE_ID = '02ddcc80-b8db-42be-9022-44c546b4dce6' -PUSHER_KEY = environ.get("PUSHER_KEY", "").strip() \ No newline at end of file +PUSHER_KEY = environ.get("PUSHER_KEY", "").strip() + +single_words = "|".join([slur.lower() for slur in SLURS.keys()]) +SLUR_REGEX = re.compile(rf"(?i)(?<=\s|>)({single_words})(?=[\s<,.])") +REPLACE_MAP = SLURS.items() + +def sub_matcher(match: re.Match) -> str: + return REPLACE_MAP.get(match.group(0)) + +def censor_slurs(body: str, logged_user) -> str: + if not logged_user or logged_user.slurreplacer: + body = SLUR_REGEX.sub(sub_matcher, body) + return body \ No newline at end of file diff --git a/files/helpers/word_censor.py b/files/helpers/word_censor.py deleted file mode 100644 index d7112a69a..000000000 --- a/files/helpers/word_censor.py +++ /dev/null @@ -1,77 +0,0 @@ -from collections import ChainMap -import re -from re import Match -from typing import Dict, Pattern - -from files.helpers.const import SLURS - - -def first_upper(phrase: str) -> str: - """Converts the first character of the phrase to uppercase, not messing with the others""" - return phrase[0].upper() + phrase[1:] - - -def first_all_upper(phrase: str) -> str: - """Converts the first character of each word to uppercase, not messing with the others""" - if " " not in phrase: - return first_upper(phrase) - - return " ".join([first_upper(word) for word in phrase.split(" ")]) - - -def get_permutations_slur(slur: str, replacer: str = "_") -> Dict[str, str]: - """ - Given a slur and a replacer, it generates all the possible permutation on the original text and assigns them to the - corresponding substitution with case - """ - stripped = slur.strip() - is_link = replacer.startswith("http") # special case for the :marseymerchant: - - # the order the things are added into the dict is important, so that the 'Correctest' version is written last - result = { - stripped.upper(): replacer.upper() if not is_link else replacer, - first_all_upper(stripped): first_all_upper(replacer) if not is_link else replacer, - stripped.lower(): replacer, - stripped: replacer, - first_upper(stripped): first_upper(replacer) if not is_link else replacer, - } - - return result - - -def create_slur_regex() -> Pattern[str]: - """Creates the regex that will find the slurs""" - single_words = "|".join([slur.lower() for slur in SLURS.keys()]) - - return re.compile(rf"(?i)(?<=\s|>)({single_words})(?=[\s<,.])") - - -def create_replace_map() -> Dict[str, str]: - """Creates the map that will be used to get the matching replaced for the given slur""" - dicts = [get_permutations_slur(slur, replacer) for (slur, replacer) in SLURS.items()] - - # flattens the list of dict to a single dict - return dict(ChainMap(*dicts)) - - -SLUR_REGEX = create_slur_regex() -REPLACE_MAP = create_replace_map() - - -def sub_matcher(match: Match) -> str: - """given a match returns the correct replacer string""" - found = match.group(0) - # if it does not find the correct capitalization, it tries the all lower, or return the original word - return REPLACE_MAP.get(found) or REPLACE_MAP.get(found.lower()) or found - - -def censor_slurs(body: str, logged_user) -> str: - """Censors all the slurs in the body if the user is not logged-in or if they have the slurreplacer active""" - - if not logged_user or logged_user.slurreplacer: - try: - body = SLUR_REGEX.sub(sub_matcher, body) - except Exception as e: - print(e) - - return body diff --git a/files/routes/awards.py b/files/routes/awards.py index 7ba92c097..0aca31849 100755 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -342,16 +342,13 @@ def award_comment(cid, v): if request.referrer and len(request.referrer) > 1: return redirect(request.referrer) else: return redirect("/") -@app.get("/admin/user_award") -@auth_required +@app.get("/admin/awards") +@admin_level_required(6) def admin_userawards_get(v): - if v.admin_level < 6: - abort(403) + return render_template("admin/awards.html", awards=list(AWARDS.values()), v=v) - return render_template("admin/user_award.html", awards=list(AWARDS.values()), v=v) - -@app.post("/admin/user_award") +@app.post("/admin/awards") @limiter.limit("1/second") @auth_required @validate_formkey @@ -399,4 +396,4 @@ def admin_userawards_post(v): g.db.commit() - return render_template("admin/user_award.html", awards=list(AWARDS.values()), v=v) \ No newline at end of file + return render_template("admin/awards.html", awards=list(AWARDS.values()), v=v) \ No newline at end of file diff --git a/files/routes/comments.py b/files/routes/comments.py index 2c477c0d2..4c718841d 100755 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -37,6 +37,13 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None): comment = get_comment(cid, v=v) + if v and request.values.get("read"): + notif = g.db.query(Notification).options(lazyload('*')).filter_by(comment_id=cid, user_id=v.id, read=False).first() + if notif: + notif.read = True + g.db.add(notif) + g.db.commit() + if comment.post and comment.post.club and not (v and v.paid_dues): abort(403) if not comment.parent_submission and not (v and (comment.author.id == v.id or comment.sentto == v.id)) and not (v and v.admin_level == 6) : abort(403) @@ -525,7 +532,7 @@ def api_comment(v): 'notification': { 'title': f'New reply by @{v.username}', 'body': c.body, - 'deep_link': f'http://{site}{c.permalink}?context=10#context', + 'deep_link': f'http://{site}{c.permalink}?context=10&read=true#context', }, }, }, diff --git a/files/routes/posts.py b/files/routes/posts.py index a5665ce56..f0f8fb433 100755 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -145,7 +145,7 @@ def post_id(pid, anything=None, v=None): elif sort == "controversial": comments = comments.order_by(-1 * Comment.upvotes * Comment.downvotes * Comment.downvotes) elif sort == "top": - comments = comments.order_by(Comment.downvotes - Comment.upvotes) + comments = comments.order_by(Comment.upvotes + Comment.downvotes) elif sort == "bottom": comments = comments.order_by(Comment.upvotes - Comment.downvotes) @@ -170,7 +170,7 @@ def post_id(pid, anything=None, v=None): elif sort == "controversial": comments = comments.order_by(-1 * Comment.upvotes * Comment.downvotes * Comment.downvotes) elif sort == "top": - comments = comments.order_by(Comment.downvotes - Comment.upvotes) + comments = comments.order_by(Comment.upvotes + Comment.downvotes) elif sort == "bottom": comments = comments.order_by(Comment.upvotes - Comment.downvotes) diff --git a/files/templates/admin/admin_home.html b/files/templates/admin/admin_home.html index c49eea823..60bd9fb2d 100755 --- a/files/templates/admin/admin_home.html +++ b/files/templates/admin/admin_home.html @@ -33,7 +33,7 @@

Grant

diff --git a/files/templates/admin/user_award.html b/files/templates/admin/awards.html similarity index 93% rename from files/templates/admin/user_award.html rename to files/templates/admin/awards.html index b50a343d6..0da7b9248 100755 --- a/files/templates/admin/user_award.html +++ b/files/templates/admin/awards.html @@ -35,7 +35,7 @@

 	
User Award Grant
-
+ diff --git a/files/templates/expanded_image_modal.html b/files/templates/expanded_image_modal.html index 2cd1cad65..af9d21036 100755 --- a/files/templates/expanded_image_modal.html +++ b/files/templates/expanded_image_modal.html @@ -10,7 +10,7 @@