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 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': [{

View File

@ -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