diff --git a/app/models/mention.rb b/app/models/mention.rb index 86759d08b..8dbc37a3f 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -8,8 +8,6 @@ class Mention < ActiveRecord::Base validates_presence_of :post validates_presence_of :person - after_create :notify_recipient - after_destroy :delete_notification def notify_recipient diff --git a/app/models/post.rb b/app/models/post.rb index dfff551f5..5e925e721 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -78,12 +78,14 @@ class Post < ActiveRecord::Base end else user.add_post_to_aspects(local_post) + user.notify_if_mentioned(local_post) Rails.logger.info("event=receive payload_type=#{self.class} update=true status=complete sender=#{self.diaspora_handle} existing_post=#{local_post.id}") return local_post end elsif !local_post self.save user.add_post_to_aspects(self) + user.notify_if_mentioned(self) Rails.logger.info("event=receive payload_type=#{self.class} update=false status=complete sender=#{self.diaspora_handle}") return self else diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 99c94e692..a3a0146f1 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -68,6 +68,14 @@ class StatusMessage < Post end end + def mentions?(person) + mentioned_people.include? person + end + + def notify_person(person) + self.mentions.where(:person_id => person.id).first.try(:notify_recipient) + end + def mentioned_people_from_string regex = /@\{([^;]+); ([^\}]+)\}/ identifiers = self.raw_message.scan(regex).map do |match| diff --git a/app/models/user.rb b/app/models/user.rb index c923a5856..ed09e77af 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -106,7 +106,15 @@ class User < ActiveRecord::Base end end + def notify_if_mentioned(post) + return unless self.contact_for(post.person) && post.respond_to?(:mentions?) + + post.notify_person(self.person) if post.mentions? self.person + end + def add_post_to_aspects(post) + return unless self.contact_for(post.person) + Rails.logger.debug("event=add_post_to_aspects user_id=#{self.id} post_id=#{post.id}") add_to_streams(post, self.aspects_with_person(post.person)) post diff --git a/spec/intergration/contact_deleting_spec.rb b/spec/integration/contact_deleting_spec.rb similarity index 100% rename from spec/intergration/contact_deleting_spec.rb rename to spec/integration/contact_deleting_spec.rb diff --git a/spec/intergration/receiving_spec.rb b/spec/integration/receiving_spec.rb similarity index 87% rename from spec/intergration/receiving_spec.rb rename to spec/integration/receiving_spec.rb index 81fbc1ddb..ec74de52d 100644 --- a/spec/intergration/receiving_spec.rb +++ b/spec/integration/receiving_spec.rb @@ -55,9 +55,21 @@ describe 'a user receives a post' do @user1.aspects.size.should == num_aspects end + it "should show bob's post to alice" do + fantasy_resque do + sm = bob.build_post(:status_message, :message => "hi") + sm.save! + sm.stub!(:socket_to_user) + bob.aspects.reload + bob.add_to_streams(sm, [bob.aspects.first]) + bob.dispatch_post(sm, :to => bob.aspects.first) + end + + alice.visible_posts.count.should == 1 + end + context 'mentions' do - it 'adds the notifications for the mentioned users reguardless of the order they are received' do - pending 'this is for mnutt' + it 'adds the notifications for the mentioned users regardless of the order they are received' do Notification.should_receive(:notify).with(@user1, anything(), @user2.person) Notification.should_receive(:notify).with(@user3, anything(), @user2.person) @@ -72,6 +84,32 @@ describe 'a user receives a post' do zord = Postzord::Receiver.new(@user3, :object => @sm, :person => @user2.person) zord.receive_object end + + it 'notifies users when receiving a mention in a post from a remote user' do + @remote_person = Factory.create(:person, :diaspora_handle => "foobar@foobar.com") + Contact.create!(:user => @user1, :person => @remote_person, :aspects => [@aspect], :pending => false) + + Notification.should_receive(:notify).with(@user1, anything(), @remote_person) + + @sm = Factory.build(:status_message, :message => "hello @{#{@user1.name}; #{@user1.diaspora_handle}}", :diaspora_handle => @remote_person.diaspora_handle, :person => @remote_person) + @sm.stub!(:socket_to_user) + @sm.save + + zord = Postzord::Receiver.new(@user1, :object => @sm, :person => @user2.person) + zord.receive_object + end + + it 'does not notify the mentioned user if the mentioned user is not friends with the post author' do + Notification.should_not_receive(:notify).with(@user1, anything(), @user3.person) + + @sm = @user3.build_post(:status_message, :message => "should not notify @{#{@user1.name}; #{@user1.diaspora_handle}}") + @sm.stub!(:socket_to_user) + @user3.add_to_streams(@sm, [@user3.aspects.first]) + @sm.save + + zord = Postzord::Receiver.new(@user1, :object => @sm, :person => @user2.person) + zord.receive_object + end end context 'update posts' do @@ -80,7 +118,7 @@ describe 'a user receives a post' do status.message = 'foo' xml = status.to_diaspora_xml - receive_with_zord(@user2, @user1.person, xml) + receive_with_zord(@user2, @user1.person, xml) status.reload.message.should == 'store this!' end diff --git a/spec/models/mention_spec.rb b/spec/models/mention_spec.rb index 8f08abcc4..9310869d5 100644 --- a/spec/models/mention_spec.rb +++ b/spec/models/mention_spec.rb @@ -12,32 +12,20 @@ describe Mention do @mentioned_user = bob @non_friend = eve - @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 + @sm = @user.build_post(:status_message, :message => "hi @{#{@mentioned_user.name}; #{@mentioned_user.diaspora_handle}}", :to => @user.aspects.first) 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 + it 'notifies the person being mentioned' do + Notification.should_receive(:notify).with(@mentioned_user, anything(), @sm.person) + @sm.receive(@mentioned_user, @mentioned_user.person) end it 'should not notify a user if they do not see the message' do - pending "this is for mnutt" connect_users(@user, @aspect1, @non_friend, @non_friend.aspects.first) Notification.should_not_receive(:notify).with(@mentioned_user, anything(), @user.person) - sm2 = @user.build_post(:status_message, :message => 'stuff') - sm2.stub!(:socket_to_user) - @user.add_to_streams(sm2, [@aspect1]) - m2 = Mention.new(:person => @mentioned_user.person, :post => @sm) - sm2.save - m2.save + sm2 = @user.post(:status_message, :message => "stuff @{#{@non_friend.name}; #{@non_friend.diaspora_handle}}", :to => @user.aspects.first) + sm2.receive(@non_friend, @non_friend.person) end end @@ -51,8 +39,11 @@ describe Mention do describe 'after destroy' do it 'destroys a notification' do @user = alice - @sm = Factory(:status_message) - @m = Mention.create(:person => @user.person, :post=> @sm) + @mentioned_user = bob + + @sm = @user.post(:status_message, :message => "hi", :to => @user.aspects.first) + @m = Mention.create!(:person => @mentioned_user.person, :post => @sm) + @m.notify_recipient lambda{ @m.destroy diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index f368c2c6b..dee3fd9d9 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -147,6 +147,23 @@ STR @sm.mentioned_people end end + + describe "#mentions?" do + it 'returns true if the person was mentioned' do + @sm.mentions?(@people[0]).should be_true + end + + it 'returns false if the person was not mentioned' do + @sm.mentions?(Factory.create(:person)).should be_false + end + end + + describe "#notify_person" do + it 'notifies the person mentioned' do + Notification.should_receive(:notify).with(alice, anything, anything) + @sm.notify_person(alice.person) + end + end end describe "XML" do before do