From a7d651236febbf4e13424c1d3bb17c74a60b412c Mon Sep 17 00:00:00 2001
From: maxwell <maxwell@joindiaspora.com>
Date: Wed, 5 Jan 2011 17:35:30 -0800
Subject: [PATCH] make the postman work across everything.  remove lots of now
 useless methods

---
 app/models/comment.rb                     |  4 +--
 app/models/jobs/post_to_services.rb       |  5 +--
 app/models/jobs/socket_webfinger.rb       |  3 +-
 app/models/notification.rb                |  2 +-
 app/models/user.rb                        | 26 ++--------------
 lib/diaspora/user/receiving.rb            | 10 +++---
 lib/diaspora/web_socket.rb                |  8 ++---
 spec/models/comment_spec.rb               | 37 ++++++-----------------
 spec/models/jobs/post_to_services_spec.rb |  5 +--
 spec/models/post_spec.rb                  |  9 ++++++
 spec/models/profile_spec.rb               | 14 +++++++++
 spec/models/request_spec.rb               |  8 +++++
 spec/models/retraction_spec.rb            | 30 ++++++++++++++++--
 spec/models/user/posting_spec.rb          |  2 +-
 14 files changed, 89 insertions(+), 74 deletions(-)

diff --git a/app/models/comment.rb b/app/models/comment.rb
index 372f385714..c2d3f29779 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -52,9 +52,9 @@ class Comment
 
   def subscribers(user)
     if user.owns?(self.post)
-      p = user.people_in_aspects(user.aspects_with_post(self.post_id))
+      p = self.post.subscribers(user)
     elsif user.owns?(self)
-     p =  [self.post.person]
+      p = [self.post.person]
     end
     p
   end
diff --git a/app/models/jobs/post_to_services.rb b/app/models/jobs/post_to_services.rb
index e29b1d609d..037de30f7e 100644
--- a/app/models/jobs/post_to_services.rb
+++ b/app/models/jobs/post_to_services.rb
@@ -5,8 +5,9 @@ module Jobs
     def self.perform(user_id, post_id, url)
       user = User.find_by_id(user_id)
       post = Post.find_by_id(post_id)
-      user.post_to_services(post, url)
+      user.services.each do |s|
+        s.post(post, url)
+      end
     end
   end
 end
-
diff --git a/app/models/jobs/socket_webfinger.rb b/app/models/jobs/socket_webfinger.rb
index 633f7b8e68..91228c8d66 100644
--- a/app/models/jobs/socket_webfinger.rb
+++ b/app/models/jobs/socket_webfinger.rb
@@ -5,8 +5,9 @@ module Jobs
     def self.perform(user_id, account, opts={})
       finger = Webfinger.new(account)
       begin
+        user = User.find_by_id(user_id)
         result = finger.fetch
