Add debug to vote checking

merge-requests/30/head
Bob Mottram 2023-01-12 11:58:41 +00:00
parent 818f54f472
commit 7d6451ae09
2 changed files with 32 additions and 6 deletions

View File

@ -3823,7 +3823,7 @@ def _is_valid_dm(base_dir: str, nickname: str, domain: str, port: int,
if not sending_to_self:
# is this a vote on a question?
if is_vote(base_dir, nickname, domain,
post_json_object):
post_json_object, debug):
# make the content the same as the vote answer
post_json_object['object']['content'] = \
post_json_object['object']['name']
@ -3908,7 +3908,8 @@ def _receive_question_vote(server, base_dir: str, nickname: str, domain: str,
"""
# if this is a reply to a question then update the votes
question_json, question_post_filename = \
question_update_votes(base_dir, nickname, domain, post_json_object)
question_update_votes(base_dir, nickname, domain,
post_json_object, debug)
if not question_json:
return
if not question_post_filename:
@ -4442,7 +4443,7 @@ def _inbox_after_initial(server, inbox_start_time,
post_json_object = message_json
nickname = handle.split('@')[0]
if is_vote(base_dir, nickname, domain, post_json_object):
if is_vote(base_dir, nickname, domain, post_json_object, debug):
_receive_question_vote(server, base_dir, nickname, domain,
http_prefix, handle, debug,
post_json_object, recent_posts_cache,

View File

@ -16,7 +16,7 @@ from utils import text_in_file
def is_vote(base_dir: str, nickname: str, domain: str,
post_json_object: {}) -> bool:
post_json_object: {}, debug: bool) -> bool:
""" is the given post a vote on a Question?
"""
post_obj = post_json_object
@ -30,26 +30,48 @@ def is_vote(base_dir: str, nickname: str, domain: str,
if not post_obj.get('name'):
return False
if debug:
print('VOTE: ' + str(post_obj))
# is the replied to post a Question?
in_reply_to = post_obj['inReplyTo']
question_post_filename = \
locate_post(base_dir, nickname, domain, in_reply_to)
if not question_post_filename:
if debug:
print('VOTE REJECT: question does not exist ' + in_reply_to)
return False
question_json = load_json(question_post_filename)
if not question_json:
if debug:
print('VOTE REJECT: invalid json ' + question_post_filename)
return False
if not has_object_dict(question_json):
if debug:
print('VOTE REJECT: question without object ' +
question_post_filename)
return False
if not question_json['object'].get('type'):
if debug:
print('VOTE REJECT: question without type ' +
question_post_filename)
return False
if question_json['type'] != 'Question':
if debug:
print('VOTE REJECT: not a question ' +
question_post_filename)
return False
# does the question have options?
if not question_json['object'].get('oneOf'):
if debug:
print('VOTE REJECT: question has no options ' +
question_post_filename)
return False
if not isinstance(question_json['object']['oneOf'], list):
if debug:
print('VOTE REJECT: question options is not a list ' +
question_post_filename)
return False
# does the reply name field match any possible question option?
@ -62,16 +84,19 @@ def is_vote(base_dir: str, nickname: str, domain: str,
found_answer_json = possible_answer
break
if not found_answer_json:
if debug:
print('VOTE REJECT: question answer not found ' +
question_post_filename + ' ' + reply_vote)
return False
return True
def question_update_votes(base_dir: str, nickname: str, domain: str,
reply_json: {}) -> ({}, str):
reply_json: {}, debug: bool) -> ({}, str):
""" For a given reply update the votes on a question
Returns the question json object if the vote totals were changed
"""
if not is_vote(base_dir, nickname, domain, reply_json):
if not is_vote(base_dir, nickname, domain, reply_json, debug):
return None, None
post_obj = reply_json