Moved notification generation to extra module.
This commit is contained in:
parent
8a5ff60677
commit
f8f86d34dd
3 changed files with 104 additions and 65 deletions
|
@ -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
|
class Ticket::Observer::Notification < ActiveRecord::Observer
|
||||||
observe :ticket, 'ticket::_article'
|
observe :ticket, 'ticket::_article'
|
||||||
|
|
||||||
|
@ -42,10 +18,11 @@ class Ticket::Observer::Notification < ActiveRecord::Observer
|
||||||
:event => event,
|
:event => event,
|
||||||
:recipient => 'to_work_on', # group|owner|to_work_on|customer
|
:recipient => 'to_work_on', # group|owner|to_work_on|customer
|
||||||
:subject => 'New Ticket (#{ticket.title})',
|
: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}
|
From: #{ticket.articles[-1].from}
|
||||||
|
|
||||||
<snip>
|
<snip>
|
||||||
#{ticket.articles[-1].body}
|
#{ticket.articles[-1].body}
|
||||||
</snip>
|
</snip>
|
||||||
|
@ -99,10 +76,11 @@ Your Zammad Team
|
||||||
:event => event,
|
:event => event,
|
||||||
:recipient => 'to_work_on', # group|owner|to_work_on|customer
|
:recipient => 'to_work_on', # group|owner|to_work_on|customer
|
||||||
:subject => 'Follow Up (#{ticket.title})',
|
: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}
|
From: #{ticket.articles[-1].from}
|
||||||
|
|
||||||
<snip>
|
<snip>
|
||||||
#{ticket.articles[-1].body}
|
#{ticket.articles[-1].body}
|
||||||
</snip>
|
</snip>
|
||||||
|
@ -176,46 +154,31 @@ From: #{ticket.articles[-1].from}
|
||||||
end
|
end
|
||||||
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
|
# send notifications
|
||||||
sender = Setting.get('notification_sender')
|
|
||||||
recipients.each do |user|
|
recipients.each do |user|
|
||||||
next if !user.email || user.email == ''
|
next if !user.email || user.email == ''
|
||||||
a = Channel::IMAP.new
|
|
||||||
subject = data[:subject]
|
# prepare subject & body
|
||||||
body = data[:body]
|
notification = {}
|
||||||
message = a.send(
|
[:subject, :body].each { |key|
|
||||||
{
|
notification[key.to_sym] = NotificationFactory.build(
|
||||||
# :in_reply_to => self.in_reply_to,
|
:string => data[key.to_sym],
|
||||||
:from => sender,
|
:objects => {
|
||||||
:to => user.email,
|
:ticket => ticket,
|
||||||
:subject => subject,
|
:article => article,
|
||||||
:body => body,
|
:recipient => user,
|
||||||
},
|
}
|
||||||
true
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
# rebuild subject
|
||||||
|
notification[:subject] = ticket.subject_build( notification[:subject] )
|
||||||
|
|
||||||
|
# send notification
|
||||||
|
NotificationFactory.send(
|
||||||
|
:recipient => user,
|
||||||
|
:subject => notification[:subject],
|
||||||
|
:body => notification[:body]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,5 +10,8 @@ require 'facebook_database'
|
||||||
require 'linked_in_database'
|
require 'linked_in_database'
|
||||||
require 'google_oauth2_database'
|
require 'google_oauth2_database'
|
||||||
|
|
||||||
|
# load notification factory (replace all tags)
|
||||||
|
require 'notification_factory'
|
||||||
|
|
||||||
# Initialize the rails application
|
# Initialize the rails application
|
||||||
Zammad::Application.initialize!
|
Zammad::Application.initialize!
|
||||||
|
|
73
lib/notification_factory.rb
Normal file
73
lib/notification_factory.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue