From 9c2f3d9e3bea3f08f2af564f1a7f7eb3dec47d45 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 1 Aug 2019 11:05:31 +0100 Subject: [PATCH] Update post shares collection when an announce arrives in inbox --- announce.py | 15 +++++++-------- inbox.py | 7 +++++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/announce.py b/announce.py index b4788944..0c36eda9 100644 --- a/announce.py +++ b/announce.py @@ -19,7 +19,7 @@ from session import postJson from webfinger import webfingerHandle from auth import createBasicAuthHeader -def undoAnnounceCollectionEntry(postFilename: str,objectUrl: str, actor: str,debug: bool) -> None: +def undoAnnounceCollectionEntry(postFilename: str,actor: str,debug: bool) -> None: """Undoes an announce for a particular actor """ with open(postFilename, 'r') as fp: @@ -31,7 +31,7 @@ def undoAnnounceCollectionEntry(postFilename: str,objectUrl: str, actor: str,deb if not postJsonObject.get('object'): if debug: pprint(postJsonObject) - print('DEBUG: post '+objectUrl+' has no object') + print('DEBUG: post has no object') return if not postJsonObject['object'].get('shares'): return @@ -59,7 +59,7 @@ def undoAnnounceCollectionEntry(postFilename: str,objectUrl: str, actor: str,deb with open(postFilename, 'w') as fp: commentjson.dump(postJsonObject, fp, indent=4, sort_keys=True) -def updateAnnounceCollection(postFilename: str,objectUrl: str, actor: str,debug: bool) -> None: +def updateAnnounceCollection(postFilename: str,actor: 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 """ @@ -68,15 +68,14 @@ def updateAnnounceCollection(postFilename: str,objectUrl: str, actor: str,debug: if not postJsonObject.get('object'): if debug: pprint(postJsonObject) - print('DEBUG: post '+objectUrl+' has no object') + print('DEBUG: post '+announceUrl+' has no object') return - if not objectUrl.endswith('/shares'): - objectUrl=objectUrl+'/shares' + postUrl=postJsonObject['id'].replace('/activity','')+'/shares' if not postJsonObject['object'].get('shares'): if debug: - print('DEBUG: Adding initial shares to '+objectUrl) + print('DEBUG: Adding initial shares (announcements) to '+postUrl) announcementsJson = { - 'id': objectUrl, + 'id': postUrl, 'type': 'Collection', "totalItems": 1, 'items': [{ diff --git a/inbox.py b/inbox.py index e51fc285..a89ff8a4 100644 --- a/inbox.py +++ b/inbox.py @@ -39,6 +39,7 @@ from like import updateLikesCollection from like import undoLikesCollectionEntry from blocking import isBlocked from filters import isFiltered +from announce import updateAnnounceCollection def validInbox(baseDir: str,nickname: str,domain: str) -> bool: """Checks whether files were correctly saved to the inbox @@ -743,13 +744,14 @@ def receiveAnnounce(session,handle: str,baseDir: str, \ return False if not os.path.isdir(baseDir+'/accounts/'+handle): print('DEBUG: unknown recipient of announce - '+handle) - # if this post in the outbox of the person? + # is this post in the outbox of the person? postFilename=locatePost(baseDir,handle.split('@')[0],handle.split('@')[1],messageJson['object']) if not postFilename: if debug: print('DEBUG: announce post not found in inbox or outbox') print(messageJson['object']) return True + updateAnnounceCollection(postFilename,messageJson['actor'],debug) if debug: print('DEBUG: announced/repeated post found in inbox') return True @@ -799,8 +801,9 @@ def receiveUndoAnnounce(session,handle: str,baseDir: str, \ if not postJsonObject.get('type'): if postJsonObject['type']!='Announce': if debug: - print("DEBUG: Attenpt to undo something which isn't an announcement") + print("DEBUG: Attempt to undo something which isn't an announcement") return False + undoAnnounceCollectionEntry(postFilename,messageJson['actor'],debug) os.remove(postFilename) return True