Update question votes

main
Bob Mottram 2019-11-29 19:22:11 +00:00
parent 82d16d5465
commit 57a7194d5e
2 changed files with 42 additions and 26 deletions

View File

@ -1795,8 +1795,10 @@ def inboxAfterCapabilities(recentPostsCache: {},maxRecentPosts: int, \
if validPostContent(postJsonObject,maxMentions,maxEmoji): if validPostContent(postJsonObject,maxMentions,maxEmoji):
# list of indexes to be updated # list of indexes to be updated
updateIndexList=['inbox'] updateIndexList=['inbox']
populateReplies(baseDir,httpPrefix,domain,messageJson,maxReplies,debug) populateReplies(baseDir,httpPrefix,domain,postJsonObject,maxReplies,debug)
questionUpdateVotes(baseDir,nickname,domain,messageJson)
questionUpdateVotes(baseDir,nickname,domain,postJsonObject)
if not isGroup: if not isGroup:
# create a DM notification file if needed # create a DM notification file if needed
if isDM(postJsonObject): if isDM(postJsonObject):

View File

@ -11,40 +11,41 @@ from utils import locatePost
from utils import loadJson from utils import loadJson
from utils import saveJson from utils import saveJson
def questionUpdateVotes(baseDir: str,nickname: str,domain: str,replyJson: {}): def questionUpdateVotes(baseDir: str,nickname: str,domain: str,replyJson: {}) -> {}:
""" 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
""" """
if not replyJson.get('object'): if not replyJson.get('object'):
return return None
if not isinstance(replyJson['object'], dict): if not isinstance(replyJson['object'], dict):
return return None
if not replyJson['object'].get('inReplyTo'): if not replyJson['object'].get('inReplyTo'):
return return None
if not replyJson['object']['inReplyTo']: if not replyJson['object']['inReplyTo']:
return return None
if not replyJson['object'].get('content'): if not replyJson['object'].get('content'):
return return 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 return None
questionJson=loadJson(questionPostFilename) questionJson=loadJson(questionPostFilename)
if not questionJson: if not questionJson:
return return None
if not questionJson.get('object'): if not questionJson.get('object'):
return return None
if not isinstance(questionJson['object'], dict): if not isinstance(questionJson['object'], dict):
return return None
if not questionJson['object'].get('type'): if not questionJson['object'].get('type'):
return return None
if questionJson['type']!='Question': if questionJson['type']!='Question':
return return None
if not questionJson['object'].get('oneOf'): if not questionJson['object'].get('oneOf'):
return return None
if not isinstance(questionJson['object']['oneOf'], list): if not isinstance(questionJson['object']['oneOf'], list):
return return None
if not questionJson['object'].get('content'): if not questionJson['object'].get('content'):
return return None
content=replyJson['object']['content'] content=replyJson['object']['content']
# does the reply content match any possible question option? # does the reply content match any possible question option?
foundAnswer=None foundAnswer=None
@ -55,7 +56,7 @@ def questionUpdateVotes(baseDir: str,nickname: str,domain: str,replyJson: {}):
foundAnswer=possibleAnswer foundAnswer=possibleAnswer
break break
if not foundAnswer: if not foundAnswer:
return return None
# update the voters file # update the voters file
votersFileSeparator=';;;' votersFileSeparator=';;;'
votersFilename=questionPostFilename.replace('.json','.voters') votersFilename=questionPostFilename.replace('.json','.voters')
@ -76,12 +77,23 @@ def questionUpdateVotes(baseDir: str,nickname: str,domain: str,replyJson: {}):
# change an entry in the voters file # change an entry in the voters file
with open(votersFilename, "r") as votersFile: with open(votersFilename, "r") as votersFile:
lines = votersFile.readlines() lines = votersFile.readlines()
with open(votersFilename, "w") as votersFile: newlines=[]
saveVotersFile=False
for voteLine in lines: for voteLine in lines:
if voteLine.startswith(replyJson['actor']+votersFileSeparator): if voteLine.startswith(replyJson['actor']+votersFileSeparator):
votersFile.write(replyJson['actor']+votersFileSeparator+foundAnswer+'\n') newVoteLine=replyJson['actor']+votersFileSeparator+foundAnswer+'\n'
if voteLine==newVoteLine:
break break
saveVotersFile=True
newlines.append(newVoteLine)
else:
newlines.append(voteLine)
if saveVotersFile:
with open(votersFilename, "w") as votersFile:
for voteLine in newlines:
votersFile.write(voteLine) votersFile.write(voteLine)
else:
return 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']:
@ -96,6 +108,8 @@ def questionUpdateVotes(baseDir: str,nickname: str,domain: str,replyJson: {}):
if possibleAnswer['replies']['totalItems']!=totalItems: if possibleAnswer['replies']['totalItems']!=totalItems:
possibleAnswer['replies']['totalItems']=totalItems possibleAnswer['replies']['totalItems']=totalItems
questionTotalsChanged=True questionTotalsChanged=True
if questionTotalsChanged: if not questionTotalsChanged:
return None
# save the question with altered totals # save the question with altered totals
saveJson(questionJson,questionPostFilename) saveJson(questionJson,questionPostFilename)
return questionJson