Ajout de "postponed_until" pour les emails
This commit is contained in:
parent
0b69d17b69
commit
9d21b2c266
2
Gemfile
2
Gemfile
|
@ -25,6 +25,8 @@ gem 'jbuilder', '~> 2.7'
|
||||||
# Use Active Storage variant
|
# Use Active Storage variant
|
||||||
# gem 'image_processing', '~> 1.2'
|
# gem 'image_processing', '~> 1.2'
|
||||||
|
|
||||||
|
gem 'chronic'
|
||||||
|
|
||||||
gem 'nokogiri', "1.11.0"
|
gem 'nokogiri', "1.11.0"
|
||||||
|
|
||||||
# rexml is no longer default gem in Ruby 3.0
|
# rexml is no longer default gem in Ruby 3.0
|
||||||
|
|
|
@ -77,6 +77,7 @@ GEM
|
||||||
regexp_parser (~> 1.5)
|
regexp_parser (~> 1.5)
|
||||||
xpath (~> 3.2)
|
xpath (~> 3.2)
|
||||||
childprocess (3.0.0)
|
childprocess (3.0.0)
|
||||||
|
chronic (0.10.2)
|
||||||
coderay (1.1.3)
|
coderay (1.1.3)
|
||||||
concurrent-ruby (1.1.7)
|
concurrent-ruby (1.1.7)
|
||||||
connection_pool (2.2.3)
|
connection_pool (2.2.3)
|
||||||
|
@ -257,6 +258,7 @@ DEPENDENCIES
|
||||||
bootsnap (>= 1.4.2)
|
bootsnap (>= 1.4.2)
|
||||||
byebug
|
byebug
|
||||||
capybara (>= 2.15)
|
capybara (>= 2.15)
|
||||||
|
chronic
|
||||||
devise
|
devise
|
||||||
elasticsearch-persistence
|
elasticsearch-persistence
|
||||||
elasticsearch-rails
|
elasticsearch-rails
|
||||||
|
|
|
@ -21,6 +21,7 @@ class Email
|
||||||
attribute :organisations, default: []
|
attribute :organisations, default: []
|
||||||
attribute :servers, default: []
|
attribute :servers, default: []
|
||||||
attribute :issues, default: []
|
attribute :issues, default: []
|
||||||
|
attribute :postponed_until, :datetime
|
||||||
attribute :created_at, :datetime, default: DateTime.now
|
attribute :created_at, :datetime, default: DateTime.now
|
||||||
attribute :updated_at, :datetime, default: DateTime.now
|
attribute :updated_at, :datetime, default: DateTime.now
|
||||||
|
|
||||||
|
@ -47,6 +48,10 @@ class Email
|
||||||
mailing_list
|
mailing_list
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def postponed?
|
||||||
|
postponed_until.present? && postponed_until > DateTime.now
|
||||||
|
end
|
||||||
|
|
||||||
def header_values(header_name)
|
def header_values(header_name)
|
||||||
headers.select { |header|
|
headers.select { |header|
|
||||||
header["name"] == header_name
|
header["name"] == header_name
|
||||||
|
|
|
@ -9,7 +9,7 @@ module EmailAction
|
||||||
|
|
||||||
attr_reader :action
|
attr_reader :action
|
||||||
|
|
||||||
def initialize(action)
|
def initialize(action:)
|
||||||
@action = action
|
@action = action
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
17
app/services/email_action/postpone.rb
Normal file
17
app/services/email_action/postpone.rb
Normal 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
|
|
@ -50,7 +50,7 @@ class RuleSetProcessor
|
||||||
email = email_action.process(email)
|
email = email_action.process(email)
|
||||||
rescue NameError => ex
|
rescue NameError => ex
|
||||||
Rails.logger.error "Skipped action##{action.id} '#{action.name}' - #{ex.inspect}"
|
Rails.logger.error "Skipped action##{action.id} '#{action.name}' - #{ex.inspect}"
|
||||||
raise InvalidRule, ex.message
|
raise InvalidRule, ex.inspect
|
||||||
end
|
end
|
||||||
|
|
||||||
email
|
email
|
||||||
|
@ -60,6 +60,8 @@ class RuleSetProcessor
|
||||||
case rule.subject_type.downcase
|
case rule.subject_type.downcase
|
||||||
when "header"
|
when "header"
|
||||||
Array(email.header_values(rule.subject_value))
|
Array(email.header_values(rule.subject_value))
|
||||||
|
when "subject"
|
||||||
|
Array(email.subject)
|
||||||
when "body"
|
when "body"
|
||||||
Array(email.plain_body)
|
Array(email.plain_body)
|
||||||
else
|
else
|
||||||
|
@ -74,7 +76,7 @@ class RuleSetProcessor
|
||||||
when "match", "matches"
|
when "match", "matches"
|
||||||
subjects.any? { |subject|
|
subjects.any? { |subject|
|
||||||
pattern = Regexp.new(rule.condition_value)
|
pattern = Regexp.new(rule.condition_value)
|
||||||
subject.match?
|
subject.match? pattern
|
||||||
}
|
}
|
||||||
when "equal", "equals"
|
when "equal", "equals"
|
||||||
subjects.any? { |subject|
|
subjects.any? { |subject|
|
||||||
|
|
|
@ -5,6 +5,7 @@ class CreateActions < ActiveRecord::Migration[6.1]
|
||||||
t.string :name, null: false
|
t.string :name, null: false
|
||||||
t.boolean :enabled, default: true, null: false
|
t.boolean :enabled, default: true, null: false
|
||||||
t.string :class_name, null: false
|
t.string :class_name, null: false
|
||||||
|
t.string :argument
|
||||||
|
|
||||||
t.timestamps
|
t.timestamps
|
||||||
end
|
end
|
||||||
|
|
11
db/schema.rb
11
db/schema.rb
|
@ -23,9 +23,10 @@ ActiveRecord::Schema.define(version: 2021_01_18_132809) do
|
||||||
|
|
||||||
create_table "actions", force: :cascade do |t|
|
create_table "actions", force: :cascade do |t|
|
||||||
t.integer "rule_set_id", null: false
|
t.integer "rule_set_id", null: false
|
||||||
t.string "name"
|
t.string "name", null: false
|
||||||
t.boolean "enabled", default: true
|
t.boolean "enabled", default: true, null: false
|
||||||
t.string "class_name"
|
t.string "class_name", null: false
|
||||||
|
t.string "argument"
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_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"
|
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.text "raw_headers"
|
||||||
t.boolean "cron"
|
t.boolean "cron"
|
||||||
t.boolean "mailing_list"
|
t.boolean "mailing_list"
|
||||||
t.string "organisations"
|
t.string "clients"
|
||||||
t.string "servers"
|
t.string "servers"
|
||||||
t.string "issues"
|
t.string "tickets"
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_at", precision: 6, null: false
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
end
|
end
|
||||||
|
|
21
test/fixtures/actions.yml
vendored
21
test/fixtures/actions.yml
vendored
|
@ -35,3 +35,24 @@ metadata:
|
||||||
name: Add metadata to mail
|
name: Add metadata to mail
|
||||||
enabled: true
|
enabled: true
|
||||||
class_name: EmailAction::MetadataMapping
|
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
|
||||||
|
|
12
test/fixtures/rule_sets.yml
vendored
12
test/fixtures/rule_sets.yml
vendored
|
@ -58,3 +58,15 @@ mailing_list:
|
||||||
name: FromMailingList
|
name: FromMailingList
|
||||||
description: Is this mail from amailing list?
|
description: Is this mail from amailing list?
|
||||||
enabled: true
|
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
|
||||||
|
|
27
test/fixtures/rules.yml
vendored
27
test/fixtures/rules.yml
vendored
|
@ -49,3 +49,30 @@ invalid_operator:
|
||||||
condition_type: contain
|
condition_type: contain
|
||||||
condition_value: MyString
|
condition_value: MyString
|
||||||
inverted: false
|
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
|
||||||
|
|
|
@ -80,4 +80,30 @@ class RuleSetProcessorTest < ActiveSupport::TestCase
|
||||||
assert_not_predicate email, :changed?
|
assert_not_predicate email, :changed?
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue