Creating the outbox feed

master
Bob Mottram 2019-06-29 14:17:02 +01:00
parent 162d3944e4
commit 255d498cf7
2 changed files with 60 additions and 9 deletions

View File

@ -13,6 +13,7 @@ from webfinger import webfingerHandle
from posts import getUserPosts from posts import getUserPosts
from posts import createPublicPost from posts import createPublicPost
from posts import deleteAllPosts from posts import deleteAllPosts
from posts import createOutbox
from session import createSession from session import createSession
import json import json
import sys import sys
@ -32,10 +33,14 @@ useTor=False
session = createSession(useTor) session = createSession(useTor)
privateKeyPem,publicKeyPem,person,wfEndpoint=createPerson(username,domain,https,True) privateKeyPem,publicKeyPem,person,wfEndpoint=createPerson(username,domain,https,True)
deleteAllPosts(username,domain) #deleteAllPosts(username,domain)
setPreferredUsername(username,domain,'badger') setPreferredUsername(username,domain,'badger')
setBio(username,domain,'Some personal info') 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) #runDaemon(domain,port,federationList,useTor)

View File

@ -249,25 +249,71 @@ def createPublicPost(username: str, domain: str, https: bool, content: str, foll
# TODO update output feed # TODO update output feed
return newPost 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' prefix='https'
if not https: if not https:
prefix='http' prefix='http'
outboxJsonFilename,outboxDir = createOutboxDir(username,domain) outboxJsonFilename,outboxDir = createOutboxDir(username,domain)
outboxItems=0
outboxHeader = {'@context': 'https://www.w3.org/ns/activitystreams', outboxHeader = {'@context': 'https://www.w3.org/ns/activitystreams',
'first': prefix+'://'+domain+'/users/'+username+'/outbox?page=true', 'first': prefix+'://'+domain+'/users/'+username+'/outbox?page=true',
'id': prefix+'://'+domain+'/users/'+username+'/outbox', 'id': prefix+'://'+domain+'/users/'+username+'/outbox',
'last': prefix+'://'+domain+'/users/'+username+'/outbox?min_id=0&page=true', 'last': prefix+'://'+domain+'/users/'+username+'/outbox?min_id=0&page=true',
'totalItems': str(outboxItems), 'totalItems': 0,
'type': 'OrderedCollection'} 'type': 'OrderedCollection'}
maxMessageId=100000000000000000
minMessageId=100000000000000000
outboxItems = {'@context': 'https://www.w3.org/ns/activitystreams', outboxItems = {'@context': 'https://www.w3.org/ns/activitystreams',
'id': prefix+'://'+domain+'/users/'+username+'/outbox?page=true', 'id': prefix+'://'+domain+'/users/'+username+'/outbox?page=true',
'next': prefix+'://'+domain+'/users/'+username+'/outbox?max_id='+str(maxMessageId)+'&page=true',
'orderedItems': [ 'orderedItems': [
], ],
'partOf': prefix+'://'+domain+'/users/'+username+'/outbox', 'partOf': prefix+'://'+domain+'/users/'+username+'/outbox',
'prev': prefix+'://'+domain+'/users/'+username+'/outbox?min_id='+str(minMessageId)+'&page=true',
'type': 'OrderedCollectionPage'} '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