From 7ca7e4bad5037067c1a8cd653eed095372e7f772 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Mon, 14 May 2007 17:03:59 +0000 Subject: [PATCH] Added mail notification when a new message is posted in the forums. Only users who "watch" the board receive notifications. To watch a board, go to the board and click on the "Watch" link. Notifications are sent by MessageObserver observer. GLoc was modified to use the mail template without language suffix when translated template (with language suffix) doesn't exist. Usefull when there's no hard coded text in the mail tempate. git-svn-id: http://redmine.rubyforge.org/svn/trunk@531 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/mailer.rb | 8 +++++++ app/models/message_observer.rb | 24 +++++++++++++++++++++ app/views/mailer/message_posted.rhtml | 4 ++++ config/environment.rb | 3 ++- vendor/plugins/gloc-1.1.0/lib/gloc-rails.rb | 3 ++- 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 app/models/message_observer.rb create mode 100644 app/views/mailer/message_posted.rhtml diff --git a/app/models/mailer.rb b/app/models/mailer.rb index 5d835289..41b5a329 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -87,4 +87,12 @@ class Mailer < ActionMailer::Base @subject = l(:mail_subject_register) @body['token'] = token end + + def message_posted(message, recipients) + set_language_if_valid(Setting.default_language) + @recipients = recipients + @from = Setting.mail_from + @subject = "[#{message.board.project.name} - #{message.board.name}] #{message.subject}" + @body['message'] = message + end end diff --git a/app/models/message_observer.rb b/app/models/message_observer.rb new file mode 100644 index 00000000..a3db7c86 --- /dev/null +++ b/app/models/message_observer.rb @@ -0,0 +1,24 @@ +# redMine - project management software +# Copyright (C) 2006-2007 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class MessageObserver < ActiveRecord::Observer + def after_create(message) + # send notification to board watchers + recipients = message.board.watcher_recipients + Mailer.deliver_message_posted(message, recipients) unless recipients.empty? + end +end diff --git a/app/views/mailer/message_posted.rhtml b/app/views/mailer/message_posted.rhtml new file mode 100644 index 00000000..10b46c41 --- /dev/null +++ b/app/views/mailer/message_posted.rhtml @@ -0,0 +1,4 @@ +<%= l(:field_author) %>: <%= @message.author.name %> +<%= url_for :only_path => false, :host => Setting.host_name, :controller => 'messages', :action => 'show', :board_id => @message.board_id, :id => @message.root %> + +<%= @message.content %> diff --git a/config/environment.rb b/config/environment.rb index e5e7d0e4..7782e6ad 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -27,9 +27,10 @@ Rails::Initializer.run do |config| # Enable page/fragment caching by setting a file-based store # (remember to create the caching directory and make it readable to the application) # config.action_controller.fragment_cache_store = :file_store, "#{RAILS_ROOT}/cache" - + # Activate observers that should always be running # config.active_record.observers = :cacher, :garbage_collector + config.active_record.observers = :message_observer # Make Active Record use UTC-base instead of local time # config.active_record.default_timezone = :utc diff --git a/vendor/plugins/gloc-1.1.0/lib/gloc-rails.rb b/vendor/plugins/gloc-1.1.0/lib/gloc-rails.rb index 006d8976..8f201bcb 100644 --- a/vendor/plugins/gloc-1.1.0/lib/gloc-rails.rb +++ b/vendor/plugins/gloc-1.1.0/lib/gloc-rails.rb @@ -92,7 +92,8 @@ module ActionMailer #:nodoc: private alias :render_message_without_gloc :render_message def render_message(method_name, body) - render_message_without_gloc("#{method_name}_#{current_language}", body) + template = File.exist?("#{template_path}/#{method_name}_#{current_language}.rhtml") ? "#{method_name}_#{current_language}" : "#{method_name}" + render_message_without_gloc(template, body) end end end