summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohan Sørensen <johan@johansorensen.com>2010-01-11 10:57:22 +0100
committerJohan Sørensen <johan@johansorensen.com>2010-01-11 10:57:22 +0100
commit34de8fb98fc8c5cd7dc3ccbc54e001a8f19672d3 (patch)
tree19a7dabbf662c567b1702e91f03f7b7c52d0ec0c
parentf963b7fa4bed6296e786ffd1a8df79f67873fe2e (diff)
downloadgitorious-mainline-outdated-34de8fb98fc8c5cd7dc3ccbc54e001a8f19672d3.zip
gitorious-mainline-outdated-34de8fb98fc8c5cd7dc3ccbc54e001a8f19672d3.tar.gz
gitorious-mainline-outdated-34de8fb98fc8c5cd7dc3ccbc54e001a8f19672d3.tar.bz2
Deliver an email for each watchable event, if it's turned on for the given favorite
-rwxr-xr-xapp/models/event.rb20
-rw-r--r--app/models/favorite.rb7
-rw-r--r--app/models/mailer.rb7
-rw-r--r--app/views/mailer/favorite_notification.html.erb29
-rw-r--r--test/unit/event_test.rb21
-rw-r--r--test/unit/favorite_test.rb28
-rw-r--r--test/unit/mailer_test.rb13
7 files changed, 123 insertions, 2 deletions
diff --git a/app/models/event.rb b/app/models/event.rb
index b308cde..c8a4439 100755
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -36,6 +36,7 @@ class Event < ActiveRecord::Base
end
after_create :create_feed_items
+ after_create :notify_subscribers
validates_presence_of :user_id, :unless => :user_email_set?
@@ -126,7 +127,19 @@ class Event < ActiveRecord::Base
git_actor.name
end
+ def favorites_for_email_notification
+ conditions = ["notify_by_email = ? and user_id != ?", true, self.user_id]
+ favorites = self.project.favorites.find(:all, :conditions => conditions)
+ # Find anyone who's just favorited the target, if it's watchable
+ if self.target.respond_to?(:watchers)
+ favorites += self.target.favorites.find(:all, :conditions => conditions)
+ end
+
+ favorites.uniq
+ end
+
protected
+
def user_email_set?
!user_email.blank?
end
@@ -136,7 +149,12 @@ class Event < ActiveRecord::Base
FeedItem.bulk_create_from_watcher_list_and_event!(watcher_ids, self)
end
- protected
+ def notify_subscribers
+ favorites_for_email_notification.each do |favorite|
+ favorite.notify_about_event(self)
+ end
+ end
+
# Get a list of user ids who are watching the project and target of
# this event, excluding the event creator (since he's probably not
# interested in his own doings).
diff --git a/app/models/favorite.rb b/app/models/favorite.rb
index e75845b..b83b7c1 100644
--- a/app/models/favorite.rb
+++ b/app/models/favorite.rb
@@ -47,8 +47,13 @@ class Favorite < ActiveRecord::Base
def event_should_be_created?
!event_exists?
end
-
+
def create_event
user.events.create(event_options) if event_should_be_created?
end
+
+ def notify_about_event(an_event)
+ notification_content = EventRendering::Text.render(an_event)
+ Mailer.deliver_favorite_notification(self.user, notification_content)
+ end
end
diff --git a/app/models/mailer.rb b/app/models/mailer.rb
index 2a739bc..9181d9c 100644
--- a/app/models/mailer.rb
+++ b/app/models/mailer.rb
@@ -85,6 +85,13 @@ class Mailer < ActionMailer::Base
body :error => err, :message => message_body, :processor => processor
end
+ def favorite_notification(user, notification_body)
+ setup_email(user)
+ @subject += "Activity: #{notification_body[0,35]}..."
+ @body[:user] = user
+ @body[:notification_body] = notification_body
+ end
+
protected
def setup_email(user)
@recipients = "#{user.email}"
diff --git a/app/views/mailer/favorite_notification.html.erb b/app/views/mailer/favorite_notification.html.erb
new file mode 100644
index 0000000..dcbaa8d
--- /dev/null
+++ b/app/views/mailer/favorite_notification.html.erb
@@ -0,0 +1,29 @@
+<%
+#--
+# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 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 Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#++
+%>
+
+Hello <%= @user.login %>,
+
+One of your favorites has a new activity:
+------------------------------------------------------------------------
+<%= @notification_body %>
+------------------------------------------------------------------------
+
+You're receiving this email because you've chosen to be notified by
+email whenever this favorite has new activity. You can manage your
+favorite subscriptions at http://<%= GitoriousConfig['gitorious_host'] %>/favorites
diff --git a/test/unit/event_test.rb b/test/unit/event_test.rb
index c9c9284..8a2b885 100644
--- a/test/unit/event_test.rb
+++ b/test/unit/event_test.rb
@@ -151,4 +151,25 @@ class EventTest < ActiveSupport::TestCase
end
end
end
+
+ context "favorite notification" do
+ setup do
+ @event = new_event(:action => Action::PUSH)
+ @favorite = users(:mike).favorites.new(:watchable => @repository)
+ @favorite.notify_by_email = true
+ @favorite.save!
+ end
+
+ should "find the favorites for the watchable with email notification turned on" do
+ assert_equal [@favorite], @event.favorites_for_email_notification
+ @favorite.update_attributes(:notify_by_email => false)
+ assert_equal [], @event.favorites_for_email_notification
+ end
+
+ should "tell the Favorite instances with email notification to notify" do
+ @event.expects(:favorites_for_email_notification).returns([@favorite])
+ @favorite.expects(:notify_about_event).with(@event)
+ @event.save!
+ end
+ end
end
diff --git a/test/unit/favorite_test.rb b/test/unit/favorite_test.rb
index 2c9bbf2..770aba8 100644
--- a/test/unit/favorite_test.rb
+++ b/test/unit/favorite_test.rb
@@ -112,4 +112,32 @@ class FavoriteTest < ActiveSupport::TestCase
end
end
+ context "event notifications" do
+ setup do
+ @user = users(:moe)
+ @favorite = @user.favorites.create!({
+ :watchable => merge_requests(:moes_to_johans),
+ :notify_by_email => true
+ })
+ @event = Event.new({
+ :target => repositories(:johans),
+ :body => "blabla",
+ :action => Action::PUSH
+ })
+ @event.user = users(:johan)
+ @event.project = projects(:johans)
+ @event.save!
+ end
+
+ should "ask the EventRendering engine to render the event" do
+ EventRendering::Text.expects(:render).with(@event).returns("some rendered event")
+ @favorite.notify_about_event(@event)
+ end
+
+ should "deliver the notification email" do
+ Mailer.expects(:deliver_favorite_notification).with(@user,
+ regexp_matches(/johan pushed/))
+ @favorite.notify_about_event(@event)
+ end
+ end
end
diff --git a/test/unit/mailer_test.rb b/test/unit/mailer_test.rb
index 311c245..37eec5a 100644
--- a/test/unit/mailer_test.rb
+++ b/test/unit/mailer_test.rb
@@ -105,4 +105,17 @@ class MailerTest < ActiveSupport::TestCase
assert_no_match /document\.cookie/, mail.subject
assert_match /Hello/, mail.subject
end
+
+ should "send a favorite notification" do
+ user = users(:mike)
+ body = "some event notification data here "*10
+ mail = Mailer.create_favorite_notification(user, body)
+
+ assert_equal [user.email], mail.to
+ assert_equal "[Gitorious] Activity: #{body[0..34]}...", mail.subject
+ assert_match(/Hello #{user.login}/, mail.body)
+ assert mail.body.include?(body), "notification body not in: #{mail.body}"
+ assert_match(/you're receiving this email because/i, mail.body)
+ assert_match(/#{GitoriousConfig['gitorious_host']}\/favorites/, mail.body)
+ end
end