Skip to content

Commit

Permalink
Refactor test chat commands and !!/report check.
Browse files Browse the repository at this point in the history
  • Loading branch information
teward committed Mar 14, 2017
1 parent 247683f commit adaa37f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
44 changes: 24 additions & 20 deletions chatcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import datahandling
import regex
from helpers import Response
from classes import Post

# TODO: pull out code block to get user_id, chat_site, room_id into function
# TODO: Return result for all functions should be similar (tuple/named tuple?)
Expand Down Expand Up @@ -784,7 +785,10 @@ def command_test(content, content_lower, *args, **kwargs):
if len(string_to_test) == 0:
return Response(command_status=True, message="Nothing to test")
result = "> "
reasons, why = FindSpam.test_post(string_to_test, string_to_test, string_to_test, "", test_as_answer, False, 1, 0)
# def test_post(title, body, user_name, site, is_answer, body_is_summary, user_rep, post_score):
fakepost = {'title': string_to_test, 'body': string_to_test,
'owner': {'display_name': string_to_test, 'reputation': 1}, 'site': "", 'IsAnswer': False, 'score': 0}
reasons, why = FindSpam.test_post(fakepost)

This comment has been minimized.

Copy link
@Undo1

Undo1 Mar 14, 2017

Member

This approach works for now, but in the future it'd probably be good to use the class.

This comment has been minimized.

Copy link
@teward

teward Mar 14, 2017

Author Member

@undo uhm... we do use the class... at least now we do. I forgot to wrap that fake post in a Post() constructor. It works now. That said, tests are failing for some things so maybe we need to look into it a bit more for debugging the failing tests.

if len(reasons) == 0:
result += "Would not be caught for title, body, and username."
return Response(command_status=True, message=result)
Expand All @@ -809,7 +813,10 @@ def command_test_answer(content, content_lower, *args, **kwargs):
if len(string_to_test) == 0:
return Response(command_status=True, message="Nothing to test")
result = "> "
reasons, why = FindSpam.test_post("Valid title", string_to_test, "Valid username", "", test_as_answer, False, 1, 0)
# def test_post(title, body, user_name, site, is_answer, body_is_summary, user_rep, post_score):
fakepost = {'title': 'Valid title', 'body': string_to_test,
'owner': {'display_name': "Valid username", 'reputation': 1}, 'site': "", 'IsAnswer': True, 'score': 0}
reasons, why = FindSpam.test_post(fakepost)
if len(reasons) == 0:
result += "Would not be caught as an answer."
return Response(command_status=True, message=result)
Expand All @@ -834,7 +841,10 @@ def command_test_question(content, content_lower, *args, **kwargs):
if len(string_to_test) == 0:
return Response(command_status=True, message="Nothing to test")
result = "> "
reasons, why = FindSpam.test_post("Valid title", string_to_test, "Valid username", "", test_as_answer, False, 1, 0)
fakepost = {'title': 'Valid title', 'body': string_to_test,
'owner': {'display_name': "Valid username", 'reputation': 1},
'site': "", 'IsAnswer': False, 'score': 0}
reasons, why = FindSpam.test_post(fakepost)
if len(reasons) == 0:
result += "Would not be caught as a question."
return Response(command_status=True, message=result)
Expand All @@ -859,8 +869,10 @@ def command_test_title(content, content_lower, *args, **kwargs):
if len(string_to_test) == 0:
return Response(command_status=True, message="Nothing to test")
result = "> "
reasons, why = FindSpam.test_post(string_to_test, "Valid question body", "Valid username", "",
test_as_answer, False, 1, 0)
fakepost = {'title': string_to_test, 'body': "Valid question body",
'owner': {'display_name': "Valid username", 'reputation': 1},
'site': "", 'IsAnswer': False, 'score': 0}
reasons, why = FindSpam.test_post(fakepost)
if len(reasons) == 0:
result += "Would not be caught as a title."
return Response(command_status=True, message=result)
Expand All @@ -885,8 +897,9 @@ def command_test_username(content, content_lower, *args, **kwargs):
if len(string_to_test) == 0:
return Response(command_status=True, message="Nothing to test")
result = "> "
reasons, why = FindSpam.test_post("Valid title", "Valid post body", string_to_test, "",
test_as_answer, False, 1, 0)
fakepost = {'title': 'Valid title', 'body': "Valid post body",
'owner': {'display_name': string_to_test, 'reputation': 1}, 'site': "", 'IsAnswer': False, 'score': 0}
reasons, why = FindSpam.test_post(fakepost)
if len(reasons) == 0:
result += "Would not be caught as a username."
return Response(command_status=True, message=result)
Expand Down Expand Up @@ -1131,6 +1144,8 @@ def command_report_post(ev_room, ev_user_id, wrap2, message_parts, message_url,
# this re-report might be attempting to correct that/fix a mistake/etc.
output.append("Post {}: Already recently reported".format(index))
continue
post_data["IsAnswer"] = post_data.post_type == "answer"
post = Post(api_response=post_data)
user = get_user_from_url(post_data.owner_url)
if user is not None:
add_blacklisted_user(user, message_url, post_data.post_url)
Expand All @@ -1139,21 +1154,10 @@ def command_report_post(ev_room, ev_user_id, wrap2, message_parts, message_url,
batch = ""
if len(urls) > 1:
batch = " (batch report: post {} out of {})".format(index, len(urls))
handle_spam(title=post_data.title,
body=post_data.body,
poster=post_data.owner_name,
handle_spam(post=post,
site=post_data.site,
post_url=post_data.post_url,
poster_url=post_data.owner_url,
post_id=post_data.post_id,
reasons=["Manually reported " + post_data.post_type + batch],
is_answer=post_data.post_type == "answer",
why=why,
owner_rep=post_data.owner_rep,
post_score=post_data.score,
up_vote_count=post_data.up_vote_count,
down_vote_count=post_data.down_vote_count,
question_id=post_data.question_id)
why=why)
if 1 < len(urls) > len(output):
add_or_update_multiple_reporter(ev_user_id, wrap2.host, time.time())
if len(output) > 0:
Expand Down
1 change: 1 addition & 0 deletions classes/Post.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def _parse_api_post(self, response):

self._title = GlobalVars.parser.unescape(response["title"])
self._body = GlobalVars.parser.unescape(response["body"])
self._post_site = response['site']
self._post_url = response["link"]
self._post_score = response["score"]
self._votes['upvotes'] = response["up_vote_count"]
Expand Down
1 change: 0 additions & 1 deletion findspam.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,6 @@ class FindSpam:
]

@staticmethod
# def test_post(title, body, user_name, site, is_answer, body_is_summary, user_rep, post_score):
def test_post(post):
result = []
why = {'title': [], 'body': [], 'username': []}
Expand Down

0 comments on commit adaa37f

Please sign in to comment.