Wildcards can be used within filters

main
Bob Mottram 2025-05-04 10:52:44 +01:00
parent 7f88c850e2
commit 54ad8667c1
2 changed files with 27 additions and 3 deletions

View File

@ -8,6 +8,7 @@ __status__ = "Production"
__module_group__ = "Moderation"
import os
import fnmatch
from utils import data_dir
from utils import acct_dir
from utils import text_in_file
@ -123,6 +124,18 @@ def _is_twitter_post(content: str) -> bool:
return False
def filtered_match(filter_str: str, content: str) -> bool:
"""Does the given filter match the content?
"""
if '*' not in filter_str:
if filter_str in content:
return True
elif fnmatch.fnmatchcase(content, filter_str):
return True
return False
def _is_filtered_base(filename: str, content: str,
system_language: str) -> bool:
"""Uses the given file containing filtered words to check
@ -146,12 +159,12 @@ def _is_filtered_base(filename: str, content: str,
if len(filter_str) < 2:
continue
if '+' not in filter_str:
if filter_str in content:
if filtered_match(filter_str, content):
return True
else:
filter_words = filter_str.replace('"', '').split('+')
for word in filter_words:
if word not in content:
for filter_wrd in filter_words:
if not filtered_match(filter_wrd, content):
return False
return True
except OSError as ex:

View File

@ -231,6 +231,7 @@ from conversation import convthread_id_to_conversation_tag
from webapp_utils import add_emoji_to_display_name
from blocking import is_blocked_nickname
from blocking import is_blocked_domain
from filters import filtered_match
TEST_SERVER_GROUP_RUNNING = False
@ -9251,6 +9252,15 @@ def _test_blocking_domain(base_dir: str) -> None:
block_federated)
def _test_filter_match() -> None:
print('filter match')
assert not filtered_match('cycle', 'Some text.')
assert filtered_match('text', 'Some text.')
assert filtered_match('* text.', 'Some text.')
assert not filtered_match('* text', 'Some text.')
assert filtered_match('* text*', 'Some text.')
def run_all_tests():
base_dir = os.getcwd()
data_dir_testing(base_dir)
@ -9269,6 +9279,7 @@ def run_all_tests():
_test_checkbox_names()
_test_thread_functions()
_test_functions()
_test_filter_match()
_test_blocking_domain(base_dir)
_test_blocking_nick(base_dir)
_test_conversation_to_convthread()