From 090f8eacc3e3fbd3f3739069a7de97adf65eed1f Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 14 Jul 2019 18:02:41 +0100 Subject: [PATCH] Remove attachments for deleted posts --- epicyon.py | 2 +- inbox.py | 2 +- posts.py | 10 +++++----- utils.py | 10 ++++++++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/epicyon.py b/epicyon.py index 6bdd2cd4e..e4f0fb9f1 100644 --- a/epicyon.py +++ b/epicyon.py @@ -504,7 +504,7 @@ if args.archive: else: print('Archiving to '+args.archive+'...') archiveMedia(baseDir,args.archive,archiveWeeks) - archivePosts(baseDir,args.archive,archiveMaxPosts) + archivePosts(baseDir,httpPrefix,args.archive,archiveMaxPosts) print('Archiving complete') sys.exit() diff --git a/inbox.py b/inbox.py index 238e3a8c4..1404bd088 100644 --- a/inbox.py +++ b/inbox.py @@ -536,7 +536,7 @@ def receiveDelete(session,handle: str,baseDir: str, \ print('DEBUG: delete post not found in inbox or outbox') print(messageJson['object']) 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: print('DEBUG: post deleted - '+postFilename) return True diff --git a/posts.py b/posts.py index 08ae92630..154fd9468 100644 --- a/posts.py +++ b/posts.py @@ -948,7 +948,7 @@ def createBoxBase(baseDir: str,boxname: str, \ return boxHeader 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 """ if archiveDir: @@ -972,16 +972,16 @@ def archivePosts(baseDir: str,archiveDir: str,maxPostsInBox=256) -> None: if not os.path.isdir(archiveDir+'/accounts/'+handle+'/outbox'): os.mkdir(archiveDir+'/accounts/'+handle+'/outbox') archiveSubdir=archiveDir+'/accounts/'+handle+'/inbox' - archivePostsForPerson(nickname,domain,baseDir, \ + archivePostsForPerson(httpPrefix,nickname,domain,baseDir, \ 'inbox',archiveSubdir, \ maxPostsInBox) if archiveDir: archiveSubdir=archiveDir+'/accounts/'+handle+'/outbox' - archivePostsForPerson(nickname,domain,baseDir, \ + archivePostsForPerson(httpPrefix,nickname,domain,baseDir, \ 'outbox',archiveSubdir, \ 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: """Retain a maximum number of posts within the given box Move any others to an archive directory @@ -1007,7 +1007,7 @@ def archivePostsForPerson(nickname: str,domain: str,baseDir: str, \ if os.path.isfile(repliesPath): os.rename(repliesPath,archivePath) else: - deletePost(baseDir,nickname,domain,filePath,False) + deletePost(baseDir,httpPrefix,nickname,domain,filePath,False) noOfPosts -= 1 if noOfPosts <= maxPostsInBox: break diff --git a/utils.py b/utils.py index ecc630279..e2c2ae9fd 100644 --- a/utils.py +++ b/utils.py @@ -8,6 +8,7 @@ __status__ = "Production" import os import datetime +import commentjson def getStatusNumber() -> (str,str): """Returns the status number and published date @@ -158,11 +159,15 @@ def removeAttachment(baseDir: str,httpPrefix: str,domain: str,postJson: {}): os.remove(mediaFilename) 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 """ + # remove any attachment with open(postFilename, 'r') as fp: postJsonObject=commentjson.load(fp) + removeAttachment(baseDir,httpPrefix,domain,postJsonObject) + + # remove any replies repliesFilename=postFilename.replace('.json','.replies') if os.path.isfile(repliesFilename): if debug: @@ -173,6 +178,7 @@ def deletePost(baseDir: str,nickname: str,domain: str,postFilename: str,debug: b if replyFile: if os.path.isfile(replyFile): deletePost(baseDir,nickname,domain,replyFile,debug) - # remove the replies file itself + # remove the replies file os.remove(repliesFilename) + # finally, remove the post itself os.remove(postFilename)