From 8bcf1b49c7a9c2cc3a4023af79d8d70c40d0efed Mon Sep 17 00:00:00 2001 From: zhitomirskiyi Date: Mon, 7 Mar 2011 18:38:06 -0800 Subject: [PATCH] notifier for the messages, we're so close --- app/mailers/notifier.rb | 18 ++++++++ app/models/jobs/mail_private_message.rb | 15 +++++++ app/models/message.rb | 4 ++ app/models/notification.rb | 4 +- app/models/notifications/private_message.rb | 15 +++++++ app/views/notifier/private_message.html.haml | 16 ++++++++ app/views/notifier/private_message.text.haml | 8 ++++ config/locales/diaspora/en.yml | 4 ++ spec/mailers/notifier_spec.rb | 31 ++++++++++++++ spec/models/jobs/mail_private_message.rb | 27 ++++++++++++ spec/models/notification_spec.rb | 3 +- .../notifications/private_message_spec.rb | 41 +++++++++++++++++++ 12 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 app/models/jobs/mail_private_message.rb create mode 100644 app/models/notifications/private_message.rb create mode 100644 app/views/notifier/private_message.html.haml create mode 100644 app/views/notifier/private_message.text.haml create mode 100644 spec/models/jobs/mail_private_message.rb create mode 100644 spec/models/notifications/private_message_spec.rb diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index 9f54eeec5..ce10e1825 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -97,6 +97,24 @@ class Notifier < ActionMailer::Base end end + def private_message(recipient_id, sender_id, message_id) + @receiver = User.find_by_id(recipient_id) + @sender = Person.find_by_id(sender_id) + @message = Message.find_by_id(message_id) + @conversation = @message.conversation + @participants = @conversation.participants + + + log_mail(recipient_id, sender_id, 'private_message') + + attachments.inline['logo_caps.png'] = ATTACHMENT + + I18n.with_locale(@receiver.language) do + mail(:to => "\"#{@receiver.name}\" <#{@receiver.email}>", + :subject => I18n.t('notifier.private_message.subject', :name => @sender.name), :host => AppConfig[:pod_uri].host) + end + end + private def log_mail recipient_id, sender_id, type log_string = "event=mail mail_type=#{type} recipient_id=#{recipient_id} sender_id=#{sender_id}" diff --git a/app/models/jobs/mail_private_message.rb b/app/models/jobs/mail_private_message.rb new file mode 100644 index 000000000..232ed9a61 --- /dev/null +++ b/app/models/jobs/mail_private_message.rb @@ -0,0 +1,15 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + + +module Job + class MailPrivateMessage < Base + @queue = :mail + def self.perform_delegate(recipient_id, actor_id, target_id) + + Notifier.private_message( recipient_id, actor_id, target_id).deliver + + end + end +end diff --git a/app/models/message.rb b/app/models/message.rb index 77c5faba0..c4d41bb0c 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -67,6 +67,10 @@ class Message < ActiveRecord::Base end end + def notification_type(user, person) + Notifications::PrivateMessage + end + private def participant_of_parent_conversation if self.parent && !self.parent.participants.include?(self.author) diff --git a/app/models/notification.rb b/app/models/notification.rb index 6af9d9b4b..4b1c33f18 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -19,9 +19,9 @@ class Notification < ActiveRecord::Base if target.respond_to? :notification_type if note_type = target.notification_type(recipient, actor) if target.is_a? Comment - n = concatenate_or_create(recipient, target.post, actor, note_type) + n = note_type.concatenate_or_create(recipient, target.post, actor, note_type) else - n = make_notification(recipient, target, actor, note_type) + n = note_type.make_notification(recipient, target, actor, note_type) end n.email_the_user(target, actor) if n n.socket_to_user(recipient, :actor => actor) if n diff --git a/app/models/notifications/private_message.rb b/app/models/notifications/private_message.rb new file mode 100644 index 000000000..3044816c8 --- /dev/null +++ b/app/models/notifications/private_message.rb @@ -0,0 +1,15 @@ +class Notifications::PrivateMessage < Notification + def mail_job + Job::MailPrivateMessage + end + def translation_key + 'private_message' + end + def self.make_notification(recipient, target, actor, notification_type) + n = notification_type.new(:target => target, + :recipient_id => recipient.id) + + n.actors << actor + n + end +end diff --git a/app/views/notifier/private_message.html.haml b/app/views/notifier/private_message.html.haml new file mode 100644 index 000000000..f6d6b48c6 --- /dev/null +++ b/app/views/notifier/private_message.html.haml @@ -0,0 +1,16 @@ +%p + = t('notifier.hello', :name => @receiver.profile.first_name) +%p + = "#{@sender.name} (#{@sender.diaspora_handle})" + = t('.private_message') +%p + = @message.text +%p + + %br + = link_to t('.sign_in'), conversation_url(@cnv) + + %br + = t('notifier.love') + %br + = t('notifier.diaspora') diff --git a/app/views/notifier/private_message.text.haml b/app/views/notifier/private_message.text.haml new file mode 100644 index 000000000..d86a91780 --- /dev/null +++ b/app/views/notifier/private_message.text.haml @@ -0,0 +1,8 @@ += t('notifier.hello', :name => @receiver.profile.first_name) += "#{@sender.name} (#{@sender.diaspora_handle})" += t('notifier.private_message.private_message') + += @message.text + += "#{t('notifier.love')} \n" += t('notifier.diaspora') diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 162542839..2819c4a9c 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -488,6 +488,10 @@ en: subject: "%{name} has mentioned you on Diaspora*" mentioned: "mentioned you in a post:" sign_in: "Sign in to view it." + private_message: + subject: "%{name} has sent you a private message yon Diaspora*" + private_message: "has sent you a private message:" + sign_in: "Sign in to view it." home: show: share_what_you_want: "Share what you want, with whom you want." diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index 968e71520..9ce3bd699 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -110,7 +110,38 @@ describe Notifier do end end + describe ".private_message" do + before do + @user2 = bob + @participant_ids = @user2.contacts.map{|c| c.person.id} + [ @user2.person.id] + @create_hash = { :author => @user2.person, :participant_ids => @participant_ids , + :subject => "cool stuff", :text => 'hey'} + + @cnv = Conversation.create(@create_hash) + + @mail = Notifier.private_message(user.id, @cnv.author.id, @cnv.id) + end + it 'goes to the right person' do + @mail.to.should == [user.email] + end + + it 'has the recipients in the body' do + @mail.body.encoded.include?(user.person.first_name).should be true + end + + it 'has the name of the sender in the body' do + @mail.body.encoded.include?(@cnv.author.name).should be true + end + + it 'has the post text in the body' do + @mail.body.encoded.should include(@cnv.messages.first.text) + end + + it 'should not include translation missing' do + @mail.body.encoded.should_not include("missing") + end + end context "comments" do let!(:connect) { connect_users(user, aspect, user2, aspect2)} let!(:sm) {user.post(:status_message, :message => "Sunny outside", :to => :all)} diff --git a/spec/models/jobs/mail_private_message.rb b/spec/models/jobs/mail_private_message.rb new file mode 100644 index 000000000..dc1c12914 --- /dev/null +++ b/spec/models/jobs/mail_private_message.rb @@ -0,0 +1,27 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe Job::MailPrivateMessage do + describe '#perfom_delegate' do + it 'should call .deliver on the notifier object' do + user1 = alice + user2 = bob + participant_ids = [user1.contacts.first.person.id, user1.person.id] + + create_hash = { :author => user1.person, :participant_ids => participant_ids , + :subject => "cool stuff", :text => 'hey'} + + cnv = Conversation.create(create_hash) + message = cnv.messages.first + + mail_mock = mock() + mail_mock.should_receive(:deliver) + Notifier.should_receive(:mentioned).with(user2.id, user1.person.id, message.id).and_return(mail_mock) + + Job::MailMentioned.perform_delegate(user2.id, user1.person.id, message.id) + end + end +end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 21b3ceaf4..eb6ce5f09 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -44,7 +44,8 @@ describe Notification do Notification.should_not_receive(:make_notificatin) Notification.notify(@user, @sm, @person) end - context 'with a request' do + + context 'with a request' do before do @request = Request.diaspora_initialize(:from => @user.person, :to => @user2.person, :into => @aspect) end diff --git a/spec/models/notifications/private_message_spec.rb b/spec/models/notifications/private_message_spec.rb new file mode 100644 index 000000000..42b05f9ec --- /dev/null +++ b/spec/models/notifications/private_message_spec.rb @@ -0,0 +1,41 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe Notifications::PrivateMessage do + before do + @user1 = alice + @user2 = bob + + @create_hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], + :subject => "cool stuff", :text => "stuff"} + + @cnv = Conversation.create(@create_hash) + @msg = @cnv.messages.first + end + + describe '#make_notifiaction' do + it 'does not save the notification' do + lambda{ + Notification.notify(@user2, @msg, @user1.person) + }.should_not change(Notification, :count) + end + + it 'does email the user' do + opts = { + :actors => [@user1.person], + :recipient_id => @user2.id} + + n = Notifications::PrivateMessage.new(opts) + Notifications::PrivateMessage.stub!(:make_notification).and_return(n) + Notification.notify(@user2, @msg, @user1.person) + n.stub!(:recipient).and_return @user2 + + @user2.should_receive(:mail) + n.email_the_user(@msg, @user1.person) + end + end +end +