Update post shares collection when an announce arrives in inbox

master
Bob Mottram 2019-08-01 11:05:31 +01:00
parent a182bd2343
commit 9c2f3d9e3b
2 changed files with 12 additions and 10 deletions

View File

@ -19,7 +19,7 @@ from session import postJson
from webfinger import webfingerHandle from webfinger import webfingerHandle
from auth import createBasicAuthHeader 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 """Undoes an announce for a particular actor
""" """
with open(postFilename, 'r') as fp: 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 not postJsonObject.get('object'):
if debug: if debug:
pprint(postJsonObject) pprint(postJsonObject)
print('DEBUG: post '+objectUrl+' has no object') print('DEBUG: post has no object')
return return
if not postJsonObject['object'].get('shares'): if not postJsonObject['object'].get('shares'):
return return
@ -59,7 +59,7 @@ def undoAnnounceCollectionEntry(postFilename: str,objectUrl: str, actor: str,deb
with open(postFilename, 'w') as fp: with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=True) 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 """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
""" """
@ -68,15 +68,14 @@ def updateAnnounceCollection(postFilename: str,objectUrl: str, actor: str,debug:
if not postJsonObject.get('object'): if not postJsonObject.get('object'):
if debug: if debug:
pprint(postJsonObject) pprint(postJsonObject)
print('DEBUG: post '+objectUrl+' has no object') print('DEBUG: post '+announceUrl+' has no object')
return return
if not objectUrl.endswith('/shares'): postUrl=postJsonObject['id'].replace('/activity','')+'/shares'
objectUrl=objectUrl+'/shares'
if not postJsonObject['object'].get('shares'): if not postJsonObject['object'].get('shares'):
if debug: if debug:
print('DEBUG: Adding initial shares to '+objectUrl) print('DEBUG: Adding initial shares (announcements) to '+postUrl)
announcementsJson = { announcementsJson = {
'id': objectUrl, 'id': postUrl,
'type': 'Collection', 'type': 'Collection',
"totalItems": 1, "totalItems": 1,
'items': [{ 'items': [{

View File

@ -39,6 +39,7 @@ from like import updateLikesCollection
from like import undoLikesCollectionEntry from like import undoLikesCollectionEntry
from blocking import isBlocked from blocking import isBlocked
from filters import isFiltered from filters import isFiltered
from announce import updateAnnounceCollection
def validInbox(baseDir: str,nickname: str,domain: str) -> bool: def validInbox(baseDir: str,nickname: str,domain: str) -> bool:
"""Checks whether files were correctly saved to the inbox """Checks whether files were correctly saved to the inbox
@ -743,13 +744,14 @@ def receiveAnnounce(session,handle: str,baseDir: str, \
return False return False
if not os.path.isdir(baseDir+'/accounts/'+handle): if not os.path.isdir(baseDir+'/accounts/'+handle):
print('DEBUG: unknown recipient of announce - '+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']) postFilename=locatePost(baseDir,handle.split('@')[0],handle.split('@')[1],messageJson['object'])
if not postFilename: if not postFilename:
if debug: if debug:
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)
if debug: if debug:
print('DEBUG: announced/repeated post found in inbox') print('DEBUG: announced/repeated post found in inbox')
return True return True
@ -799,8 +801,9 @@ def receiveUndoAnnounce(session,handle: str,baseDir: str, \
if not postJsonObject.get('type'): if not postJsonObject.get('type'):
if postJsonObject['type']!='Announce': if postJsonObject['type']!='Announce':
if debug: 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 return False
undoAnnounceCollectionEntry(postFilename,messageJson['actor'],debug)
os.remove(postFilename) os.remove(postFilename)
return True return True