From 329c86e73105f64d16534d2fe07d0ad4225e2154 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 31 Aug 2020 10:05:20 +0100 Subject: [PATCH] Implement mute and unmute more efficiently by avoiding regenerating the post --- daemon.py | 2 +- posts.py | 58 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/daemon.py b/daemon.py index fdc9a0ea..68917649 100644 --- a/daemon.py +++ b/daemon.py @@ -5901,7 +5901,7 @@ class PubServer(BaseHTTPRequestHandler): 'unbookmark shown done', 'delete shown done') - # mute a post from the web interface icon + # The mute button is pressed if htmlGET and '?mute=' in self.path: pageNumber = 1 if '?page=' in self.path: diff --git a/posts.py b/posts.py index 95026c11..01233b0a 100644 --- a/posts.py +++ b/posts.py @@ -31,7 +31,6 @@ from webfinger import webfingerHandle from httpsig import createSignedHeader from utils import removeIdEnding from utils import siteIsActive -from utils import removePostFromCache from utils import getCachedPostFilename from utils import getStatusNumber from utils import createPersonDir @@ -3535,19 +3534,25 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str, if not postJsonObject: return + # change the mute icon in the cached post file + cachedPostFilename = \ + getCachedPostFilename(baseDir, nickname, domain, postJsonObject) + if cachedPostFilename: + with open(cachedPostFilename, 'r') as cacheFile: + postHtml = cacheFile.read() + if '/unmute.png' in postHtml: + postHtml = postHtml.replace('/unmute.png', '/mute.png') + newCacheFile = open(cachedPostFilename, 'w+') + if newCacheFile: + newCacheFile.write(postHtml) + newCacheFile.close() + print('MUTE: ' + postFilename) muteFile = open(postFilename + '.muted', 'w+') if muteFile: muteFile.write('\n') muteFile.close() - # remove cached posts so that the muted version gets created - cachedPostFilename = \ - getCachedPostFilename(baseDir, nickname, domain, postJsonObject) - if cachedPostFilename: - if os.path.isfile(cachedPostFilename): - os.remove(cachedPostFilename) - # if the post is in the recent posts cache then mark it as muted if recentPostsCache.get('index'): postId = \ @@ -3557,6 +3562,12 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str, if recentPostsCache['json'].get(postId): postJsonObject['muted'] = True recentPostsCache['json'][postId] = json.dumps(postJsonObject) + if recentPostsCache.get('html'): + if recentPostsCache['html'].get(postId): + postHtml = recentPostsCache['html'][postId] + if '/unmute.png' in postHtml: + recentPostsCache['html'][postId] = \ + postHtml.replace('/unmute.png', '/mute.png') print('MUTE: ' + postId + ' marked as muted in recent posts cache') @@ -3577,13 +3588,36 @@ def unmutePost(baseDir: str, nickname: str, domain: str, postId: str, if os.path.isfile(muteFilename): os.remove(muteFilename) - # remove cached posts so that it gets recreated + # change the mute icon in the cached post file cachedPostFilename = \ getCachedPostFilename(baseDir, nickname, domain, postJsonObject) if cachedPostFilename: - if os.path.isfile(cachedPostFilename): - os.remove(cachedPostFilename) - removePostFromCache(postJsonObject, recentPostsCache) + with open(cachedPostFilename, 'r') as cacheFile: + postHtml = cacheFile.read() + if '/mute.png' in postHtml: + postHtml = postHtml.replace('/mute.png', '/unmute.png') + newCacheFile = open(cachedPostFilename, 'w+') + if newCacheFile: + newCacheFile.write(postHtml) + newCacheFile.close() + + # if the post is in the recent posts cache then mark it as muted + if recentPostsCache.get('index'): + postId = \ + removeIdEnding(postJsonObject['id']).replace('/', '#') + if postId in recentPostsCache['index']: + print('UNMUTE: ' + postId + ' is in recent posts cache') + if recentPostsCache['json'].get(postId): + postJsonObject['muted'] = False + recentPostsCache['json'][postId] = json.dumps(postJsonObject) + if recentPostsCache.get('html'): + if recentPostsCache['html'].get(postId): + postHtml = recentPostsCache['html'][postId] + if '/mute.png' in postHtml: + recentPostsCache['html'][postId] = \ + postHtml.replace('/mute.png', '/unmute.png') + print('UNMUTE: ' + postId + + ' marked as unmuted in recent posts cache') def sendBlockViaServer(baseDir: str, session,