diff --git a/epicyon.py b/epicyon.py index 735e5c0f..6b14c50a 100644 --- a/epicyon.py +++ b/epicyon.py @@ -14,6 +14,7 @@ from posts import getUserPosts from posts import createPublicPost from posts import deleteAllPosts from posts import createOutbox +from posts import archivePosts from session import createSession import json import sys @@ -37,6 +38,7 @@ privateKeyPem,publicKeyPem,person,wfEndpoint=createPerson(username,domain,https, 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') +archivePosts(username,domain,4) outboxHeader,outboxJson=createOutbox(username,domain,https,3,None) pprint(outboxHeader) print('\n') diff --git a/posts.py b/posts.py index 9e8f5e9c..a736d0ae 100644 --- a/posts.py +++ b/posts.py @@ -166,6 +166,18 @@ def createOutboxDir(username: str,domain: str) -> str: os.mkdir(outboxDir) return outboxDir +def createOutboxArchive(username: str,domain: str) -> str: + """Creates an archive directory for outbox posts + """ + handle=username.lower()+'@'+domain.lower() + baseDir=os.getcwd() + if not os.path.isdir(baseDir+'/accounts/'+handle): + os.mkdir(baseDir+'/accounts/'+handle) + outboxArchiveDir=baseDir+'/accounts/'+handle+'/outboxarchive' + if not os.path.isdir(outboxArchiveDir): + os.mkdir(outboxArchiveDir) + return outboxArchiveDir + def deleteAllPosts(username: str, domain: str) -> None: """Deletes all posts for a person """ @@ -322,3 +334,24 @@ def createOutbox(username: str,domain: str,https: bool,noOfItems: int,startMessa except Exception as e: print(e) return outboxHeader,outboxItems + +def archivePosts(username: str,domain: str,maxPostsInOutbox=256) -> None: + """Retain a maximum number of posts within the outbox + Move any others to an archive directory + """ + outboxDir = createOutboxDir(username,domain) + archiveDir = createOutboxArchive(username,domain) + postsInOutbox=sorted(os.listdir(outboxDir), reverse=False) + noOfPosts=len(postsInOutbox) + if noOfPosts<=maxPostsInOutbox: + return + + for postFilename in postsInOutbox: + filePath = os.path.join(outboxDir, postFilename) + if os.path.isfile(filePath): + archivePath = os.path.join(archiveDir, postFilename) + os.rename(filePath,archivePath) + # TODO: possibly archive any associated media files + noOfPosts -= 1 + if noOfPosts <= maxPostsInOutbox: + break