From 255d498cf7b040200dda0bbbbd2a182cbaed63c6 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 29 Jun 2019 14:17:02 +0100 Subject: [PATCH] Creating the outbox feed --- epicyon.py | 9 ++++++-- posts.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/epicyon.py b/epicyon.py index 6b9281ba..735e5c0f 100644 --- a/epicyon.py +++ b/epicyon.py @@ -13,6 +13,7 @@ from webfinger import webfingerHandle from posts import getUserPosts from posts import createPublicPost from posts import deleteAllPosts +from posts import createOutbox from session import createSession import json import sys @@ -32,10 +33,14 @@ useTor=False session = createSession(useTor) privateKeyPem,publicKeyPem,person,wfEndpoint=createPerson(username,domain,https,True) -deleteAllPosts(username,domain) +#deleteAllPosts(username,domain) setPreferredUsername(username,domain,'badger') setBio(username,domain,'Some personal info') -createPublicPost(username, domain, https, "G'day world!", False, True, None, None, 'Not suitable for Vogons') +#createPublicPost(username, domain, https, "G'day world!", False, True, None, None, 'Not suitable for Vogons') +outboxHeader,outboxJson=createOutbox(username,domain,https,3,None) +pprint(outboxHeader) +print('\n') +pprint(outboxJson) #runDaemon(domain,port,federationList,useTor) diff --git a/posts.py b/posts.py index 74354bc7..b7ad46d1 100644 --- a/posts.py +++ b/posts.py @@ -249,25 +249,71 @@ def createPublicPost(username: str, domain: str, https: bool, content: str, foll # TODO update output feed return newPost -def createOutbox(username: str,domain: str,https: bool,noOfItems: int): +def createOutbox(username: str,domain: str,https: bool,noOfItems: int,startMessageId=None) -> ({},{}): + """Constructs the outbox feed + """ prefix='https' if not https: prefix='http' outboxJsonFilename,outboxDir = createOutboxDir(username,domain) - outboxItems=0 outboxHeader = {'@context': 'https://www.w3.org/ns/activitystreams', 'first': prefix+'://'+domain+'/users/'+username+'/outbox?page=true', 'id': prefix+'://'+domain+'/users/'+username+'/outbox', 'last': prefix+'://'+domain+'/users/'+username+'/outbox?min_id=0&page=true', - 'totalItems': str(outboxItems), + 'totalItems': 0, 'type': 'OrderedCollection'} - maxMessageId=100000000000000000 - minMessageId=100000000000000000 outboxItems = {'@context': 'https://www.w3.org/ns/activitystreams', 'id': prefix+'://'+domain+'/users/'+username+'/outbox?page=true', - 'next': prefix+'://'+domain+'/users/'+username+'/outbox?max_id='+str(maxMessageId)+'&page=true', 'orderedItems': [ ], 'partOf': prefix+'://'+domain+'/users/'+username+'/outbox', - 'prev': prefix+'://'+domain+'/users/'+username+'/outbox?min_id='+str(minMessageId)+'&page=true', 'type': 'OrderedCollectionPage'} + + # counter for posts loop + postCtr=0 + + # post filenames sorted in descending order + postsInOutbox=sorted(os.listdir(outboxDir), reverse=True) + + # number of posts in outbox + outboxHeader['totalItems']=len(postsInOutbox) + prevPostFilename=None + + # Generate first and last entries within header + if len(postsInOutbox)>0: + postId = postsInOutbox[len(postsInOutbox)-1].split('#statuses#')[1].replace('#activity','') + outboxHeader['last']= \ + prefix+'://'+domain+'/users/'+username+'/outbox?min_id='+postId+'&page=true' + postId = postsInOutbox[0].split('#statuses#')[1].replace('#activity','') + outboxHeader['first']= \ + prefix+'://'+domain+'/users/'+username+'/outbox?max_id='+postId+'&page=true' + + # Insert posts + for postFilename in postsInOutbox: + if startMessageId and prevPostFilename: + if '#statuses#'+startMessageId in postFilename: + postId = prevPostFilename.split('#statuses#')[1].replace('#activity','') + outboxHeader['prev']= \ + prefix+'://'+domain+'/users/'+username+'/outbox?min_id='+postId+'&page=true' + filePath = os.path.join(outboxDir, postFilename) + try: + if os.path.isfile(filePath): + if postCtr <= noOfItems: + with open(filePath, 'r') as fp: + p=commentjson.load(fp) + if postCtr < noOfItems: + outboxItems['orderedItems'].append(p) + elif postCtr == noOfItems: + if '/statuses/' in p['id']: + postId = p['id'].split('/statuses/')[1].replace('/activity','') + outboxHeader['next']= \ + prefix+'://'+domain+'/users/'+ \ + username+'/outbox?max_id='+ \ + postId+'&page=true' + postCtr += 1 + prevPostFilename = postFilename + if postCtr > noOfItems: + break + except Exception as e: + print(e) + return outboxHeader,outboxItems