From adda2dfa5e9f3d96ccbde57f297a5f284d924807 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Tue, 19 Jun 2018 12:41:18 +0800 Subject: [PATCH] Refine email followup search logic (fixes #2038) --- app/models/channel/filter/follow_up_check.rb | 4 ++-- app/models/ticket/number/date.rb | 4 ++-- app/models/ticket/number/increment.rb | 4 ++-- spec/models/channel/email_parser_spec.rb | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/models/channel/filter/follow_up_check.rb b/app/models/channel/filter/follow_up_check.rb index bafa49762..d41d32ff0 100644 --- a/app/models/channel/filter/follow_up_check.rb +++ b/app/models/channel/filter/follow_up_check.rb @@ -18,7 +18,7 @@ module Channel::Filter::FollowUpCheck # get ticket# from body if setting.include?('body') - ticket = Ticket::Number.check(mail[:body]) + ticket = Ticket::Number.check(mail[:body].html2text) if ticket Rails.logger.debug { "Follow up for '##{ticket.number}' in body." } mail['x-zammad-ticket-id'.to_sym] = ticket.id @@ -30,7 +30,7 @@ module Channel::Filter::FollowUpCheck if setting.include?('attachment') && mail[:attachments] mail[:attachments].each do |attachment| next if !attachment[:data] - ticket = Ticket::Number.check(attachment[:data]) + ticket = Ticket::Number.check(attachment[:data].html2text) next if !ticket Rails.logger.debug { "Follow up for '##{ticket.number}' in attachment." } mail['x-zammad-ticket-id'.to_sym] = ticket.id diff --git a/app/models/ticket/number/date.rb b/app/models/ticket/number/date.rb index e32b53874..a939e3297 100644 --- a/app/models/ticket/number/date.rb +++ b/app/models/ticket/number/date.rb @@ -86,12 +86,12 @@ module Ticket::Number::Date end # probe format - string.scan(/#{Regexp.quote(ticket_hook)}#{Regexp.quote(ticket_hook_divider)}(\d{4,10}#{system_id}\d{2,40})/i) do + string.scan(/\b#{Regexp.quote(ticket_hook)}#{Regexp.quote(ticket_hook_divider)}(\d{4,10}#{system_id}\d{2,40})\b/i) do ticket = Ticket.find_by(number: $1) break if ticket end if !ticket - string.scan(/#{Regexp.quote(ticket_hook)}\s{0,2}(\d{4,10}#{system_id}\d{2,40})/i) do + string.scan(/\b#{Regexp.quote(ticket_hook)}\s{0,2}(\d{4,10}#{system_id}\d{2,40})\b/i) do ticket = Ticket.find_by(number: $1) break if ticket end diff --git a/app/models/ticket/number/increment.rb b/app/models/ticket/number/increment.rb index 0e5b0d9a7..adbc39281 100644 --- a/app/models/ticket/number/increment.rb +++ b/app/models/ticket/number/increment.rb @@ -83,12 +83,12 @@ module Ticket::Number::Increment end # probe format - string.scan(/#{Regexp.quote(ticket_hook)}#{Regexp.quote(ticket_hook_divider)}(#{system_id}\d{2,48})/i) do + string.scan(/\b#{Regexp.quote(ticket_hook)}#{Regexp.quote(ticket_hook_divider)}(#{system_id}\d{2,48})\b/i) do ticket = Ticket.find_by(number: $1) break if ticket end if !ticket - string.scan(/#{Regexp.quote(ticket_hook)}\s{0,2}(#{system_id}\d{2,48})/i) do + string.scan(/\b#{Regexp.quote(ticket_hook)}\s{0,2}(#{system_id}\d{2,48})\b/i) do ticket = Ticket.find_by(number: $1) break if ticket end diff --git a/spec/models/channel/email_parser_spec.rb b/spec/models/channel/email_parser_spec.rb index 85b1017c0..49ffaccdf 100644 --- a/spec/models/channel/email_parser_spec.rb +++ b/spec/models/channel/email_parser_spec.rb @@ -28,6 +28,24 @@ RSpec.describe Channel::EmailParser, type: :model do .to change { ticket.articles.length } end end + + context 'as part of a larger word' do + let(:raw_mail) { File.read(mail_file).sub(/(?<=Hallo) =\n(?=Martin,)/, test_string) } + + it 'creates a separate ticket' do + expect { described_class.new.process({}, raw_mail) } + .not_to change { ticket.articles.length } + end + end + + context 'in html attributes' do + let(:raw_mail) { File.read(mail_file).sub(%r{}m, %(
)) } + + it 'creates a separate ticket' do + expect { described_class.new.process({}, raw_mail) } + .not_to change { ticket.articles.length } + end + end end end end