Remove cached html announce when its state changes

main2
Bob Mottram 2019-10-19 19:21:14 +01:00
parent 236aef17ba
commit 2ef2141a9a
2 changed files with 19 additions and 6 deletions

View File

@ -16,6 +16,7 @@ from utils import urlPermitted
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import locatePost from utils import locatePost
from utils import getCachedPostFilename
from posts import sendSignedJson from posts import sendSignedJson
from posts import getPersonBox from posts import getPersonBox
from session import postJson from session import postJson
@ -42,7 +43,7 @@ def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool:
domain,port=getDomainFromActor(messageJson['actor']) domain,port=getDomainFromActor(messageJson['actor'])
postFilename=locatePost(baseDir,nickname,domain,messageJson['object']) postFilename=locatePost(baseDir,nickname,domain,messageJson['object'])
if postFilename: if postFilename:
updateAnnounceCollection(postFilename,messageJson['actor'],debug) updateAnnounceCollection(baseDir,postFilename,messageJson['actor'],domain,debug)
return True return True
if messageJson['type']=='Undo': if messageJson['type']=='Undo':
if not isinstance(messageJson['object'], dict): if not isinstance(messageJson['object'], dict):
@ -59,11 +60,11 @@ def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool:
domain,port=getDomainFromActor(messageJson['actor']) domain,port=getDomainFromActor(messageJson['actor'])
postFilename=locatePost(baseDir,nickname,domain,messageJson['object']['object']) postFilename=locatePost(baseDir,nickname,domain,messageJson['object']['object'])
if postFilename: if postFilename:
undoAnnounceCollectionEntry(postFilename,messageJson['actor'],debug) undoAnnounceCollectionEntry(baseDir,postFilename,messageJson['actor'],domain,debug)
return True return True
return False 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" """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 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. 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) time.sleep(2)
tries+=1 tries+=1
if postJsonObject: 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'): if not postJsonObject.get('type'):
return return
if postJsonObject['type']!='Create': if postJsonObject['type']!='Create':
@ -125,7 +132,7 @@ def undoAnnounceCollectionEntry(postFilename: str,actor: str,debug: bool) -> Non
time.sleep(1) time.sleep(1)
tries+=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 """Updates the announcements collection within a post
Confusingly this is known as "shares", but isn't the same as shared items within shares.py 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. 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) time.sleep(1)
tries+=1 tries+=1
if postJsonObject: 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 not postJsonObject.get('object'):
if debug: if debug:
pprint(postJsonObject) pprint(postJsonObject)

View File

@ -1066,7 +1066,7 @@ def receiveAnnounce(session,handle: str,isGroup: bool,baseDir: str, \
print('DEBUG: announce post not found in inbox or outbox') print('DEBUG: announce post not found in inbox or outbox')
print(messageJson['object']) print(messageJson['object'])
return True return True
updateAnnounceCollection(postFilename,messageJson['actor'],debug) updateAnnounceCollection(baseDir,postFilename,messageJson['actor'],domain,debug)
if debug: if debug:
print('DEBUG: Downloading announce post '+messageJson['actor']+' -> '+messageJson['object']) print('DEBUG: Downloading announce post '+messageJson['actor']+' -> '+messageJson['object'])
postJsonObject=downloadAnnounce(session,baseDir,httpPrefix,nickname,domain,messageJson,__version__) postJsonObject=downloadAnnounce(session,baseDir,httpPrefix,nickname,domain,messageJson,__version__)
@ -1170,7 +1170,7 @@ def receiveUndoAnnounce(session,handle: str,isGroup: bool,baseDir: str, \
if debug: if debug:
print("DEBUG: Attempt to undo something which isn't an announcement") print("DEBUG: Attempt to undo something which isn't an announcement")
return False return False
undoAnnounceCollectionEntry(postFilename,messageJson['actor'],debug) undoAnnounceCollectionEntry(baseDir,postFilename,messageJson['actor'],domain,debug)
if os.path.isfile(postFilename): if os.path.isfile(postFilename):
os.remove(postFilename) os.remove(postFilename)
return True return True