diff --git a/app/controllers/requests_controller.rb b/app/controllers/requests_controller.rb index 88c3ab97e..a3c9ca48c 100644 --- a/app/controllers/requests_controller.rb +++ b/app/controllers/requests_controller.rb @@ -32,19 +32,25 @@ class RequestsController < ApplicationController end def create - aspect = current_user.aspect_by_id(params[:request][:into]) - account = params[:request][:to].strip - person = Person.by_account_identifier(account) - @request = Request.instantiate(:to => person, - :from => current_user.person, - :into => aspect) - if @request.save - current_user.dispatch_request(@request) - flash.now[:notice] = I18n.t('requests.create.sent') - redirect_to :back - else - flash.now[:error] = @request.errors.full_messages.join(', ') - redirect_to :back - end + aspect = current_user.aspect_by_id(params[:request][:into]) + account = params[:request][:to].strip + person = Person.by_account_identifier(account) + existing_request = Request.from(person).to(current_user.person).where(:sent => false).first if person + if existing_request + current_user.accept_and_respond(existing_request.id, aspect.id) + redirect_to :back + else + @request = Request.instantiate(:to => person, + :from => current_user.person, + :into => aspect) + if @request.save + current_user.dispatch_request(@request) + flash.now[:notice] = I18n.t('requests.create.sent') + redirect_to :back + else + flash.now[:error] = @request.errors.full_messages.join(', ') + redirect_to :back + end + end end end diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index 0a7c2deab..a3ad5fd22 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -6,6 +6,7 @@ module Diaspora module UserModules module Connecting def send_contact_request_to(desired_contact, aspect) + ##THIS METHOD IS ONLY USED IN TEST HELPERS request = Request.instantiate(:to => desired_contact, :from => self.person, :into => aspect) diff --git a/spec/controllers/requests_controller_spec.rb b/spec/controllers/requests_controller_spec.rb index 4df765d57..5074d38d6 100644 --- a/spec/controllers/requests_controller_spec.rb +++ b/spec/controllers/requests_controller_spec.rb @@ -14,14 +14,16 @@ describe RequestsController do @user.aspects.create!(:name => "lame-os") @user.reload + + @other_user = make_user + @other_user.aspects.create!(:name => "meh") + @other_user.reload end - + describe '#destroy' do before do - @other_user = make_user - @other_user.aspects.create!(:name => "meh") - @other_user.reload - + + @other_user.send_contact_request_to(@user.person, @other_user.aspects.first) @user.reload # so it can find its pending requests. @friend_request = @user.pending_requests.first @@ -46,6 +48,22 @@ describe RequestsController do end describe '#create' do + it 'autoaccepts and when sending a request to someone who sent me a request' do + #pending "When a user sends a request to person who requested them the request should be auto accepted" + @other_user.send_contact_request_to(@user.person, @other_user.aspects[0]) + @user.reload.pending_requests.count.should == 1 + @user.contact_for(@other_user.person).should be_nil + + post(:create, :request => { + :to => @other_user.diaspora_handle, + :into => @user.aspects[0].id + } + ) + @user.reload.pending_requests.count.should == 0 + @user.contact_for(@other_user.person).should_not be_nil + @user.aspects[0].contacts.all(:person_id => @other_user.person.id).should_not be_nil + end + it "redirects when requesting to be contacts with yourself" do post(:create, :request => { :to => @user.diaspora_handle, diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index b7171f6ef..4cf0b0266 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -59,21 +59,6 @@ describe Diaspora::UserModules::Connecting do end end - context 'requests that cross paths' do - it 'autoaccepts and when sending a request to someone who sent me a request' do - pending "When a user sends a request to person who requested them the request should be auto accepted" - user.send_contact_request_to(user2.person, aspect) - user2.reload.pending_requests.count.should == 1 - user2.contact_for(user.person).should be_nil - - user2.send_contact_request_to(user.person, aspect2) - user2.reload.pending_requests.count.should == 0 - user2.contact_for(user.person).should_not be_nil - - - end - end - context 'received a contact request' do let(:request_for_user) {Request.instantiate(:to => user.person, :from => person)} @@ -117,7 +102,7 @@ describe Diaspora::UserModules::Connecting do }.should raise_error(MongoMapper::DocumentNotValid, /already connected/) end - it 'should not be able to contact request yourself' do + it 'should not be able to contact request no-one' do proc { user.send_contact_request_to(nil, aspect) }.should raise_error(MongoMapper::DocumentNotValid) end