diff --git a/app/models/user.rb b/app/models/user.rb index 75fbc60c797bc1d326a266b8815837777c434a87..8daf17e31531c0e56134728b1d9a8c27aa4f5782 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -166,8 +166,7 @@ class User #socket post Rails.logger.info("event=dispatch user=#{diaspora_handle} post=#{post.id.to_s}") - push_to_aspects(post, aspect_ids) - post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to?(:socket_to_uid) && !post.pending + push_to_aspects(post, aspects_from_ids(aspect_ids)) if post.public self.services.each do |service| @@ -200,6 +199,7 @@ class User self.raw_visible_posts << post self.save + post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to? :socket_to_uid target_aspects = aspects_from_ids(aspect_ids) target_aspects.each do |aspect| aspect.posts << post @@ -219,23 +219,13 @@ class User end end - def push_to_aspects(post, aspect_ids) - if aspect_ids == :all || aspect_ids == "all" - aspects = self.aspects - elsif aspect_ids.is_a?(Array) && aspect_ids.first.class == Aspect - aspects = aspect_ids - else - aspects = self.aspects.find_all_by_id(aspect_ids) - end + def push_to_aspects(post, aspects) #send to the aspects - target_contacts = [] - - aspects.each { |aspect| - target_contacts = target_contacts | aspect.contacts + target_contacts = aspects.inject([]) { |contacts,aspect| + contacts = contacts | aspect.contacts } push_to_hub(post) if post.respond_to?(:public) && post.public - push_to_people(post, self.person_objects(target_contacts)) end @@ -250,7 +240,7 @@ class User person.reload # Sadly, we need this for Ruby 1.9. # person.owner will always return a ProxyObject. # calling nil? performs a necessary evaluation. - unless person.owner.nil? + if person.owner_id Rails.logger.info("event=push_to_person route=local sender=#{self.diaspora_handle} recipient=#{person.diaspora_handle} payload_type=#{post.class}") person.owner.receive(post.to_diaspora_xml, self.person) else diff --git a/spec/models/user/posting_spec.rb b/spec/models/user/posting_spec.rb index 8c00057654c748c8a246b59d6b3084645e3d8c4b..66473f901cebb1d83d88f93289254fac8a3301ad 100644 --- a/spec/models/user/posting_spec.rb +++ b/spec/models/user/posting_spec.rb @@ -37,6 +37,10 @@ describe User do aspect1.reload.post_ids.should include @post.id end + it 'sockets the post to the poster' do + @post.should_receive(:socket_to_uid).with(user.id, anything) + user.add_to_streams(@post, @aspect_ids) + end end describe '#aspects_from_ids' do @@ -67,45 +71,34 @@ describe User do end describe '#dispatch_post' do - it 'should put the post in the aspect post array' do - post = user.post(:status_message, :message => "hey", :to => aspect.id) - aspect.reload - aspect.posts.should include post - end - - it "should add the post to that user's visible posts" do - status_message = user.post :status_message, :message => "hi", :to => aspect.id - user.reload - user.raw_visible_posts.include?(status_message).should be true + let(:status) {user.build_post(:status_message, @status_opts)} + before do + @message = "hello, world!" + @status_opts = {:to => "all", :message => @message} end - it "posts to services if post is public" do - message = "hello, world!" - user.should_receive(:post_to_twitter).with(service1, message).exactly(1).times - user.should_receive(:post_to_facebook).with(service2, message).exactly(1).times - user.post :status_message, :message => message, :to => "all", :public => true + @status_opts[:public] = true + status.save + user.should_receive(:post_to_twitter).with(service1, @message).once + user.should_receive(:post_to_facebook).with(service2, @message).once + user.dispatch_post(status, :to => "all") end it "does not post to services if post is not public" do - user.should_receive(:post_to_twitter).exactly(0).times - user.should_receive(:post_to_facebook).exactly(0).times - user.post :status_message, :message => "hi", :to => "all" - end - - - it 'should not socket a pending post' do - sm = user.build_post(:status_message, :message => "your mom", :to => aspect.id, :pending => true) - sm.should_not_receive(:socket_to_uid) - user.dispatch_post(sm, :to => aspect.id) + @status_opts[:public] = false + status.save + user.should_not_receive(:post_to_twitter) + user.should_not_receive(:post_to_facebook) + user.dispatch_post(status, :to => "all") end end describe '#post' do it 'should not create a post with invalid aspect' do - pending "this would just causes db polution" - post_count = Post.count - proc { user.post(:status_message, :message => "hey", :to => aspect2.id) }.should raise_error /Cannot post to an aspect you do not own./ - Post.count.should == post_count + pending "this would just cause db polution" + proc { + user.post(:status_message, :message => "hey", :to => aspect2.id) + }.should_not change(Post, :count) end end @@ -139,12 +132,12 @@ describe User do describe '#push_to_aspects' do it 'should push a post to a aspect' do user.should_receive(:push_to_person).twice - user.push_to_aspects(post, aspect.id) + user.push_to_aspects(post, [aspect]) end it 'should push a post to contacts in all aspects' do user.should_receive(:push_to_person).exactly(3).times - user.push_to_aspects(post, :all) + user.push_to_aspects(post, user.aspects) end end