From f8f86d34dd837a67d0af8bf73eb348ee91403c8a Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 21 Apr 2012 21:23:48 +0200 Subject: [PATCH] Moved notification generation to extra module. --- app/models/ticket/observer/notification.rb | 93 +++++++--------------- config/environment.rb | 3 + lib/notification_factory.rb | 73 +++++++++++++++++ 3 files changed, 104 insertions(+), 65 deletions(-) create mode 100644 lib/notification_factory.rb diff --git a/app/models/ticket/observer/notification.rb b/app/models/ticket/observer/notification.rb index 762d6485c..4948374b5 100644 --- a/app/models/ticket/observer/notification.rb +++ b/app/models/ticket/observer/notification.rb @@ -1,27 +1,3 @@ -class String - def message_quote - quote = self.split("\n") - body_quote = '' - quote.each do |line| - body_quote = body_quote + '> ' + line + "\n" - end - body_quote - end - def word_wrap(*args) - options = args.extract_options! - unless args.blank? - options[:line_width] = args[0] || 80 - end - options.reverse_merge!(:line_width => 80) - - lines = self - lines.split("\n").collect do |line| - line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line - end * "\n" - end - -end - class Ticket::Observer::Notification < ActiveRecord::Observer observe :ticket, 'ticket::_article' @@ -42,10 +18,11 @@ class Ticket::Observer::Notification < ActiveRecord::Observer :event => event, :recipient => 'to_work_on', # group|owner|to_work_on|customer :subject => 'New Ticket (#{ticket.title})', - :body => 'New Ticket (#{ticket.title}) in Group #{ticket.group.name} + :body => 'Hi #{recipient.firstname}, + +a new Ticket (#{ticket.title}) in Group #{ticket.group.name} From: #{ticket.articles[-1].from} - #{ticket.articles[-1].body} @@ -99,10 +76,11 @@ Your Zammad Team :event => event, :recipient => 'to_work_on', # group|owner|to_work_on|customer :subject => 'Follow Up (#{ticket.title})', - :body => 'Follow Up (#{ticket.title}) in Group #{ticket.group.name} + :body => 'Hi #{recipient.firstname}, + +a follow Up (#{ticket.title}) in Group #{ticket.group.name} From: #{ticket.articles[-1].from} - #{ticket.articles[-1].body} @@ -176,46 +154,31 @@ From: #{ticket.articles[-1].from} end end - # prepare subject & body - [:subject, :body].each { |key| - data[key.to_sym].gsub!( /\#\{(.+?)\}/ ) { |s| - - # use quoted text - callback = $1 - callback.gsub!( /.body$/ ) { |item| - item = item + '.word_wrap( :line_width => 80 ).message_quote.chomp' - } - - # use config params - callback.gsub!( /^config.(.+?)$/ ) { |item| - name = $1 - item = "Setting.get('#{$1}')" - } - - # replace value - s = eval callback - } - } - - # rebuild subject - data[:subject] = ticket.subject_build( data[:subject] ) - # send notifications - sender = Setting.get('notification_sender') recipients.each do |user| next if !user.email || user.email == '' - a = Channel::IMAP.new - subject = data[:subject] - body = data[:body] - message = a.send( - { - # :in_reply_to => self.in_reply_to, - :from => sender, - :to => user.email, - :subject => subject, - :body => body, - }, - true + + # prepare subject & body + notification = {} + [:subject, :body].each { |key| + notification[key.to_sym] = NotificationFactory.build( + :string => data[key.to_sym], + :objects => { + :ticket => ticket, + :article => article, + :recipient => user, + } + ) + } + + # rebuild subject + notification[:subject] = ticket.subject_build( notification[:subject] ) + + # send notification + NotificationFactory.send( + :recipient => user, + :subject => notification[:subject], + :body => notification[:body] ) end end diff --git a/config/environment.rb b/config/environment.rb index dcf6ca723..e0f1bdf97 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -10,5 +10,8 @@ require 'facebook_database' require 'linked_in_database' require 'google_oauth2_database' +# load notification factory (replace all tags) +require 'notification_factory' + # Initialize the rails application Zammad::Application.initialize! diff --git a/lib/notification_factory.rb b/lib/notification_factory.rb new file mode 100644 index 000000000..58f56089d --- /dev/null +++ b/lib/notification_factory.rb @@ -0,0 +1,73 @@ +class String + def message_quote + quote = self.split("\n") + body_quote = '' + quote.each do |line| + body_quote = body_quote + '> ' + line + "\n" + end + body_quote + end + def word_wrap(*args) + options = args.extract_options! + unless args.blank? + options[:line_width] = args[0] || 82 + end + options.reverse_merge!(:line_width => 82) + + lines = self + lines.split("\n").collect do |line| + line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line + end * "\n" + end +end + +module NotificationFactory + def self.build(data) + data[:string].gsub!( /\#\{(.+?)\}/ ) { |s| + + # use quoted text + callback = $1 + callback.gsub!( /\.body$/ ) { |item| + item = item + '.word_wrap( :line_width => 82 ).message_quote.chomp' + } + + # use config params + callback.gsub!( /^config\.(.+?)$/ ) { |item| + name = $1 + item = "Setting.get('#{$1}')" + } + + # use object params + callback.gsub!( /^(.+?)(\..+?)$/ ) { |item| + object_name = $1 + object_method = $2 + replace = nil + if data[:objects][object_name.to_sym] + replace = "data[:objects]['#{object_name}'.to_sym]#{object_method}" + else + replace = $1 + $2 + end + item = replace + } + + # replace value + s = eval callback + } + return data[:string] + end + + def self.send(data) + sender = Setting.get('notification_sender') + a = Channel::IMAP.new + message = a.send( + { +# :in_reply_to => self.in_reply_to, + :from => sender, + :to => data[:recipient].email, + :subject => data[:subject], + :body => data[:body], + }, + true + ) + end +end \ No newline at end of file