Update announce posts in cache

main
Bob Mottram 2019-11-24 13:46:28 +00:00
parent 0aedab61fb
commit f6f35d69d6
4 changed files with 32 additions and 12 deletions

View File

@ -10,6 +10,7 @@ import os
import time import time
import json import json
from pprint import pprint from pprint import pprint
from utils import removePostFromCache
from utils import getStatusNumber from utils import getStatusNumber
from utils import createOutboxDir from utils import createOutboxDir
from utils import urlPermitted from utils import urlPermitted
@ -25,7 +26,8 @@ from session import postJson
from webfinger import webfingerHandle from webfinger import webfingerHandle
from auth import createBasicAuthHeader from auth import createBasicAuthHeader
def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool: def outboxAnnounce(recentPostsCache: {}, \
baseDir: str,messageJson: {},debug: bool) -> bool:
""" Adds or removes announce entries from the shares collection """ Adds or removes announce entries from the shares collection
within a given post within a given post
""" """
@ -45,7 +47,7 @@ def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool:
domain,port=getDomainFromActor(messageJson['actor']) domain,port=getDomainFromActor(messageJson['actor'])
postFilename=locatePost(baseDir,nickname,domain,messageJson['object']) postFilename=locatePost(baseDir,nickname,domain,messageJson['object'])
if postFilename: if postFilename:
updateAnnounceCollection(baseDir,postFilename, \ updateAnnounceCollection(recentPostsCache,baseDir,postFilename, \
messageJson['actor'],domain,debug) messageJson['actor'],domain,debug)
return True return True
if messageJson['type']=='Undo': if messageJson['type']=='Undo':
@ -65,13 +67,15 @@ def outboxAnnounce(baseDir: str,messageJson: {},debug: bool) -> bool:
locatePost(baseDir,nickname,domain, \ locatePost(baseDir,nickname,domain, \
messageJson['object']['object']) messageJson['object']['object'])
if postFilename: if postFilename:
undoAnnounceCollectionEntry(baseDir,postFilename, \ undoAnnounceCollectionEntry(recentPostsCache, \
baseDir,postFilename, \
messageJson['actor'], \ messageJson['actor'], \
domain,debug) domain,debug)
return True return True
return False return False
def undoAnnounceCollectionEntry(baseDir: str,postFilename: str, \ def undoAnnounceCollectionEntry(recentPostsCache: {}, \
baseDir: str,postFilename: str, \
actor: str,domain: str,debug: bool) -> None: actor: str,domain: str,debug: bool) -> None:
"""Undoes an announce for a particular actor by removing it from """Undoes an announce for a particular actor by removing it from
the "shares" collection within a post. Note that the "shares" the "shares" collection within a post. Note that the "shares"
@ -86,6 +90,7 @@ def undoAnnounceCollectionEntry(baseDir: str,postFilename: str, \
getCachedPostFilename(baseDir,nickname,domain,postJsonObject) getCachedPostFilename(baseDir,nickname,domain,postJsonObject)
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
os.remove(cachedPostFilename) os.remove(cachedPostFilename)
removePostFromCache(postJsonObject,recentPostsCache)
if not postJsonObject.get('type'): if not postJsonObject.get('type'):
return return
@ -140,6 +145,7 @@ def updateAnnounceCollection(baseDir: str,postFilename: str, \
getCachedPostFilename(baseDir,nickname,domain,postJsonObject) getCachedPostFilename(baseDir,nickname,domain,postJsonObject)
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
os.remove(cachedPostFilename) os.remove(cachedPostFilename)
removePostFromCache(postJsonObject,recentPostsCache)
if not postJsonObject.get('object'): if not postJsonObject.get('object'):
if debug: if debug:

View File

@ -601,7 +601,8 @@ class PubServer(BaseHTTPRequestHandler):
inboxUpdateIndex('outbox',self.server.baseDir, \ inboxUpdateIndex('outbox',self.server.baseDir, \
self.postToNickname+'@'+self.server.domain, \ self.postToNickname+'@'+self.server.domain, \
savedFilename,self.server.debug) savedFilename,self.server.debug)
if outboxAnnounce(self.server.baseDir,messageJson,self.server.debug): if outboxAnnounce(self.server.recentPostsCache, \
self.server.baseDir,messageJson,self.server.debug):
if self.server.debug: if self.server.debug:
print('DEBUG: Updated announcements (shares) collection for the post associated with the Announce activity') print('DEBUG: Updated announcements (shares) collection for the post associated with the Announce activity')
if not self.server.session: if not self.server.session:

10
like.py
View File

@ -10,6 +10,7 @@ import os
import json import json
import time import time
from pprint import pprint from pprint import pprint
from utils import removePostFromCache
from utils import urlPermitted from utils import urlPermitted
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
@ -35,6 +36,7 @@ def undoLikesCollectionEntry(baseDir: str,postFilename: str,objectUrl: str, \
getCachedPostFilename(baseDir,nickname,domain,postJsonObject) getCachedPostFilename(baseDir,nickname,domain,postJsonObject)
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
os.remove(cachedPostFilename) os.remove(cachedPostFilename)
removePostFromCache(postJsonObject,recentPostsCache)
if not postJsonObject.get('type'): if not postJsonObject.get('type'):
return return
@ -116,13 +118,7 @@ def updateLikesCollection(recentPostsCache: {}, \
getCachedPostFilename(baseDir,nickname,domain,postJsonObject) getCachedPostFilename(baseDir,nickname,domain,postJsonObject)
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
os.remove(cachedPostFilename) os.remove(cachedPostFilename)
# if the post exists in the recent posts cache then remove it removePostFromCache(postJsonObject,recentPostsCache)
if postJsonObject.get('id') and recentPostsCache.get('index'):
postId=postJsonObject['id'].replace('/activity','').replace('/','#')
if postId in recentPostsCache['index']:
del recentPostsCache['json'][postId]
del recentPostsCache['html'][postId]
recentPostsCache['index'].remove(postId)
if not postJsonObject.get('object'): if not postJsonObject.get('object'):
if debug: if debug:

View File

@ -451,3 +451,20 @@ def getCachedPostFilename(baseDir: str,nickname: str,domain: str, \
getCachedPostDirectory(baseDir,nickname,domain)+ \ getCachedPostDirectory(baseDir,nickname,domain)+ \
'/'+postJsonObject['id'].replace('/activity','').replace('/','#')+'.html' '/'+postJsonObject['id'].replace('/activity','').replace('/','#')+'.html'
return cachedPostFilename return cachedPostFilename
def removePostFromCache(postJsonObject: {},recentPostsCache: {}):
""" if the post exists in the recent posts cache then remove it
"""
if not postJsonObject.get('id'):
return
if not recentPostsCache.get('index'):
return
postId=postJsonObject['id'].replace('/activity','').replace('/','#')
if postId not in recentPostsCache['index']:
return
del recentPostsCache['json'][postId]
del recentPostsCache['html'][postId]
recentPostsCache['index'].remove(postId)