From 7ace73b84bf76a1c84b1ce02c1a302b4c4e1be76 Mon Sep 17 00:00:00 2001 From: maxwell Date: Wed, 16 Feb 2011 17:14:44 -0800 Subject: [PATCH] iz ms wip fixing contactController destroy --- app/controllers/contacts_controller.rb | 10 +++++++ app/controllers/people_controller.rb | 5 ---- app/views/people/_share_with_pane.html.haml | 4 +-- app/views/shared/_contact_list.html.haml | 2 +- config/locales/diaspora/en.yml | 3 +++ lib/diaspora/user/connecting.rb | 21 ++++++++------- lib/tasks/backup.rake | 7 +++++ public/javascripts/contact-list.js | 22 ++++++++------- spec/controllers/contacts_controller_spec.rb | 28 ++++++++++++++++++-- spec/intergration/receiving_spec.rb | 8 +++--- spec/models/person_spec.rb | 4 +-- spec/models/user/connecting_spec.rb | 6 ++--- 12 files changed, 83 insertions(+), 37 deletions(-) diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb index aa64ba2c1..202c07bce 100644 --- a/app/controllers/contacts_controller.rb +++ b/app/controllers/contacts_controller.rb @@ -30,6 +30,16 @@ class ContactsController < ApplicationController end end + def destroy + contact = current_user.contacts.where(:id => params[:id]).first + if current_user.disconnect(contact) + flash[:notice] = I18n.t('contacts.destroy.success', :name => contact.person.name) + else + flash[:error] = I18n.t('contacts.destroy.failure', :name => contact.person.name) + end + redirect_to contact.person + end + private def request_to_aspect(aspect, person) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 69e8dff6b..738706647 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -76,11 +76,6 @@ class PeopleController < ApplicationController end end - def destroy - current_user.disconnect(Person.where(:id => params[:id]).first) - redirect_to root_url - end - def retrieve_remote if params[:diaspora_handle] webfinger(params[:diaspora_handle], :single_aspect_form => true) diff --git a/app/views/people/_share_with_pane.html.haml b/app/views/people/_share_with_pane.html.haml index c4ab539de..71694e59d 100644 --- a/app/views/people/_share_with_pane.html.haml +++ b/app/views/people/_share_with_pane.html.haml @@ -2,7 +2,7 @@ -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. -.aspect_list#aspects_list{:data=>{:person_id=>person.id}} +.aspect_list#aspects_list(( {:data=>{:contact_id=>contact.id} }) if contact) %ul - for aspect in aspects_with_person = render :partial => 'aspects/aspect_list_item', @@ -30,4 +30,4 @@ .right = link_to t('aspects.aspect_contacts.done_editing'), "#", :class => "button", :onClick => '$.facebox.close();' - if contact - = link_to t('people.profile_sidebar.remove_contact'), person, :confirm => t('are_you_sure'), :method => :delete + = link_to t('people.profile_sidebar.remove_contact'), contact, :confirm => t('are_you_sure'), :method => :delete diff --git a/app/views/shared/_contact_list.html.haml b/app/views/shared/_contact_list.html.haml index 63ec275bc..cebcc7ec5 100644 --- a/app/views/shared/_contact_list.html.haml +++ b/app/views/shared/_contact_list.html.haml @@ -7,7 +7,7 @@ = search_field_tag :contact_search, "", :class => 'contact_list_search', :results => 5, :placeholder => t('.all_contacts') %ul - for contact in contacts - %li{:data=>{:guid=>contact.person.id}} + %li{:data=>{:contact_id=>contact.id}} = person_image_tag contact.person %h4.name = link_to contact.person.name, contact.person diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 5063a6ec2..750b6565b 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -39,6 +39,9 @@ en: one: "1 contact" few: "%{count} contacts" other: "%{count} contacts" + destroy: + success: "Successfully disconnected from %{name}" + failure: "Failed to disconnect from %{name}" new_requests: zero: "no new requests" one: "new request!" diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index 7066854b0..177387481 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -74,18 +74,19 @@ module Diaspora end def disconnect(bad_contact) - Rails.logger.info("event=disconnect user=#{diaspora_handle} target=#{bad_contact.diaspora_handle}") + person = bad_contact.person + Rails.logger.info("event=disconnect user=#{diaspora_handle} target=#{person.diaspora_handle}") retraction = Retraction.for(self) - retraction.subscribers = [bad_contact]#HAX + retraction.subscribers = [person]#HAX Postzord::Dispatch.new(self, retraction).post remove_contact(bad_contact) end - def remove_contact(bad_person) - contact = contact_for(bad_person) - posts = raw_visible_posts.where(:person_id => bad_person.id).all + def remove_contact(contact) + bad_person_id = contact.person_id + posts = raw_visible_posts.where(:person_id => bad_person_id).all visibilities = PostVisibility.joins(:post, :aspect).where( - :posts => {:person_id => bad_person.id}, + :posts => {:person_id => bad_person_id}, :aspects => {:user_id => self.id} ) visibility_ids = visibilities.map{|v| v.id} @@ -95,12 +96,12 @@ module Diaspora post.destroy end end - raise "Contact not deleted" unless contact.destroy + contact.destroy end - def disconnected_by(bad_contact) - Rails.logger.info("event=disconnected_by user=#{diaspora_handle} target=#{bad_contact.diaspora_handle}") - remove_contact bad_contact + def disconnected_by(person) + Rails.logger.info("event=disconnected_by user=#{diaspora_handle} target=#{person.diaspora_handle}") + remove_contact(self.contact_for(person)) end def activate_contact(person, aspect) diff --git a/lib/tasks/backup.rake b/lib/tasks/backup.rake index 196456f34..2d914a8ce 100644 --- a/lib/tasks/backup.rake +++ b/lib/tasks/backup.rake @@ -4,6 +4,7 @@ namespace :backup do require 'cloudfiles' task :mysql do + NUMBER_OF_DAYS = 3 puts("event=backup status=start type=mysql") db = YAML::load(File.open(File.join(File.dirname(__FILE__), '..','..', 'config', 'database.yml'))) user = db['production']['user'] @@ -28,6 +29,12 @@ namespace :backup do puts("event=backup status=success type=mysql") `rm /tmp/backup/#{tar_name}` `rm -rf /tmp/backup/mysql/` + + files = mysql_container.objects + files.sort!.pop(NUMBER_OF_DAYS * 24) + files.each do |file| + mysql_container.delete_object(file) + end else puts("event=backup status=failure type=mysql") end diff --git a/public/javascripts/contact-list.js b/public/javascripts/contact-list.js index f85cfbb05..51bc93854 100644 --- a/public/javascripts/contact-list.js +++ b/public/javascripts/contact-list.js @@ -22,13 +22,13 @@ var List = { }); }); }, - disconnectUser: function(person_id){ + disconnectUser: function(contact_id){ $.ajax({ - url: "/people/" + person_id, + url: "/contacts/" + contact_id, type: "DELETE", success: function(){ if( $('.contact_list').length == 1){ - $('.contact_list li[data-guid='+person_id+']').fadeOut(200); + $('.contact_list li[data-contact_id='+contact_id+']').fadeOut(200); } else if($('#aspects_list').length == 1) { $.facebox.close(); }; @@ -60,16 +60,16 @@ $(document).ready(function() { $('.added').live('ajax:failure', function(data, html, xhr) { if(confirm(Diaspora.widgets.i18n.t('shared.contact_list.cannot_remove'))){ - var person_id; + var contact_id; if( $('.contact_list').length == 1){ - person_id = $(this).parents('li').attr("data-guid"); - $('.contact_list li[data-guid='+person_id+']').fadeOut(200); + contact_id = $(this).parents('li').attr("data-contact_id"); + $('.contact_list li[data-contact_id='+contact_id+']').fadeOut(200); } else if($('#aspects_list').length == 1) { - person_id = $(this).parents('#aspects_list').attr("data-person_id"); + contact_id = $(this).parents('#aspects_list').attr("data-contact_id"); }; - List.disconnectUser(person_id); + List.disconnectUser(contact_id); }; $(this).fadeTo(200,1); }); @@ -88,7 +88,11 @@ $(document).ready(function() { $(".badges").prepend(json.badge_html); $(this).parent().html(json.button_html); - $('.aspect_list ul').find('.add').each(function(a,b){$(b).attr('href', $(b).attr('href').replace('contacts','aspect_memberships'));}) + if($('#aspects_list').length == 1) { + $('.aspect_list').attr('data-contact_id', json.contact_id); + alert('^- TODO'); + $('.aspect_list ul').find('.add').each(function(a,b){$(b).attr('href', $(b).attr('href').replace('contacts','aspect_memberships'));}) + }; $(this).fadeTo(200,1); }); diff --git a/spec/controllers/contacts_controller_spec.rb b/spec/controllers/contacts_controller_spec.rb index 59c2271a6..e2ea06747 100644 --- a/spec/controllers/contacts_controller_spec.rb +++ b/spec/controllers/contacts_controller_spec.rb @@ -24,7 +24,7 @@ describe ContactsController do request.env["HTTP_REFERER"] = 'http://' + request.host end - describe 'new' do + describe '#new' do it 'succeeds' do pending "This is going to be new request" @@ -33,7 +33,7 @@ describe ContactsController do end end - describe 'create' do + describe '#create' do context 'with an incoming request' do before do @user3 = Factory.create(:user) @@ -74,4 +74,28 @@ describe ContactsController do end end end + + describe '#destroy' do + it 'disconnects from the person' do + @user.should_receive(:disconnect).with(@contact) + delete :destroy, :id => @contact.id + end + + it 'flases success if the contact is not destroyed' do + @user.stub!(:disconnect).and_return(true) + delete :destroy, :id => @contact.id + flash[:notice].should_not be_empty + end + + it 'flases failure if the contact is not destroyed' do + @user.stub!(:disconnect).and_return(false) + delete :destroy, :id => @contact.id + flash[:error].should_not be_empty + end + + it 'redirects back to the person page' do + delete :destroy, :id => @contact.id + response.should redirect_to(@contact.person) + end + end end diff --git a/spec/intergration/receiving_spec.rb b/spec/intergration/receiving_spec.rb index 559d1b5dd..7c89021a6 100644 --- a/spec/intergration/receiving_spec.rb +++ b/spec/intergration/receiving_spec.rb @@ -20,6 +20,8 @@ describe 'a user receives a post' do @user3 = eve @aspect3 = @user3.aspects.first + + @contact = @user1.contact_for(@user2.person) end it 'streams only one message to the everyone aspect when a multi-aspected contacts posts' do @@ -89,7 +91,7 @@ describe 'a user receives a post' do end it 'removes posts upon disconnecting' do - @user1.disconnect(@user2.person) + @user1.disconnect(@contact) @user1.reload @user1.raw_visible_posts.should_not include @status_message end @@ -128,7 +130,7 @@ describe 'a user receives a post' do @status_message.reload @status_message.user_refs.should == 3 - @user1.disconnect(@user2.person) + @user1.disconnect(@contact) @status_message.reload @status_message.user_refs.should == 2 end @@ -146,7 +148,7 @@ describe 'a user receives a post' do @status_message.post_visibilities.reset @status_message.user_refs.should == 4 - @user1.disconnect(@user2.person) + @user1.disconnect(@contact) @status_message.post_visibilities.reset @status_message.user_refs.should == 3 end diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index ebf8ed23e..319c5eb72 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -138,14 +138,14 @@ describe Person do it 'should not delete an orphaned contact' do @user.activate_contact(@person, @aspect) - lambda {@user.disconnect(@person)}.should_not change(Person, :count) + lambda {@user.disconnect(@user.contact_for(@person))}.should_not change(Person, :count) end it 'should not delete an un-orphaned contact' do @user.activate_contact(@person, @aspect) @user2.activate_contact(@person, @aspect2) - lambda {@user.disconnect(@person)}.should_not change(Person, :count) + lambda {@user.disconnect(@user.contact_for(@person))}.should_not change(Person, :count) end end diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index 09ef01297..4bd35dafa 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -250,7 +250,7 @@ describe Diaspora::UserModules::Connecting do it 'should disconnect the other user on the same seed' do lambda { - user2.disconnect user.person }.should change { + user2.disconnect user2.contact_for(user.person) }.should change { user2.reload.contacts.count }.by(-1) aspect2.reload.contacts.count.should == 0 end @@ -280,14 +280,14 @@ describe Diaspora::UserModules::Connecting do it "deletes the disconnected user's posts from visible_posts" do user2.reload.raw_visible_posts.include?(@message).should be_true - user2.disconnect user.person + user2.disconnect user2.contact_for(user.person) user2.reload.raw_visible_posts.include?(@message).should be_false end it "deletes the disconnected user's posts from the aspect's posts" do Post.count.should == 1 aspect2.reload.posts.include?(@message).should be_true - user2.disconnect user.person + user2.disconnect user2.contact_for(user.person) aspect2.reload.posts.include?(@message).should be_false Post.count.should == 1 end