pulling in

This commit is contained in:
zhitomirskiyi 2011-03-06 21:36:03 -08:00
parent e00a033e9e
commit 80a3a3446a
8 changed files with 87 additions and 25 deletions

View file

@ -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

View file

@ -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

View file

@ -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|

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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