-        result.socket_to_uid(user_id, opts)
+        result.socket_to_uid(user, opts)
       rescue
         Diaspora::WebSocket.queue_to_user(user_id,
           {:class => 'people',
diff --git a/app/models/notification.rb b/app/models/notification.rb
index 2fbccc99e5..b8f8f2e2f0 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -29,8 +29,8 @@ class Notification
                             :kind => kind,
                             :person_id => person.id,
                             :user_id => user.id)
-        n.socket_to_uid(user.id) if n
         n.email_the_user(object) unless user.disable_mail || !n
+        n.socket_to_uid(user) if n
         n
        end
     end
diff --git a/app/models/user.rb b/app/models/user.rb
index ea6da134dc..fb6ed9dc44 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -157,7 +157,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
+    post.socket_to_uid(self, :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
@@ -198,28 +198,6 @@ class User
   def dispatch_comment(comment)
     mailman = Postzord::Dispatch.new(self, comment)
     mailman.post 
-    #if owns? comment.post
-      ##push DOWNSTREAM (to original audience)
-      #Rails.logger.info "event=dispatch_comment direction=downstream user=#{self.diaspora_handle} comment=#{comment.id}"
-      #aspects = aspects_with_post(comment.post_id)
-
-      ##just socket to local users, as the comment has already
-      ##been associated and saved by post owner
-      ##  (we'll push to all of their aspects for now, the comment won't
-      ##   show up via js where corresponding posts are not present)
-
-      #people_in_aspects(aspects, :type => 'local').each do |person|
-        #comment.socket_to_uid(person.owner_id, :aspect_ids => 'all')
-      #end
-
-      ##push to remote people
-      #push_to_people(comment, people_in_aspects(aspects, :type => 'remote'))
-
-    #elsif owns? comment
-      ##push UPSTREAM (to poster)
-      #Rails.logger.info "event=dispatch_comment direction=upstream user=#{self.diaspora_handle} comment=#{comment.id}"
-      #push_to_people comment, [comment.post.person]
-    #end
   end
 
   ######### Mailer #######################
@@ -234,8 +212,8 @@ class User
     aspect_ids = aspects_with_post(post.id)
     aspect_ids.map! { |aspect| aspect.id.to_s }
 
-    post.unsocket_from_uid(self.id, :aspect_ids => aspect_ids) if post.respond_to? :unsocket_from_uid
     retraction = Retraction.for(post)
+    post.unsocket_from_uid(self, retraction, :aspect_ids => aspect_ids) if post.respond_to? :unsocket_from_uid
     mailman = Postzord::Dispatch.new(self, retraction)
     mailman.post 
 
diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb
index fac7e7d233..a9d8f05390 100644
--- a/lib/diaspora/user/receiving.rb
+++ b/lib/diaspora/user/receiving.rb
@@ -59,8 +59,6 @@ module Diaspora
             receive_object(object, person)
             Rails.logger.info("event=receive status=complete recipient=#{self.diaspora_handle} sender=#{salmon_author.diaspora_handle} payload_type#{object.class}")
 
-
-
             return object
           end
         end
@@ -92,7 +90,7 @@ module Diaspora
           end
           disconnected_by visible_person_by_id(retraction.post_id)
         else
-          retraction.perform self.id
+          retraction.perform self
           aspects = self.aspects_with_person(retraction.person)
           aspects.each{ |aspect| aspect.post_ids.delete(retraction.post_id.to_id)
             aspect.save
@@ -138,7 +136,7 @@ module Diaspora
           dispatch_comment comment
         end
 
-        comment.socket_to_uid(self.id, :aspect_ids => comment.post.aspect_ids)
+        comment.socket_to_uid(self, :aspect_ids => comment.post.aspect_ids)
         comment
       end
 
@@ -146,7 +144,7 @@ module Diaspora
         post.class.find_by_id(post.id)
       end
 
-      def receive_post post
+      def receive_post(post)
         #exsists locally, but you dont know about it
         #does not exsist locally, and you dont know about it
 
@@ -191,7 +189,7 @@ module Diaspora
           aspect.posts << post
           aspect.save
         end
-        post.socket_to_uid(id, :aspect_ids => aspects.map{|x| x.id}) if (post.respond_to?(:socket_to_uid) && !self.owns?(post))
+        post.socket_to_uid(self, :aspect_ids => aspects.map{|x| x.id}) if (post.respond_to?(:socket_to_uid) && !self.owns?(post))
         post
       end
     end
diff --git a/lib/diaspora/web_socket.rb b/lib/diaspora/web_socket.rb
index c25bebf842..04b14f6528 100644
--- a/lib/diaspora/web_socket.rb
+++ b/lib/diaspora/web_socket.rb
@@ -49,12 +49,12 @@ module Diaspora
   end
 
   module Socketable
-    def socket_to_uid(id, opts={})
-      SocketsController.new.outgoing(id, self, opts)
+    def socket_to_uid(user, opts={})
+      SocketsController.new.outgoing(user, self, opts)
     end
 
-    def unsocket_from_uid(id, opts={})
-      SocketsController.new.outgoing(id, Retraction.for(self), opts)
+    def unsocket_from_uid(user, retraction, opts={})
+      SocketsController.new.outgoing(user, retraction, opts)
     end
   end
 end
diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb
index 3ca7bc750a..a109b9499b 100644
--- a/spec/models/comment_spec.rb
+++ b/spec/models/comment_spec.rb
@@ -98,10 +98,12 @@ describe Comment do
     end
   end
 
-  describe 'comment propagation' do
+  context 'comment propagation' do
     before do
       @person = Factory.create(:person)
       user.activate_contact(@person, Aspect.first(:id => aspect.id))
+      @person3 = Factory.create(:person)
+      user.activate_contact(@person3, Aspect.first(:id => aspect.id))
 
       @person2 = Factory.create(:person)
       @person_status = Factory.build(:status_message, :person => @person)
@@ -119,17 +121,20 @@ describe Comment do
       Postzord::Dispatch.should_receive(:new).and_return(m)
       user.comment "yo", :on => @person_status
     end
-    
-    
 
     describe '#subscribers' do
       it 'returns the posts original audience, if the post is owned by the user' do
+        comment = user.build_comment "yo", :on => @person_status
+        comment.subscribers(user).should =~ [@person]
       end
 
       it 'returns the owner of the original post, if the user owns the comment' do
+        comment = user.build_comment "yo", :on => @user_status
+        comment.subscribers(user).should =~ [@person, @person3, user2.person]
       end
     end
 
+  context 'testing a method only used for testing' do
     it "should send a user's comment on a person's post to that person" do
       m = mock()
       m.stub!(:post)
@@ -137,31 +142,7 @@ describe Comment do
 
       user.comment "yo", :on => @person_status
     end
-
-    #context 'posts from a remote person' do
-      #before(:all) do
-        #stub_comment_signature_verification
-      #end
-      #before do
-        #@mailman = Postzord::Dipatch.new(user, @person_status)
-      #end
-      
-      #it 'should not send a comment a person made on his own post to anyone' do
-        #@mailman.should_not_receive(:deliver_to_local)
-        #comment = Comment.new(:person_id => @person.id, :diaspora_handle => @person.diaspora_handle, :text => "cats", :post => @person_status)
-        #user.receive comment.to_diaspora_xml, @person
-      #end
-
-      #it 'should not send a comment a person made on a person post to anyone' do
-        #@mailman.should_not_receive(:deliver_to_local)
-        #comment = Comment.new(:person_id => @person2.id, :diaspora_handle => @person.diaspora_handle, :text => "cats", :post => @person_status)
-        #user.receive comment.to_diaspora_xml, @person
-      #end
-
-      #after(:all) do
-        #unstub_mocha_stubs
-      #end
-  #end
+  end
 
     it 'should not clear the aspect post array on receiving a comment' do
       aspect.post_ids.include?(@user_status.id).should be true
diff --git a/spec/models/jobs/post_to_services_spec.rb b/spec/models/jobs/post_to_services_spec.rb
index 23b8751c57..0d84516b5c 100644
--- a/spec/models/jobs/post_to_services_spec.rb
+++ b/spec/models/jobs/post_to_services_spec.rb
@@ -6,9 +6,10 @@ describe Jobs::PostToServices do
     aspect = user.aspects.create(:name => "yeah")
     post = user.post(:status_message, :message => 'foo', :to => aspect.id)
     User.stub!(:find_by_id).with(user.id.to_s).and_return(user)
-    user.stub!(:services).and_return([])
-    user.should_receive(:post_to_services)
+    m = mock()
     url = "foobar"
+    m.should_receive(:post).with(anything, url)
+    user.stub!(:services).and_return([m])
     Jobs::PostToServices.perform(user.id.to_s, post.id.to_s, url)
   end
 end
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 9369798358..c69a155e8c 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -36,4 +36,13 @@ describe Post do
       post.mutable?.should == false   
     end
   end
+
+  describe '#subscribers' do
+    it 'returns the people contained in the aspects the post appears in' do
+      
+      post = @user.post :status_message, :message => "hello", :to => @aspect.id
+
+      post.subscribers(@user).should =~ []
+    end
+  end
 end
diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb
index b8a1257108..4a56b4fc83 100644
--- a/spec/models/profile_spec.rb
+++ b/spec/models/profile_spec.rb
@@ -74,6 +74,20 @@ describe Profile do
     end
   end
 
+  describe '#subscribers' do
+    it 'returns all non-pending contacts for a user' do
+      user = make_user
+      aspect = user.aspects.create(:name => "zord")
+      person = Factory.create(:person)
+      user.activate_contact(person, Aspect.first(:id => aspect.id))
+
+      person2 = Factory.create(:person)
+      user.activate_contact(person2, Aspect.first(:id => aspect.id))
+
+      user.profile.subscribers(user).should =~ [person, person2]
+    end
+  end
+
   describe 'date=' do
     let(:profile) { make_user.profile }
 
diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb
index aaa1fc1e4f..eb460588c6 100644
--- a/spec/models/request_spec.rb
+++ b/spec/models/request_spec.rb
@@ -80,6 +80,7 @@ describe Request do
       Request.from(@user).to(@user2.person).first.should == @request
     end
   end
+
   describe '#notification_type' do
     before do
       @request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect)    
@@ -94,6 +95,13 @@ describe Request do
     end
   end
 
+  describe '#subscribers' do
+    it 'returns an array with to field on a request' do
+      request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect)    
+      request.subscribers(@user).should =~ [@user2.person]
+    end
+  end
+
   describe '.hashes_for_person' do
     before do
       @user = make_user
diff --git a/spec/models/retraction_spec.rb b/spec/models/retraction_spec.rb
index 78e21d05d2..0b77625059 100644
--- a/spec/models/retraction_spec.rb
+++ b/spec/models/retraction_spec.rb
@@ -20,14 +20,38 @@ describe Retraction do
     end
   end
 
+  describe '#subscribers' do
+    it 'returns the subscribers to the post for all objects other than person' do
+      retraction = Retraction.for(post)
+      obj = retraction.instance_variable_get(:@object)
+      wanted_subscribers = obj.subscribers(user)
+      obj.should_receive(:subscribers).with(user).and_return(wanted_subscribers)
+      retraction.subscribers(user).should =~ wanted_subscribers
+    end
+
+    context 'hax' do
+      it 'barfs if the type is a person, and subscribers instance varabile is not set' do
+        retraction = Retraction.for(user)
+        obj = retraction.instance_variable_get(:@object)
+
+        proc{retraction.subscribers(user)}.should raise_error
+      end
+
+      it 'returns manually set subscribers' do
+        retraction = Retraction.for(user)
+        retraction.subscribers = "fooey"
+        retraction.subscribers(user).should == 'fooey'
+      end
+    end
+  end
+
   describe 'dispatching' do
-    it 'should dispatch a message on delete' do
+    it 'should dispatch a retraction on delete' do
       Factory.create(:person)
       m = mock()
       m.should_receive(:post)
-      Postzord::Dispatch.should_receive(:new).and_return(m)
+      Postzord::Dispatch.should_receive(:new).with(instance_of(User), instance_of(Retraction)).and_return(m)
       post.destroy
     end
   end
-
 end
diff --git a/spec/models/user/posting_spec.rb b/spec/models/user/posting_spec.rb
index 515446cd51..8750736a60 100644
--- a/spec/models/user/posting_spec.rb
+++ b/spec/models/user/posting_spec.rb
@@ -38,7 +38,7 @@ describe User do
     end
 
     it 'sockets the post to the poster' do
-      @post.should_receive(:socket_to_uid).with(user.id, anything)
+      @post.should_receive(:socket_to_uid).with(user, anything)
       user.add_to_streams(@post, @aspect_ids)
     end
   end
-- 
GitLab