From 40f6a88b1a6102381aca4418a2fb92035ff42fca Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sat, 27 Aug 2022 04:00:05 +0000 Subject: [PATCH 1/2] sneed --- schema.sql | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/schema.sql b/schema.sql index 4ea0800af..2ecfebecc 100644 --- a/schema.sql +++ b/schema.sql @@ -860,7 +860,9 @@ CREATE TABLE public.users ( imginn boolean, earlylife integer, bite integer, - old_house character varying(16) + old_house character varying(16), + owoify integer, + marsify integer ); From 9c03ab2d5e47f9c42d03cfead6db64414c0f1976 Mon Sep 17 00:00:00 2001 From: TLSM Date: Sat, 27 Aug 2022 00:48:44 -0400 Subject: [PATCH 2/2] Fix owoify award affecting links, emoji, markup. --- files/helpers/owoify.py | 54 ++++++++++++++++++++++++++++++++++++++++ files/routes/awards.py | 6 ++--- files/routes/comments.py | 6 ++--- 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 files/helpers/owoify.py diff --git a/files/helpers/owoify.py b/files/helpers/owoify.py new file mode 100644 index 000000000..abcca7bc1 --- /dev/null +++ b/files/helpers/owoify.py @@ -0,0 +1,54 @@ +from owoify.utility.interleave_arrays import interleave_arrays +from owoify.utility.presets import * +from owoify.structures.word import Word + +import re +import files.helpers.regex as help_re +import files.helpers.sanitize as sanitize + +# Includes, excerpts, and modifies some functions from: +# https://github.com/deadshot465/owoify-py @ owoify/owoify.py + +OWO_WORD_REGEX = re.compile(r'[^\s]+') +OWO_SPACE_REGEX = re.compile(r'\s+') + +OWO_EXCLUDE_PATTERNS = [ + re.compile(r'\]\('), # links []() and images ![]() + # NB: May not be effective when URL part contains literal spaces vs %20 + # Also relies on owoify replacements currently not affecting symbols. + sanitize.url_re, # bare links + re.compile(r':[!#@a-z0-9_\-]+:', flags=re.I|re.A), # emoji + help_re.mention_regex, # mentions + help_re.poll_regex, # polls + help_re.choice_regex, + help_re.command_regex, # markup commands +] + +def owoify(source: str) -> str: + word_matches = OWO_WORD_REGEX.findall(source) + space_matches = OWO_SPACE_REGEX.findall(source) + + words = [Word(s) for s in word_matches] + spaces = [Word(s) for s in space_matches] + + words = list(map(lambda w: owoify_map_token_custom(w), words)) + + result = interleave_arrays(words, spaces) + result_strings = list(map(lambda w: str(w), result)) + return ''.join(result_strings) + +def owoify_map_token_custom(token): + for pattern in OWO_EXCLUDE_PATTERNS: + # if pattern appears anywhere in token, do not owoify. + if pattern.search(token.word): + return token + + # Original Owoification Logic (sans cases for higher owo levels) + for func in SPECIFIC_WORD_MAPPING_LIST: + token = func(token) + + for func in OWO_MAPPING_LIST: + token = func(token) + # End Original Owoification Logic + + return token diff --git a/files/routes/awards.py b/files/routes/awards.py index 743d4b4d8..a72c9b07a 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -11,7 +11,7 @@ from .front import frontlist from flask import g, request from files.helpers.sanitize import filter_emojis_only from files.helpers.marsify import marsify -import owoify +from files.helpers.owoify import owoify from copy import deepcopy @app.get("/shop") @@ -351,7 +351,7 @@ def award_thing(v, thing_type, id): if author.owoify: author.owoify += 21600 else: author.owoify = int(time.time()) + 21600 body = thing.body - body = owoify.owoify(body) + body = owoify(body) if author.marsify: body = marsify(body) thing.body_html = sanitize(body, limit_pings=5) g.db.add(thing) @@ -359,7 +359,7 @@ def award_thing(v, thing_type, id): if author.marsify: author.marsify += 21600 else: author.marsify = int(time.time()) + 21600 body = thing.body - if author.owoify: body = owoify.owoify(body) + if author.owoify: body = owoify(body) body = marsify(body) thing.body_html = sanitize(body, limit_pings=5) g.db.add(thing) diff --git a/files/routes/comments.py b/files/routes/comments.py index 1d97e8f4d..d9db61363 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -15,7 +15,7 @@ from flask import * from files.__main__ import app, limiter from files.helpers.sanitize import filter_emojis_only from files.helpers.marsify import marsify -import owoify +from files.helpers.owoify import owoify import requests from shutil import copyfile from json import loads @@ -293,7 +293,7 @@ def comment(v): body_for_sanitize = body if v.owoify: - body_for_sanitize = owoify.owoify(body_for_sanitize) + body_for_sanitize = owoify(body_for_sanitize) if v.marsify: body_for_sanitize = marsify(body_for_sanitize) @@ -729,7 +729,7 @@ def edit_comment(cid, v): body_for_sanitize = body if v.owoify: - body_for_sanitize = owoify.owoify(body_for_sanitize) + body_for_sanitize = owoify(body_for_sanitize) if v.marsify: body_for_sanitize = marsify(body_for_sanitize)