From bdc952453232d2b9d19296e4c164cde520c59ca3 Mon Sep 17 00:00:00 2001 From: Jason McBrayer Date: Fri, 15 Feb 2019 11:28:07 -0500 Subject: [PATCH] Simplify filter matching. Concatenate filter phrases and do the re.search only once. --- brutaldon/views.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/brutaldon/views.py b/brutaldon/views.py index 900b381..05cef2c 100644 --- a/brutaldon/views.py +++ b/brutaldon/views.py @@ -164,16 +164,14 @@ def get_filters(mastodon, context=None): return [] def toot_matches_filters(toot, filters=[]): - for filter in filters: + def maybe_rewrite_filter(filter): if filter.whole_word: - filter = f"\b{filter}\b" - try: - if re.search(filter.phrase, toot.spoiler_text, re.I) or re.search(filter.phrase, toot.content, re.I): - return True - except AttributeError: # probably a reblog or favorite - continue - return False - + return f"\\b{filter.phrase}\\b" + else: + return filter.phrase + phrases = [maybe_rewrite_filter(x) for x in filters] + pattern = "|".join(phrases) + return re.search(pattern, toot.spoiler_text + toot.content, re.I) ### ### View functions