From 86d9a41d25b00e9908d97e525c212a7d938d2102 Mon Sep 17 00:00:00 2001
From: Raphael <raphael@joindiaspora.com>
Date: Thu, 9 Sep 2010 16:06:42 -0700
Subject: [PATCH] send_friend_request_to now takes a person and group, rather
 than a receive url and a group id

---
 app/controllers/dev_utilities_controller.rb |  2 +-
 app/controllers/publics_controller.rb       |  1 -
 app/controllers/requests_controller.rb      |  2 +-
 app/helpers/requests_helper.rb              | 15 +++++----------
 db/seeds/dev.rb                             |  2 +-
 db/seeds/tom.rb                             |  2 +-
 lib/diaspora/user/friending.rb              | 18 ++++--------------
 spec/controllers/publics_controller_spec.rb |  2 +-
 spec/lib/diaspora_parser_spec.rb            |  4 ++--
 spec/models/comments_spec.rb                |  2 +-
 spec/models/person_spec.rb                  |  6 +++---
 spec/models/request_spec.rb                 |  2 +-
 spec/models/user/user_friending_spec.rb     |  6 +++---
 spec/spec_helper.rb                         |  2 +-
 spec/user_encryption_spec.rb                |  4 ++--
 15 files changed, 27 insertions(+), 43 deletions(-)

diff --git a/app/controllers/dev_utilities_controller.rb b/app/controllers/dev_utilities_controller.rb
index a16c1fd56a..e633d40214 100644
--- a/app/controllers/dev_utilities_controller.rb
+++ b/app/controllers/dev_utilities_controller.rb
@@ -30,7 +30,7 @@ def warzombie
         backer_email = "#{backer['username']}@#{backer['username']}.joindiaspora.com"
         rel_hash = relationship_flow(backer_email)
         logger.info "Zombefriending #{backer['given_name']} #{backer['family_name']}"
-        current_user.send_request(rel_hash, current_user.groups.first.id)
+        current_user.send_friend_request_to(rel_hash[:friend], current_user.groups.first.id)
       end
     end
   end
diff --git a/app/controllers/publics_controller.rb b/app/controllers/publics_controller.rb
index d1255f3366..bf9a61e216 100644
--- a/app/controllers/publics_controller.rb
+++ b/app/controllers/publics_controller.rb
@@ -31,7 +31,6 @@ class PublicsController < ApplicationController
       Rails.logger.error("Received post #{params[:xml]} for nonexistent person #{params[:id]}")
       return
     end
-    puts params[:xml]
     if params[:xml].include? "xml version='1.0'"
       @user.receive_salmon params[:xml]
     else
diff --git a/app/controllers/requests_controller.rb b/app/controllers/requests_controller.rb
index b62eb630eb..59e58ba3cd 100644
--- a/app/controllers/requests_controller.rb
+++ b/app/controllers/requests_controller.rb
@@ -45,7 +45,7 @@ class RequestsController < ApplicationController
     Rails.logger.debug("Sending request: #{rel_hash}")
     
     begin
-      @request = current_user.send_request(rel_hash, params[:request][:group_id])
+      @request = current_user.send_friend_request_to(rel_hash[:friend], group)
     rescue Exception => e
       raise e unless e.message.include? "already friends"
       message = "You are already friends with #{params[:request][:destination_url]}!"
diff --git a/app/helpers/requests_helper.rb b/app/helpers/requests_helper.rb
index 37a5485c4a..e40f6bde82 100644
--- a/app/helpers/requests_helper.rb
+++ b/app/helpers/requests_helper.rb
@@ -27,17 +27,12 @@ module RequestsHelper
 
   def relationship_flow(identifier)
     action = :none
-    url = nil
-    local_person = Person.by_webfinger identifier
-    if local_person
-      action = (local_person == current_user.person ? :none : :friend)
-      url = local_person.receive_url
-    elsif !(identifier.include?(request.host) || identifier.include?("localhost"))
-      f = Redfinger.finger(identifier)
-      action = subscription_mode(f)
-      url = subscription_url(action, f)
+    person = nil
+    person = Person.by_webfinger identifier
+    if person
+      action = (person == current_user.person ? :none : :friend)
     end
-    { action => url }
+    { action => person }
   end
 
 end
diff --git a/db/seeds/dev.rb b/db/seeds/dev.rb
index c449790572..92653b8d9a 100644
--- a/db/seeds/dev.rb
+++ b/db/seeds/dev.rb
@@ -25,6 +25,6 @@ user2.person.save!
 
 # friending users
 group = user.group(:name => "other dudes")
