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):
# list of indexes to be updated
updateIndexList=['inbox']
populateReplies(baseDir,httpPrefix,domain,messageJson,maxReplies,debug)
questionUpdateVotes(baseDir,nickname,domain,messageJson)
populateReplies(baseDir,httpPrefix,domain,postJsonObject,maxReplies,debug)
questionUpdateVotes(baseDir,nickname,domain,postJsonObject)
if not isGroup:
# create a DM notification file if needed
if isDM(postJsonObject):

View File

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