From 255a32c0dcab5a97e608eb6e78795b7d1c65cf57 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 19 Oct 2019 18:50:05 +0100 Subject: [PATCH] Remove cached post on like update --- inbox.py | 5 +++-- like.py | 26 ++++++++++++++++++++------ tests.py | 3 ++- utils.py | 15 +++++++++++++++ webinterface.py | 17 ++--------------- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/inbox.py b/inbox.py index ad787a7d1..399712b2e 100644 --- a/inbox.py +++ b/inbox.py @@ -879,7 +879,8 @@ def receiveLike(session,handle: str,isGroup: bool,baseDir: str, \ return True if debug: print('DEBUG: liked post found in inbox') - updateLikesCollection(postFilename,messageJson['object'],messageJson['actor'],debug) + + updateLikesCollection(baseDir,postFilename,messageJson['object'],messageJson['actor'],domain,debug) return True def receiveUndoLike(session,handle: str,isGroup: bool,baseDir: str, \ @@ -930,7 +931,7 @@ def receiveUndoLike(session,handle: str,isGroup: bool,baseDir: str, \ return True if debug: print('DEBUG: liked post found in inbox. Now undoing.') - undoLikesCollectionEntry(postFilename,messageJson['object'],messageJson['actor'],debug) + undoLikesCollectionEntry(baseDir,postFilename,messageJson['object'],messageJson['actor'],domain,debug) return True def receiveDelete(session,handle: str,isGroup: bool,baseDir: str, \ diff --git a/like.py b/like.py index 4afda6f45..dea55a61c 100644 --- a/like.py +++ b/like.py @@ -6,6 +6,7 @@ __maintainer__ = "Bob Mottram" __email__ = "bob@freedombone.net" __status__ = "Production" +import os import json import time import commentjson @@ -14,13 +15,14 @@ from utils import urlPermitted from utils import getNicknameFromActor from utils import getDomainFromActor from utils import locatePost +from utils import getCachedPostFilename from posts import sendSignedJson from session import postJson from webfinger import webfingerHandle from auth import createBasicAuthHeader from posts import getPersonBox -def undoLikesCollectionEntry(postFilename: str,objectUrl: str,actor: str,debug: bool) -> None: +def undoLikesCollectionEntry(baseDir: str,postFilename: str,objectUrl: str,actor: str,domain: str,debug: bool) -> None: """Undoes a like for a particular actor """ postJsonObject=None @@ -36,6 +38,12 @@ def undoLikesCollectionEntry(postFilename: str,objectUrl: str,actor: str,debug: tries+=1 if postJsonObject: + # remove any cached version of this post so that the like icon is changed + nickname=getNicknameFromActor(actor) + cachedPostFilename=getCachedPostFilename(baseDir,nickname,domain,postJsonObject) + if os.path.isfile(cachedPostFilename): + os.remove(cachedPostFilename) + if not postJsonObject.get('type'): return if postJsonObject['type']!='Create': @@ -110,7 +118,7 @@ def noOfLikes(postJsonObject: {}) -> int: postJsonObject['object']['likes']['totalItems']=0 return len(postJsonObject['object']['likes']['items']) -def updateLikesCollection(postFilename: str,objectUrl: str, actor: str,debug: bool) -> None: +def updateLikesCollection(baseDir: str,postFilename: str,objectUrl: str, actor: str,domain: str,debug: bool) -> None: """Updates the likes collection within a post """ postJsonObject=None @@ -126,6 +134,12 @@ def updateLikesCollection(postFilename: str,objectUrl: str, actor: str,debug: bo tries+=1 if postJsonObject: + # remove any cached version of this post so that the like icon is changed + nickname=getNicknameFromActor(actor) + cachedPostFilename=getCachedPostFilename(baseDir,nickname,domain,postJsonObject) + if os.path.isfile(cachedPostFilename): + os.remove(cachedPostFilename) + if not postJsonObject.get('object'): if debug: pprint(postJsonObject) @@ -226,7 +240,7 @@ def like(session,baseDir: str,federationList: [],nickname: str,domain: str,port: print('DEBUG: like objectUrl: '+objectUrl) return None - updateLikesCollection(postFilename,objectUrl,newLikeJson['actor'],debug) + updateLikesCollection(baseDir,postFilename,objectUrl,newLikeJson['actor'],domain,debug) sendSignedJson(newLikeJson,session,baseDir, \ nickname,domain,port, \ @@ -321,7 +335,7 @@ def undolike(session,baseDir: str,federationList: [],nickname: str,domain: str,p if not postFilename: return None - undoLikesCollectionEntry(postFilename,objectUrl,newLikeJson['actor'],debug) + undoLikesCollectionEntry(baseDir,postFilename,objectUrl,newLikeJson['actor'],domain,debug) sendSignedJson(newUndoLikeJson,session,baseDir, \ nickname,domain,port, \ @@ -562,7 +576,7 @@ def outboxLike(baseDir: str,httpPrefix: str, \ print('DEBUG: c2s like post not found in inbox or outbox') print(messageId) return True - updateLikesCollection(postFilename,messageId,messageJson['actor'],debug) + updateLikesCollection(baseDir,postFilename,messageId,messageJson['actor'],domain,debug) if debug: print('DEBUG: post liked via c2s - '+postFilename) @@ -619,6 +633,6 @@ def outboxUndoLike(baseDir: str,httpPrefix: str, \ print('DEBUG: c2s undo like post not found in inbox or outbox') print(messageId) return True - undoLikesCollectionEntry(postFilename,messageId,messageJson['actor'],debug) + undoLikesCollectionEntry(baseDir,postFilename,messageId,messageJson['actor'],domain,debug) if debug: print('DEBUG: post undo liked via c2s - '+postFilename) diff --git a/tests.py b/tests.py index 33aea7ad4..db8d3de67 100644 --- a/tests.py +++ b/tests.py @@ -405,7 +405,8 @@ def testPostMessageBetweenServers(): if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])>0: if len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==1: if len([name for name in os.listdir(mediaPath) if os.path.isfile(os.path.join(mediaPath, name))])>0: - break + if len([name for name in os.listdir(queuePath) if os.path.isfile(os.path.join(queuePath, name))])==0: + break time.sleep(1) # Image attachment created diff --git a/utils.py b/utils.py index 8eab88e97..3d225d255 100644 --- a/utils.py +++ b/utils.py @@ -379,3 +379,18 @@ def copytree(src: str, dst: str, symlinks=False, ignore=None): shutil.copytree(s, d, symlinks, ignore) else: shutil.copy2(s, d) + +def getCachedPostDirectory(baseDir: str,nickname: str,domain: str) -> str: + """Returns the directory where the html post cache exists + """ + htmlPostCacheDir=baseDir+'/accounts/'+nickname+'@'+domain+'/postcache' + return htmlPostCacheDir + +def getCachedPostFilename(baseDir: str,nickname: str,domain: str, \ + postJsonObject: {}) -> str: + """Returns the html cache filename for the given post + """ + cachedPostFilename= \ + getCachedPostDirectory(baseDir,nickname,domain)+ \ + '/'+postJsonObject['id'].replace('/activity','').replace('/','#')+'.html' + return cachedPostFilename diff --git a/webinterface.py b/webinterface.py index 17b9faaf4..d6b7d2a1a 100644 --- a/webinterface.py +++ b/webinterface.py @@ -23,6 +23,8 @@ from utils import locatePost from utils import noOfAccounts from utils import isPublicPost from utils import getDisplayName +from utils import getCachedPostDirectory +from utils import getCachedPostFilename from follow import isFollowingActor from webfinger import webfingerHandle from posts import isDM @@ -1712,21 +1714,6 @@ def postContainsPublic(postJsonObject: {}) -> bool: break return containsPublic -def getCachedPostDirectory(baseDir: str,nickname: str,domain: str) -> str: - """Returns the directory where the html post cache exists - """ - htmlPostCacheDir=baseDir+'/accounts/'+nickname+'@'+domain+'/postcache' - return htmlPostCacheDir - -def getCachedPostFilename(baseDir: str,nickname: str,domain: str, \ - postJsonObject: {}) -> str: - """Returns the html cache filename for the given post - """ - cachedPostFilename= \ - getCachedPostDirectory(baseDir,nickname,domain)+ \ - '/'+postJsonObject['id'].replace('/activity','').replace('/','#')+'.html' - return cachedPostFilename - def loadIndividualPostAsHtmlFromCache(baseDir: str,nickname: str,domain: str, \ postJsonObject: {}) -> str: """If a cached html version of the given post exists then load it and