From de88136753c409750d11178b6156b3ed6d2644c3 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 18 Jan 2024 14:03:35 +0000 Subject: [PATCH] Load cached automatic content warnings at startup --- content.py | 19 ++++++++++++++++++- daemon.py | 3 ++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/content.py b/content.py index 44745d57f..d04e457fe 100644 --- a/content.py +++ b/content.py @@ -15,6 +15,7 @@ import email.parser import urllib.parse from shutil import copyfile from dateutil.parser import parse +from utils import is_account_dir from utils import get_url_from_post from utils import is_right_to_left_text from utils import language_right_to_left @@ -2268,10 +2269,26 @@ def _load_auto_cw(base_dir: str, nickname: str, domain: str) -> []: return [] +def load_auto_cw_cache(base_dir: str) -> {}: + """Returns a dictionary containing the automatic content warning lists + for each account + """ + auto_cw_cache = {} + for _, dirs, _ in os.walk(base_dir + '/accounts'): + for handle in dirs: + if not is_account_dir(handle): + continue + nickname = handle.split('@')[0] + domain = handle.split('@')[1] + auto_cw_cache[nickname] = _load_auto_cw(base_dir, nickname, domain) + break + return auto_cw_cache + + def add_auto_cw(base_dir: str, nickname: str, domain: str, subject: str, content: str, auto_cw_cache: {}) -> str: - """Appends any automatic CW to the subject line + """Appends any automatic content warnings to the subject line and returns the new subject line """ new_subject = subject diff --git a/daemon.py b/daemon.py index 3fa35efaa..927fda2d5 100644 --- a/daemon.py +++ b/daemon.py @@ -391,6 +391,7 @@ from utils import get_actor_from_post from manualapprove import manual_deny_follow_request_thread from manualapprove import manual_approve_follow_request_thread from announce import create_announce +from content import load_auto_cw_cache from content import add_name_emojis_to_tags from content import load_dogwhistles from content import valid_url_lengths @@ -24543,7 +24544,7 @@ def run_daemon(no_of_books: int, httpd.max_cached_readers = 24 # cache for automatic content warnings - httpd.auto_cw_cache = {} + httpd.auto_cw_cache = load_auto_cw_cache(base_dir) # list of websites which are currently down httpd.sites_unavailable = load_unavailable_sites(base_dir)