From 8acdf212345e9c73d221a8d024d19b9b4af2a833 Mon Sep 17 00:00:00 2001 From: zhitomirskiyi Date: Tue, 26 Oct 2010 14:49:37 -0700 Subject: [PATCH] MS IZ got rid of the requests for me scope in the user --- app/models/request.rb | 2 - app/models/user.rb | 10 ++--- lib/diaspora/user/friending.rb | 11 +++-- spec/models/request_spec.rb | 11 ----- spec/models/user/user_friending_spec.rb | 53 +++++++++++++++++-------- spec/spec_helper.rb | 1 + 6 files changed, 48 insertions(+), 40 deletions(-) diff --git a/app/models/request.rb b/app/models/request.rb index a6efdc987..2226a7b45 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -26,8 +26,6 @@ class Request validates_presence_of :destination_url, :callback_url before_validation :clean_link - scope :for_user, lambda{ |user| where(:destination_url => user.person.receive_url) } - def self.instantiate(options = {}) person = options[:from] self.new(:destination_url => options[:to], diff --git a/app/models/user.rb b/app/models/user.rb index 6a184b156..850a2bb66 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -32,11 +32,11 @@ class User key :invites, Integer, :default => 5 key :invitation_token, String key :invitation_sent_at, DateTime - key :inviter_ids, Array - key :friend_ids, Array - key :pending_request_ids, Array - key :visible_post_ids, Array - key :visible_person_ids, Array + key :inviter_ids, Array, :typecast => 'ObjectId' + key :friend_ids, Array, :typecast => 'ObjectId' + key :pending_request_ids, Array, :typecast => 'ObjectId' + key :visible_post_ids, Array, :typecast => 'ObjectId' + key :visible_person_ids, Array, :typecast => 'ObjectId' key :invite_messages, Hash diff --git a/lib/diaspora/user/friending.rb b/lib/diaspora/user/friending.rb index 4a19ffd68..131b5b96a 100644 --- a/lib/diaspora/user/friending.rb +++ b/lib/diaspora/user/friending.rb @@ -29,9 +29,8 @@ module Diaspora end def accept_friend_request(friend_request_id, aspect_id) - request = Request.find_by_id(friend_request_id) - pending_requests.delete(request) - + request = pending_requests.find!(friend_request_id) + pending_request_ids.delete(request.id.to_id) activate_friend(request.person, aspect_by_id(aspect_id)) request.reverse_for(self) @@ -45,16 +44,16 @@ module Diaspora end def accept_and_respond(friend_request_id, aspect_id) - requester = Request.find_by_id(friend_request_id).person + requester = pending_requests.find!(friend_request_id).person reversed_request = accept_friend_request(friend_request_id, aspect_id) dispatch_friend_acceptance reversed_request, requester end def ignore_friend_request(friend_request_id) - request = Request.find_by_id(friend_request_id) + request = pending_requests.find!(friend_request_id) person = request.person - self.pending_requests.delete(request) + self.pending_request_ids.delete(request.id) self.save person.save diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index 6483c45fa..6e5dfbf57 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -31,17 +31,6 @@ describe Request do xml.should include user.exported_key end - it 'should allow me to see only friend requests sent to me' do - remote_person = Factory.build(:person, :diaspora_handle => "robert@grimm.com", :url => "http://king.com/") - - Request.instantiate(:into => aspect.id, :from => user.person, :to => remote_person.receive_url).save - Request.instantiate(:into => aspect.id, :from => user.person, :to => remote_person.receive_url).save - Request.instantiate(:into => aspect.id, :from => user.person, :to => remote_person.receive_url).save - Request.instantiate(:into => aspect.id, :from => remote_person, :to => user.receive_url).save - - Request.for_user(user).all.count.should == 1 - end - it 'should strip the destination url' do person_request = Request.new person_request.destination_url = " http://google.com/ " diff --git a/spec/models/user/user_friending_spec.rb b/spec/models/user/user_friending_spec.rb index 63a472031..8a4386601 100644 --- a/spec/models/user/user_friending_spec.rb +++ b/spec/models/user/user_friending_spec.rb @@ -25,7 +25,7 @@ describe Diaspora::UserModules::Friending do end context 'friend requesting' do - it "should assign a request to a aspect" do + it "should assign a request to a aspect for the user that sent it out" do aspect.requests.size.should == 0 user.send_friend_request_to(friend, aspect) @@ -34,21 +34,42 @@ describe Diaspora::UserModules::Friending do aspect.requests.size.should == 1 end - it "should be able to accept a pending friend request" do - r = Request.instantiate(:to => user.receive_url, :from => friend) - r.save + describe '#receive_friend_request' do + it 'adds a request to pending if it was not sent by user' do + r = Request.instantiate(:to => user.receive_url, :from => friend) + r.save + user.receive_friend_request(r) + user.reload.pending_requests.should include r + end + + 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) + end - proc { user.accept_friend_request(r.id, aspect.id) }.should change { - Request.for_user(user).all.count }.by(-1) end - it 'should be able to ignore a pending friend request' do - friend = Factory.create(:person) - r = Request.instantiate(:to => user.receive_url, :from => friend) - r.save + context 'received a friend request' do - proc { user.ignore_friend_request(r.id) }.should change { - Request.for_user(user).count }.by(-1) + let(:request_for_user) {Request.instantiate(:to => user.receive_url, :from => friend)} + let(:request2_for_user) {Request.instantiate(:to => user.receive_url, :from => person_one)} + before do + request_for_user.save + user.receive_friend_request(request_for_user) + user.receive_friend_request(request2_for_user) + user.reload + end + + it "should delete an accepted friend request" do + proc { user.accept_friend_request(request2_for_user.id, aspect.id) }.should change( + user.reload.pending_requests, :count ).by(-1) + end + + it 'should be able to ignore a pending friend request' do + proc { user.ignore_friend_request(request_for_user.id) }.should change ( + user.reload.pending_requests, :count ).by(-1) + end end it 'should not be able to friend request an existing friend' do @@ -168,20 +189,20 @@ describe Diaspora::UserModules::Friending do user.receive_friend_request @request person_two.destroy - user.pending_requests.size.should be 1 + user.reload.pending_requests.size.should be 1 user.friends.size.should be 0 user.receive_friend_request @request_two - user.pending_requests.size.should be 2 + user.reload.pending_requests.size.should be 2 user.friends.size.should be 0 user.accept_friend_request @request.id, aspect.id - user.pending_requests.size.should be 1 + user.reload.pending_requests.size.should be 1 user.friends.size.should be 1 user.friends.include?(person_one).should be true user.ignore_friend_request @request_two.id - user.pending_requests.size.should be 0 + user.reload.pending_requests.size.should be 0 user.friends.size.should be 1 user.friends.include?(person_two).should be false end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 29d731b29..11f18d8d2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -67,6 +67,7 @@ ImageUploader.enable_processing = false def friend_users(user1, aspect1, user2, aspect2) request = user1.send_friend_request_to(user2.person, aspect1) + user2.receive_friend_request(request) reversed_request = user2.accept_friend_request( request.id, aspect2.id) user1.reload user1.receive reversed_request.to_diaspora_xml, user2.person