Skip to content

Commit

Permalink
Improve methods to detect rules
Browse files Browse the repository at this point in the history
  • Loading branch information
entantoencuanto committed May 13, 2024
1 parent 3cbac17 commit a9ebf8c
Showing 1 changed file with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@ def scores
end
end

def about_blank_detection_method(_rule = {})
about.blank?
end

def activities_blank_detection_method(_rule = {})
UserActivities.new(__getobj__).blank?
end

def links_in_comments_or_about_detection_method(rule = {})
allowlist = rule["allowlist"].split(/\s/).compact_blank
blocklist = rule["blocklist"].split(/\s/).compact_blank
LinksParser.new(about, allowlist:, blocklist:).has_blocked_links? ||
Decidim::Comments::Comment.where(author: self).any? { |comment| LinksParser.new(comment.translated_body).has_blocked_links? }
end

def email_unconfirmed_detection_method(rule = {})
!confirmed? && email_domain_detection_method(rule)
end

def email_domain_detection_method(rule = {})
email_domain = email.split("@").last
allowlist = rule["allowlist"].split(/\s/).compact_blank
blocklist = rule["blocklist"].split(/\s/).compact_blank - allowlist

email_domain.blank? ||
(allowlist.present? && allowlist.all? { |name| name != email_domain }) ||
(blocklist.present? && blocklist.any? { |name| name == email_domain })
end

private

def current_rules
Expand All @@ -39,30 +68,21 @@ def rule_value(rule)
return 0 unless rule["enabled"]

positive = calculate_positive(rule)
return rule["weight"] if (rule["blocklist"] && positive) || (!rule["blocklist"] && !positive)
return rule["weight"] if (rule["block_if_detected"] && positive) || (!rule["block_if_detected"] && !positive)

0
end

def calculate_positive(rule)
case rule["type"]
when "about_blank"
about.blank?
when "activities_blank"
UserActivities.new(__getobj__).blank?
when "links_in_comments_or_about"
LinksParser.new(about).has_blocked_links? ||
Decidim::Comments::Comment.where(author: self).any? { |comment| LinksParser.new(comment.translated_body).has_blocked_links? }
when "email_unconfirmed"
!confirmed?
when "email_domain"
email_domain = email.split("@").last
domains = rule["variable"].split(/\s/).compact_blank
email_domain.present? && domains.any? { |name| name == email_domain }
end
return if USERS_AUTOBLOCKS_TYPES.keys.exclude?(rule["type"])
return unless respond_to?("#{rule["type"]}_detection_method")

send "#{rule["type"]}_detection_method", rule
end

class LinksParser
attr_reader :allowlist, :blocklist

def initialize(text, allowlist: [], blocklist: [])
@text = text
@allowlist = allowlist
Expand Down

0 comments on commit a9ebf8c

Please sign in to comment.