-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix risk triggers #522
Merged
Merged
Fix risk triggers #522
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,18 @@ | ||
class RiskTrigger < ActiveRecord::Base | ||
def risky?(notice) | ||
if notice.submitter.try(:email) == 'google@lumendatabase.org' && | ||
notice.try(:type) == 'Defamation' | ||
false | ||
else | ||
begin | ||
field_present?(notice) && condition_matches?(notice) | ||
rescue NoMethodError => ex | ||
Rails.logger.warn "Invalid risk trigger (#{id}): #{ex}" | ||
false | ||
end | ||
end | ||
end | ||
ALLOWED_MATCHING_TYPES = %w[any all].freeze | ||
|
||
private | ||
has_many :risk_trigger_conditions, dependent: :destroy | ||
|
||
def field_present?(notice) | ||
notice.send(field).present? | ||
end | ||
validates_presence_of :name, :matching_type | ||
validates_inclusion_of :matching_type, in: ALLOWED_MATCHING_TYPES | ||
|
||
def risky?(notice) | ||
return false unless risk_trigger_conditions.any? | ||
|
||
def condition_matches?(notice) | ||
if negated? | ||
notice.send(condition_field) != condition_value | ||
if matching_type == 'any' | ||
risk_trigger_conditions.any? { |condition| condition.risky?(notice) } | ||
else | ||
notice.send(condition_field) == condition_value | ||
risk_trigger_conditions.all? { |condition| condition.risky?(notice) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class RiskTriggerCondition < ActiveRecord::Base | ||
ALLOWED_FIELDS = ( | ||
Notice.attribute_names.map { |field| 'notice.' + field } + | ||
Entity.attribute_names.map do |field| | ||
%w[submitter recipient principal sender].map do |entity| | ||
entity + '.' + field | ||
end | ||
end.flatten | ||
).freeze | ||
|
||
ALLOWED_MATCHING_TYPES = %w[exact broad].freeze | ||
|
||
belongs_to :risk_trigger | ||
|
||
validates_presence_of :field, :value | ||
validates_inclusion_of :field, in: ALLOWED_FIELDS | ||
validates_inclusion_of :matching_type, in: ALLOWED_MATCHING_TYPES | ||
|
||
def risky?(notice) | ||
field_present?(notice) && condition_matches?(notice) | ||
rescue NoMethodError => ex | ||
Rails.logger.warn 'Invalid risk trigger condition (trigger condition ' \ | ||
"id:#{id}, notice id: #{notice.id}): #{ex}" | ||
false | ||
end | ||
|
||
private | ||
|
||
def field_present?(notice) | ||
notice_field_value(notice).present? | ||
end | ||
|
||
def condition_matches?(notice) | ||
notice_field_value = notice_field_value(notice) | ||
|
||
if matching_type == 'exact' | ||
match_exact(notice_field_value) | ||
else | ||
match_broad(notice_field_value) | ||
end | ||
end | ||
|
||
def notice_field_value(notice) | ||
field.gsub('notice.', '').split('.').inject(notice, :send) | ||
end | ||
|
||
def match_exact(notice_field_value) | ||
if negated? | ||
notice_field_value != value | ||
else | ||
notice_field_value == value | ||
end | ||
end | ||
|
||
def match_broad(notice_field_value) | ||
if negated? | ||
!notice_field_value.include?(value) | ||
else | ||
notice_field_value.include?(value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class RenameRiskTriggers < ActiveRecord::Migration | ||
def change | ||
rename_table :risk_triggers, :risk_trigger_conditions | ||
remove_column :risk_trigger_conditions, :field | ||
rename_column :risk_trigger_conditions, :condition_field, :field | ||
rename_column :risk_trigger_conditions, :condition_value, :value | ||
add_column :risk_trigger_conditions, :type, :string | ||
add_column :risk_trigger_conditions, :matching_type, :string | ||
change_column_null :risk_trigger_conditions, :field, false | ||
change_column_null :risk_trigger_conditions, :value, false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class CreateNewRiskTriggers < ActiveRecord::Migration | ||
def change | ||
# Create a new table | ||
create_table :risk_triggers do |t| | ||
t.string :name, null: false | ||
t.string :matching_type, null: false | ||
t.string :comment | ||
t.boolean :force_not_risky_assessment, default: false, null: false | ||
end | ||
|
||
# Add a reference field to the risk_trigger_conditions | ||
add_reference :risk_trigger_conditions, :risk_trigger, index: true | ||
|
||
# Create a default risk trigger | ||
default_risk_trigger = RiskTrigger.create( | ||
name: 'Default', | ||
matching_type: 'all' | ||
) | ||
|
||
# Assign all the exisiting conditions to the default trigger | ||
RiskTriggerCondition.update_all(risk_trigger_id: default_risk_trigger) | ||
peter-hank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Create a risk trigger and its conditions from the ones that were hard-coded in the RiskTrigger model | ||
google_risk_trigger = RiskTrigger.create( | ||
name: 'Google Defamation', | ||
matching_type: 'all', | ||
force_not_risky_assessment: true | ||
) | ||
RiskTriggerCondition.create( | ||
field: 'notice.type', | ||
value: 'Defamation', | ||
negated: false, | ||
risk_trigger: google_risk_trigger, | ||
matching_type: 'exact' | ||
) | ||
RiskTriggerCondition.create( | ||
field: 'submitter.email', | ||
value: 'google@lumendatabase.org', | ||
negated: false, | ||
risk_trigger: google_risk_trigger, | ||
matching_type: 'exact' | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
RiskTrigger.create!( | ||
field: :body, | ||
condition_field: :country_code, | ||
condition_value: 'us', | ||
negated: true | ||
risk_trigger = RiskTrigger.create!( | ||
name: 'Submitter country code not US', | ||
matching_type: 'all' | ||
) | ||
|
||
RiskTriggerCondition.create!( | ||
field: 'submitter.country_code', | ||
value: 'us', | ||
negated: true, | ||
risk_trigger: risk_trigger, | ||
matching_type: 'exact' | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it correct that this
each
logic boils down totrigger.force_not_risky_assessment? && trigger.risky?(notice)
?If so, I think
high_risk?
is justThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thatandromeda no really, if any of
risk_triggers
isforce_not_risky_assessment?
and isrisky?
then the assessment is notrisky?
, it would berisky?
in your example