Improved loop detection (added more steps).

This commit is contained in:
Martin Edenhofer 2017-06-24 00:09:51 +02:00
parent 11c8fdbb1c
commit 9b8810fa58
2 changed files with 13 additions and 94 deletions

View file

@ -825,9 +825,11 @@ perform changes on ticket
# loop protection / check if maximal count of trigger mail has reached
map = {
10 => 10,
30 => 15,
60 => 25,
180 => 50,
600 => 100,
}
skip = false
map.each { |minutes, count|
@ -843,18 +845,20 @@ perform changes on ticket
}
next if skip
map = {
1 => 150,
3 => 250,
6 => 450,
10 => 30,
30 => 60,
60 => 120,
180 => 240,
600 => 360,
}
skip = false
map.each { |hours, count|
map.each { |minutes, count|
already_sent = Ticket::Article.where(
sender: Ticket::Article::Sender.find_by(name: 'System'),
type: Ticket::Article::Type.find_by(name: 'email'),
).where("ticket_articles.created_at > ? AND ticket_articles.to LIKE '%#{recipient_email.strip}%'", Time.zone.now - hours.hours).count
).where("ticket_articles.created_at > ? AND ticket_articles.to LIKE '%#{recipient_email.strip}%'", Time.zone.now - minutes.minutes).count
next if already_sent < count
logger.info "Send no trigger based notification to #{recipient_email} because already sent #{count} in total within last #{hours} hour(s) (loop protection)"
logger.info "Send no trigger based notification to #{recipient_email} because already sent #{count} in total within last #{minutes} minutes (loop protection)"
skip = true
break
}

View file

@ -3120,9 +3120,8 @@ class TicketTriggerTest < ActiveSupport::TestCase
Observer::Transaction.commit
ticket1.reload
assert_equal(22, ticket1.articles.count)
assert_equal(21, ticket1.articles.count)
assert_equal('some_loop_sender@example.com', ticket1.articles[20].from)
assert_equal('nicole.braun@zammad.org', ticket1.articles[21].to)
Ticket::Article.create(
ticket_id: ticket1.id,
@ -3141,92 +3140,8 @@ class TicketTriggerTest < ActiveSupport::TestCase
Observer::Transaction.commit
ticket1.reload
assert_equal(24, ticket1.articles.count)
assert_equal('some_loop_sender@example.com', ticket1.articles[22].from)
assert_equal('nicole.braun@zammad.org', ticket1.articles[23].to)
Ticket::Article.create(
ticket_id: ticket1.id,
from: 'some_loop_sender@example.com',
to: 'some_loop_recipient@example.com',
subject: 'some subject 1234',
message_id: 'some@id',
content_type: 'text/html',
body: 'some message <b>note</b><br>new line',
internal: false,
sender: Ticket::Article::Sender.find_by(name: 'Customer'),
type: Ticket::Article::Type.find_by(name: 'email'),
updated_by_id: 1,
created_by_id: 1,
)
Observer::Transaction.commit
ticket1.reload
assert_equal(26, ticket1.articles.count)
assert_equal('some_loop_sender@example.com', ticket1.articles[24].from)
assert_equal('nicole.braun@zammad.org', ticket1.articles[25].to)
Ticket::Article.create(
ticket_id: ticket1.id,
from: 'some_loop_sender@example.com',
to: 'some_loop_recipient@example.com',
subject: 'some subject 1234',
message_id: 'some@id',
content_type: 'text/html',
body: 'some message <b>note</b><br>new line',
internal: false,
sender: Ticket::Article::Sender.find_by(name: 'Customer'),
type: Ticket::Article::Type.find_by(name: 'email'),
updated_by_id: 1,
created_by_id: 1,
)
Observer::Transaction.commit
ticket1.reload
assert_equal(28, ticket1.articles.count)
assert_equal('some_loop_sender@example.com', ticket1.articles[26].from)
assert_equal('nicole.braun@zammad.org', ticket1.articles[27].to)
Ticket::Article.create(
ticket_id: ticket1.id,
from: 'some_loop_sender@example.com',
to: 'some_loop_recipient@example.com',
subject: 'some subject 1234',
message_id: 'some@id',
content_type: 'text/html',
body: 'some message <b>note</b><br>new line',
internal: false,
sender: Ticket::Article::Sender.find_by(name: 'Customer'),
type: Ticket::Article::Type.find_by(name: 'email'),
updated_by_id: 1,
created_by_id: 1,
)
Observer::Transaction.commit
ticket1.reload
assert_equal(30, ticket1.articles.count)
assert_equal('some_loop_sender@example.com', ticket1.articles[28].from)
assert_equal('nicole.braun@zammad.org', ticket1.articles[29].to)
Ticket::Article.create(
ticket_id: ticket1.id,
from: 'some_loop_sender@example.com',
to: 'some_loop_recipient@example.com',
subject: 'some subject 1234',
message_id: 'some@id',
content_type: 'text/html',
body: 'some message <b>note</b><br>new line',
internal: false,
sender: Ticket::Article::Sender.find_by(name: 'Customer'),
type: Ticket::Article::Type.find_by(name: 'email'),
updated_by_id: 1,
created_by_id: 1,
)
Observer::Transaction.commit
ticket1.reload
assert_equal(31, ticket1.articles.count)
assert_equal('some_loop_sender@example.com', ticket1.articles[30].from)
assert_equal(22, ticket1.articles.count)
assert_equal('some_loop_sender@example.com', ticket1.articles[21].from)
end