Archive old posts from outbox

master
Bob Mottram 2019-06-29 14:44:21 +01:00
parent 09aceda930
commit e2aaa123d7
2 changed files with 35 additions and 0 deletions

View File

@ -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')

View File

@ -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