When deleting a post also remove it from the recent posts cache in memory

main
Bob Mottram 2020-06-24 14:30:50 +01:00
parent f5be8eb132
commit 0e3c41db2b
8 changed files with 38 additions and 17 deletions

View File

@ -6671,7 +6671,8 @@ class PubServer(BaseHTTPRequestHandler):
self.server.httpPrefix, self.server.httpPrefix,
nickname, self.server.domain, nickname, self.server.domain,
postFilename, postFilename,
self.server.debug) self.server.debug,
self.server.recentPostsCache)
if callingDomain.endswith('.onion') and \ if callingDomain.endswith('.onion') and \
self.server.onionDomain: self.server.onionDomain:
actorStr = \ actorStr = \

View File

@ -227,7 +227,8 @@ def deletePostPub(session, baseDir: str, federationList: [],
def outboxDelete(baseDir: str, httpPrefix: str, def outboxDelete(baseDir: str, httpPrefix: str,
nickname: str, domain: str, nickname: str, domain: str,
messageJson: {}, debug: bool, messageJson: {}, debug: bool,
allowDeletion: bool) -> None: allowDeletion: bool,
recentPostsCache: {}) -> None:
""" When a delete request is received by the outbox from c2s """ When a delete request is received by the outbox from c2s
""" """
if not messageJson.get('type'): if not messageJson.get('type'):
@ -289,6 +290,6 @@ def outboxDelete(baseDir: str, httpPrefix: str,
print(messageId) print(messageId)
return True return True
deletePost(baseDir, httpPrefix, deleteNickname, deleteDomain, deletePost(baseDir, httpPrefix, deleteNickname, deleteDomain,
postFilename, debug) postFilename, debug, recentPostsCache)
if debug: if debug:
print('DEBUG: post deleted via c2s - ' + postFilename) print('DEBUG: post deleted via c2s - ' + postFilename)

View File

@ -1313,7 +1313,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, httpPrefix, args.archive, archiveMaxPosts) archivePosts(baseDir, httpPrefix, args.archive, {}, archiveMaxPosts)
print('Archiving complete') print('Archiving complete')
sys.exit() sys.exit()

View File

@ -1240,7 +1240,8 @@ def receiveDelete(session, handle: str, isGroup: bool, baseDir: str,
httpPrefix: str, domain: str, port: int, httpPrefix: str, domain: str, port: int,
sendThreads: [], postLog: [], cachedWebfingers: {}, sendThreads: [], postLog: [], cachedWebfingers: {},
personCache: {}, messageJson: {}, federationList: [], personCache: {}, messageJson: {}, federationList: [],
debug: bool, allowDeletion: bool) -> bool: debug: bool, allowDeletion: bool,
recentPostsCache: {}) -> bool:
"""Receives a Delete activity within the POST section of HTTPServer """Receives a Delete activity within the POST section of HTTPServer
""" """
if messageJson['type'] != 'Delete': if messageJson['type'] != 'Delete':
@ -1305,7 +1306,8 @@ def receiveDelete(session, handle: str, isGroup: bool, baseDir: str,
print(messageId) print(messageId)
return True return True
deletePost(baseDir, httpPrefix, handle.split('@')[0], deletePost(baseDir, httpPrefix, handle.split('@')[0],
handle.split('@')[1], postFilename, debug) handle.split('@')[1], postFilename, debug,
recentPostsCache)
if debug: if debug:
print('DEBUG: post deleted - ' + postFilename) print('DEBUG: post deleted - ' + postFilename)
return True return True
@ -2045,7 +2047,8 @@ def inboxAfterCapabilities(recentPostsCache: {}, maxRecentPosts: int,
personCache, personCache,
messageJson, messageJson,
federationList, federationList,
debug, allowDeletion): debug, allowDeletion,
recentPostsCache):
if debug: if debug:
print('DEBUG: Delete accepted from ' + actor) print('DEBUG: Delete accepted from ' + actor)
return False return False

View File

