diff --git a/app/models/mention.rb b/app/models/mention.rb index deb065b67..be0dd3b72 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -7,4 +7,15 @@ class Mention < ActiveRecord::Base belongs_to :person validates_presence_of :post validates_presence_of :person + + after_create :notify_recipient + + def notify_recipient + Notification.notify(person.owner, self, post.person) unless person.remote? + end + + + def notification_type + 'mentioned' + end end diff --git a/app/models/notification.rb b/app/models/notification.rb index 7f2e5a182..fdad5a759 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -30,16 +30,18 @@ class Notification < ActiveRecord::Base end end - def email_the_user(comment, actor) + def email_the_user(target, actor) case self.action when "new_request" self.recipient.mail(Job::MailRequestReceived, self.recipient_id, actor.id) when "request_accepted" self.recipient.mail(Job::MailRequestAcceptance, self.recipient_id, actor.id) when "comment_on_post" - self.recipient.mail(Job::MailCommentOnPost, self.recipient_id, actor.id, comment.id) + self.recipient.mail(Job::MailCommentOnPost, self.recipient_id, actor.id, target.id) when "also_commented" - self.recipient.mail(Job::MailAlsoCommented, self.recipient_id, actor.id, comment.id) + self.recipient.mail(Job::MailAlsoCommented, self.recipient_id, actor.id, target.id) + when "mentioned" + self.recipient.mail(Job::MailMentioned, self.recipient_id, actor.id, target.id) end end diff --git a/db/schema.rb b/db/schema.rb index 3c0a97c16..a1012b771 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -404,7 +404,7 @@ ActiveRecord::Schema.define(:version => 20110209204702) do add_index "profiles", ["first_name", "searchable"], :name => "index_profiles_on_first_name_and_searchable" add_index "profiles", ["last_name", "searchable"], :name => "index_profiles_on_last_name_and_searchable" add_index "profiles", ["mongo_id"], :name => "index_profiles_on_mongo_id" - add_index "profiles", ["person_id"], :name => "index_profiles_on_person_id", :unique => true + add_index "profiles", ["person_id"], :name => "index_profiles_on_person_id" create_table "requests", :force => true do |t| t.integer "sender_id", :null => false @@ -466,8 +466,8 @@ ActiveRecord::Schema.define(:version => 20110209204702) do t.datetime "created_at" t.datetime "updated_at" t.string "mongo_id" - t.string "invitation_service" - t.string "invitation_identifier" + t.string "invitation_service", :limit => 127 + t.string "invitation_identifier", :limit => 127 end add_index "users", ["email"], :name => "index_users_on_email" diff --git a/spec/models/jobs/mail_mentioned_spec.rb b/spec/models/jobs/mail_mentioned_spec.rb new file mode 100644 index 000000000..c37e21388 --- /dev/null +++ b/spec/models/jobs/mail_mentioned_spec.rb @@ -0,0 +1,17 @@ +# 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::MailMentioned do + describe '#perfom_delegate' do + it 'should call .deliver on the notifier object' do + user = alice + sm = Factory(:status_message) + m = Mention.new(:person => user.person, :post=> sm) + + Notification.notify(user, m, sm.person) + end + end +end diff --git a/spec/models/mention_spec.rb b/spec/models/mention_spec.rb new file mode 100644 index 000000000..e691b5a26 --- /dev/null +++ b/spec/models/mention_spec.rb @@ -0,0 +1,33 @@ +# 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 Mention do + describe 'before create' do + before do + @user = alice + @sm = Factory(:status_message) + @m = Mention.new(:person => @user.person, :post=> @sm) + + end + it 'notifies the person being mention' do + Notification.should_receive(:notify).with(@user, @m, @sm.person) + @m.save + end + + it 'should only notify if the person is local' do + m = Mention.new(:person => Factory(:person), :post => @sm) + Notification.should_not_receive(:notify) + m.save + end + end + + describe '#notification_type' do + it "returns 'mentioned'" do + Mention.new.notification_type.should == 'mentioned' + end + end +end + diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 320b65086..410d8e459 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -12,6 +12,14 @@ describe StatusMessage do @aspect = @user.aspects.first end + describe '.before_create' do + it 'calls create_mentions' do + status = Factory.build(:status_message) + status.should_receive(:create_mentions) + status.save + end + end + describe '#diaspora_handle=' do it 'sets #person' do person = Factory.create(:person) @@ -108,6 +116,7 @@ STR end end describe '#create_mentions' do + it 'creates a mention for everyone mentioned in the message' do @sm.should_receive(:mentioned_people_from_string).and_return(@people) @sm.mentions.delete_all