From 81476025702348e2c247f5aa1bef9696cd453678 Mon Sep 17 00:00:00 2001 From: maxwell Date: Fri, 9 Jul 2010 18:10:11 -0700 Subject: [PATCH] DG MS friends now are delete --- app/models/retraction.rb | 25 ++++++++++++++++++------- app/models/user.rb | 10 +++++++++- lib/common.rb | 2 +- spec/factories.rb | 1 + spec/models/person_spec.rb | 15 +++++++++++++-- spec/models/user_spec.rb | 6 +++--- 6 files changed, 45 insertions(+), 14 deletions(-) diff --git a/app/models/retraction.rb b/app/models/retraction.rb index a4d10bb59..1ccfa33f9 100644 --- a/app/models/retraction.rb +++ b/app/models/retraction.rb @@ -2,21 +2,32 @@ class Retraction include ROXML include Diaspora::Webhooks - def self.for(post) - result = self.new - result.post_id = post.id - result.person_id = post.person.id - result + def self.for(object) + retraction = self.new + retraction.post_id= object.id + retraction.person_id = person_id_from(object) + retraction.type = object.class + retraction end xml_accessor :post_id xml_accessor :person_id + xml_accessor :type attr_accessor :post_id attr_accessor :person_id - + attr_accessor :type + def perform - Post.delete(self.post_id) + self.type.constantize.delete(self.post_id) + end + + def self.person_id_from(object) + if object.is_a? Person + object.id + else + object.person.id + end end end diff --git a/app/models/user.rb b/app/models/user.rb index a7bdcd294..872d35a48 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -46,7 +46,7 @@ class User < Person end def ignore_friend_request(friend_request_id) - request = Request.where(:id => friend_request_id).first + request = Request.first(:id => friend_request_id) person = request.person person.destroy unless person.active request.destroy @@ -61,6 +61,14 @@ class User < Person end end + def unfriend(friend_id) + bad_friend = Person.first(:id => friend_id, :active => true) + if bad_friend + Retraction.for(self).push_to_url(bad_friend.url) + bad_friend.destroy + end + end + ###Helpers############ def mine?(post) diff --git a/lib/common.rb b/lib/common.rb index c0fd6947f..a049b561f 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -76,7 +76,7 @@ module Diaspora end def people_with_permissions - Person.where( :_type => "Person" ).all + Person.friends.all end def self.build_xml_for(posts) diff --git a/spec/factories.rb b/spec/factories.rb index 5467a6b39..46e09e3dd 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -11,6 +11,7 @@ end Factory.define :person do |p| p.email "bob@aol.com" + p.active true p.sequence(:url) {|n|"http://google-#{n}.com/"} p.profile Profile.new( :first_name => "Robert", :last_name => "Grimm" ) end diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 69b5ff29a..8c6dd30bf 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -26,9 +26,9 @@ describe Person do end it 'should only return active friends' do - Factory.create(:person, :active => true) - Factory.create(:person) Factory.create(:person) + Factory.create(:person, :active => false) + Factory.create(:person, :active => false) Person.friends.all.count.should == 1 end @@ -57,5 +57,16 @@ describe Person do s.comments.count.should == 1 end + it 'should let a user unfriend another user' do + u = Factory.create(:user) + + f = Factory.create(:person, :active => true) + + + Person.friends.all.count.should == 1 + u.unfriend(f.id) + Person.friends.all.count.should == 0 + + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 201ec6318..8c0f1cf7b 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -9,7 +9,7 @@ describe User do it "should be able to accept a pending friend request" do @user = Factory.create(:user) - @friend = Factory.create(:person) + @friend = Factory.create(:person, :active => false) r = Request.instantiate(:to => @user.url, :from => @friend) r.save Person.all.count.should == 2 @@ -21,7 +21,7 @@ describe User do it 'should be able to ignore a pending friend request' do @user = Factory.create(:user) - @friend = Factory.create(:person) + @friend = Factory.create(:person, :active => false) r = Request.instantiate(:to => @user.url, :from => @friend) r.save @@ -36,7 +36,7 @@ describe User do it 'should not be able to friend request an existing friend' do @user = Factory.create(:user) - @friend = Factory.create(:person, :active => true) + @friend = Factory.create(:person) @user.send_friend_request_to( @friend.url ).should be nil end