diff --git a/app/models/comment.rb b/app/models/comment.rb index 35f5ace2f54259bb0d397ada5d13712a87bd2d9f..6aa5049baf90e0fd5c411a670d2a52b5522a9eeb 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -3,6 +3,7 @@ class Comment include ROXML include Diaspora::Webhooks + xml_accessor :text xml_accessor :person, :as => Person xml_accessor :post_id @@ -17,11 +18,18 @@ class Comment key :person_id, ObjectId belongs_to :person, :class_name => "Person" - + after_save :send_friends_comments_on_my_posts def ==(other) (self.message == other.message) && (self.person.email == other.person.email) end - -end - + + + protected + + def send_friends_comments_on_my_posts + if (User.first.mine?(self.post) && self.person.is_a?(Friend)) + self.push_to(self.post.friends_with_permissions) + end + end +end \ No newline at end of file diff --git a/lib/common.rb b/lib/common.rb index 16d84f2d63220780f860626f0f1199c76c501459..76c2cccc02462e16b6f6d4ace30fec2b0a791fc9 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -69,10 +69,12 @@ module Diaspora def push_to(recipients) - recipients.map!{|x| x = x.url + "receive/"} - xml = self.class.build_xml_for([self]) - @@queue.add_post_request( recipients, xml ) - @@queue.process + unless recipients.empty? + recipients.map!{|x| x = x.url + "receive/"} + xml = self.class.build_xml_for([self]) + @@queue.add_post_request( recipients, xml ) + @@queue.process + end end diff --git a/spec/lib/common_spec.rb b/spec/lib/common_spec.rb index 8a37fc20a961817cdec6873c24710e4e90333d77..61e7fa3cc249be1e50aa783bdddbcebcd57fc8e6 100644 --- a/spec/lib/common_spec.rb +++ b/spec/lib/common_spec.rb @@ -8,6 +8,7 @@ describe Diaspora do describe Webhooks do before do @user = Factory.create(:user, :email => "bob@aol.com") + @friend = Factory.create(:friend) end describe "header" do @@ -53,9 +54,7 @@ describe Diaspora do Factory.create(:friend, :url => "http://www.alice.com/") Factory.create(:friend, :url => "http://www.jane.com/") - @post.friends_with_permissions.should include("http://www.bob.com/receive/") - @post.friends_with_permissions.should include("http://www.alice.com/receive/") - @post.friends_with_permissions.should include("http://www.jane.com/receive/") + @post.friends_with_permissions.should == Friend.all end it "should send an owners post to their friends" do @@ -73,7 +72,7 @@ describe Diaspora do it "should ensure one url is created for every friend" do 5.times {Factory.create(:friend)} - @post.friends_with_permissions.size.should == 5 + @post.friends_with_permissions.size.should == 6 end it "should build an xml object containing multiple Post types" do diff --git a/spec/helpers/parser_spec.rb b/spec/lib/parser_spec.rb similarity index 93% rename from spec/helpers/parser_spec.rb rename to spec/lib/parser_spec.rb index fc9e0e885e5e46e0fb6c60e3b701d074c237a12f..a9e3e7e453e16a9395d79a56811f0ece87a7ff59 100644 --- a/spec/helpers/parser_spec.rb +++ b/spec/lib/parser_spec.rb @@ -103,20 +103,6 @@ describe "parser in application helper" do comment.post.should == post end - - - - it 'should parse a person out of a post' do - @user.comment "foo", :on => @status_messages.first - xml = Comment.build_xml_for([Comment.first]) - puts xml - objs = parse_objects_from_xml(xml) - - puts objs.inspect - - - - end end end diff --git a/spec/models/comments_spec.rb b/spec/models/comments_spec.rb index 00f8c12401472f85b254830e5358f913cb546960..b5ebaf23c5397e65a2137680bba59809e1cf1482 100644 --- a/spec/models/comments_spec.rb +++ b/spec/models/comments_spec.rb @@ -15,21 +15,47 @@ describe Comment do it "should be able to comment on a friend's status" do friend = Factory.create :friend - status = Factory.create(:status_message, :person => @friend) + status = Factory.create(:status_message, :person => friend) @user.comment "sup dog", :on => status StatusMessage.first.comments.first.text.should == "sup dog" StatusMessage.first.comments.first.person.should == @user end - - - it 'should be able to send a post owner any new comments a user adds' do - friend = Factory.create(:friend) - status = Factory.create(:status_message, :person => friend) - - Comment.send(:class_variable_get, :@@queue).should_receive(:add_post_request) - @user.comment "yo", :on => status + it 'should not send out comments when we have no friends' do + status = Factory.create(:status_message, :person => @user) + Comment.send(:class_variable_get, :@@queue).should_not_receive(:add_post_request) + @user.comment "sup dog", :on => status end + describe 'comment propagation' do + before do + @friend = Factory.create(:friend) + @friend_two = Factory.create(:friend) + @friend_status = Factory.create(:status_message, :person => @friend) + @user_status = Factory.create(:status_message, :person => @user) + end + + it "should send a user's comment on a friend's post to that friend" do + Comment.send(:class_variable_get, :@@queue).should_receive(:add_post_request) + @user.comment "yo", :on => @friend_status + end + + it 'should send a user comment on his own post to lots of friends' do + allowed_urls = @user_status.friends_with_permissions.map!{|x| x = x.url + "receive/"} + Comment.send(:class_variable_get, :@@queue).should_receive(:add_post_request).with(allowed_urls, anything ) + @user.comment "yo", :on => @user_status + end + + it 'should send a comment a friend made on your post to all friends' do + Comment.send(:class_variable_get, :@@queue).should_receive(:add_post_request) + com = Comment.create(:person => @friend, :text => "balls", :post => @user_status) + end + + it 'should not send a comment a friend made on a friend post to anyone' do + Comment.send(:class_variable_get, :@@queue).should_not_receive(:add_post_request) + com = Comment.create(:person => @friend, :text => "balls", :post => @friend_status) + end + + end end -end +end \ No newline at end of file