diff --git a/app/models/user.rb b/app/models/user.rb index 7e29f84c0..c8801b87f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,10 +27,12 @@ class User < Person ######### Friend Requesting def send_friend_request_to(friend_url) - p = Request.instantiate(:to => friend_url, :from => self) - if p.save - p.push_to_url friend_url - p + unless Person.where(:url => friend_url).first + p = Request.instantiate(:to => friend_url, :from => self) + if p.save + p.push_to_url friend_url + p + end end end @@ -48,6 +50,14 @@ class User < Person request.destroy end + def ignore_friend_request(friend_request_id) + request = Request.where(:id => friend_request_id).first + person = request.person + + person.destroy unless person.active + request.destroy + end + def receive_friend_request(friend_request) if Request.where(:callback_url => friend_request.callback_url).first friend_request.activate_friend @@ -56,7 +66,6 @@ class User < Person friend_request.save end end - def mine?(post) self == post.person diff --git a/spec/lib/parser_spec.rb b/spec/lib/parser_spec.rb index f73945c50..36150b3ea 100644 --- a/spec/lib/parser_spec.rb +++ b/spec/lib/parser_spec.rb @@ -117,11 +117,11 @@ describe "parser in application helper" do xml = Request.build_xml_for [request] @person.destroy - Person.friends.all.count.should be 0 + Person.all.count.should be 1 store_objects_from_xml(xml) - Person.friends.all.count.should be 1 + Person.all.count.should be 2 - Person.friends.first.id.should == original_person_id + Person.where(:url => request.callback_url).first.id.should == original_person_id end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 0979db2bb..201ec6318 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -19,10 +19,26 @@ describe User do Person.where(:id => @friend.id).first.active.should == true end + it 'should be able to ignore a pending friend request' do + @user = Factory.create(:user) + @friend = Factory.create(:person) + r = Request.instantiate(:to => @user.url, :from => @friend) + r.save - + Person.count.should == 2 + @friend.active.should == false + @user.ignore_friend_request(r.id) + Person.count.should == 1 + Request.count.should == 0 + end + it 'should not be able to friend request an existing friend' do + @user = Factory.create(:user) + @friend = Factory.create(:person, :active => true) + + @user.send_friend_request_to( @friend.url ).should be nil + end end