diff --git a/app/models/request.rb b/app/models/request.rb index cba28cdef..7f3ab6eff 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -9,7 +9,6 @@ class Request include Diaspora::Webhooks include ROXML - xml_reader :_id xml_reader :diaspora_handle xml_reader :destination_url xml_reader :callback_url @@ -35,9 +34,11 @@ class Request end def reverse_for accepting_user - self.diaspora_handle = accepting_user.diaspora_handle - self.destination_url = self.callback_url - self.save + Request.new( + :diaspora_handle => accepting_user.diaspora_handle, + :destination_url => self.callback_url, + :callback_url => self.destination_url + ) end protected diff --git a/app/models/user.rb b/app/models/user.rb index a2687dac6..d0acdfe12 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -332,7 +332,7 @@ class User else u = User.find_by_email(opts[:email]) if u.nil? - elsif friends.include?(u.person) + elsif contact_for(u.person) raise "You are already friends with this person" elsif not u.invited? self.send_friend_request_to(u.person, aspect_object) @@ -342,7 +342,7 @@ class User end end request = Request.instantiate( - :to => "http://local_request.example.com", + :to => "http://local_request.example.com", :from => self.person, :into => aspect_id ) @@ -368,7 +368,11 @@ class User if invitable.inviters.include?(inviter) raise "You already invited this person" else - invitable.pending_requests << request + invitable.pending_requests << Request.create( + :diaspora_handle => request.diaspora_handle, + :callback_url => request.callback_url, + :destination_url => request.destination_url) + invitable.inviters << inviter message = attributes.delete(:invite_message) if message diff --git a/lib/diaspora/user/friending.rb b/lib/diaspora/user/friending.rb index 30b35161f..b93ed39ab 100644 --- a/lib/diaspora/user/friending.rb +++ b/lib/diaspora/user/friending.rb @@ -34,7 +34,6 @@ module Diaspora activate_friend(request.person, aspect_by_id(aspect_id)) request.reverse_for(self) - request end def dispatch_friend_acceptance(request, requester) @@ -63,27 +62,26 @@ module Diaspora def receive_friend_request(friend_request) Rails.logger.info("receiving friend request #{friend_request.to_json}") - from_me = request_from_me?(friend_request) - know_about_request = know_about_request?(friend_request) - destination_aspect = self.aspect_by_id(friend_request.aspect_id) if friend_request.aspect_id - #response from a friend request you sent - if from_me && know_about_request && destination_aspect + if original_request = original_request(friend_request) + destination_aspect = self.aspect_by_id(original_request.aspect_id) activate_friend(friend_request.person, destination_aspect) Rails.logger.info("#{self.real_name}'s friend request has been accepted") friend_request.destroy + original_request.destroy Notifier.request_accepted(self, friend_request.person, destination_aspect) #this is a new friend request - elsif !from_me + elsif !request_from_me?(friend_request) self.pending_requests << friend_request self.save Rails.logger.info("#{self.real_name} has received a friend request") friend_request.save Notifier.new_request(self, friend_request.person).deliver else - Rails.logger.info("unsolicited friend request: #{friend_request.to_json}") + Rails.logger.info("#{self.real_name} is trying to receive a friend request from himself.") end + friend_request end def unfriend(bad_friend) @@ -128,11 +126,11 @@ module Diaspora end def request_from_me?(request) - request.callback_url == person.receive_url + request.diaspora_handle == self.diaspora_handle end - def know_about_request?(request) - pending_request_ids.include?(request.id.to_id) unless request.nil? || request.id.nil? + def original_request(response) + pending_requests.find_by_destination_url(response.callback_url) unless response.nil? || response.id.nil? end def requests_for_me diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb index e0dd3ef86..444770780 100644 --- a/lib/diaspora/user/receiving.rb +++ b/lib/diaspora/user/receiving.rb @@ -84,8 +84,8 @@ module Diaspora def receive_request request, person request.person = person request.person.save! - old_request = Request.find(request.id) - Rails.logger.info("I got a reqest_id #{request.id} with old request #{old_request.inspect}") + old_request = Request.find_by_diaspora_handle(request.diaspora_handle) + Rails.logger.info("I got a request from #{request.diaspora_handle} with old request #{old_request.inspect}") request.aspect_id = old_request.aspect_id if old_request request.save receive_friend_request(request) diff --git a/spec/helper_methods.rb b/spec/helper_methods.rb index 4dc1ca8f2..97b7a3e48 100644 --- a/spec/helper_methods.rb +++ b/spec/helper_methods.rb @@ -29,9 +29,9 @@ module HelperMethods def friend_users(user1, aspect1, user2, aspect2) request = user1.send_friend_request_to(user2.person, aspect1) - user2.receive request.to_diaspora_xml, user1.person + new_request = user2.receive request.to_diaspora_xml, user1.person - reversed_request = user2.accept_friend_request( request.id, aspect2.id) + reversed_request = user2.accept_friend_request( new_request.id, aspect2.id) user1.reload user1.receive reversed_request.to_diaspora_xml, user2.person diff --git a/spec/lib/diaspora/parser_spec.rb b/spec/lib/diaspora/parser_spec.rb index 6ad1da099..2b396f517 100644 --- a/spec/lib/diaspora/parser_spec.rb +++ b/spec/lib/diaspora/parser_spec.rb @@ -60,9 +60,9 @@ describe Diaspora::Parser do it "should activate the Person if I initiated a request to that url" do request = user.send_friend_request_to(user3.person, aspect) user.reload - request.reverse_for user3 + reversed = request.reverse_for user3 - xml = user3.salmon(request).xml_for(user.person) + xml = user3.salmon(reversed).xml_for(user.person) user3.delete diff --git a/spec/lib/em-webfinger_spec.rb b/spec/lib/em-webfinger_spec.rb index 5441d3f91..35159bae1 100644 --- a/spec/lib/em-webfinger_spec.rb +++ b/spec/lib/em-webfinger_spec.rb @@ -28,7 +28,7 @@ describe EMWebfinger do let(:non_diaspora_hcard) {File.open(File.join(Rails.root, 'spec/fixtures/evan_hcard')).read} context 'setup' do - let(:action){ Proc.new{|person| puts person.inspect }} + let(:action){ Proc.new{|person| person.inspect }} describe '#intialize' do it 'sets account ' do @@ -60,7 +60,7 @@ describe EMWebfinger do n = EMWebfinger.new("mbs@gmail.com") n.stub(:fetch).and_return(true) - n.on_person{|person| puts "foo"} + n.on_person{|person| 1+1} n.instance_variable_get(:@callbacks).count.should be 1 end diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index daab156b7..6e7a0af22 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -35,10 +35,6 @@ describe Request do end it 'recognized when a request is not from me' do - deliverable = Object.new - deliverable.stub!(:deliver) - Notifier.stub!(:new_request).and_return(deliverable) - user2.receive_salmon(user.salmon(request).xml_for(user2.person)) user2.reload user2.request_from_me?(request).should == false @@ -47,36 +43,31 @@ describe Request do context 'quering request through user' do it 'finds requests for that user' do - deliverable = Object.new - deliverable.stub!(:deliver) - Notifier.stub!(:new_request).and_return(deliverable) - + len = user2.requests_for_me.size user2.receive_salmon(user.salmon(request).xml_for(user2.person)) - user2.reload - user2.requests_for_me.include?(request).should == true + user2.reload.requests_for_me.size.should == len + 1 end end describe 'serialization' do + before do + @request = user.send_friend_request_to person, aspect + @xml = @request.to_xml.to_s + end it 'should not generate xml for the User as a Person' do - request = user.send_friend_request_to person, aspect - xml = request.to_xml.to_s - - xml.should_not include user.person.profile.first_name + @xml.should_not include user.person.profile.first_name end it 'should serialize the handle and not the sender' do - request = user.send_friend_request_to person, aspect - xml = request.to_xml.to_s - - xml.should include user.person.diaspora_handle + @xml.should include user.person.diaspora_handle end it 'should not serialize the exported key' do - request = user.send_friend_request_to person, aspect - xml = request.to_xml.to_s + @xml.should_not include user.person.exported_key + end - xml.should_not include user.person.exported_key + it 'does not serialize the id' do + @xml.should_not include @request.id.to_s end end diff --git a/spec/models/user/invite_spec.rb b/spec/models/user/invite_spec.rb index 6384978d6..c8db5ce28 100644 --- a/spec/models/user/invite_spec.rb +++ b/spec/models/user/invite_spec.rb @@ -60,7 +60,7 @@ describe User do it 'adds a pending request to the invited user' do invited_user = inviter.invite_user(:email => "marcy@example.com", :aspect_id => aspect.id) invited_user.reload - invited_user.pending_requests.find_by_callback_url(inviter.receive_url).nil?.should == false + invited_user.pending_requests.find_by_callback_url(inviter.receive_url).should_not be_nil end it 'adds a pending request to the inviter' do @@ -72,7 +72,7 @@ describe User do it 'throws if you try to add someone you"re friends with' do friend_users(inviter, aspect, another_user, wrong_aspect) inviter.reload - proc{inviter.invite_user(:email => another_user.email, :aspect_id => aspect.id)}.should raise_error /You are already friends with that person/ + proc{inviter.invite_user(:email => another_user.email, :aspect_id => aspect.id)}.should raise_error /already friends/ end it 'sends a friend request to a user with that email into the aspect' do @@ -123,14 +123,18 @@ describe User do :password_confirmation => "secret", :person => {:profile => {:first_name => "Bob", :last_name => "Smith"}} ) - u.pending_requests + u.pending_requests.count.should == 1 - request = u.pending_requests.first + + received_request = u.pending_requests.first + aspect2 = u.aspects.create(:name => "dudes") + + reversed_request = u.accept_friend_request(received_request.id, aspect2.id) u.reload - inviter - inviter.receive_salmon(u.salmon(u.accept_friend_request(request.id, aspect2.id)).xml_for(inviter.person)) - inviter.contact_for(u.person).should_not be_nil + + inviter.receive_salmon(u.salmon(reversed_request).xml_for(inviter.person)) + inviter.reload.contact_for(u.person).should_not be_nil end end end diff --git a/spec/models/user/user_friending_spec.rb b/spec/models/user/user_friending_spec.rb index d34338745..0b9bdac7a 100644 --- a/spec/models/user/user_friending_spec.rb +++ b/spec/models/user/user_friending_spec.rb @@ -48,8 +48,9 @@ describe Diaspora::UserModules::Friending do it 'should autoaccept a request the user sent' do request = user.send_friend_request_to(user2.person, aspect) - request.reverse_for(user2) - proc{user.receive_friend_request(request)}.should change(user.reload.friends, :count).by(1) + proc{ + user.receive_friend_request(request.reverse_for(user2)) + }.should change(user.reload.friends, :count).by(1) end end @@ -76,25 +77,19 @@ describe Diaspora::UserModules::Friending do end it 'should ignore a friend request from yourself' do - - user.pending_requests.delete_all - user.save - request = user.send_friend_request_to(user.person, aspect) - request.reverse_for(user) - request.aspect_id = nil + reversed_request = request_from_myself.reverse_for(user) + user.pending_requests.delete_all user.save - proc { user.receive_friend_request(request) }.should change( - - user.reload.pending_requests, :count ).by(0) + proc { user.receive_friend_request(reversed_request) + }.should change(user.reload.pending_requests, :count).by(0) end end it 'should not be able to friend request an existing friend' do friend_users(user, aspect, user2, aspect2) - - proc { user.send_friend_request_to(user2.person, aspect1) }.should raise_error + proc { user.send_friend_request_to(user2.person, aspect1) }.should raise_error /already friends/ end it 'should not be able to friend request yourself' do @@ -104,8 +99,7 @@ describe Diaspora::UserModules::Friending do it 'should send an email on acceptance if a friend request' do Notifier.should_receive(:request_accepted) request = user.send_friend_request_to(user2.person, aspect) - request.reverse_for(user2) - user.receive_friend_request(request) + user.receive_friend_request(request.reverse_for(user2)) end @@ -132,18 +126,18 @@ describe Diaspora::UserModules::Friending do context 'request from one remote person to one local user' do before do - user2.receive @req_three_xml, user.person + @received_request = user2.receive @req_three_xml, user.person end it 'should befriend the user other user on the same pod' do proc { - user2.accept_friend_request @request_three.id, aspect2.id + user2.accept_friend_request @received_request.id, aspect2.id }.should_not change(Person, :count) user2.contact_for(user.person).should_not be_nil end it 'should not delete the ignored user on the same pod' do proc { - user2.ignore_friend_request @request_three.id + user2.ignore_friend_request @received_request.id }.should_not change(Person, :count) user2.contact_for(user.person).should be_nil end @@ -158,24 +152,24 @@ describe Diaspora::UserModules::Friending do context 'Two users receiving requests from one person' do before do - user.receive @req_xml, person_one - user2.receive @req_two_xml, person_one + @req_to_user = user.receive @req_xml, person_one + @req_to_user2 = user2.receive @req_two_xml, person_one end describe '#accept_friend_request' do it 'should both users should befriend the same person' do - user.accept_friend_request @request.id, aspect.id + user.accept_friend_request @req_to_user.id, aspect.id user.contact_for(person_one).should_not be_nil - user2.accept_friend_request @request_two.id, aspect2.id + user2.accept_friend_request @req_to_user2.id, aspect2.id user2.contact_for(person_one).should_not be_nil end it 'should keep the person around if one of the users rejects him' do - user.accept_friend_request @request.id, aspect.id + user.accept_friend_request @req_to_user.id, aspect.id user.contact_for(person_one).should_not be_nil - user2.ignore_friend_request @request_two.id + user2.ignore_friend_request @req_to_user2.id user2.contact_for(person_one).should be_nil end end @@ -203,21 +197,21 @@ describe Diaspora::UserModules::Friending do end it "keeps the right counts of friends" do - user.receive @request.to_diaspora_xml, person_one + received_req = user.receive @request.to_diaspora_xml, person_one user.reload.pending_requests.size.should == 1 user.friends.size.should be 0 - user.receive @request_two.to_diaspora_xml, person_two + received_req2 = user.receive @request_two.to_diaspora_xml, person_two user.reload.pending_requests.size.should == 2 user.friends.size.should be 0 - user.accept_friend_request @request.id, aspect.id + user.accept_friend_request received_req.id, aspect.id user.reload.pending_requests.size.should == 1 user.friends.size.should be 1 user.contact_for(person_one).should_not be_nil - user.ignore_friend_request @request_two.id + user.ignore_friend_request received_req2.id user.reload.pending_requests.size.should == 0 user.friends.size.should be 1 user.contact_for(person_two).should be_nil