From 43b7d98ce2c0de39a6215fc8f1030c10918dd042 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 27 May 2025 13:34:19 +0100 Subject: [PATCH] Move searchableBy functions to a separate module --- daemon.py | 2 +- daemon_post_receive.py | 2 +- searchable.py | 60 ++++++++++++++++++++++++++++++++++++++++++ utils.py | 43 ------------------------------ 4 files changed, 62 insertions(+), 45 deletions(-) create mode 100644 searchable.py diff --git a/daemon.py b/daemon.py index 9f495bdb3..ee00af8d5 100644 --- a/daemon.py +++ b/daemon.py @@ -49,7 +49,7 @@ from shares import expire_shares from categories import load_city_hashtags from categories import update_hashtag_categories from languages import load_default_post_languages -from utils import load_searchable_by_default +from searchable import load_searchable_by_default from utils import set_accounts_data_dir from utils import data_dir from utils import check_bad_path diff --git a/daemon_post_receive.py b/daemon_post_receive.py index 4eef69097..8be560d90 100644 --- a/daemon_post_receive.py +++ b/daemon_post_receive.py @@ -30,9 +30,9 @@ from media import attach_media from city import get_spoofed_city from flags import is_image_file from flags import is_float +from searchable import set_searchable_by from utils import date_utcnow from utils import date_from_string_format -from utils import set_searchable_by from utils import get_instance_url from utils import save_json from utils import remove_post_from_cache diff --git a/searchable.py b/searchable.py new file mode 100644 index 000000000..72a4f6e78 --- /dev/null +++ b/searchable.py @@ -0,0 +1,60 @@ +__filename__ = "searchable.py" +__author__ = "Bob Mottram" +__license__ = "AGPL3+" +__version__ = "1.6.0" +__maintainer__ = "Bob Mottram" +__email__ = "bob@libreserver.org" +__status__ = "Production" +__module_group__ = "Core" + +# Whether posts are searchable +# See https://codeberg.org/fediverse/fep/src/branch/main/fep/268d/fep-268d.md + +import os +from utils import acct_dir +from utils import data_dir +from utils import text_in_file +from utils import is_account_dir + + +def load_searchable_by_default(base_dir: str) -> {}: + """loads the searchable_by states for each account + """ + result = {} + dir_str = data_dir(base_dir) + for _, dirs, _ in os.walk(dir_str): + for account in dirs: + if not is_account_dir(account): + continue + nickname = account.split('@')[0] + filename = os.path.join(dir_str, account) + '/.searchableByDefault' + if os.path.isfile(filename): + try: + with open(filename, 'r', encoding='utf-8') as fp_search: + result[nickname] = fp_search.read().strip() + except OSError: + print('EX: unable to load searchableByDefault ' + filename) + break + return result + + +def set_searchable_by(base_dir: str, nickname: str, domain: str, + searchable_by: str) -> None: + """Sets the searchable_by state for an account from the dropdown on + new post screen + """ + if not searchable_by: + return + filename = acct_dir(base_dir, nickname, domain) + '/.searchableByDefault' + + # already the same state? + if os.path.isfile(filename): + if text_in_file(searchable_by, filename, True): + return + + # write the new state + try: + with open(filename, 'w+', encoding='utf-8') as fp_search: + fp_search.write(searchable_by) + except OSError: + print('EX: unable to write searchableByDropdown ' + filename) diff --git a/utils.py b/utils.py index 2b523fbd6..bea8bf324 100644 --- a/utils.py +++ b/utils.py @@ -5052,49 +5052,6 @@ def account_is_indexable(actor_json: {}) -> bool: return False -def load_searchable_by_default(base_dir: str) -> {}: - """loads the searchable_by states for each account - """ - result = {} - dir_str = data_dir(base_dir) - for _, dirs, _ in os.walk(dir_str): - for account in dirs: - if not is_account_dir(account): - continue - nickname = account.split('@')[0] - filename = os.path.join(dir_str, account) + '/.searchableByDefault' - if os.path.isfile(filename): - try: - with open(filename, 'r', encoding='utf-8') as fp_search: - result[nickname] = fp_search.read().strip() - except OSError: - print('EX: unable to load searchableByDefault ' + filename) - break - return result - - -def set_searchable_by(base_dir: str, nickname: str, domain: str, - searchable_by: str) -> None: - """Sets the searchable_by state for an account from the dropdown on - new post screen - """ - if not searchable_by: - return - filename = acct_dir(base_dir, nickname, domain) + '/.searchableByDefault' - - # already the same state? - if os.path.isfile(filename): - if text_in_file(searchable_by, filename, True): - return - - # write the new state - try: - with open(filename, 'w+', encoding='utf-8') as fp_search: - fp_search.write(searchable_by) - except OSError: - print('EX: unable to write searchableByDropdown ' + filename) - - def browser_supports_download_filename(ua_str: str) -> bool: """Does the browser indicated by the user agent string support specifying a default download filename?