-request = user.send_friend_request_to(user2.receive_url, group.id)
+request = user.send_friend_request_to(user2, group)
 reversed_request = user2.accept_friend_request( request.id, user2.group(:name => "presidents").id )
 user.receive reversed_request.to_diaspora_xml
diff --git a/db/seeds/tom.rb b/db/seeds/tom.rb
index 864caa6f2d..7a4a52f3a6 100644
--- a/db/seeds/tom.rb
+++ b/db/seeds/tom.rb
@@ -26,7 +26,7 @@ user2.person.save!
 
 # friending users
 group = user.group(:name => "other dudes")
-request = user.send_friend_request_to(user2.receive_url, group.id)
+request = user.send_friend_request_to(user2, group)
 reversed_request = user2.accept_friend_request( request.id, user2.group(:name => "presidents").id )
 user.receive reversed_request.to_diaspora_xml
 user.group(:name => "Presidents")
diff --git a/lib/diaspora/user/friending.rb b/lib/diaspora/user/friending.rb
index e80ee5aa59..b249347ddb 100644
--- a/lib/diaspora/user/friending.rb
+++ b/lib/diaspora/user/friending.rb
@@ -1,19 +1,17 @@
 module Diaspora
   module UserModules
     module Friending
-      def send_friend_request_to(friend_url, group_id)
-        raise "You are already friends with that person!" if self.friends.detect{ |x| x.receive_url == friend_url}
-        request = Request.instantiate(:to => friend_url, :from => self.person, :into => group_id)
+      def send_friend_request_to(friend, group)
+        raise "You are already friends with that person!" if self.friends.detect{ |x| x.receive_url == friend.receive_url}
+        request = Request.instantiate(:to => friend.receive_url, :from => self.person, :into => group.id)
         if request.save
           self.pending_requests << request
           self.save
 
-          group = self.group_by_id(group_id)
-
           group.requests << request
           group.save
           
-          request.push_to_url friend_url
+          request.push_to_url friend.receive_url
         end
         request
       end
@@ -101,14 +99,6 @@ module Diaspora
         remove_friend bad_friend
       end
 
-      def send_request(rel_hash, group)
-        if rel_hash[:friend]
-          self.send_friend_request_to(rel_hash[:friend], group)
-        else
-          raise "you can't do anything to that url"
-        end
-      end
-      
       def activate_friend(person, group)
         person.user_refs += 1
         group.people << person
diff --git a/spec/controllers/publics_controller_spec.rb b/spec/controllers/publics_controller_spec.rb
index 63d6153633..d574b28f13 100644
--- a/spec/controllers/publics_controller_spec.rb
+++ b/spec/controllers/publics_controller_spec.rb
@@ -37,7 +37,7 @@ describe PublicsController do
 
       @user3 = Factory.create(:user)
 
-      req = @user2.send_friend_request_to(@user.person.url, group.id)
+      req = @user2.send_friend_request_to(@user.person, group)
 
       @xml = req.to_diaspora_xml
   
diff --git a/spec/lib/diaspora_parser_spec.rb b/spec/lib/diaspora_parser_spec.rb
index 5197080dc3..b0b8f6a978 100644
--- a/spec/lib/diaspora_parser_spec.rb
+++ b/spec/lib/diaspora_parser_spec.rb
@@ -96,7 +96,7 @@ describe Diaspora::Parser do
     end
 
     it "should activate the Person if I initiated a request to that url" do 
-      request = @user.send_friend_request_to( @user2.receive_url, @group.id)
+      request = @user.send_friend_request_to( @user2.person, @group)
 
       request.reverse_for @user2 
 
@@ -118,7 +118,7 @@ describe Diaspora::Parser do
 
     it 'should process retraction for a person' do
       person_count = Person.all.count
-      request = @user.send_friend_request_to( @user2.receive_url, @group.id)
+      request = @user.send_friend_request_to( @user2.person, @group)
       request.reverse_for @user2 
       xml = request.to_diaspora_xml 
 
diff --git a/spec/models/comments_spec.rb b/spec/models/comments_spec.rb
index 95d2c977c1..bdf85884d7 100644
--- a/spec/models/comments_spec.rb
+++ b/spec/models/comments_spec.rb
@@ -36,7 +36,7 @@ describe Comment do
       before do
 
 
