2
0
mirror of https://github.com/jfmcbrayer/brutaldon synced 2024-12-23 05:43:41 +01:00

Simplify filter matching.

Concatenate filter phrases and do the re.search only once.
This commit is contained in:
Jason McBrayer 2019-02-15 11:28:07 -05:00
parent dfccfab05b
commit bdc9524532

View File

@ -164,16 +164,14 @@ def get_filters(mastodon, context=None):
return [] return []
def toot_matches_filters(toot, filters=[]): def toot_matches_filters(toot, filters=[]):
for filter in filters: def maybe_rewrite_filter(filter):
if filter.whole_word: if filter.whole_word:
filter = f"\b{filter}\b" return f"\\b{filter.phrase}\\b"
try: else:
if re.search(filter.phrase, toot.spoiler_text, re.I) or re.search(filter.phrase, toot.content, re.I): return filter.phrase
return True phrases = [maybe_rewrite_filter(x) for x in filters]
except AttributeError: # probably a reblog or favorite pattern = "|".join(phrases)
continue return re.search(pattern, toot.spoiler_text + toot.content, re.I)
return False
### ###
### View functions ### View functions