From b19cdf2e252e166b1ae566dc9f2c67a9175e191d Mon Sep 17 00:00:00 2001
From: Derrick Camerino <derrick@l33t.local>
Date: Sun, 31 Oct 2010 03:10:12 -0700
Subject: [PATCH] fixing N socketing bug

---
 lib/diaspora/user/receiving.rb   |  9 ++++-----
 spec/models/user/receive_spec.rb | 16 ++++++++++++----
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb
index a651b47932..25ccad7fbb 100644
--- a/lib/diaspora/user/receiving.rb
+++ b/lib/diaspora/user/receiving.rb
@@ -20,7 +20,6 @@ module Diaspora
         Rails.logger.debug("Receiving object for #{self.real_name}:\n#{object.inspect}")
         Rails.logger.debug("From: #{object.person.inspect}") if object.person
 
-
         if object.is_a?(Comment) || object.is_a?(Post)
           e = EMWebfinger.new(object.diaspora_handle)
 
@@ -34,7 +33,7 @@ module Diaspora
 
               raise "Not friends with that person" unless self.contact_for(salmon_author)
 
-              if object.is_a?(Comment) 
+              if object.is_a?(Comment)
                 receive_comment object, xml
               else
                 receive_post object, xml
@@ -137,11 +136,11 @@ module Diaspora
         self.save
 
         aspects = self.aspects_with_person(post.person)
-        aspects.each{ |aspect|
+        aspects.each do |aspect|
           aspect.posts << post
           aspect.save
-          post.socket_to_uid(id, :aspect_ids => [aspect.id]) if (post.respond_to?(:socket_to_uid) && !self.owns?(post))
-        }
+        end
+        post.socket_to_uid(id, :aspect_ids => aspects.map(&:id)) if (post.respond_to?(:socket_to_uid) && !self.owns?(post))
       end
     end
   end
diff --git a/spec/models/user/receive_spec.rb b/spec/models/user/receive_spec.rb
index 04500d40df..6e959c5080 100644
--- a/spec/models/user/receive_spec.rb
+++ b/spec/models/user/receive_spec.rb
@@ -19,6 +19,14 @@ describe User do
     friend_users(user, aspect, user2, aspect2)
   end
 
+  it 'should stream only one message to the everyone aspect when a multi-aspected friend posts' do
+    user.add_person_to_aspect(user2.person.id, user.aspect(:name => "villains").id)
+    status = user2.post(:status_message, :message => "Users do things", :to => aspect2.id)
+    xml = status.to_diaspora_xml
+    Diaspora::WebSocket.should_receive(:queue_to_user).exactly(:once)
+    user.receive xml, user2.person
+  end
+
   it 'should be able to parse and store a status message from xml' do
     status_message = user2.post :status_message, :message => 'store this!', :to => aspect2.id
 
@@ -113,11 +121,11 @@ describe User do
       post_in_db.comments.should == []
       user2.receive_salmon(@xml)
       post_in_db.reload
-      
+
       post_in_db.comments.include?(@comment).should be true
       post_in_db.comments.first.person.should == local_person
     end
-    
+
     it 'should correctly marshal a stranger for the downstream user' do
       remote_person = user3.person
       remote_person.delete
@@ -127,13 +135,13 @@ describe User do
       Person.should_receive(:by_account_identifier).twice.and_return{ |handle| if handle == user.person.diaspora_handle; user.person.save
         user.person; else; remote_person.save; remote_person; end }
 
-      
+
       user2.reload.raw_visible_posts.size.should == 1
       post_in_db = user2.raw_visible_posts.first
       post_in_db.comments.should == []
       user2.receive_salmon(@xml)
       post_in_db.reload
-      
+
       post_in_db.comments.include?(@comment).should be true
       post_in_db.comments.first.person.should == remote_person
     end
-- 
GitLab