Update announce list for associated post if the announce is sent via c2s

master
Bob Mottram 2019-08-01 12:43:22 +01:00
parent 36561a9e54
commit c7ecd001a9
4 changed files with 55 additions and 6 deletions

View File

@ -8,17 +8,54 @@ __status__ = "Production"
import json import json
import commentjson import commentjson
from pprint import pprint
from utils import getStatusNumber from utils import getStatusNumber
from utils import createOutboxDir from utils import createOutboxDir
from utils import urlPermitted 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 posts import sendSignedJson from posts import sendSignedJson
from posts import getPersonBox from posts import getPersonBox
from session import postJson from session import postJson
from webfinger import webfingerHandle from webfinger import webfingerHandle
from auth import createBasicAuthHeader from auth import createBasicAuthHeader
def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool:
""" Adds or removes announce entries from the shares collection
within a given post
"""
if not messageJson.get('actor'):
return False
if not messageJson.get('type'):
return False
if not messageJson.get('object'):
return False
if messageJson['type']=='Announce':
if not isinstance(messageJson['object'], str):
return
nickname=getNicknameFromActor(messageJson['actor'])
domain,port=getDomainFromActor(messageJson['actor'])
postFilename=locatePost(baseDir,nickname,domain,messageJson['object'])
if postFilename:
updateAnnounceCollection(postFilename,messageJson['actor'],debug)
return True
if messageJson['type']=='Undo':
if not isinstance(messageJson['object'], dict):
return
if not messageJson['object'].get('type'):
return False
if messageJson['object']['type']=='Announce':
if not isinstance(messageJson['object']['object'], str):
return
nickname=getNicknameFromActor(messageJson['actor'])
domain,port=getDomainFromActor(messageJson['actor'])
postFilename=locatePost(baseDir,nickname,domain,messageJson['object']['object'])
if postFilename:
undoAnnounceCollectionEntry(postFilename,messageJson['actor'],debug)
return True
return False
def undoAnnounceCollectionEntry(postFilename: str,actor: str,debug: bool) -> None: def undoAnnounceCollectionEntry(postFilename: str,actor: 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
@ -27,14 +64,16 @@ def undoAnnounceCollectionEntry(postFilename: str,actor: str,debug: bool) -> Non
with open(postFilename, 'r') as fp: with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp) postJsonObject=commentjson.load(fp)
if not postJsonObject.get('type'): if not postJsonObject.get('type'):
if postJsonObject['type']!='Create':
return return
if postJsonObject['type']!='Create':
return return
if not postJsonObject.get('object'): if not postJsonObject.get('object'):
if debug: if debug:
pprint(postJsonObject) pprint(postJsonObject)
print('DEBUG: post has no object') print('DEBUG: post has no object')
return return
if not isinstance(postJsonObject['object'], dict):
return
if not postJsonObject['object'].get('shares'): if not postJsonObject['object'].get('shares'):
return return
if not postJsonObject['object']['shares'].get('items'): if not postJsonObject['object']['shares'].get('items'):
@ -73,6 +112,8 @@ def updateAnnounceCollection(postFilename: str,actor: str,debug: bool) -> None:
pprint(postJsonObject) pprint(postJsonObject)
print('DEBUG: post '+announceUrl+' has no object') print('DEBUG: post '+announceUrl+' has no object')
return return
if not isinstance(postJsonObject['object'], dict):
return
postUrl=postJsonObject['id'].replace('/activity','')+'/shares' postUrl=postJsonObject['id'].replace('/activity','')+'/shares'
if not postJsonObject['object'].get('shares'): if not postJsonObject['object'].get('shares'):
if debug: if debug:

View File

@ -78,6 +78,7 @@ from utils import getDomainFromActor
from manualapprove import manualDenyFollowRequest from manualapprove import manualDenyFollowRequest
from manualapprove import manualApproveFollowRequest from manualapprove import manualApproveFollowRequest
from announce import createAnnounce from announce import createAnnounce
from announce import outboxAnnounce
import os import os
import sys import sys
@ -258,6 +259,9 @@ class PubServer(BaseHTTPRequestHandler):
postId, \ postId, \
self.postToNickname, \ self.postToNickname, \
domainFull,messageJson,'outbox') domainFull,messageJson,'outbox')
if outboxAnnounce(self.server.baseDir,messageJson,self.server.debug):
if self.server.debug:
print('DEBUG: Updated announcements (shares) collection for the post associated with the Announce activity')
if not self.server.session: if not self.server.session:
if self.server.debug: if self.server.debug:
print('DEBUG: creating new session for c2s') print('DEBUG: creating new session for c2s')

View File

@ -25,14 +25,16 @@ def undoLikesCollectionEntry(postFilename: str,objectUrl: str, actor: str,debug:
with open(postFilename, 'r') as fp: with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp) postJsonObject=commentjson.load(fp)
if not postJsonObject.get('type'): if not postJsonObject.get('type'):
if postJsonObject['type']!='Create':
return return
if postJsonObject['type']!='Create':
return return
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 '+objectUrl+' has no object')
return return
if not isinstance(postJsonObject['object'], dict):
return
if not postJsonObject['object'].get('likes'): if not postJsonObject['object'].get('likes'):
return return
if not postJsonObject['object']['likes'].get('items'): if not postJsonObject['object']['likes'].get('items'):

View File

@ -346,11 +346,12 @@ def deleteAllPosts(baseDir: str,nickname: str, domain: str,boxname: str) -> None
def savePostToBox(baseDir: str,httpPrefix: str,postId: str, \ def savePostToBox(baseDir: str,httpPrefix: str,postId: str, \
nickname: str, domain: str,postJsonObject: {}, \ nickname: str, domain: str,postJsonObject: {}, \
boxname: str) -> None: boxname: str) -> str:
"""Saves the give json to the give box """Saves the give json to the give box
Returns the filename
""" """
if boxname!='inbox' and boxname!='outbox': if boxname!='inbox' and boxname!='outbox':
return return None
originalDomain=domain originalDomain=domain
if ':' in domain: if ':' in domain:
domain=domain.split(':')[0] domain=domain.split(':')[0]
@ -369,6 +370,7 @@ def savePostToBox(baseDir: str,httpPrefix: str,postId: str, \
filename=boxDir+'/'+postId.replace('/','#')+'.json' filename=boxDir+'/'+postId.replace('/','#')+'.json'
with open(filename, 'w') as fp: with open(filename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
return filename
def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \
toUrl: str, ccUrl: str, httpPrefix: str, content: str, \ toUrl: str, ccUrl: str, httpPrefix: str, content: str, \