@ -299,7 +299,8 @@ def postMessageToOutbox(messageJson: {}, postToNickname: str,
outboxDelete(baseDir, httpPrefix, outboxDelete(baseDir, httpPrefix,
postToNickname, domain, postToNickname, domain,
messageJson, debug, messageJson, debug,
allowDeletion) allowDeletion,
recentPostsCache)
if debug: if debug:
print('DEBUG: handle block requests') print('DEBUG: handle block requests')

View File

@ -2718,17 +2718,21 @@ def createBoxIndexed(recentPostsCache: {},
def expireCache(baseDir: str, personCache: {}, def expireCache(baseDir: str, personCache: {},
httpPrefix: str, archiveDir: str, maxPostsInBox=32000): httpPrefix: str, archiveDir: str,
recentPostsCache: {},
maxPostsInBox=32000):
"""Thread used to expire actors from the cache and archive old posts """Thread used to expire actors from the cache and archive old posts
""" """
while True: while True:
# once per day # once per day
time.sleep(60 * 60 * 24) time.sleep(60 * 60 * 24)
expirePersonCache(baseDir, personCache) expirePersonCache(baseDir, personCache)
archivePosts(baseDir, httpPrefix, archiveDir, maxPostsInBox) archivePosts(baseDir, httpPrefix, archiveDir, recentPostsCache,
maxPostsInBox)
def archivePosts(baseDir: str, httpPrefix: str, archiveDir: str, def archivePosts(baseDir: str, httpPrefix: str, archiveDir: str,
recentPostsCache: {},
maxPostsInBox=32000) -> None: maxPostsInBox=32000) -> None:
"""Archives posts for all accounts """Archives posts for all accounts
""" """
@ -2760,18 +2764,19 @@ def archivePosts(baseDir: str, httpPrefix: str, archiveDir: str,
handle + '/inbox' handle + '/inbox'
archivePostsForPerson(httpPrefix, nickname, domain, baseDir, archivePostsForPerson(httpPrefix, nickname, domain, baseDir,
'inbox', archiveSubdir, 'inbox', archiveSubdir,
maxPostsInBox) recentPostsCache, maxPostsInBox)
if archiveDir: if archiveDir:
archiveSubdir = archiveDir + '/accounts/' + \ archiveSubdir = archiveDir + '/accounts/' + \
handle + '/outbox' handle + '/outbox'
archivePostsForPerson(httpPrefix, nickname, domain, baseDir, archivePostsForPerson(httpPrefix, nickname, domain, baseDir,
'outbox', archiveSubdir, 'outbox', archiveSubdir,
maxPostsInBox) recentPostsCache, maxPostsInBox)
def archivePostsForPerson(httpPrefix: str, nickname: str, domain: str, def archivePostsForPerson(httpPrefix: str, nickname: str, domain: str,
baseDir: str, baseDir: str,
boxname: str, archiveDir: str, boxname: str, archiveDir: str,
recentPostsCache: {},
maxPostsInBox=32000) -> None: maxPostsInBox=32000) -> 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
@ -2855,7 +2860,8 @@ def archivePostsForPerson(httpPrefix: str, nickname: str, domain: str,
if os.path.isfile(repliesPath): if os.path.isfile(repliesPath):
os.rename(repliesPath, archivePath) os.rename(repliesPath, archivePath)
else: else:
deletePost(baseDir, httpPrefix, nickname, domain, filePath, False) deletePost(baseDir, httpPrefix, nickname, domain,
filePath, False, recentPostsCache)
# remove cached html posts # remove cached html posts
postCacheFilename = \ postCacheFilename = \

View File

@ -1076,8 +1076,8 @@ def testCreatePerson():
deleteAllPosts(baseDir, nickname, domain, 'outbox') deleteAllPosts(baseDir, nickname, domain, 'outbox')
setDisplayNickname(baseDir, nickname, domain, 'badger') setDisplayNickname(baseDir, nickname, domain, 'badger')
setBio(baseDir, nickname, domain, 'Randomly roaming in your backyard') setBio(baseDir, nickname, domain, 'Randomly roaming in your backyard')
archivePostsForPerson(nickname, domain, baseDir, 'inbox', None, 4) archivePostsForPerson(nickname, domain, baseDir, 'inbox', None, {}, 4)
archivePostsForPerson(nickname, domain, baseDir, 'outbox', None, 4) archivePostsForPerson(nickname, domain, baseDir, 'outbox', None, {}, 4)
createPublicPost(baseDir, nickname, domain, port, httpPrefix, createPublicPost(baseDir, nickname, domain, port, httpPrefix,
"G'day world!", False, True, clientToServer, "G'day world!", False, True, clientToServer,
None, None, useBlurhash, None, None, None, None, useBlurhash, None, None,

View File

@ -444,7 +444,7 @@ def isReplyToBlogPost(baseDir: str, nickname: str, domain: str,
def deletePost(baseDir: str, httpPrefix: str, def deletePost(baseDir: str, httpPrefix: str,
nickname: str, domain: str, postFilename: str, nickname: str, domain: str, postFilename: str,
debug: bool) -> None: debug: bool, recentPostsCache: {}) -> None:
"""Recursively deletes a post and its replies and attachments """Recursively deletes a post and its replies and attachments
""" """
postJsonObject = loadJson(postFilename, 1) postJsonObject = loadJson(postFilename, 1)
@ -463,6 +463,14 @@ def deletePost(baseDir: str, httpPrefix: str,
postJsonObject): postJsonObject):
return return
# remove from recent posts cache in memory
postId = \
postJsonObject['id'].replace('/activity', '').replace('/', '#')
if postId in recentPostsCache['index']:
recentPostsCache['index'].remove(postId)
if recentPostsCache['json'].get(postId):
del recentPostsCache['json'][postId]
# remove any attachment # remove any attachment
removeAttachment(baseDir, httpPrefix, domain, postJsonObject) removeAttachment(baseDir, httpPrefix, domain, postJsonObject)
@ -543,7 +551,8 @@ def deletePost(baseDir: str, httpPrefix: str,
if replyFile: if replyFile:
if os.path.isfile(replyFile): if os.path.isfile(replyFile):
deletePost(baseDir, httpPrefix, deletePost(baseDir, httpPrefix,
nickname, domain, replyFile, debug) nickname, domain, replyFile, debug,
recentPostsCache)
# remove the replies file # remove the replies file
os.remove(repliesFilename) os.remove(repliesFilename)
# finally, remove the post itself # finally, remove the post itself