diff --git a/cache.py b/cache.py index 4fedcd36f..ab6fb19d0 100644 --- a/cache.py +++ b/cache.py @@ -8,6 +8,7 @@ __status__ = "Production" __module_group__ = "Core" import os +import json from session import download_image from session import url_exists from session import get_json @@ -357,3 +358,36 @@ def cache_svg_images(session, base_dir: str, http_prefix: str, else: cached = True return cached + + +def update_recent_posts_cache(recent_posts_cache: {}, max_recent_posts: int, + post_json_object: {}, html_str: str) -> None: + """Store recent posts in memory so that they can be quickly recalled + """ + if not post_json_object.get('id'): + return + post_id = post_json_object['id'] + if '#' in post_id: + post_id = post_id.split('#', 1)[0] + post_id = remove_id_ending(post_id).replace('/', '#') + if recent_posts_cache.get('index'): + if post_id in recent_posts_cache['index']: + return + recent_posts_cache['index'].append(post_id) + post_json_object['muted'] = False + recent_posts_cache['json'][post_id] = json.dumps(post_json_object) + recent_posts_cache['html'][post_id] = html_str + + while len(recent_posts_cache['html'].items()) > max_recent_posts: + post_id = recent_posts_cache['index'][0] + recent_posts_cache['index'].pop(0) + if recent_posts_cache['json'].get(post_id): + del recent_posts_cache['json'][post_id] + if recent_posts_cache['html'].get(post_id): + del recent_posts_cache['html'][post_id] + else: + recent_posts_cache['index'] = [post_id] + recent_posts_cache['json'] = {} + recent_posts_cache['html'] = {} + recent_posts_cache['json'][post_id] = json.dumps(post_json_object) + recent_posts_cache['html'][post_id] = html_str diff --git a/tests.py b/tests.py index 1a2581230..057691720 100644 --- a/tests.py +++ b/tests.py @@ -30,6 +30,7 @@ from httpsig import sign_post_headers from httpsig import sign_post_headers_new from httpsig import verify_post_headers from httpsig import message_content_digest +from cache import update_recent_posts_cache from cache import cache_svg_images from cache import store_person_in_cache from cache import get_person_from_cache @@ -100,7 +101,6 @@ from utils import get_full_domain from utils import valid_nickname from utils import first_paragraph_from_string from utils import remove_id_ending -from utils import update_recent_posts_cache from utils import follow_person from utils import get_nickname_from_actor from utils import get_domain_from_actor diff --git a/utils.py b/utils.py index 3b7c28656..aacfb451a 100644 --- a/utils.py +++ b/utils.py @@ -2830,39 +2830,6 @@ def get_cached_post_filename(base_dir: str, nickname: str, domain: str, return cached_post_filename + '.html' -def update_recent_posts_cache(recent_posts_cache: {}, max_recent_posts: int, - post_json_object: {}, html_str: str) -> None: - """Store recent posts in memory so that they can be quickly recalled - """ - if not post_json_object.get('id'): - return - post_id = post_json_object['id'] - if '#' in post_id: - post_id = post_id.split('#', 1)[0] - post_id = remove_id_ending(post_id).replace('/', '#') - if recent_posts_cache.get('index'): - if post_id in recent_posts_cache['index']: - return - recent_posts_cache['index'].append(post_id) - post_json_object['muted'] = False - recent_posts_cache['json'][post_id] = json.dumps(post_json_object) - recent_posts_cache['html'][post_id] = html_str - - while len(recent_posts_cache['html'].items()) > max_recent_posts: - post_id = recent_posts_cache['index'][0] - recent_posts_cache['index'].pop(0) - if recent_posts_cache['json'].get(post_id): - del recent_posts_cache['json'][post_id] - if recent_posts_cache['html'].get(post_id): - del recent_posts_cache['html'][post_id] - else: - recent_posts_cache['index'] = [post_id] - recent_posts_cache['json'] = {} - recent_posts_cache['html'] = {} - recent_posts_cache['json'][post_id] = json.dumps(post_json_object) - recent_posts_cache['html'][post_id] = html_str - - def file_last_modified(filename: str) -> str: """Returns the date when a file was last modified """ diff --git a/webapp_post.py b/webapp_post.py index 3c53e7314..c0709c6a6 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -14,6 +14,7 @@ from dateutil.parser import parse from auth import create_password from git import is_git_patch from cache import get_person_from_cache +from cache import update_recent_posts_cache from bookmarks import bookmark_from_id from bookmarks import bookmarked_by_person from announce import announced_by_person @@ -74,7 +75,6 @@ from utils import get_cached_post_filename from utils import get_protocol_prefixes from utils import get_display_name from utils import display_name_is_emoji -from utils import update_recent_posts_cache from utils import remove_id_ending from utils import get_nickname_from_actor from utils import get_domain_from_actor