split 'mutual' flag into 'sharing' and 'receiving' on Contact
This commit is contained in:
parent
648a10e6d3
commit
e854bd0a32
10 changed files with 54 additions and 66 deletions
|
|
@ -16,6 +16,7 @@ class Contact < ActiveRecord::Base
|
||||||
has_many :posts, :through => :post_visibilities
|
has_many :posts, :through => :post_visibilities
|
||||||
|
|
||||||
validate :not_contact_for_self
|
validate :not_contact_for_self
|
||||||
|
|
||||||
validates_uniqueness_of :person_id, :scope => :user_id
|
validates_uniqueness_of :person_id, :scope => :user_id
|
||||||
|
|
||||||
def dispatch_request
|
def dispatch_request
|
||||||
|
|
@ -46,12 +47,8 @@ class Contact < ActiveRecord::Base
|
||||||
:aspect_memberships => {:aspect_id => incoming_aspect_ids}).where(people[:id].not_eq(self.user.person.id)).select('DISTINCT people.*')
|
:aspect_memberships => {:aspect_id => incoming_aspect_ids}).where(people[:id].not_eq(self.user.person.id)).select('DISTINCT people.*')
|
||||||
end
|
end
|
||||||
|
|
||||||
def sharing?
|
def mutual?
|
||||||
self.persisted? && (self.mutual? || self.aspect_memberships.size == 0)
|
self.sharing && self.receiving
|
||||||
end
|
|
||||||
|
|
||||||
def receiving?
|
|
||||||
self.aspect_memberships.size > 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
||||||
|
|
@ -62,11 +62,8 @@ class Request
|
||||||
|
|
||||||
contact = user.contacts.find_or_initialize_by_person_id(self.sender.id)
|
contact = user.contacts.find_or_initialize_by_person_id(self.sender.id)
|
||||||
|
|
||||||
if contact.receiving?
|
contact.sharing = true
|
||||||
contact.update_attributes(:mutual => true)
|
contact.save
|
||||||
else
|
|
||||||
contact.save
|
|
||||||
end
|
|
||||||
|
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -11,3 +11,4 @@
|
||||||
%ul
|
%ul
|
||||||
- for aspect in aspects
|
- for aspect in aspects
|
||||||
= render 'aspects/aspect', :aspect => aspect, :contacts => aspect.contacts
|
= render 'aspects/aspect', :aspect => aspect, :contacts => aspect.contacts
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
class ContactRemovePendingAddMutual < ActiveRecord::Migration
|
class ContactRemovePendingAddSharingAndReceiving < ActiveRecord::Migration
|
||||||
def self.up
|
def self.up
|
||||||
add_column :contacts, :mutual, :boolean, :default => false, :null => false
|
add_column :contacts, :sharing, :boolean, :default => false, :null => false
|
||||||
|
add_column :contacts, :receiving, :boolean, :default => false, :null => false
|
||||||
|
|
||||||
execute( <<SQL
|
execute( <<SQL
|
||||||
UPDATE contacts
|
UPDATE contacts
|
||||||
SET contacts.mutual = true
|
SET contacts.sharing = true, contacts.receiving = true
|
||||||
WHERE contacts.pending = false
|
WHERE contacts.pending = false
|
||||||
SQL
|
SQL
|
||||||
)
|
)
|
||||||
|
|
@ -43,11 +44,12 @@ SQL
|
||||||
execute( <<SQL
|
execute( <<SQL
|
||||||
UPDATE contacts
|
UPDATE contacts
|
||||||
SET contacts.pending = false
|
SET contacts.pending = false
|
||||||
WHERE contacts.mutual = true
|
WHERE contacts.receiving = true AND contacts.sharing = true
|
||||||
SQL
|
SQL
|
||||||
)
|
)
|
||||||
|
|
||||||
remove_column :contacts, :mutual
|
remove_column :contacts, :sharing
|
||||||
|
remove_column :contacts, :receiving
|
||||||
|
|
||||||
remove_foreign_key :aspect_memberships, :aspects
|
remove_foreign_key :aspect_memberships, :aspects
|
||||||
add_foreign_key :aspect_memberships, :aspects
|
add_foreign_key :aspect_memberships, :aspects
|
||||||
|
|
@ -73,7 +73,8 @@ ActiveRecord::Schema.define(:version => 20110421120744) do
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
t.string "mongo_id"
|
t.string "mongo_id"
|
||||||
t.boolean "mutual", :default => false, :null => false
|
t.boolean "sharing", :default => false, :null => false
|
||||||
|
t.boolean "receiving", :default => false, :null => false
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "contacts", ["mongo_id"], :name => "index_contacts_on_mongo_id"
|
add_index "contacts", ["mongo_id"], :name => "index_contacts_on_mongo_id"
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,9 @@ module Diaspora
|
||||||
contact = self.contacts.find_or_initialize_by_person_id(person.id)
|
contact = self.contacts.find_or_initialize_by_person_id(person.id)
|
||||||
unless contact.receiving?
|
unless contact.receiving?
|
||||||
contact.dispatch_request
|
contact.dispatch_request
|
||||||
|
contact.receiving = true
|
||||||
if contact.sharing?
|
|
||||||
contact.mutual = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
contact.aspects << aspect
|
contact.aspects << aspect
|
||||||
contact.save
|
contact.save
|
||||||
|
|
||||||
|
|
@ -27,10 +25,10 @@ module Diaspora
|
||||||
def remove_contact(contact, opts={:force => false})
|
def remove_contact(contact, opts={:force => false})
|
||||||
posts = contact.posts.all
|
posts = contact.posts.all
|
||||||
|
|
||||||
if !contact.mutual || opts[:force]
|
if !contact.mutual? || opts[:force]
|
||||||
contact.destroy
|
contact.destroy
|
||||||
else
|
else
|
||||||
contact.update_attributes(:mutual => false)
|
contact.update_attributes(:sharing => false)
|
||||||
end
|
end
|
||||||
|
|
||||||
posts.each do |p|
|
posts.each do |p|
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,13 @@ module HelperMethods
|
||||||
def connect_users(user1, aspect1, user2, aspect2)
|
def connect_users(user1, aspect1, user2, aspect2)
|
||||||
user1.contacts.create!(:person => user2.person,
|
user1.contacts.create!(:person => user2.person,
|
||||||
:aspects => [aspect1],
|
:aspects => [aspect1],
|
||||||
:mutual => true)
|
:sharing => true,
|
||||||
|
:receiving => true)
|
||||||
|
|
||||||
user2.contacts.create!(:person => user1.person,
|
user2.contacts.create!(:person => user1.person,
|
||||||
:aspects => [aspect2],
|
:aspects => [aspect2],
|
||||||
:mutual => true)
|
:sharing => true,
|
||||||
|
:receiving => true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def stub_success(address = 'abc@example.com', opts = {})
|
def stub_success(address = 'abc@example.com', opts = {})
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ describe Contact do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'validations' do
|
context 'validations' do
|
||||||
let(:contact){Contact.new}
|
let(:contact){Contact.new}
|
||||||
|
|
||||||
it 'requires a user' do
|
it 'requires a user' do
|
||||||
|
|
@ -44,6 +44,20 @@ describe Contact do
|
||||||
contact.person = person
|
contact.person = person
|
||||||
contact.should_not be_valid
|
contact.should_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'validates that sharing and aspect membership count are consistant' do
|
||||||
|
pending
|
||||||
|
person = Factory(:person)
|
||||||
|
|
||||||
|
contact2 = alice.contacts.create(:person=>person)
|
||||||
|
contact2.should be_valid
|
||||||
|
|
||||||
|
contact2.aspect_memberships.create(:aspect => alice.aspects.first)
|
||||||
|
contact2.should_not be_sharing
|
||||||
|
contact2.should_not be_valid
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#contacts' do
|
describe '#contacts' do
|
||||||
|
|
@ -99,7 +113,6 @@ describe Contact do
|
||||||
@contact.contacts.should == []
|
@contact.contacts.should == []
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'requesting' do
|
context 'requesting' do
|
||||||
|
|
@ -132,38 +145,4 @@ describe Contact do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'sharing/receiving status' do
|
|
||||||
before do
|
|
||||||
alice.share_with(eve.person, alice.aspects.first)
|
|
||||||
|
|
||||||
@follower = eve.contact_for(alice.person)
|
|
||||||
@following = alice.contact_for(eve.person)
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#sharing?' do
|
|
||||||
it 'returns true if contact has no aspect visibilities' do
|
|
||||||
@follower.should be_sharing
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns false if contact has aspect visibilities' do
|
|
||||||
@following.should_not be_sharing
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns false if contact is not persisted' do
|
|
||||||
Contact.new.should_not be_sharing
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#receiving?' do
|
|
||||||
it 'returns false if contact has no aspect visibilities' do
|
|
||||||
@follower.should_not be_receiving
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns true if contact has aspect visibilities' do
|
|
||||||
@following.should be_receiving
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,12 @@ describe Request do
|
||||||
}.from(false).to(true)
|
}.from(false).to(true)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'sets sharing' do
|
||||||
|
Request.diaspora_initialize(:from => eve.person, :to => alice.person,
|
||||||
|
:into => eve.aspects.first).receive(alice, eve.person)
|
||||||
|
alice.contact_for(eve.person).should be_sharing
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'xml' do
|
context 'xml' do
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,9 @@ describe Diaspora::UserModules::Connecting do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'removes a contacts mutual flag' do
|
it 'removes a contacts mutual flag' do
|
||||||
bob.contacts.find_by_person_id(alice.person.id).mutual.should be_true
|
bob.contacts.find_by_person_id(alice.person.id).should be_mutual
|
||||||
bob.remove_contact(bob.contact_for(alice.person))
|
bob.remove_contact(bob.contact_for(alice.person))
|
||||||
bob.contacts(true).find_by_person_id(alice.person.id).mutual.should be_false
|
bob.contacts(true).find_by_person_id(alice.person.id).should_not be_mutual
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -83,8 +83,8 @@ describe Diaspora::UserModules::Connecting do
|
||||||
|
|
||||||
it 'does set mutual on share-back request' do
|
it 'does set mutual on share-back request' do
|
||||||
eve.share_with(alice.person, eve.aspects.first)
|
eve.share_with(alice.person, eve.aspects.first)
|
||||||
|
|
||||||
alice.share_with(eve.person, alice.aspects.first)
|
alice.share_with(eve.person, alice.aspects.first)
|
||||||
|
|
||||||
alice.contacts.find_by_person_id(eve.person.id).should be_mutual
|
alice.contacts.find_by_person_id(eve.person.id).should be_mutual
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -116,16 +116,21 @@ describe Diaspora::UserModules::Connecting do
|
||||||
alice.share_with(eve.person, alice.aspects.first)
|
alice.share_with(eve.person, alice.aspects.first)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not dispatch a request on adding to aspect aspect' do
|
it 'does not dispatch a request if contact already marked as receiving' do
|
||||||
a2 = alice.aspects.create(:name => "two")
|
a2 = alice.aspects.create(:name => "two")
|
||||||
|
|
||||||
contact = alice.contacts.create(:person => eve.person, :aspects => [eve.aspects.first])
|
contact = alice.contacts.create(:person => eve.person, :receiving => true)
|
||||||
alice.contacts.stub!(:find_or_initialize_by_person_id).and_return(contact)
|
alice.contacts.stub!(:find_or_initialize_by_person_id).and_return(contact)
|
||||||
|
|
||||||
contact.should_not_receive(:dispatch_request)
|
contact.should_not_receive(:dispatch_request)
|
||||||
alice.share_with(eve.person, a2)
|
alice.share_with(eve.person, a2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'sets receiving' do
|
||||||
|
alice.share_with(eve.person, alice.aspects.first)
|
||||||
|
alice.contact_for(eve.person).should be_receiving
|
||||||
|
end
|
||||||
|
|
||||||
it "should mark the corresponding notification as 'read'" do
|
it "should mark the corresponding notification as 'read'" do
|
||||||
notification = Factory.create(:notification, :target => eve.person)
|
notification = Factory.create(:notification, :target => eve.person)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue