Commit 68375fdb authored by danielgrippi's avatar danielgrippi

added mutual flag, removed pending from contact, specs are green. wip

parent 07f7eff7
--format Fuubar
--profile
--color
\ No newline at end of file
--color
--tag ~performance
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......@@ -3,8 +3,6 @@
# the COPYRIGHT file.
class Contact < ActiveRecord::Base
default_scope where(:pending => false)
belongs_to :user
validates_presence_of :user
......
......@@ -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"
......
......@@ -3,8 +3,6 @@
# the COPYRIGHT file.
class PostVisibility < ActiveRecord::Base
default_scope where(:hidden => false)
belongs_to :contact
belongs_to :post
end
......@@ -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
......
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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
......
......@@ -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'
......
%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
......
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
......@@ -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"
......
......@@ -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
......@@ -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={})
......
......@@ -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
......@@ -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
......
......@@ -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'
......
......@@ -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 = {})
......
......@@ -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)
......
......@@ -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
......@@ -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
......
......@@ -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
......
# 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