Improving inbox post distribution

master
Bob Mottram 2019-07-09 00:05:48 +01:00
parent d3c64fa797
commit 18f29fb9b0
2 changed files with 32 additions and 74 deletions

View File

@ -306,6 +306,7 @@ def runInboxQueue(baseDir: str,httpPrefix: str,sendThreads: [],postLog: [],cache
currSessionTime=int(time.time()) currSessionTime=int(time.time())
sessionLastUpdate=currSessionTime sessionLastUpdate=currSessionTime
session=createSession(domain,port,useTor) session=createSession(domain,port,useTor)
inboxHandle='inbox@'+domain
if debug: if debug:
print('DEBUG: Inbox queue running') print('DEBUG: Inbox queue running')
@ -418,21 +419,6 @@ def runInboxQueue(baseDir: str,httpPrefix: str,sendThreads: [],postLog: [],cache
pprint(recipientsDict) pprint(recipientsDict)
print('*************************************') print('*************************************')
# is this sent to the shared inbox? (actor is the 'inbox' account)
sentToSharedInbox=False
if queueJson['post'].get('actor'):
if queueJson['post']['actor'].endswith('/inbox'):
sentToSharedInbox=True
if sentToSharedInbox:
# if this is arriving at the shared inbox then
# don't do the capabilities checks
capabilitiesPassed=True
# TODO how to handle capabilities in the shared inbox scenario?
# should 'capability' be a list instead of a single value?
else:
# check that capabilities are accepted
capabilitiesPassed=False
if queueJson['post'].get('capability'): if queueJson['post'].get('capability'):
if not isinstance(queueJson['post']['capability'], list): if not isinstance(queueJson['post']['capability'], list):
if debug: if debug:
@ -440,61 +426,33 @@ def runInboxQueue(baseDir: str,httpPrefix: str,sendThreads: [],postLog: [],cache
os.remove(queueFilename) os.remove(queueFilename)
queue.pop(0) queue.pop(0)
continue continue
for handle,capsId in recipientsDict.items():
# check that capabilities are accepted
if queueJson['post'].get('capability'):
capabilityIdList=queueJson['post']['capability'] capabilityIdList=queueJson['post']['capability']
# does the capability id list within the post contain the id
if capabilityIdList: # of one of the recipients?
capabilitiesPassed= \ if capsId in capabilityIdList:
inboxCheckCapabilities(baseDir,queueJson['nickname'], \ if debug:
queueJson['domain'], \ print('DEBUG: object capabilities passed')
queueJson['post']['actor'], \ print('copy from '+queueFilename+' to '+queueJson['destination'].replace(inboxHandle,handle))
queue,queueJson, \ copyfile(queueFilename,queueJson['destination'].replace(inboxHandle,handle))
capabilityIdList[0],debug) else:
if ocapAlways and not capabilitiesPassed:
# Allow follow types through
# i.e. anyone can make a follow request
if queueJson['post'].get('type'):
if queueJson['post']['type']=='Follow' or \
queueJson['post']['type']=='Accept':
capabilitiesPassed=True
if not capabilitiesPassed:
if debug: if debug:
print('DEBUG: object capabilities check failed') print('DEBUG: object capabilities check failed')
pprint(queueJson['post']) pprint(queueJson['post'])
os.remove(queueFilename) else:
queue.pop(0) if not ocapAlways:
if debug:
print('DEBUG: not enforcing object capabilities')
print('copy from '+queueFilename+' to '+queueJson['destination'].replace(inboxHandle,handle))
copyfile(queueFilename,queueJson['destination'].replace(inboxHandle,handle))
continue continue
if debug: if debug:
print('DEBUG: Queue post accepted') print('DEBUG: Queue post accepted')
if queueJson['sharedInbox']:
if '/users/' in keyId:
# Who is this from? Use the actor from the keyId where we obtained the public key
fromList=keyId.replace('https://','').replace('http://','').replace('dat://','').replace('#main-key','').split('/users/')
fromNickname=fromList[1]
fromDomain=fromList[0]
# get the followers of the sender
followList=getFollowersOfPerson(baseDir,fromNickname,fromDomain)
for followerHandle in followList:
followerDir=baseDir+'/accounts/'+followerHandle
if os.path.isdir(followerDir):
if not os.path.isdir(followerDir+'/inbox'):
os.mkdir(followerDir+'/inbox')
postId=queueJson['post']['id'].replace('/activity','')
destination=followerDir+'/inbox/'+postId.replace('/','#')+'.json'
if os.path.isfile(destination):
# post already exists in this person's inbox
continue
# We could do this in a more storage space efficient way
# by linking to the inbox of inbox@domain
# However, this allows for easy deletion by individuals
# without affecting any other people
copyfile(queueFilename, destination)
# copy to followers
# remove item from shared inbox
os.remove(queueFilename) os.remove(queueFilename)
else:
# move to the destination inbox
os.rename(queueFilename,queueJson['destination'])
queue.pop(0) queue.pop(0)

View File

@ -403,7 +403,7 @@ def testFollowBetweenServers():
for i in range(20): for i in range(20):
time.sleep(1) time.sleep(1)
if os.path.isdir(inboxPath): if os.path.isdir(inboxPath):
if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])>1: if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])>0:
aliceMessageArrived=True aliceMessageArrived=True
print('Alice message sent to Bob!') print('Alice message sent to Bob!')
break break
@ -431,7 +431,7 @@ def testFollowBetweenServers():
assert 'bob@'+bobDomain in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read() assert 'bob@'+bobDomain in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read()
os.chdir(baseDir) os.chdir(baseDir)
#shutil.rmtree(baseDir+'/.tests') shutil.rmtree(baseDir+'/.tests')
def testFollowersOfPerson(): def testFollowersOfPerson():
print('testFollowersOfPerson') print('testFollowersOfPerson')