diff --git a/utils.py b/utils.py index d2f731618..b8d6ae965 100644 --- a/utils.py +++ b/utils.py @@ -1265,14 +1265,24 @@ def _isBookmarked(baseDir: str, nickname: str, domain: str, return False -def _deleteFromRecentPosts(postJsonObject: {}, recentPostsCache: {}) -> None: - """Remove the given post from the recent posts cache +def removePostFromCache(postJsonObject: {}, recentPostsCache: {}) -> None: + """ if the post exists in the recent posts cache then remove it """ if not recentPostsCache: return - postId = \ - removeIdEnding(postJsonObject['id']).replace('/', '#') + if not postJsonObject.get('id'): + return + + if not recentPostsCache.get('index'): + return + + postId = postJsonObject['id'] + if '#' in postId: + postId = postId.split('#', 1)[0] + postId = removeIdEnding(postId).replace('/', '#') + if postId not in recentPostsCache['index']: + return if recentPostsCache.get('index'): if postId in recentPostsCache['index']: @@ -1371,7 +1381,7 @@ def deletePost(baseDir: str, httpPrefix: str, return # remove from recent posts cache in memory - _deleteFromRecentPosts(postJsonObject, recentPostsCache) + removePostFromCache(postJsonObject, recentPostsCache) # remove any attachment _removeAttachment(baseDir, httpPrefix, domain, postJsonObject) @@ -1607,29 +1617,6 @@ def getCachedPostFilename(baseDir: str, nickname: str, domain: str, return cachedPostFilename + '.html' -def removePostFromCache(postJsonObject: {}, recentPostsCache: {}): - """ if the post exists in the recent posts cache then remove it - """ - if not postJsonObject.get('id'): - return - - if not recentPostsCache.get('index'): - return - - postId = postJsonObject['id'] - if '#' in postId: - postId = postId.split('#', 1)[0] - postId = removeIdEnding(postId).replace('/', '#') - if postId not in recentPostsCache['index']: - return - - if recentPostsCache['json'].get(postId): - del recentPostsCache['json'][postId] - if recentPostsCache['html'].get(postId): - del recentPostsCache['html'][postId] - recentPostsCache['index'].remove(postId) - - def updateRecentPostsCache(recentPostsCache: {}, maxRecentPosts: int, postJsonObject: {}, htmlStr: str) -> None: """Store recent posts in memory so that they can be quickly recalled