Ajout de "postponed_until" pour les emails

This commit is contained in:
Jérémy Lecour 2021-01-26 16:00:49 +01:00 committed by Jérémy Lecour
parent 0b69d17b69
commit 9d21b2c266
12 changed files with 124 additions and 8 deletions

View file

@ -25,6 +25,8 @@ gem 'jbuilder', '~> 2.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
gem 'chronic'
gem 'nokogiri', "1.11.0"
# rexml is no longer default gem in Ruby 3.0

View file

@ -77,6 +77,7 @@ GEM
regexp_parser (~> 1.5)
xpath (~> 3.2)
childprocess (3.0.0)
chronic (0.10.2)
coderay (1.1.3)
concurrent-ruby (1.1.7)
connection_pool (2.2.3)
@ -257,6 +258,7 @@ DEPENDENCIES
bootsnap (>= 1.4.2)
byebug
capybara (>= 2.15)
chronic
devise
elasticsearch-persistence
elasticsearch-rails

View file

@ -21,6 +21,7 @@ class Email
attribute :organisations, default: []
attribute :servers, default: []
attribute :issues, default: []
attribute :postponed_until, :datetime
attribute :created_at, :datetime, default: DateTime.now
attribute :updated_at, :datetime, default: DateTime.now
@ -47,6 +48,10 @@ class Email
mailing_list
end
def postponed?
postponed_until.present? && postponed_until > DateTime.now
end
def header_values(header_name)
headers.select { |header|
header["name"] == header_name

View file

@ -9,7 +9,7 @@ module EmailAction
attr_reader :action
def initialize(action)
def initialize(action:)
@action = action
end

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
module EmailAction
class Postpone < Base
def process(email)
if date = Chronic.parse(action.argument)
email.postponed_until = date
else
Rails.logger.warn "Skipped action##{action.id} '#{action.name}' - Unparsable argument for Chronic : '#{action.argument}'"
end
email
end
end
end

View file

@ -50,7 +50,7 @@ class RuleSetProcessor
email = email_action.process(email)
rescue NameError => ex
Rails.logger.error "Skipped action##{action.id} '#{action.name}' - #{ex.inspect}"
raise InvalidRule, ex.message
raise InvalidRule, ex.inspect
end
email
@ -60,6 +60,8 @@ class RuleSetProcessor
case rule.subject_type.downcase
when "header"
Array(email.header_values(rule.subject_value))
when "subject"
Array(email.subject)
when "body"
Array(email.plain_body)
else
@ -74,7 +76,7 @@ class RuleSetProcessor
when "match", "matches"
subjects.any? { |subject|
pattern = Regexp.new(rule.condition_value)
subject.match?
subject.match? pattern
}
when "equal", "equals"
subjects.any? { |subject|

View file

@ -5,6 +5,7 @@ class CreateActions < ActiveRecord::Migration[6.1]
t.string :name, null: false
t.boolean :enabled, default: true, null: false
t.string :class_name, null: false
t.string :argument
t.timestamps
end

View file

@ -23,9 +23,10 @@ ActiveRecord::Schema.define(version: 2021_01_18_132809) do
create_table "actions", force: :cascade do |t|
t.integer "rule_set_id", null: false
t.string "name"
t.boolean "enabled", default: true
t.string "class_name"
t.string "name", null: false
t.boolean "enabled", default: true, null: false
t.string "class_name", null: false
t.string "argument"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["rule_set_id"], name: "index_actions_on_rule_set_id"
@ -70,9 +71,9 @@ ActiveRecord::Schema.define(version: 2021_01_18_132809) do
t.text "raw_headers"
t.boolean "cron"
t.boolean "mailing_list"
t.string "organisations"
t.string "clients"
t.string "servers"
t.string "issues"
t.string "tickets"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

View file

@ -35,3 +35,24 @@ metadata:
name: Add metadata to mail
enabled: true
class_name: EmailAction::MetadataMapping
postpone_future_valid:
rule_set: postpone_future_valid
name: Postpone for 5 days
enabled: true
class_name: EmailAction::Postpone
argument: <%= Chronic.parse "5 days from now" %>
postpone_past_valid:
rule_set: postpone_past_valid
name: Postpone in the past
enabled: true
class_name: EmailAction::Postpone
argument: <%= Chronic.parse "2 days ago" %>
postpone_invalid:
rule_set: postpone_invalid
name: Postpone to invalid date
enabled: true
class_name: EmailAction::Postpone
argument: Foo Bar Baz

View file

@ -58,3 +58,15 @@ mailing_list:
name: FromMailingList
description: Is this mail from amailing list?
enabled: true
postpone_future_valid:
name: Postpone to valid date in the future
enabled: true
postpone_past_valid:
name: Postpone to valid date in the past
enabled: true
postpone_invalid:
name: Postpone to invalid date
enabled: true

View file

@ -49,3 +49,30 @@ invalid_operator:
condition_type: contain
condition_value: MyString
inverted: false
postpone_future_valid:
rule_set: postpone_future_valid
name: Match Postponable in Subject
enabled: true
subject_type: Subject
condition_type: match
condition_value: Postponable
inverted: false
postpone_past_valid:
rule_set: postpone_past_valid
name: Match Postponable in Subject
enabled: true
subject_type: Subject
condition_type: match
condition_value: Postponable
inverted: false
postpone_invalid:
rule_set: postpone_invalid
name: Match Postponable in Subject
enabled: true
subject_type: Subject
condition_type: match
condition_value: Postponable
inverted: false

View file

@ -80,4 +80,30 @@ class RuleSetProcessorTest < ActiveSupport::TestCase
assert_not_predicate email, :changed?
end
test "postponed to valid future date" do
email = Email.new(subject: "Postponable")
processor = RuleSetProcessor.new
email = processor.process(rule_sets(:postpone_future_valid), email)
assert_not_nil email.postponed_until
assert_predicate email, :postponed?
end
test "postponed to valid past date" do
email = Email.new(subject: "Postponable")
processor = RuleSetProcessor.new
email = processor.process(rule_sets(:postpone_past_valid), email)
assert_not_nil email.postponed_until
assert_not_predicate email, :postponed?
end
test "postponed to invalid date" do
email = Email.new(subject: "Postponable")
processor = RuleSetProcessor.new
email = processor.process(rule_sets(:postpone_invalid), email)
assert_nil email.postponed_until
assert_not_predicate email, :postponed?
end
end