diff --git a/.rspec b/.rspec index f5a15e39ed65d4d5d0e1b53cfdc952e196f72d67..3707d59bec21622343efeba1583551ee099aa8b3 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,4 @@ --format Fuubar --profile ---color \ No newline at end of file +--color +--tag ~performance diff --git a/app/controllers/aspect_memberships_controller.rb b/app/controllers/aspect_memberships_controller.rb index 56ba76dba6e012dddb989a6d9f96f93a8450bb2d..19f3717221cad9e556f27c51d218c01b8ae4123f 100644 --- a/app/controllers/aspect_memberships_controller.rb +++ b/app/controllers/aspect_memberships_controller.rb @@ -64,7 +64,6 @@ class AspectMembershipsController < ApplicationController end end - def update @person = Person.find(params[:person_id]) @from_aspect = current_user.aspects.where(:id => params[:aspect_id]).first diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index c17943342fb23a0795a9809cecdc81375229fca5..694e3533e26ad7cb4ae4507f42aa6839b0387322 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -78,7 +78,7 @@ class PeopleController < ApplicationController @contacts_of_contact = [] end - if (@person != current_user.person) && (!@contact || @contact.pending) + if (@person != current_user.person) && (!@contact || @contact.mutual) @commenting_disabled = true else @commenting_disabled = false diff --git a/app/models/aspect.rb b/app/models/aspect.rb index 0fd4a82534114d325fef151dd7132b831eb2e8b2..0955e637f4fadf81aafc2ccaa43c18af9d19bc3a 100644 --- a/app/models/aspect.rb +++ b/app/models/aspect.rb @@ -5,7 +5,7 @@ class Aspect < ActiveRecord::Base belongs_to :user - has_many :aspect_memberships + has_many :aspect_memberships, :dependent => :destroy has_many :contacts, :through => :aspect_memberships has_many :aspect_visibilities diff --git a/app/models/aspect_membership.rb b/app/models/aspect_membership.rb index e64baaef807e979096d8032f397c0e829752ae44..f79d3152068404276147b34a45e4bf390a008ec6 100644 --- a/app/models/aspect_membership.rb +++ b/app/models/aspect_membership.rb @@ -9,14 +9,4 @@ class AspectMembership < ActiveRecord::Base has_one :user, :through => :contact has_one :person, :through => :contact - before_destroy :ensure_membership - - def ensure_membership - if self.contact.aspect_memberships.count == 1 - errors[:base] << I18n.t('shared.contact_list.cannot_remove') - false - else - true - end - end end diff --git a/app/models/contact.rb b/app/models/contact.rb index bfbdccc7be2500ffdbd4936b5ee11d373418d252..cfab568a70775adf1e78ceee02c1dc02c923d170 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -3,8 +3,6 @@ # the COPYRIGHT file. class Contact < ActiveRecord::Base - default_scope where(:pending => false) - belongs_to :user validates_presence_of :user diff --git a/app/models/invitation.rb b/app/models/invitation.rb index aad3b1ab63677444ab480ffb0b2634d2fba317fb..e85ed1d3355967789b3f72d892f5f2d2cb767291 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -19,7 +19,7 @@ class Invitation < ActiveRecord::Base if opts[:from].contact_for(opts[:from].person) raise "You are already connceted to this person" elsif not existing_user.invited? - opts[:from].send_contact_request_to(existing_user.person, opts[:into]) + opts[:from].share_with(existing_user.person, opts[:into]) return elsif Invitation.where(:sender_id => opts[:from].id, :recipient_id => existing_user.id).first raise "You already invited this person" diff --git a/app/models/post_visibility.rb b/app/models/post_visibility.rb index 060b16fee0de50c5ef968e7daa8f9a4ddabdcf6c..69122f04672ffe81187dadf0a65b5be7842829ac 100644 --- a/app/models/post_visibility.rb +++ b/app/models/post_visibility.rb @@ -3,8 +3,6 @@ # the COPYRIGHT file. class PostVisibility < ActiveRecord::Base - default_scope where(:hidden => false) - belongs_to :contact belongs_to :post end diff --git a/app/models/request.rb b/app/models/request.rb index ff0542f2cc7f2cc5ead90a872d23766d4cfd28ce..7cfa8495a4e09cf8fc1679dce80f3d2ca0560dae 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -66,10 +66,7 @@ class Request < ActiveRecord::Base def receive(user, person) Rails.logger.info("event=receive payload_type=request sender=#{self.sender} to=#{self.recipient}") - - user.contacts.create(:person_id => person.id, :pending => false) - - #user.receive_contact_request(self) + user.contacts.create(:person_id => person.id) self.save self end diff --git a/app/models/user.rb b/app/models/user.rb index 9f65fe2180c4c436e6de3066b90b4feecc1725c0..b31108d19b5c5a299b04ea55a06e7c49201ac775 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -328,15 +328,15 @@ class User < ActiveRecord::Base end def disconnect_everyone - Contact.unscoped.where(:user_id => self.id).each { |contact| + self.contacts.each do |contact| unless contact.person.owner.nil? - contact.person.owner.disconnected_by self.person - remove_contact(contact) + contact.person.owner.disconnected_by(self.person) + remove_contact(contact, :force => true) else - self.disconnect contact + self.disconnect(contact) end - } - self.aspects.delete_all + end + self.aspects.destroy_all end def remove_mentions diff --git a/app/views/aspects/_aspect_listings.haml b/app/views/aspects/_aspect_listings.haml index 33d410d93b604ed397116e0da8375ddbf0ae6bc5..cda632f9269cd9e0282272ec6027db11edd0df1c 100644 --- a/app/views/aspects/_aspect_listings.haml +++ b/app/views/aspects/_aspect_listings.haml @@ -10,4 +10,4 @@ %ul - for aspect in aspects - = render 'aspects/aspect', :aspect => aspect, :contacts => aspect.contacts.reject{|x| x.pending == true} + = render 'aspects/aspect', :aspect => aspect, :contacts => aspect.contacts diff --git a/app/views/people/_aspect_list.haml b/app/views/people/_aspect_list.haml index b6297eb9e38a0cc6c2e8b281b3e73a5e75feb1fb..93a6998a97731d025c9160ca4b1a3a75ec3e69e4 100644 --- a/app/views/people/_aspect_list.haml +++ b/app/views/people/_aspect_list.haml @@ -5,11 +5,9 @@ - content_for :head do =javascript_include_tag 'contact-list' .aspects - - if !contact || !contact.persisted? - - elsif contact.pending - %h4 - = t('people.person.pending_request') - - else + + / TODO(*) add following method in contact + - if contact && contact.persisted? .badges{:class => ("hidden" if !contact.persisted?)} = aspect_badges(aspects_with_person, :link => true) %p diff --git a/app/views/people/_profile_sidebar.html.haml b/app/views/people/_profile_sidebar.html.haml index af8fc81dcff27d497aba520384d57a89f7fb43a5..326c8da3666145ac489312de24a3a8a57b746c27 100644 --- a/app/views/people/_profile_sidebar.html.haml +++ b/app/views/people/_profile_sidebar.html.haml @@ -35,7 +35,7 @@ %br %hr{:style=>"width:300px;"} - -if user_signed_in? && ((contact.persisted? && !contact.pending?) || person == current_user.person || @incoming_request) + -if user_signed_in? && ((contact.persisted? && contact.mutual?) || person == current_user.person || @incoming_request) %ul#profile_information - unless person.profile.bio.blank? %li diff --git a/app/views/people/show.html.haml b/app/views/people/show.html.haml index 401d25808ed5fd05f5b04deaeeadc0de26eaf08f..9fc24bb1e0a8410c9050d9110c07bb1ac1ef9de3 100644 --- a/app/views/people/show.html.haml +++ b/app/views/people/show.html.haml @@ -36,7 +36,7 @@ = javascript_tag "$(document).ready(function() {jQuery.facebox({ ajax: '#{new_contact_path(:person_id => @person.id)}' });});" - else - - if user_signed_in? && @contact.person && @contact.pending? == false + - if user_signed_in? && @contact.mutual? .right = link_to t('.mention'), new_status_message_path(:person_id => @person.id), :class => 'button', :rel => 'facebox' = link_to t('.message'), new_conversation_path(:contact_id => @contact.id, :name => @contact.person.name, :contact_id => @contact.id), :class => 'button', :rel => 'facebox' diff --git a/app/views/services/_remote_friend.html.haml b/app/views/services/_remote_friend.html.haml index cc37bb47d523399682e2f6e4c8b7728a134a7dad..d3d46b4fc1025e6e491fed81352bc963805fa1a4 100644 --- a/app/views/services/_remote_friend.html.haml +++ b/app/views/services/_remote_friend.html.haml @@ -1,7 +1,8 @@ %li.remote_friend{:id => "uid_" + friend.uid, :uid => friend.uid} .right - -if friend.contact && !friend.contact.pending - = t('people.person.already_connected') + -if friend.contact + / TODO(*) add following method in Contact + sharing - elsif friend.invitation_id = t('invitations.new.already_invited') %br diff --git a/db/migrate/20110405171412_contact_remove_pending_add_mutual.rb b/db/migrate/20110405171412_contact_remove_pending_add_mutual.rb new file mode 100644 index 0000000000000000000000000000000000000000..5518561cdde2eb4c3895b73f87e1208b8e8dbef9 --- /dev/null +++ b/db/migrate/20110405171412_contact_remove_pending_add_mutual.rb @@ -0,0 +1,42 @@ +class ContactRemovePendingAddMutual < ActiveRecord::Migration + def self.up + add_column :contacts, :mutual, :boolean, :default => false, :null => false + + execute( <<SQL + UPDATE contacts + SET contacts.mutual = true + WHERE contacts.pending = false +SQL +) + + remove_foreign_key "contacts", "people" + remove_index :contacts, [:person_id, :pending] + remove_index :contacts, [:user_id, :pending] + + add_index :contacts, :person_id + add_foreign_key "contacts", "people", :name => "contacts_person_id_fk", :dependent => :delete + + remove_column :contacts, :pending + end + + def self.down + + remove_foreign_key "contacts", "people" + remove_index :contacts, :person_id + + add_column :contacts, :pending, :default => true, :null => false + add_index :contacts, [:user_id, :pending] + + add_index :contacts, [:person_id, :pending] + add_foreign_key "contacts", "people", :name => "contacts_person_id_fk", :dependent => :delete + + execute( <<SQL + UPDATE contacts + SET contacts.pending = false + WHERE contacts.mutual = true +SQL +) + + remove_column :contacts, :mutual + end +end diff --git a/db/schema.rb b/db/schema.rb index 8d57025da488099f40db771b70ee8e6d43c4f8ba..db74ad98ea831151f52c601a5594ef87e508c82b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110331222629) do +ActiveRecord::Schema.define(:version => 20110405171412) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -68,17 +68,16 @@ ActiveRecord::Schema.define(:version => 20110331222629) do add_index "comments", ["post_id"], :name => "index_comments_on_post_id" create_table "contacts", :force => true do |t| - t.integer "user_id", :null => false - t.integer "person_id", :null => false - t.boolean "pending", :default => true, :null => false + t.integer "user_id", :null => false + t.integer "person_id", :null => false t.datetime "created_at" t.datetime "updated_at" t.string "mongo_id" + t.boolean "mutual", :default => false, :null => false end add_index "contacts", ["mongo_id"], :name => "index_contacts_on_mongo_id" - add_index "contacts", ["person_id", "pending"], :name => "index_contacts_on_person_id_and_pending" - add_index "contacts", ["user_id", "pending"], :name => "index_contacts_on_user_id_and_pending" + add_index "contacts", ["person_id"], :name => "index_contacts_on_person_id" add_index "contacts", ["user_id", "person_id"], :name => "index_contacts_on_user_id_and_person_id", :unique => true create_table "conversation_visibilities", :force => true do |t| @@ -250,6 +249,7 @@ ActiveRecord::Schema.define(:version => 20110331222629) do add_index "posts", ["status_message_id", "pending"], :name => "index_posts_on_status_message_id_and_pending" add_index "posts", ["status_message_id"], :name => "index_posts_on_status_message_id" add_index "posts", ["type", "pending", "id"], :name => "index_posts_on_type_and_pending_and_id" + add_index "posts", ["type"], :name => "index_posts_on_type" create_table "profiles", :force => true do |t| t.string "diaspora_handle" diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index 3c5d25e8ea1f69d082f792418e703641867373f7..94e0be86a13c2bae45dee5d0444de767e1004bc9 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -5,7 +5,6 @@ module Diaspora module UserModules module Connecting - def share_with(person, aspect) contact = self.contacts.find_or_initialize_by_person_id(person.id) unless contact.persisted? @@ -21,87 +20,32 @@ module Diaspora contact end - def receive_contact_request(request) - self.contacts.find_or_create_by_person_id(request.sender.id) + contact = self.contacts.find_or_initialize_by_person_id(request.sender.id) + if contact.persisted? && !contact.mutual? + contact.mutual = true + end + contact.save request end -=begin - def send_contact_request_to(desired_contact, aspect) - self.contacts.new(:person => desired_contact, - :pending => true) - - contact.aspects << aspect + def remove_contact(contact, opts={:force => false}) + posts = contact.posts.all - if contact.save! - request = contact.dispatch_request - request + if !contact.mutual || opts[:force] + contact.destroy else - nil - end - end - - def dispatch_contact_acceptance(request, requester) - Postzord::Dispatch.new(self, request).post - request.destroy unless request.sender.owner - end - - def accept_contact_request(request, aspect) - activate_contact(request.sender, aspect) - - if notification = Notification.where(:target_id=>request.id).first - notification.update_attributes(:unread=>false) + contact.update_attributes(:mutual => false) + contact.post_visibilities.destroy_all + contact.aspect_memberships.destroy_all end - request.destroy - request.reverse_for(self) - end - - def accept_and_respond(contact_request_id, aspect_id) - request = Request.where(:recipient_id => self.person.id, :id => contact_request_id).first - requester = request.sender - reversed_request = accept_contact_request(request, aspects.where(:id => aspect_id).first ) - dispatch_contact_acceptance reversed_request, requester - end - - def ignore_contact_request(contact_request_id) - request = Request.where(:recipient_id => self.person.id, :id => contact_request_id).first - request.destroy - end - - def receive_contact_request(contact_request) - #response from a contact request you sent - if original_contact = self.contact_for(contact_request.sender) - receive_request_acceptance(contact_request, original_contact) - #this is a new contact request - elsif contact_request.sender != self.person - if contact_request.save! - Rails.logger.info("event=contact_request status=received_new_request from=#{contact_request.sender.diaspora_handle} to=#{self.diaspora_handle}") + posts.each do |p| + if p.user_refs < 1 + p.destroy end - else - Rails.logger.info "event=contact_request status=abort from=#{contact_request.sender.diaspora_handle} to=#{self.diaspora_handle} reason=self-love" - return nil end - contact_request - end - - def receive_request_acceptance(received_request, contact) - contact.pending = false - contact.save - Rails.logger.info("event=contact_request status=received_acceptance from=#{received_request.sender.diaspora_handle} to=#{self.diaspora_handle}") - - received_request.destroy - self.save - end - - def activate_contact(person, aspect) - new_contact = Contact.create!(:user => self, - :person => person, - :aspects => [aspect], - :pending => false) end -=end def disconnect(bad_contact) person = bad_contact.person @@ -112,24 +56,12 @@ module Diaspora remove_contact(bad_contact) end - def remove_contact(contact) - bad_person_id = contact.person_id - posts = contact.posts.all - contact.destroy - posts.each do |post| - if post.user_refs < 1 - post.destroy - end - end - end - def disconnected_by(person) Rails.logger.info("event=disconnected_by user=#{diaspora_handle} target=#{person.diaspora_handle}") if contact = self.contact_for(person) remove_contact(contact) end end - end end end diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index 2d848f18ae6622e501e74474d70fbff8e7384513..7bf1e2f25071cc53a2072f0f8b561cf305bcbbec 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -48,7 +48,7 @@ module Diaspora end def contact_for_person_id(person_id) - Contact.unscoped.where(:user_id => self.id, :person_id => person_id).includes(:person => :profile).first if person_id + Contact.where(:user_id => self.id, :person_id => person_id).includes(:person => :profile).first end def people_in_aspects(requested_aspects, opts={}) diff --git a/spec/controllers/aspect_memberships_controller_spec.rb b/spec/controllers/aspect_memberships_controller_spec.rb index 828eb9c44795a021cda682a9be5d2df4aa4d88c2..6a660f926dd35b7e27c1839c8a1f3c43e1ab2c7e 100644 --- a/spec/controllers/aspect_memberships_controller_spec.rb +++ b/spec/controllers/aspect_memberships_controller_spec.rb @@ -34,7 +34,6 @@ describe AspectMembershipsController do end end - describe "#destroy" do it 'removes contacts from an aspect' do @user.add_contact_to_aspect(@contact, @aspect1) @@ -47,17 +46,6 @@ describe AspectMembershipsController do @aspect0.contacts.include?(@contact).should be false end - describe "#update" do - it 'calls the move_contact method' do - @controller.stub!(:current_user).and_return(@user) - @user.should_receive(:move_contact) - put :update, :id => 123, - :person_id => @user.person.id, - :aspect_id => @aspect0.id, - :to => @aspect1.id - end - end - context 'aspect membership does not exist' do it 'person does not exist' do delete :destroy, @@ -77,14 +65,16 @@ describe AspectMembershipsController do response.body.should include "Could not find the selected person in that aspect" end end + end - it 'has the error of cannot delete contact from last aspect if its the last aspect' do - delete :destroy, - :format => 'js', :id => 123, - :person_id => @user2.person.id, - :aspect_id => @aspect0.id - response.should_not be_success - response.body.should include "Cannot remove person from last aspect" + describe "#update" do + it 'calls the move_contact method' do + @controller.stub!(:current_user).and_return(@user) + @user.should_receive(:move_contact) + put :update, :id => 123, + :person_id => @user.person.id, + :aspect_id => @aspect0.id, + :to => @aspect1.id end end end diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index 8e5b0f10fe7ae0fee2be6056124b3f6a4a71f73f..25ca2d7db05fb853a8cf37a9d928834ab14db034 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -9,13 +9,13 @@ describe AspectsController do render_views before do - @bob = bob + @bob = bob @alice = alice @alice.getting_started = false @alice.save sign_in :user, @alice - @alices_aspect_1 = @alice.aspects.first - @alices_aspect_2 = @alice.aspects.create(:name => "another aspect") + @alices_aspect_1 = @alice.aspects.first + @alices_aspect_2 = @alice.aspects.create(:name => "another aspect") @controller.stub(:current_user).and_return(@alice) request.env["HTTP_REFERER"] = 'http://' + request.host @@ -142,7 +142,7 @@ describe AspectsController do get :index assigns(:posts).should == @posts.reverse get :index, :sort_order => "updated_at" - assigns(:posts).should == @posts + assigns(:posts).map(&:id).should == @posts.map(&:id) end it "doesn't allow SQL injection" do @@ -232,6 +232,7 @@ describe AspectsController do get :manage response.should be_success end + it "performs reasonably", :performance => true do require 'benchmark' 8.times do |n| @@ -245,32 +246,31 @@ describe AspectsController do get :manage }.should < 4.5 end + it "assigns aspect to manage" do get :manage assigns(:aspect).should == :manage end - it "assigns contacts to only non-pending" do - contact = @alice.contact_for(bob.person) - Contact.unscoped.where(:user_id => @alice.id).count.should == 1 - @alice.send_contact_request_to(Factory(:user).person, @alices_aspect_1) - Contact.unscoped.where(:user_id => @alice.id).count.should == 2 + it "assigns contacts" do get :manage contacts = assigns(:contacts) - contacts.count.should == 1 - contacts.first.should == contact + contacts.to_set.should == alice.contacts.to_set end end describe "#update" do - before do + it "doesn't overwrite name" do + @alices_aspect_1 = @alice.aspects.create(:name => "Bruisers") - end - it "doesn't overwrite random attributes" do - new_user = Factory.create :user - params = {"name" => "Bruisers"} + + new_user = Factory.create :user + params = {"name" => "Bruisers"} + params[:user_id] = new_user.id - put('update', :id => @alices_aspect_1.id, "aspect" => params) + + put(:update, :id => @alices_aspect_1.id, :aspect => params) + Aspect.find(@alices_aspect_1.id).user_id.should == @alice.id end end diff --git a/spec/controllers/services_controller_spec.rb b/spec/controllers/services_controller_spec.rb index 1c50558b6451a5f5db551b2f5dc2132885e31055..b6136cbf0c2c2c5911a72377592bb4fbb5990504 100644 --- a/spec/controllers/services_controller_spec.rb +++ b/spec/controllers/services_controller_spec.rb @@ -39,7 +39,7 @@ describe ServicesController do end describe '#create' do - it 'creates a new OmniauthService' do + it 'creates a new OmniauthService' do request.env['omniauth.auth'] = omniauth_auth lambda{ post :create, :provider => 'twitter' diff --git a/spec/helper_methods.rb b/spec/helper_methods.rb index 865268eecfc8c3618c30d340bd7c3a40cd18b465..2f992628dcbef04e2be98bd7cf2e0d9337a73591 100644 --- a/spec/helper_methods.rb +++ b/spec/helper_methods.rb @@ -5,15 +5,11 @@ module HelperMethods def connect_users(user1, aspect1, user2, aspect2) user1.contacts.create!(:person => user2.person, :aspects => [aspect1], - :pending => false) + :mutual => true) user2.contacts.create!(:person => user1.person, :aspects => [aspect2], - :pending => false) - user1.reload - user2.reload - aspect1.reload - aspect2.reload + :mutual => true) end def stub_success(address = 'abc@example.com', opts = {}) diff --git a/spec/integration/receiving_spec.rb b/spec/integration/receiving_spec.rb index 8c0d4f22f316c53b25a62252e26d6a56bc046f20..eadf17c4054638f233ed6305e5c30c3e498b9034 100644 --- a/spec/integration/receiving_spec.rb +++ b/spec/integration/receiving_spec.rb @@ -83,7 +83,7 @@ describe 'a user receives a post' do it 'notifies users when receiving a mention in a post from a remote user' do @remote_person = Factory.create(:person, :diaspora_handle => "foobar@foobar.com") - Contact.create!(:user => alice, :person => @remote_person, :aspects => [@aspect], :pending => false) + Contact.create!(:user => alice, :person => @remote_person, :aspects => [@aspect]) Notification.should_receive(:notify).with(alice, anything(), @remote_person) @@ -153,8 +153,8 @@ describe 'a user receives a post' do end it "adds a received post to the the contact" do - alice.raw_visible_posts.include?(@status_message).should be_true - @contact.posts.include?(@status_message).should be_true + alice.raw_visible_posts.should include(@status_message) + @contact.posts.should include(@status_message) end it 'removes posts upon disconnecting' do @@ -177,7 +177,7 @@ describe 'a user receives a post' do @post.post_visibilities.reset end - it 'deletes a post if the noone links to it' do + it 'deletes a post if the no one links to it' do lambda { alice.disconnected_by(@person) }.should change(Post, :count).by(-1) diff --git a/spec/models/aspect_membership_spec.rb b/spec/models/aspect_membership_spec.rb index efb6be88073688043b72a089294e2d859ddaa50d..12f1c7fc405e2dfa1326def4f8ab2752d52bd510 100644 --- a/spec/models/aspect_membership_spec.rb +++ b/spec/models/aspect_membership_spec.rb @@ -5,30 +5,15 @@ require 'spec_helper' describe AspectMembership do - before do - @user = alice - @user2 = bob - @aspect = @user.aspects.create(:name => 'Boozers') - @contact = @user.contact_for(@user2.person) - end - it 'has an aspect' do - am = AspectMembership.new(:aspect => @aspect) - am.aspect.should == @aspect - end + describe '#before_delete' do + it 'calls disconnect' do + pending + alice.should_receive(:disconnect).with(alice.contact_for(bob)) - it 'has a contact' do - am = AspectMembership.new(:contact => @contact) - am.contact.should == @contact - end - - context 'validations' do - describe '#ensure_membership' do - it 'does not destroy from the final aspect' do - am = @contact.aspect_memberships.first - am.destroy - am.errors.should_not be_empty - end + alice.aspects.create(:name => "two") + alice.aspects.first.destroy end end + end diff --git a/spec/models/invitation_spec.rb b/spec/models/invitation_spec.rb index 9434935b019dbfb95d5e7896d5bb06997e0bce1b..1a0404799321bfd090963ad404da18b8f3309305 100644 --- a/spec/models/invitation_spec.rb +++ b/spec/models/invitation_spec.rb @@ -7,7 +7,6 @@ require 'spec_helper' describe Invitation do let(:user) { alice } let(:aspect) { user.aspects.first } - let(:user2) { eve } before do user.invites = 20 @@ -18,11 +17,11 @@ describe Invitation do describe 'validations' do before do aspect - @invitation = Invitation.new(:sender => user, :recipient => user2, :aspect => aspect) + @invitation = Invitation.new(:sender => user, :recipient => eve, :aspect => aspect) end it 'is valid' do @invitation.sender.should == user - @invitation.recipient.should == user2 + @invitation.recipient.should == eve @invitation.aspect.should == aspect @invitation.should be_valid end @@ -41,7 +40,7 @@ describe Invitation do end it 'has a message' do - @invitation = Invitation.new(:sender => user, :recipient => user2, :aspect => aspect) + @invitation = Invitation.new(:sender => user, :recipient => eve, :aspect => aspect) @invitation.message = "!" @invitation.message.should == "!" end @@ -54,7 +53,7 @@ describe Invitation do @identifier = "maggie@example.org" inv.invitation_identifier.should == @identifier inv.invitation_service.should == 'email' - inv.persisted?.should be_false + inv.should_not be_persisted lambda { inv.reload }.should raise_error ActiveRecord::RecordNotFound @@ -162,12 +161,8 @@ describe Invitation do end it 'sends a contact request to a user with that email into the aspect' do - user2 - user.should_receive(:send_contact_request_to) { |a, b| - a.should == user2.person - b.should == aspect - } - Invitation.invite(:from => user, :service => 'email', :identifier => user2.email, :into => aspect) + user.should_receive(:share_with).with(eve.person, aspect) + Invitation.invite(:from => user, :service => 'email', :identifier => eve.email, :into => aspect) end it 'decrements the invite count of the from user' do @@ -322,10 +317,10 @@ describe Invitation do }.should change(Invitation, :count).by(-1) end - it 'creates a contact for the inviter' do + it 'creates a contact for the inviter and invitee' do lambda { @invitation.share_with! - }.should change(Contact.unscoped, :count).by(1) + }.should change(Contact, :count).by(2) end end end diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 64ffb55af7be18525d77ed9e44b7fe00529ac9a9..913093690c1d4f549c0690e1eb54f579f5805a6a 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -301,7 +301,7 @@ describe Person do @casey_grippi.profile.first_name = "AAA" @casey_grippi.profile.save - requestor.send_contact_request_to(@user.person, requestor.aspects.first) + requestor.share_with(@user.person, requestor.aspects.first) people = Person.search("AAA", @user) people.map{|p| p.name}.should == [requestor.person, @yevgeniy_dodis, @robert_grimm, @casey_grippi, @eugene_weinstein].map{|p|p.name} end diff --git a/spec/models/post_visibility_spec.rb b/spec/models/post_visibility_spec.rb deleted file mode 100644 index 2c3fcfd0aab054253d9c3e09c06ff481a332b55f..0000000000000000000000000000000000000000 --- a/spec/models/post_visibility_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright (c) 2010, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. -# -require 'spec_helper' - -describe PostVisibility do - before do - @alice = alice - @bob = bob - - @status = @alice.post(:status_message, :text => "hello", :public => true, :to => @alice.aspects.first) - @vis = @status.post_visibilities.first - @vis.hidden = true - @vis.save - end - - it 'is default scoped to not-hidden' do - PostVisibility.where(:id => @vis.id).should == [] - PostVisibility.unscoped.where(:id => @vis.id).should == [@vis] - end -end diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index fc50dd0294bda57d0b2c64298bf4c072117a3b0d..c1107095ec679fe0b7c66885bba2f2a7beb8cb91 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -56,7 +56,8 @@ describe Request do end it 'returns request_accepted' do - @user.contacts.create(:person_id => @person.id, :pending => true) + pending 'TODO(*) take out request accepted' + @user.contacts.create(:person_id => @person.id) @request.notification_type(@user, @person).should == Notifications::RequestAccepted end diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index ba5d5cf4ba105cd966114ded118592b0ec5ce6de..7f0eda4a8c906c973af859636df6ceb5529fc5a8 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -22,23 +22,33 @@ describe Diaspora::UserModules::Connecting do @r = Request.diaspora_initialize(:to => alice.person, :from => person) end - it 'creates no contact' do + it 'creates a contact' do lambda { received_req = @r.receive(alice, person_one) }.should change(Contact, :count).by(1) end end - describe '#receive_request_acceptance' do + describe '#receive_contact_request' do before do - @original_request = alice.send_contact_request_to(eve.person, aspect) - @acceptance = @original_request.reverse_for(eve) + @request = Request.new(:sender => eve.person, :recipient => alice.person) end - it 'connects to the acceptor' do - alice.receive_contact_request(@acceptance) - alice.contact_for(eve.person).should_not be_nil + it 'sets mutual on an existing contact' do + alice.share_with(eve.person, aspect) + lambda{ + alice.receive_contact_request(@request) + }.should change{ + alice.contacts.find_by_person_id(eve.person.id).mutual + }.from(false).to(true) + end + + it 'does not set mutual' do + alice.receive_contact_request(@request) + alice.contacts.find_by_person_id(eve.person.id).should_not be_mutual end - it 'deletes the acceptance' do + + it 'doesnt set mutual on a contact' do + pending alice.receive_contact_request(@acceptance) Request.where(:sender_id => eve.person.id, :recipient_id => alice.person.id).should be_empty end @@ -64,46 +74,65 @@ describe Diaspora::UserModules::Connecting do describe 'disconnecting' do describe '#remove_contact' do - it 'should remove the contact from all aspects they are in' do - contact = alice.contact_for(bob.person) - new_aspect = alice.aspects.create(:name => 'new') - alice.add_contact_to_aspect( contact, new_aspect) + it 'removed non mutual contacts' do + alice.share_with(eve.person, alice.aspects.first) + lambda { + alice.remove_contact alice.contact_for(eve.person) + }.should change { + alice.contacts(true).count + }.by(-1) + end - lambda { alice.remove_contact(contact) }.should change( - contact.aspects, :count).from(2).to(0) + it 'removes a contacts mutual flag' do + lambda{ + bob.remove_contact(bob.contact_for(alice.person)) + }.should change { + bob.contacts.find_by_person_id(alice.person.id).mutual + }.from(true).to(false) end - context 'with a post' do - it "deletes the disconnected user's posts from visible_posts" do - StatusMessage.delete_all - message = alice.post(:status_message, :text => "hi", :to => alice.aspects.first.id) - bob.reload.raw_visible_posts.include?(message).should be_true - bob.disconnect bob.contact_for(alice.person) - bob.reload.raw_visible_posts.include?(message).should be_false - end + it "deletes the disconnected user's posts from visible_posts" do + StatusMessage.delete_all + message = alice.post(:status_message, :text => "hi", :to => alice.aspects.first.id) + + bob.reload.raw_visible_posts.should include(message) + bob.disconnect bob.contact_for(alice.person) + bob.reload.raw_visible_posts.should_not include(message) end - end - describe '#disconnected_by' do - it 'removes a contacts mutual flag' do - pending 'needs migration' - alice.share_with(eve.person, alice.aspects.first) + it 'should remove the contact from all aspects they are in' do + contact = alice.contact_for(bob.person) + new_aspect = alice.aspects.create(:name => 'new') + alice.add_contact_to_aspect(contact, new_aspect) - alice.contacts.where(:person_id => eve.person.id).mutual.should be_true - eve.disconnected_by(alice.person) - alice.contacts.where(:person_id => eve.person.id).mutual.should be_false + lambda { + alice.remove_contact(contact) + }.should change(contact.aspects(true), :count).from(2).to(0) + end + end + describe '#disconnected_by' do + it 'calls remove contact' do + bob.should_receive(:remove_contact).with(bob.contact_for(alice.person)) + bob.disconnected_by(alice.person) end end describe '#disconnect' do - it 'disconnects a contact on the same seed' do - bob.aspects.first.contacts.count.should == 2 - lambda { - bob.disconnect bob.contact_for(alice.person) }.should change { - bob.contacts(true).count }.by(-1) - bob.aspects.first.contacts(true).count.should == 1 + it 'calls remove contact' do + contact = bob.contact_for(alice.person) + + bob.should_receive(:remove_contact).with(contact) + bob.disconnect contact + end + + it 'dispatches a retraction' do + p = mock() + Postzord::Dispatch.should_receive(:new).and_return(p) + p.should_receive(:post) + + bob.disconnect bob.contact_for(eve.person) end end end diff --git a/spec/models/user/invite_spec.rb b/spec/models/user/invite_spec.rb index 9d52f2e1baacd7fa864f4a138d0716427e9af096..8756a485ebeb16ed9a1d15663e1e791214db0d0a 100644 --- a/spec/models/user/invite_spec.rb +++ b/spec/models/user/invite_spec.rb @@ -48,13 +48,11 @@ describe User do inviter.invite_user(aspect.id, 'email', @email).email.should == @email end - - it 'throws if you try to add someone you"re connected to' do + it "throws if you try to add someone you're connected to" do connect_users(inviter, aspect, another_user, wrong_aspect) - inviter.reload proc{ inviter.invite_user(aspect.id, 'email', another_user.email) - }.should raise_error ActiveRecord::RecordInvalid + }.should raise_error ActiveRecord::RecordNotUnique end end diff --git a/spec/models/user/querying_spec.rb b/spec/models/user/querying_spec.rb index 0acd76edd27ed71f88849fd68db08558ef993135..d38e6c96d3343589ed8ed28eb16beae13c1307ee 100644 --- a/spec/models/user/querying_spec.rb +++ b/spec/models/user/querying_spec.rb @@ -13,11 +13,10 @@ describe User do describe "#raw_visible_posts" do it "returns all the posts the user can see" do - connect_users(eve, @eves_aspect, alice, @alices_aspect) self_post = alice.post(:status_message, :text => "hi", :to => @alices_aspect.id) - visible_post = eve.post(:status_message, :text => "hello", :to => @eves_aspect.id) - dogs = eve.aspects.create(:name => "dogs") - invisible_post = eve.post(:status_message, :text => "foobar", :to => dogs.id) + visible_post = bob.post(:status_message, :text => "hello", :to => bob.aspects.first.id) + dogs = bob.aspects.create(:name => "dogs") + invisible_post = bob.post(:status_message, :text => "foobar", :to => dogs.id) stream = alice.raw_visible_posts stream.should include(self_post) @@ -119,10 +118,6 @@ describe User do end it "only returns non-pending contacts" do - alice.send_contact_request_to(Factory(:user).person, @alices_aspect) - @alices_aspect.reload - alice.reload - alice.people_in_aspects([@alices_aspect]).should == [bob.person] end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 0151d740d0e8047db231802f8d26f969eedb70a1..48d506d11095fcebbc669324f6298a3a167a64e5 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -228,7 +228,7 @@ describe User do end it "returns false if the user has already sent a request to that person" do - alice.send_contact_request_to(eve.person, alice.aspects.first) + alice.share_with(eve.person, alice.aspects.first) alice.reload eve.reload alice.can_add?(eve.person).should be_false @@ -332,22 +332,6 @@ describe User do end end - describe 'foreign key between aspects and contacts' do - it 'should delete an empty aspect' do - empty_aspect = alice.aspects.create(:name => 'decoy') - alice.aspects(true).include?(empty_aspect).should == true - empty_aspect.destroy - alice.aspects(true).include?(empty_aspect).should == false - end - - it 'should not delete an aspect with contacts' do - aspect = alice.aspects.first - aspect.contacts.count.should > 0 - proc { aspect.destroy }.should raise_error ActiveRecord::StatementInvalid - alice.aspects.include?(aspect).should == true - end - end - describe '#update_post' do it 'sends a notification to aspects' do m = mock() @@ -425,14 +409,18 @@ describe User do it 'removes all contacts' do lambda { alice.destroy - }.should change { alice.contacts(true).count }.by(-1) + }.should change { + alice.contacts.count + }.by(-1) end it 'removes all service connections' do Services::Facebook.create(:access_token => 'what', :user_id => alice.id) lambda { alice.destroy - }.should change { alice.services(true).count }.by(-1) + }.should change { + alice.services.count + }.by(-1) end describe '#remove_person' do @@ -471,7 +459,7 @@ describe User do end it 'has no error when the user has sent local requests' do - alice.send_contact_request_to(eve.person, alice.aspects.first) + alice.share_with(eve.person, alice.aspects.first) lambda { alice.destroy }.should_not raise_error diff --git a/spec/support/user_methods.rb b/spec/support/user_methods.rb index 4382b752b355558a38b800e532bfc951d2db26f0..5ac07ec16572cebb6392bbc4bdd31a660bbb0706 100644 --- a/spec/support/user_methods.rb +++ b/spec/support/user_methods.rb @@ -1,16 +1,10 @@ class User - def send_contact_request_to(desired_contact, aspect) - fantasy_resque do - contact = Contact.new(:person => desired_contact, - :user => self, - :pending => true) - contact.aspects << aspect - if contact.save! - contact.dispatch_request - else - nil - end + alias_method :share_with_original, :share_with + + def share_with(*args) + fantasy_resque do + share_with_original(*args) end end