Remove attachments for deleted posts

master
Bob Mottram 2019-07-14 18:02:41 +01:00
parent e1a6528307
commit 090f8eacc3
4 changed files with 15 additions and 9 deletions

View File

@ -504,7 +504,7 @@ if args.archive:
else: else:
print('Archiving to '+args.archive+'...') print('Archiving to '+args.archive+'...')
archiveMedia(baseDir,args.archive,archiveWeeks) archiveMedia(baseDir,args.archive,archiveWeeks)
archivePosts(baseDir,args.archive,archiveMaxPosts) archivePosts(baseDir,httpPrefix,args.archive,archiveMaxPosts)
print('Archiving complete') print('Archiving complete')
sys.exit() sys.exit()

View File

@ -536,7 +536,7 @@ def receiveDelete(session,handle: str,baseDir: str, \
print('DEBUG: delete post not found in inbox or outbox') print('DEBUG: delete post not found in inbox or outbox')
print(messageJson['object']) print(messageJson['object'])
return True return True
deletePost(baseDir,handle.split('@')[0],handle.split('@')[1],postFilename,debug) deletePost(baseDir,httpPrefix,handle.split('@')[0],handle.split('@')[1],postFilename,debug)
if debug: if debug:
print('DEBUG: post deleted - '+postFilename) print('DEBUG: post deleted - '+postFilename)
return True return True

View File

@ -948,7 +948,7 @@ def createBoxBase(baseDir: str,boxname: str, \
return boxHeader return boxHeader
return boxItems return boxItems
def archivePosts(baseDir: str,archiveDir: str,maxPostsInBox=256) -> None: def archivePosts(baseDir: str,httpPrefix: str,archiveDir: str,maxPostsInBox=256) -> None:
"""Archives posts for all accounts """Archives posts for all accounts
""" """
if archiveDir: if archiveDir:
@ -972,16 +972,16 @@ def archivePosts(baseDir: str,archiveDir: str,maxPostsInBox=256) -> None:
if not os.path.isdir(archiveDir+'/accounts/'+handle+'/outbox'): if not os.path.isdir(archiveDir+'/accounts/'+handle+'/outbox'):
os.mkdir(archiveDir+'/accounts/'+handle+'/outbox') os.mkdir(archiveDir+'/accounts/'+handle+'/outbox')
archiveSubdir=archiveDir+'/accounts/'+handle+'/inbox' archiveSubdir=archiveDir+'/accounts/'+handle+'/inbox'
archivePostsForPerson(nickname,domain,baseDir, \ archivePostsForPerson(httpPrefix,nickname,domain,baseDir, \
'inbox',archiveSubdir, \ 'inbox',archiveSubdir, \
maxPostsInBox) maxPostsInBox)
if archiveDir: if archiveDir:
archiveSubdir=archiveDir+'/accounts/'+handle+'/outbox' archiveSubdir=archiveDir+'/accounts/'+handle+'/outbox'
archivePostsForPerson(nickname,domain,baseDir, \ archivePostsForPerson(httpPrefix,nickname,domain,baseDir, \
'outbox',archiveSubdir, \ 'outbox',archiveSubdir, \
maxPostsInBox) maxPostsInBox)
def archivePostsForPerson(nickname: str,domain: str,baseDir: str, \ def archivePostsForPerson(httpPrefix: str,nickname: str,domain: str,baseDir: str, \
boxname: str,archiveDir: str,maxPostsInBox=256) -> None: boxname: str,archiveDir: str,maxPostsInBox=256) -> None:
"""Retain a maximum number of posts within the given box """Retain a maximum number of posts within the given box
Move any others to an archive directory Move any others to an archive directory
@ -1007,7 +1007,7 @@ def archivePostsForPerson(nickname: str,domain: str,baseDir: str, \
if os.path.isfile(repliesPath): if os.path.isfile(repliesPath):
os.rename(repliesPath,archivePath) os.rename(repliesPath,archivePath)
else: else:
deletePost(baseDir,nickname,domain,filePath,False) deletePost(baseDir,httpPrefix,nickname,domain,filePath,False)
noOfPosts -= 1 noOfPosts -= 1
if noOfPosts <= maxPostsInBox: if noOfPosts <= maxPostsInBox:
break break

View File

@ -8,6 +8,7 @@ __status__ = "Production"
import os import os
import datetime import datetime
import commentjson
def getStatusNumber() -> (str,str): def getStatusNumber() -> (str,str):
"""Returns the status number and published date """Returns the status number and published date
@ -158,11 +159,15 @@ def removeAttachment(baseDir: str,httpPrefix: str,domain: str,postJson: {}):
os.remove(mediaFilename) os.remove(mediaFilename)
postJson['attachment']=[] postJson['attachment']=[]
def deletePost(baseDir: str,nickname: str,domain: str,postFilename: str,debug: bool): def deletePost(baseDir: str,httpPrefix: str,nickname: str,domain: str,postFilename: str,debug: bool):
"""Recursively deletes a post and its replies and attachments """Recursively deletes a post and its replies and attachments
""" """
# remove any attachment
with open(postFilename, 'r') as fp: with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp) postJsonObject=commentjson.load(fp)
removeAttachment(baseDir,httpPrefix,domain,postJsonObject)
# remove any replies
repliesFilename=postFilename.replace('.json','.replies') repliesFilename=postFilename.replace('.json','.replies')
if os.path.isfile(repliesFilename): if os.path.isfile(repliesFilename):
if debug: if debug:
@ -173,6 +178,7 @@ def deletePost(baseDir: str,nickname: str,domain: str,postFilename: str,debug: b
if replyFile: if replyFile:
if os.path.isfile(replyFile): if os.path.isfile(replyFile):
deletePost(baseDir,nickname,domain,replyFile,debug) deletePost(baseDir,nickname,domain,replyFile,debug)
# remove the replies file itself # remove the replies file
os.remove(repliesFilename) os.remove(repliesFilename)
# finally, remove the post itself
os.remove(postFilename) os.remove(postFilename)