-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
369 additions
and
61 deletions.
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
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,69 @@ | ||
class RiskTriggerCondition < ActiveRecord::Base | ||
ALLOWED_FIELDS = %w[ | ||
title subject source tag_list language jurisdiction_list action_taken body | ||
type | ||
submitter.name submitter.kind submitter.address_line_1 | ||
submitter.address_line_2 submitter.city submitter.state submitter.zip | ||
submitter.country_code submitter.phone submitter.email submitter.url | ||
recipient.name recipient.kind recipient.address_line_1 | ||
recipient.address_line_2 recipient.city recipient.state recipient.zip | ||
recipient.country_code recipient.phone recipient.email recipient.url | ||
principal.name principal.kind principal.address_line_1 | ||
principal.address_line_2 principal.city principal.state principal.zip | ||
principal.country_code principal.phone principal.email principal.url | ||
sender.name sender.kind sender.address_line_1 | ||
sender.address_line_2 sender.city sender.state sender.zip | ||
sender.country_code sender.phone sender.email sender.url | ||
].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 (#{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.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,38 @@ | ||
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 | ||
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) | ||
|
||
# Create a risk trigger conditions from the ones that were hard-coded in the RiskTrigger model | ||
RiskTriggerCondition.create( | ||
field: 'type', | ||
value: 'Defamation', | ||
negated: true, | ||
risk_trigger_id: default_risk_trigger, | ||
matching_type: 'exact' | ||
) | ||
RiskTriggerCondition.create( | ||
field: 'submitter.email', | ||
value: 'google@lumendatabase.org', | ||
negated: true, | ||
risk_trigger_id: default_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
Oops, something went wrong.