Receiving question updates

main
Bob Mottram 2019-11-26 10:43:37 +00:00
parent 4bea7f3fd2
commit fe3fb78801
2 changed files with 46 additions and 11 deletions

View File

@ -739,6 +739,42 @@ def personReceiveUpdate(baseDir: str, \
os.remove(avatarFilename)
return True
def receiveUpdateToQuestion(recentPostsCache: {},messageJson: {}, \
baseDir: str,nickname: str,domain: str) -> None:
"""Updating a question as new votes arrive
"""
# message url of the question
if not messageJson.get('id'):
return
if not messageJson.get('actor'):
return
messageId=messageJson['id'].replace('/activity','')
if '#' in messageId:
messageId=messageId.split('#',1)[0]
# find the question post
postFilename=locatePost(baseDir,nickname,domain,messageId)
if not postFilename:
return
# load the json for the question
postJsonObject=loadJson(postFilename,1)
if not postJsonObject:
return
if not postJsonObject.get('actor'):
return
# does the actor match?
if postJsonObject['actor']!=messageJson['actor']:
return
saveJson(messageJson,postFilename)
# ensure that the cached post is removed if it exists, so
# that it then will be recreated
cachedPostFilename= \
getCachedPostFilename(baseDir,nickname,domain,messageJson)
if cachedPostFilename:
if os.path.isfile(cachedPostFilename):
os.remove(cachedPostFilename)
# remove from memory cache
removePostFromCache(messageJson,recentPostsCache)
def receiveUpdate(recentPostsCache: {},session,baseDir: str, \
httpPrefix: str,domain :str,port: int, \
sendThreads: [],postLog: [],cachedWebfingers: {}, \
@ -772,15 +808,8 @@ def receiveUpdate(recentPostsCache: {},session,baseDir: str, \
return False
if messageJson['object']['type']=='Question':
# ensure that the cached post is removed if it exists, so
# that it then will be recreated
cachedPostFilename= \
getCachedPostFilename(baseDir,nickname,domain,messageJson)
if cachedPostFilename:
if os.path.isfile(cachedPostFilename):
os.remove(cachedPostFilename)
# remove from memory cache
removePostFromCache(messageJson,recentPostsCache)
receiveUpdateToQuestion(recentPostsCache,messageJson, \
baseDir,nickname,domain)
if debug:
print('DEBUG: Question update was received')
return True

View File

@ -464,7 +464,10 @@ def removePostFromCache(postJsonObject: {},recentPostsCache: {}):
if not recentPostsCache.get('index'):
return
postId=postJsonObject['id'].replace('/activity','').replace('/','#')
postId=postJsonObject['id']
if '#' in postId:
postId=postId.split('#',1)[0]
postId=postId.replace('/activity','').replace('/','#')
if postId not in recentPostsCache['index']:
return
@ -480,7 +483,10 @@ def updateRecentPostsCache(recentPostsCache: {},maxRecentPosts: int, \
"""
if not postJsonObject.get('id'):
return
postId=postJsonObject['id'].replace('/activity','').replace('/','#')
postId=postJsonObject['id']
if '#' in postId:
postId=postId.split('#',1)[0]
postId=postId.replace('/activity','').replace('/','#')
if recentPostsCache.get('index'):
if postId in recentPostsCache['index']:
return