diff --git a/posts.py b/posts.py index 43e7c336..98dc2193 100644 --- a/posts.py +++ b/posts.py @@ -1810,8 +1810,8 @@ def createMediaTimeline(session,baseDir: str,nickname: str,domain: str,port: int def createOutbox(session,baseDir: str,nickname: str,domain: str,port: int,httpPrefix: str, \ itemsPerPage: int,headerOnly: bool,authorized: bool,pageNumber=None) -> {}: - return createBoxBase(session,baseDir,'outbox',nickname,domain,port,httpPrefix, \ - itemsPerPage,headerOnly,authorized,False,pageNumber) + return createBoxIndexed(session,baseDir,'outbox',nickname,domain,port,httpPrefix, \ + itemsPerPage,headerOnly,authorized,False,pageNumber) def createModeration(baseDir: str,nickname: str,domain: str,port: int,httpPrefix: str, \ itemsPerPage: int,headerOnly: bool,ocapAlways: bool,pageNumber=None) -> {}: @@ -2053,229 +2053,6 @@ def createSharedInboxIndex(baseDir: str,sharedBoxDir: str, \ postsCtr+=1 return postsCtr -def createBoxBase(session,baseDir: str,boxname: str, \ - nickname: str,domain: str,port: int,httpPrefix: str, \ - itemsPerPage: int,headerOnly: bool,authorized :bool, \ - ocapAlways: bool,pageNumber=None) -> {}: - """Constructs the box feed for a person with the given nickname - """ - if not authorized: - pageNumber=1 - - if boxname!='inbox' and boxname!='dm' and \ - boxname!='tlreplies' and boxname!='tlmedia' and \ - boxname!='outbox' and boxname!='tlbookmarks': - return None - - if boxname!='dm' and boxname!='tlreplies' and \ - boxname!='tlmedia' and boxname!='tlbookmarks': - boxDir = createPersonDir(nickname,domain,baseDir,boxname) - else: - # extract DMs or replies or media from the inbox - boxDir = createPersonDir(nickname,domain,baseDir,'inbox') - - announceCacheDir=baseDir+'/cache/announce/'+nickname - - sharedBoxDir=None - if boxname=='inbox' or boxname=='tlreplies' or \ - boxname=='tlmedia': - sharedBoxDir = createPersonDir('inbox',domain,baseDir,boxname) - - # bookmarks timeline is like the inbox but has its own separate index - indexBoxName=boxname - if boxname=='tlbookmarks': - indexBoxName='bookmarks' - - if port: - if port!=80 and port!=443: - if ':' not in domain: - domain=domain+':'+str(port) - - boxActor=httpPrefix+'://'+domain+'/users/'+nickname - - pageStr='?page=true' - if pageNumber: - try: - pageStr='?page='+str(pageNumber) - except: - pass - boxHeader = {'@context': 'https://www.w3.org/ns/activitystreams', - 'first': httpPrefix+'://'+domain+'/users/'+nickname+'/'+boxname+'?page=true', - 'id': httpPrefix+'://'+domain+'/users/'+nickname+'/'+boxname, - 'last': httpPrefix+'://'+domain+'/users/'+nickname+'/'+boxname+'?page=true', - 'totalItems': 0, - 'type': 'OrderedCollection'} - boxItems = {'@context': 'https://www.w3.org/ns/activitystreams', - 'id': httpPrefix+'://'+domain+'/users/'+nickname+'/'+boxname+pageStr, - 'orderedItems': [ - ], - 'partOf': httpPrefix+'://'+domain+'/users/'+nickname+'/'+boxname, - 'type': 'OrderedCollectionPage'} - - # counter for posts so far added to the target page - postsOnPageCtr=0 - - # post filenames sorted in descending order - postsInBoxDict={} - postsInBox={} - - indexFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/'+indexBoxName+'.index' - lookedUpFromIndex=False - postsCtr=0 - if os.path.isfile(indexFilename): - print('DEBUG: using index file to construct timeline') - maxPostCtr=None - if not pageNumber: - minPageNumber=4 - else: - minPageNumber=pageNumber+4 - maxPostCtr=itemsPerPage*minPageNumber - with open(indexFilename, 'r') as indexFile: - while postsCtr0: - lastPage=int(postsCtr/itemsPerPage) - if lastPage<1: - lastPage=1 - boxHeader['last']= \ - httpPrefix+'://'+domain+'/users/'+nickname+'/'+boxname+'?page='+str(lastPage) - - # Insert posts - currPage=1 - postsCtr=0 - for statusNumber,postFilename in postsInBox.items(): - # Are we at the starting page yet? - if prevPostFilename and currPage==pageNumber and postsCtr==0: - # update the prev entry for the last message id - if '#statuses#' in prevPostFilename: - postId = prevPostFilename.split('#statuses#')[1].replace('#activity','') - boxHeader['prev']= \ - httpPrefix+'://'+domain+'/users/'+nickname+'/'+ \ - boxname+'?min_id='+postId+'&page=true' - - # get the full path of the post file - filePath = postFilename - try: - if os.path.isfile(filePath): - # is this a valid timeline post? - isPost=False - # must be a "Note" or "Announce" type - with open(filePath, 'r') as file: - postStr = file.read() - if '"Note"' in postStr or '"Announce"' in postStr or \ - ('"Question"' in postStr and '"Create"' in postStr): - isPost=True - if boxname=='dm': - if '#Public' in postStr or '/followers' in postStr: - isPost=False - elif boxname=='tlreplies': - if boxActor not in postStr: - isPost=False - elif boxname=='tlmedia': - if '"Create"' in postStr: - if 'mediaType' not in postStr or 'image/' not in postStr: - isPost=False - - if isPost: - isTimelinePost=False - if currPage <= pageNumber and postsOnPageCtr <= itemsPerPage: - # get the post as json - p = json.loads(postStr) - - if lookedUpFromIndex: - isTimelinePost=True - else: - if (boxname!='dm' and boxname!='tlreplies' and boxname!='tlmedia'): - isTimelinePost=True - else: - if boxname=='dm': - if isDM(p): - isTimelinePost=True - elif boxname=='tlreplies': - if isDM(p) or isReply(p,boxActor): - isTimelinePost=True - elif boxname=='tlmedia': - if isImageMedia(session,baseDir,httpPrefix,nickname,domain,p): - isTimelinePost=True - - if isTimelinePost and currPage == pageNumber: - # remove any capability so that it's not displayed - if p.get('capability'): - del p['capability'] - # Don't show likes, replies or shares (announces) to unauthorized viewers - if not authorized: - if p.get('object'): - if isinstance(p['object'], dict): - if p['object'].get('likes'): - p['likes']={} - if p['object'].get('replies'): - p['replies']={} - if p['object'].get('shares'): - p['shares']={} - if p['object'].get('bookmarks'): - p['bookmarks']={} - # insert it into the box feed - if postsOnPageCtr < itemsPerPage: - if not headerOnly: - boxItems['orderedItems'].append(p) - postsOnPageCtr += 1 - if postsOnPageCtr >= itemsPerPage: - # if this is the last post update the next message ID - if '/statuses/' in p['id']: - postId = p['id'].split('/statuses/')[1].replace('/activity','') - boxHeader['next']= \ - httpPrefix+'://'+domain+'/users/'+ \ - nickname+'/'+boxname+'?max_id='+ \ - postId+'&page=true' - - # remember the last post filename for use with prev - prevPostFilename = postFilename - if postsOnPageCtr >= itemsPerPage: - break - # count the pages - if isTimelinePost: - postsCtr += 1 - if postsCtr >= itemsPerPage: - postsCtr = 0 - currPage += 1 - except Exception as e: - print(e) - if headerOnly: - return boxHeader - return boxItems - def addPostToTimeline(filePath: str,boxname: str,postsInBox: [],boxActor: str) -> bool: """ is this a valid timeline post? """