From 2ef2141a9aa5d6520772651541d281b058d45d40 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 19 Oct 2019 19:21:14 +0100 Subject: [PATCH] Remove cached html announce when its state changes --- announce.py | 21 +++++++++++++++++---- inbox.py | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/announce.py b/announce.py index 79452769..a0e86525 100644 --- a/announce.py +++ b/announce.py @@ -16,6 +16,7 @@ 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 posts import getPersonBox from session import postJson @@ -42,7 +43,7 @@ def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool: domain,port=getDomainFromActor(messageJson['actor']) postFilename=locatePost(baseDir,nickname,domain,messageJson['object']) if postFilename: - updateAnnounceCollection(postFilename,messageJson['actor'],debug) + updateAnnounceCollection(baseDir,postFilename,messageJson['actor'],domain,debug) return True if messageJson['type']=='Undo': if not isinstance(messageJson['object'], dict): @@ -59,11 +60,11 @@ def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool: domain,port=getDomainFromActor(messageJson['actor']) postFilename=locatePost(baseDir,nickname,domain,messageJson['object']['object']) if postFilename: - undoAnnounceCollectionEntry(postFilename,messageJson['actor'],debug) + undoAnnounceCollectionEntry(baseDir,postFilename,messageJson['actor'],domain,debug) return True return False -def undoAnnounceCollectionEntry(postFilename: str,actor: str,debug: bool) -> None: +def undoAnnounceCollectionEntry(baseDir: str,postFilename: str,actor: str,domain: str,debug: bool) -> None: """Undoes an announce for a particular actor by removing it from the "shares" collection within a post. Note that the "shares" collection has no relation to shared items in shares.py. It's shares of posts, not shares of physical objects. @@ -80,6 +81,12 @@ def undoAnnounceCollectionEntry(postFilename: str,actor: str,debug: bool) -> Non time.sleep(2) tries+=1 if postJsonObject: + # remove any cached version of this announce 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': @@ -125,7 +132,7 @@ def undoAnnounceCollectionEntry(postFilename: str,actor: str,debug: bool) -> Non time.sleep(1) tries+=1 -def updateAnnounceCollection(postFilename: str,actor: str,debug: bool) -> None: +def updateAnnounceCollection(baseDir: str,postFilename: str,actor: str,domain: str,debug: bool) -> None: """Updates the announcements collection within a post Confusingly this is known as "shares", but isn't the same as shared items within shares.py It's shares of posts, not shares of physical objects. @@ -142,6 +149,12 @@ def updateAnnounceCollection(postFilename: str,actor: str,debug: bool) -> None: time.sleep(1) tries+=1 if postJsonObject: + # remove any cached version of this announce 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) diff --git a/inbox.py b/inbox.py index 11ac0a26..a559b4c2 100644 --- a/inbox.py +++ b/inbox.py @@ -1066,7 +1066,7 @@ def receiveAnnounce(session,handle: str,isGroup: bool,baseDir: str, \ print('DEBUG: announce post not found in inbox or outbox') print(messageJson['object']) return True - updateAnnounceCollection(postFilename,messageJson['actor'],debug) + updateAnnounceCollection(baseDir,postFilename,messageJson['actor'],domain,debug) if debug: print('DEBUG: Downloading announce post '+messageJson['actor']+' -> '+messageJson['object']) postJsonObject=downloadAnnounce(session,baseDir,httpPrefix,nickname,domain,messageJson,__version__) @@ -1170,7 +1170,7 @@ def receiveUndoAnnounce(session,handle: str,isGroup: bool,baseDir: str, \ if debug: print("DEBUG: Attempt to undo something which isn't an announcement") return False - undoAnnounceCollectionEntry(postFilename,messageJson['actor'],debug) + undoAnnounceCollectionEntry(baseDir,postFilename,messageJson['actor'],domain,debug) if os.path.isfile(postFilename): os.remove(postFilename) return True