Implement mute and unmute more efficiently by avoiding regenerating the post

main
Bob Mottram 2020-08-31 10:05:20 +01:00
parent 0e1bfe94d7
commit 329c86e731
2 changed files with 47 additions and 13 deletions

View File

@ -5901,7 +5901,7 @@ class PubServer(BaseHTTPRequestHandler):
'unbookmark shown done', 'unbookmark shown done',
'delete 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: if htmlGET and '?mute=' in self.path:
pageNumber = 1 pageNumber = 1
if '?page=' in self.path: if '?page=' in self.path:

View File

@ -31,7 +31,6 @@ from webfinger import webfingerHandle
from httpsig import createSignedHeader from httpsig import createSignedHeader
from utils import removeIdEnding from utils import removeIdEnding
from utils import siteIsActive from utils import siteIsActive
from utils import removePostFromCache
from utils import getCachedPostFilename from utils import getCachedPostFilename
from utils import getStatusNumber from utils import getStatusNumber
from utils import createPersonDir from utils import createPersonDir
@ -3535,19 +3534,25 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str,
if not postJsonObject: if not postJsonObject:
return 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) print('MUTE: ' + postFilename)
muteFile = open(postFilename + '.muted', 'w+') muteFile = open(postFilename + '.muted', 'w+')
if muteFile: if muteFile:
muteFile.write('\n') muteFile.write('\n')
muteFile.close() 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 the post is in the recent posts cache then mark it as muted
if recentPostsCache.get('index'): if recentPostsCache.get('index'):
postId = \ postId = \
@ -3557,6 +3562,12 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str,
if recentPostsCache['json'].get(postId): if recentPostsCache['json'].get(postId):
postJsonObject['muted'] = True postJsonObject['muted'] = True
recentPostsCache['json'][postId] = json.dumps(postJsonObject) 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 + print('MUTE: ' + postId +
' marked as muted in recent posts cache') ' 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): if os.path.isfile(muteFilename):
os.remove(muteFilename) os.remove(muteFilename)
# remove cached posts so that it gets recreated # change the mute icon in the cached post file
cachedPostFilename = \ cachedPostFilename = \
getCachedPostFilename(baseDir, nickname, domain, postJsonObject) getCachedPostFilename(baseDir, nickname, domain, postJsonObject)
if cachedPostFilename: if cachedPostFilename:
if os.path.isfile(cachedPostFilename): with open(cachedPostFilename, 'r') as cacheFile:
os.remove(cachedPostFilename) postHtml = cacheFile.read()
removePostFromCache(postJsonObject, recentPostsCache) 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, def sendBlockViaServer(baseDir: str, session,