diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 5c97f7dd1..5023acd0a 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -3,6 +3,13 @@ module NotificationsHelper target_type = note.action translation = t("notifications.#{target_type}") case target_type + when 'mentioned' + post = Mention.find(note.target_id).post + if post + "#{translation} #{link_to t('notifications.post'), object_path(post)}".html_safe + else + "#{translation} #{t('notifications.deleted')} #{t('notifications.post')}" + end when 'request_accepted' translation when 'new_request' @@ -40,7 +47,7 @@ module NotificationsHelper end def notification_people_link(note) - note.actors.collect{ |person| link_to("#{person.name.titlecase}", person_path(person))}.join(" , ").html_safe + note.actors.collect{ |person| link_to("#{h(person.name.titlecase)}", person_path(person))}.join(", ").html_safe end def peoples_names(note) diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index a0419afbe..2c9eae557 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -46,6 +46,19 @@ class Notifier < ActionMailer::Base :subject => I18n.t('notifier.request_accepted.subject', :name => @sender.name), :host => AppConfig[:pod_uri].host) end + def mentioned(recipient_id, sender_id, target_id) + @receiver = User.find_by_id(recipient_id) + @sender = Person.find_by_id(sender_id) + @post = Mention.find_by_id(target_id).post + + log_mail(recipient_id, sender_id, 'mentioned') + + attachments.inline['logo_caps.png'] = ATTACHMENT + + mail(:to => "\"#{@receiver.name}\" <#{@receiver.email}>", + :subject => I18n.t('notifier.mentioned.subject', :name => @sender.name), :host => AppConfig[:pod_uri].host) + end + def comment_on_post(recipient_id, sender_id, comment_id) @receiver = User.find_by_id(recipient_id) @sender = Person.find_by_id(sender_id) diff --git a/app/models/jobs/mail_mentioned.rb b/app/models/jobs/mail_mentioned.rb new file mode 100644 index 000000000..fbf4cfe13 --- /dev/null +++ b/app/models/jobs/mail_mentioned.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 MailMentioned < Base + @queue = :mail + def self.perform_delegate(recipient_id, actor_id, target_id) + + Notifier.mentioned( recipient_id, actor_id, target_id).deliver + + end + end +end diff --git a/app/models/mention.rb b/app/models/mention.rb index be0dd3b72..4a5b2a4ea 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -15,7 +15,7 @@ class Mention < ActiveRecord::Base end - def notification_type + def notification_type(recipient,actor) 'mentioned' end end diff --git a/app/models/status_message.rb b/app/models/status_message.rb index dc500e0f9..237babf55 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -33,7 +33,7 @@ class StatusMessage < Post write_attribute(:message, text) end - def formatted_message + def formatted_message(opts = {}) return self.raw_message unless self.raw_message people = self.mentioned_people regex = /@\{([^;]+); ([^\}]+)\}/ @@ -43,7 +43,12 @@ class StatusMessage < Post person = people.detect{ |p| p.diaspora_handle == inner_captures.last } - person ? "#{ERB::Util.h(person.name)}" : ERB::Util.h(inner_captures.first) + + if opts[:plain_text] + person ? ERB::Util.h(person.name) : ERB::Util.h(inner_captures.first) + else + person ? "#{ERB::Util.h(person.name)}" : ERB::Util.h(inner_captures.first) + end end form_message end diff --git a/app/views/notifier/mentioned.html.haml b/app/views/notifier/mentioned.html.haml new file mode 100644 index 000000000..b3763cb76 --- /dev/null +++ b/app/views/notifier/mentioned.html.haml @@ -0,0 +1,15 @@ +%p + = t('notifier.hello', :name => @receiver.profile.first_name) +%p + = "#{@sender.name} (#{@sender.diaspora_handle})" + = t('.mentioned') + + = @post.message + + %br + = link_to t('.sign_in'), status_message_url(@post) + + %br + = t('notifier.love') + %br + = t('notifier.diaspora') diff --git a/app/views/notifier/mentioned.text.haml b/app/views/notifier/mentioned.text.haml new file mode 100644 index 000000000..eb4ec1709 --- /dev/null +++ b/app/views/notifier/mentioned.text.haml @@ -0,0 +1,8 @@ += t('notifier.hello', :name => @receiver.profile.first_name) += "#{@sender.name} (#{@sender.diaspora_handle})" += t('notifier.mentioned.mentioned') + += @post.formatted_message(:plain_text => true) + += "#{t('notifier.love')} \n" += t('notifier.diaspora') diff --git a/app/views/shared/_notification.haml b/app/views/shared/_notification.haml deleted file mode 100644 index 5a6d7699e..000000000 --- a/app/views/shared/_notification.haml +++ /dev/null @@ -1,9 +0,0 @@ --# Copyright (c) 2010, Diaspora Inc. This file is --# licensed under the Affero General Public License version 3 or later. See --# the COPYRIGHT file. - -= link_to t('.new', :type => object.class.to_s, :from => peoples_names(note)), object_path(object.post) - - -= link_to "#{note.actor.name.titelize}", person_path(note.actor) -= object_link(note) diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 693ea1835..541343ee5 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -204,6 +204,7 @@ en: new_request: "offered to share with you." comment_on_post: "commented on your" also_commented: "also commented on your contact's" + mentioned: "has mentioned you their" post: 'post' deleted: 'deleted' index: @@ -457,9 +458,12 @@ en: commented: "has commented on your post:" sign_in: "Sign in to view it." also_commented: - subject: "%{name} has also commented." + subject: "%{name} has also commented on your contact's post." commented: "has also commented on %{post_author}'s post:" sign_in: "Sign in to view it." + mentioned: + subject: "%{name} has mentioned you on Diaspora*" + mentioned: "mentioned you in a post:" 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 e64bd47eb..f3db5ab38 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -36,7 +36,7 @@ describe Notifier do end end - describe '#single_admin' do + describe '.single_admin' do it 'mails a user' do mail = Notifier.single_admin("Welcome to bureaucracy!", user) mail.to.should == [user.email] @@ -45,7 +45,7 @@ describe Notifier do end end - describe "#new_request" do + describe ".new_request" do let!(:request_mail) {Notifier.new_request(user.id, person.id)} it 'goes to the right person' do request_mail.to.should == [user.email] @@ -65,7 +65,7 @@ describe Notifier do end end - describe "#request_accepted" do + describe ".request_accepted" do let!(:request_accepted_mail) {Notifier.request_accepted(user.id, person.id)} it 'goes to the right person' do request_accepted_mail.to.should == [user.email] @@ -80,11 +80,42 @@ describe Notifier do end end + + describe ".mentioned" do + before do + @user = alice + @sm = Factory(:status_message) + @m = Mention.create(:person => @user.person, :post=> @sm) + + @mail = Notifier.mentioned(@user.id, @sm.person.id, @m.id) + end + it 'goes to the right person' do + @mail.to.should == [@user.email] + end + + it 'has the receivers name in the body' do + @mail.body.encoded.include?(@user.person.profile.first_name).should be true + end + + it 'has the name of person mentioning in the body' do + @mail.body.encoded.include?(@sm.person.name).should be true + end + + it 'has the post text in the body' do + @mail.body.encoded.should include(@sm.message) + 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)} let!(:comment) { user2.comment("Totally is", :on => sm )} - describe "#comment_on_post" do + describe ".comment_on_post" do let!(:comment_mail) {Notifier.comment_on_post(user.id, person.id, comment.id).deliver} @@ -105,7 +136,7 @@ describe Notifier do end end - describe "#also commented" do + describe ".also commented" do let!(:comment_mail) {Notifier.also_commented(user.id, person.id, comment.id)} diff --git a/spec/models/jobs/mail_mentioned_spec.rb b/spec/models/jobs/mail_mentioned_spec.rb index c37e21388..a186ee623 100644 --- a/spec/models/jobs/mail_mentioned_spec.rb +++ b/spec/models/jobs/mail_mentioned_spec.rb @@ -11,7 +11,11 @@ describe Job::MailMentioned do sm = Factory(:status_message) m = Mention.new(:person => user.person, :post=> sm) - Notification.notify(user, m, sm.person) + mail_mock = mock() + mail_mock.should_receive(:deliver) + Notifier.should_receive(:mentioned).with(user.id, sm.person.id, m.id).and_return(mail_mock) + + Job::MailMentioned.perform_delegate(user.id, sm.person.id, m.id) end end end diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 410d8e459..1e01ed2a6 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -85,6 +85,14 @@ STR can mention people like Raphaellike Raphael #{link_to(@people[2].name, person_path(@people[2]), :class => 'mention')} can mention people like Raph STR end + + context 'with :plain_text option' do + it 'removes the mention syntax and displays the unformatted name' do + status = Factory(:status_message, :message => "@{Barack Obama; barak@joindiaspora.com } is so cool @{Barack Obama; barak@joindiaspora.com } ") + status.formatted_message(:plain_text => true).should == 'Barack Obama is so cool Barack Obama ' + end + end + it 'leaves the name of people that cannot be found' do @sm.stub(:mentioned_people).and_return([]) @sm.formatted_message.should == <<-STR