When a question vote arrives then update the inbox index

merge-requests/30/head
Bob Mottram 2021-11-03 21:53:30 +00:00
parent 802cd86d38
commit 50319d153c
2 changed files with 26 additions and 22 deletions

View File

@ -2789,9 +2789,13 @@ def _inboxAfterInitial(recentPostsCache: {}, maxRecentPosts: int,
maxReplies, debug) maxReplies, debug)
# if this is a reply to a question then update the votes # if this is a reply to a question then update the votes
questionJson = questionUpdateVotes(baseDir, nickname, domain, questionJson, questionPostFilename = \
postJsonObject) questionUpdateVotes(baseDir, nickname, domain, postJsonObject)
if questionJson: if questionJson and questionPostFilename:
removePostFromCache(postJsonObject, recentPostsCache)
# add id to inbox index
inboxUpdateIndex('inbox', baseDir, handle,
questionPostFilename, debug)
# Is this a question created by this instance? # Is this a question created by this instance?
idPrefix = httpPrefix + '://' + domain idPrefix = httpPrefix + '://' + domain
if questionJson['object']['id'].startswith(idPrefix): if questionJson['object']['id'].startswith(idPrefix):

View File

@ -15,42 +15,42 @@ from utils import hasObjectDict
def questionUpdateVotes(baseDir: str, nickname: str, domain: str, def questionUpdateVotes(baseDir: str, nickname: str, domain: str,
replyJson: {}) -> {}: replyJson: {}) -> ({}, str):
""" For a given reply update the votes on a question """ For a given reply update the votes on a question
Returns the question json object if the vote totals were changed Returns the question json object if the vote totals were changed
""" """
if not hasObjectDict(replyJson): if not hasObjectDict(replyJson):
return None return None, None
if not replyJson['object'].get('inReplyTo'): if not replyJson['object'].get('inReplyTo'):
return None return None, None
if not replyJson['object']['inReplyTo']: if not replyJson['object']['inReplyTo']:
return None return None, None
if not isinstance(replyJson['object']['inReplyTo'], str): if not isinstance(replyJson['object']['inReplyTo'], str):
return None return None, None
if not replyJson['object'].get('name'): if not replyJson['object'].get('name'):
return None return None, None
inReplyTo = replyJson['object']['inReplyTo'] inReplyTo = replyJson['object']['inReplyTo']
questionPostFilename = locatePost(baseDir, nickname, domain, inReplyTo) questionPostFilename = locatePost(baseDir, nickname, domain, inReplyTo)
if not questionPostFilename: if not questionPostFilename:
return None return None, None
questionJson = loadJson(questionPostFilename) questionJson = loadJson(questionPostFilename)
if not questionJson: if not questionJson:
return None return None, None
if not hasObjectDict(questionJson): if not hasObjectDict(questionJson):
return None return None, None
if not questionJson['object'].get('type'): if not questionJson['object'].get('type'):
return None return None, None
if questionJson['type'] != 'Question': if questionJson['type'] != 'Question':
return None return None, None
if not questionJson['object'].get('oneOf'): if not questionJson['object'].get('oneOf'):
return None return None, None
if not isinstance(questionJson['object']['oneOf'], list): if not isinstance(questionJson['object']['oneOf'], list):
return None return None, None
if not questionJson['object'].get('content'): if not questionJson['object'].get('content'):
return None return None, None
replyVote = replyJson['object']['name'] replyVote = replyJson['object']['name']
# does the reply name field match any possible question option? # does the reply name field match any possible question option?
foundAnswer = None foundAnswer = None, None
for possibleAnswer in questionJson['object']['oneOf']: for possibleAnswer in questionJson['object']['oneOf']:
if not possibleAnswer.get('name'): if not possibleAnswer.get('name'):
continue continue
@ -58,7 +58,7 @@ def questionUpdateVotes(baseDir: str, nickname: str, domain: str,
foundAnswer = possibleAnswer foundAnswer = possibleAnswer
break break
if not foundAnswer: if not foundAnswer:
return None return None, None
# update the voters file # update the voters file
votersFileSeparator = ';;;' votersFileSeparator = ';;;'
votersFilename = questionPostFilename.replace('.json', '.voters') votersFilename = questionPostFilename.replace('.json', '.voters')
@ -97,7 +97,7 @@ def questionUpdateVotes(baseDir: str, nickname: str, domain: str,
for voteLine in newlines: for voteLine in newlines:
votersFile.write(voteLine) votersFile.write(voteLine)
else: else:
return None return None, None
# update the vote counts # update the vote counts
questionTotalsChanged = False questionTotalsChanged = False
for possibleAnswer in questionJson['object']['oneOf']: for possibleAnswer in questionJson['object']['oneOf']:
@ -114,10 +114,10 @@ def questionUpdateVotes(baseDir: str, nickname: str, domain: str,
possibleAnswer['replies']['totalItems'] = totalItems possibleAnswer['replies']['totalItems'] = totalItems
questionTotalsChanged = True questionTotalsChanged = True
if not questionTotalsChanged: if not questionTotalsChanged:
return None return None, None
# save the question with altered totals # save the question with altered totals
saveJson(questionJson, questionPostFilename) saveJson(questionJson, questionPostFilename)
return questionJson return questionJson, questionPostFilename
def isQuestion(postObjectJson: {}) -> bool: def isQuestion(postObjectJson: {}) -> bool: