From 34d0cb3680a9cec3ec726ee0f13e6a50ac9b1fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Tatarevi=C4=87ov=C3=A1?= Date: Fri, 26 Feb 2016 15:16:30 +0100 Subject: [PATCH 1/3] Tracking email details in journal notes --- .../009_create_custom_owner_name_field.rb | 19 ++++++++++ lib/mail_handler_patch.rb | 37 +++++++++++++++---- 2 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 db/migrate/009_create_custom_owner_name_field.rb diff --git a/db/migrate/009_create_custom_owner_name_field.rb b/db/migrate/009_create_custom_owner_name_field.rb new file mode 100644 index 0000000..29f64fa --- /dev/null +++ b/db/migrate/009_create_custom_owner_name_field.rb @@ -0,0 +1,19 @@ +class CreateCustomOwnerNameField < ActiveRecord::Migration + def self.up + c = CustomField.new( + :name => 'owner-name', + :editable => true, + :field_format => 'string') + c.type = 'IssueCustomField' # cannot be set by mass assignement! + c.save + Tracker.all.each do |t| + execute "INSERT INTO custom_fields_trackers (custom_field_id,tracker_id) VALUES (#{c.id},#{t.id})" + end + end + + def self.down + c = CustomField.find_by_name('owner-name') + execute "DELETE FROM custom_fields_trackers WHERE custom_field_id=#{c.id}" + c.delete + end +end diff --git a/lib/mail_handler_patch.rb b/lib/mail_handler_patch.rb index 2f7a015..64b80ed 100644 --- a/lib/mail_handler_patch.rb +++ b/lib/mail_handler_patch.rb @@ -5,6 +5,7 @@ def self.included(base) # :nodoc: base.class_eval do alias_method_chain :dispatch_to_default, :helpdesk + alias_method_chain :receive_issue_reply, :helpdesk end end @@ -20,14 +21,7 @@ def dispatch_to_default_with_helpdesk # permission treat_user_as_supportclient enabled if roles.any? {|role| role.allowed_to?(:treat_user_as_supportclient) } sender_email = @email.from.first - email_details = "From: " + @email[:from].formatted.first + "\n" - email_details << "To: " + @email[:to].formatted.join(', ') + "\n" - if !@email.cc.nil? - email_details << "Cc: " + @email[:cc].formatted.join(', ') + "\n" - end - email_details << "Date: " + @email[:date].to_s + "\n" - email_details = "
\n" + Mail::Encodings.unquote_and_convert_to(email_details, 'utf-8') + "
" - issue.description = email_details + issue.description + issue.description = email_details(@email) + issue.description issue.save custom_field = CustomField.find_by_name('owner-email') custom_value = CustomValue.where( @@ -35,6 +29,13 @@ def dispatch_to_default_with_helpdesk first custom_value.value = sender_email custom_value.save(:validate => false) # skip validation! + address = Mail::Address.new(@email[:from].formatted.first) + custom_field = CustomField.find_by_name('owner-name') + custom_value = CustomValue.where( + "customized_id = ? AND custom_field_id = ?", issue.id, custom_field.id). + first + custom_value.value = address.display_name + custom_value.save(:validate => false) # skip validation! # regular email sending to known users is done # on the first issue.save. So we need to send # the notification email to the supportclient @@ -63,6 +64,26 @@ def add_attachments(obj) end end + def email_details(email) + details = "From: " + @email[:from].formatted.first + "\n" + details << "To: " + @email[:to].formatted.join(', ') + "\n" + if !@email.cc.nil? + details << "Cc: " + @email[:cc].formatted.join(', ') + "\n" + end + details << "Date: " + @email[:date].to_s + "\n" + "
\n" + Mail::Encodings.unquote_and_convert_to(details, 'utf-8') + "
" + end + + # Overrides the receive_issue_reply method to add + # email details to the journal note + def receive_issue_reply_with_helpdesk(issue_id, from_journal=nil) + receive_issue_reply_without_helpdesk(issue_id, from_journal=nil) + issue = Issue.find_by_id(issue_id) + last_journal = Journal.find(issue.last_journal_id) + last_journal.notes = email_details(@email) + last_journal.notes + last_journal.save + end + end # module InstanceMethods end # module MailHandlerPatch end # module RedmineHelpdesk From 37f1033cfb7ceb00248c745c9b81ce9086f67ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Tatarevi=C4=87ov=C3=A1?= Date: Fri, 26 Feb 2016 23:30:01 +0100 Subject: [PATCH 2/3] Fixed email receiving when owner-name custom field is not enabled on project --- lib/mail_handler_patch.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/mail_handler_patch.rb b/lib/mail_handler_patch.rb index 64b80ed..b909eb7 100644 --- a/lib/mail_handler_patch.rb +++ b/lib/mail_handler_patch.rb @@ -29,13 +29,17 @@ def dispatch_to_default_with_helpdesk first custom_value.value = sender_email custom_value.save(:validate => false) # skip validation! - address = Mail::Address.new(@email[:from].formatted.first) custom_field = CustomField.find_by_name('owner-name') - custom_value = CustomValue.where( - "customized_id = ? AND custom_field_id = ?", issue.id, custom_field.id). - first - custom_value.value = address.display_name - custom_value.save(:validate => false) # skip validation! + unless custom_field.nil? + custom_value = CustomValue.where( + "customized_id = ? AND custom_field_id = ?", issue.id, custom_field.id). + first + unless custom_value.nil? + address = Mail::Address.new(@email[:from].formatted.first) + custom_value.value = address.display_name + custom_value.save(:validate => false) # skip validation! + end + end # regular email sending to known users is done # on the first issue.save. So we need to send # the notification email to the supportclient From 31d1e8683d7314cf35924246554c8eb9cc52392f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandra=20Tatarevi=C4=87ov=C3=A1?= Date: Sat, 27 Feb 2016 09:27:58 +0100 Subject: [PATCH 3/3] Removed unused parameter --- lib/mail_handler_patch.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mail_handler_patch.rb b/lib/mail_handler_patch.rb index b909eb7..e856e3c 100644 --- a/lib/mail_handler_patch.rb +++ b/lib/mail_handler_patch.rb @@ -21,7 +21,7 @@ def dispatch_to_default_with_helpdesk # permission treat_user_as_supportclient enabled if roles.any? {|role| role.allowed_to?(:treat_user_as_supportclient) } sender_email = @email.from.first - issue.description = email_details(@email) + issue.description + issue.description = email_details + issue.description issue.save custom_field = CustomField.find_by_name('owner-email') custom_value = CustomValue.where( @@ -68,7 +68,7 @@ def add_attachments(obj) end end - def email_details(email) + def email_details details = "From: " + @email[:from].formatted.first + "\n" details << "To: " + @email[:to].formatted.join(', ') + "\n" if !@email.cc.nil? @@ -84,7 +84,7 @@ def receive_issue_reply_with_helpdesk(issue_id, from_journal=nil) receive_issue_reply_without_helpdesk(issue_id, from_journal=nil) issue = Issue.find_by_id(issue_id) last_journal = Journal.find(issue.last_journal_id) - last_journal.notes = email_details(@email) + last_journal.notes + last_journal.notes = email_details + last_journal.notes last_journal.save end