From 73616a3e7a43daf97fd1dedd32e66efd6be8f14f Mon Sep 17 00:00:00 2001
From: Maxwell Salzberg <maxwell@joindiaspora.com>
Date: Fri, 29 Jul 2011 17:20:10 -0700
Subject: [PATCH] new users now autofollow diasporahq@joindiaspora.com; upon
 connecting to a user, you now get post visibilities for their public posts
 (that your pod knows about)

---
 app/models/user.rb                                |  5 ++++-
 lib/diaspora/user/connecting.rb                   | 13 ++++++++++++-
 spec/controllers/invitations_controller_spec.rb   |  1 +
 spec/controllers/registrations_controller_spec.rb |  1 +
 spec/models/user/connecting_spec.rb               | 15 +++++++++++++++
 spec/models/user_spec.rb                          |  9 +++++++++
 6 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/app/models/user.rb b/app/models/user.rb
index b6be53b8a0..746db1bda6 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -345,7 +345,10 @@ class User < ActiveRecord::Base
 
   def seed_aspects
     self.aspects.create(:name => I18n.t('aspects.seed.family'))
-    self.aspects.create(:name => I18n.t('aspects.seed.work'))
+    work = self.aspects.create(:name => I18n.t('aspects.seed.work'))
+    default_account = Webfinger.new('diasporahq@joindiaspora.com').fetch
+    self.share_with(default_account, work)
+    work
   end
 
   def self.generate_key
diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb
index 668ab2709a..3500d461d0 100644
--- a/lib/diaspora/user/connecting.rb
+++ b/lib/diaspora/user/connecting.rb
@@ -22,10 +22,21 @@ module Diaspora
         if notification = Notification.where(:target_id => person.id).first
           notification.update_attributes(:unread=>false)
         end
-
+        
+        register_post_visibilities(contact)
         contact
       end
 
+      def register_post_visibilities(contact)
+        #should have select here, but proven hard to test
+        posts = Post.where(:author_id => contact.person_id, :public => true).limit(100)
+        posts.map! do |post|
+          PostVisibility.new(:contact_id => contact.id, :post_id => post.id)
+        end
+        PostVisibility.import(posts) unless posts.empty?
+        nil
+      end
+
       def remove_contact(contact, opts={:force => false})
         posts = contact.posts.all
 
diff --git a/spec/controllers/invitations_controller_spec.rb b/spec/controllers/invitations_controller_spec.rb
index 6bcb9d23c7..8ac3bc7646 100644
--- a/spec/controllers/invitations_controller_spec.rb
+++ b/spec/controllers/invitations_controller_spec.rb
@@ -12,6 +12,7 @@ describe InvitationsController do
     @aspect = @user.aspects.first
 
     request.env["devise.mapping"] = Devise.mappings[:user]
+    Webfinger.stub_chain(:new, :fetch).and_return(Factory(:person))
   end
 
   describe "#create" do
diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb
index f6cc2deaea..6954f23c41 100644
--- a/spec/controllers/registrations_controller_spec.rb
+++ b/spec/controllers/registrations_controller_spec.rb
@@ -16,6 +16,7 @@ describe RegistrationsController do
       :password_confirmation => "password"
       }
     }
+    Webfinger.stub_chain(:new, :fetch).and_return(Factory(:person))
   end
 
   describe '#check_registrations_open!' do
diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb
index 855c3db728..369fc81411 100644
--- a/spec/models/user/connecting_spec.rb
+++ b/spec/models/user/connecting_spec.rb
@@ -76,6 +76,16 @@ describe Diaspora::UserModules::Connecting do
     end
   end
 
+  describe '#register_post_visibilities' do
+    it 'creates post visibilites for up to 100 posts' do
+      Post.stub_chain(:where, :limit).and_return([Factory(:status_message, :public => true)])
+      c = Contact.create!(:user_id => alice.id, :person_id => eve.person.id)
+      expect{
+        alice.register_post_visibilities(c)
+      }.to change(PostVisibility, :count).by(1)
+    end
+  end
+
   describe '#share_with' do
     it 'finds or creates a contact' do
       lambda {
@@ -104,6 +114,11 @@ describe Diaspora::UserModules::Connecting do
       }.should change(contact.aspects, :count).by(1)
     end
 
+    it 'calls #register_post_visibilities with a contact' do
+      eve.should_receive(:register_post_visibilities)
+      eve.share_with(alice.person, eve.aspects.first)
+    end
+
     context 'dispatching' do
       it 'dispatches a request on initial request' do
         contact = alice.contacts.new(:person => eve.person)
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 6177a88dec..391eb9ee7b 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -171,6 +171,15 @@ describe User do
     end
   end
 
+  describe '#seed_aspects' do
+    it 'follows the default account' do
+      Webfinger.stub_chain(:new, :fetch).and_return(Factory(:person))
+      expect{
+       eve.seed_aspects
+      }.to change(eve.contacts, :count).by(1)
+    end
+  end
+
   describe ".build" do
     context 'with valid params' do
       before do
-- 
GitLab