-        request = @user.send_friend_request_to(@user2.receive_url, @group.id)
+        request = @user.send_friend_request_to(@user2, @group)
         reversed_request = @user2.accept_friend_request( request.id, @group2.id )
         @user.receive reversed_request.to_diaspora_xml
         
diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb
index 8f0e21ef99..fc203d593f 100644
--- a/spec/models/person_spec.rb
+++ b/spec/models/person_spec.rb
@@ -60,7 +60,7 @@ describe Person do
 
   describe "unfriending" do
     it 'should delete an orphaned friend' do
-      request = @user.send_friend_request_to @person.receive_url, @group.id
+      request = @user.send_friend_request_to @person, @group
 
       @user.activate_friend(@person, @group) 
       @user.reload
@@ -74,8 +74,8 @@ describe Person do
     end
 
     it 'should not delete an un-orphaned friend' do
-      request = @user.send_friend_request_to @person.receive_url, @group.id
-      request2 = @user2.send_friend_request_to @person.receive_url, @group2.id
+      request = @user.send_friend_request_to @person, @group
+      request2 = @user2.send_friend_request_to @person, @group2
 
       @user.activate_friend(@person, @group) 
       @user2.activate_friend(@person, @group2)
diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb
index c5b49cc642..b02d6e6828 100644
--- a/spec/models/request_spec.rb
+++ b/spec/models/request_spec.rb
@@ -15,7 +15,7 @@ describe Request do
 
   it 'should generate xml for the User as a Person' do 
 
-    request = @user.send_friend_request_to "http://www.google.com/", @group.id
+    request = @user.send_friend_request_to Factory.create(:person), @group
 
     xml = request.to_xml.to_s
 
diff --git a/spec/models/user/user_friending_spec.rb b/spec/models/user/user_friending_spec.rb
index 6bf4c11173..50e5817fc0 100644
--- a/spec/models/user/user_friending_spec.rb
+++ b/spec/models/user/user_friending_spec.rb
@@ -12,7 +12,7 @@ describe User do
       group = @user.group(:name => "Dudes")
       group.requests.size.should == 0
 
-      @user.send_friend_request_to(friend.receive_url, group.id)
+      @user.send_friend_request_to(friend, group)
 
       group.reload
       group.requests.size.should == 1
@@ -48,7 +48,7 @@ describe User do
       @user.save
 
 
-      proc {@user.send_friend_request_to( friend.receive_url, @group.id )}.should raise_error
+      proc {@user.send_friend_request_to( friend, @group)}.should raise_error
     end
 
 
@@ -183,7 +183,7 @@ describe User do
       @user2 = Factory.create :user
       @group2 = @user2.group(:name => "Gross people")
       
-      request = @user.send_friend_request_to( @user2.receive_url, @group.id)
+      request = @user.send_friend_request_to( @user2, @group)
       request.reverse_for @user2 
       @user2.activate_friend(@user.person, @group2)
       @user.receive request.to_diaspora_xml
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index ff5cb46f33..3c5526e0d5 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -62,7 +62,7 @@ end
   end
 
   def friend_users(user1, group1, user2, group2)
-    request = user1.send_friend_request_to(user2.receive_url, group1.id)
+    request = user1.send_friend_request_to(user2.person, group1)
     reversed_request = user2.accept_friend_request( request.id, group2.id) 
     user1.receive reversed_request.to_diaspora_xml
   end
diff --git a/spec/user_encryption_spec.rb b/spec/user_encryption_spec.rb
index 6efafed4e3..73b9db0b93 100644
--- a/spec/user_encryption_spec.rb
+++ b/spec/user_encryption_spec.rb
@@ -32,7 +32,7 @@ describe 'user encryption' do
   describe 'key exchange on friending' do
     it 'should send over a public key' do
       message_queue.stub!(:add_post_request)
-      request = @user.send_friend_request_to("http://example.com/", @group.id)
+      request = @user.send_friend_request_to(Factory.create(:person), @group)
       request.to_diaspora_xml.include?( @user.exported_key).should be true
     end
 
@@ -44,7 +44,7 @@ describe 'user encryption' do
       original_key = remote_user.exported_key
       
       request = remote_user.send_friend_request_to(
-        @user.receive_url, remote_user.group(:name => "temp").id)
+        @user.person, remote_user.group(:name => "temp"))
       
       xml = request.to_diaspora_xml
       
-- 
GitLab