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
|
||||
|
||||
validate :not_contact_for_self
|
||||
|
||||
validates_uniqueness_of :person_id, :scope => :user_id
|
||||
|
||||
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.*')
|
||||
end
|
||||
|
||||
def sharing?
|
||||
self.persisted? && (self.mutual? || self.aspect_memberships.size == 0)
|
||||
end
|
||||
|
||||
def receiving?
|
||||
self.aspect_memberships.size > 0
|
||||
def mutual?
|
||||
self.sharing && self.receiving
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
|||
|
|
@ -62,11 +62,8 @@ class Request
|
|||
|
||||
contact = user.contacts.find_or_initialize_by_person_id(self.sender.id)
|
||||
|
||||
if contact.receiving?
|
||||
contact.update_attributes(:mutual => true)
|
||||
else
|
||||
contact.save
|
||||
end
|
||||
contact.sharing = true
|
||||
contact.save
|
||||
|
||||
self
|
||||
end
|
||||
|
|
|
|||
|
|
@ -11,3 +11,4 @@
|
|||
%ul
|
||||
- for aspect in aspects
|
||||
= render 'aspects/aspect', :aspect => aspect, :contacts => aspect.contacts
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
class ContactRemovePendingAddMutual < ActiveRecord::Migration
|
||||
class ContactRemovePendingAddSharingAndReceiving < ActiveRecord::Migration
|
||||
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
|
||||
UPDATE contacts
|
||||
SET contacts.mutual = true
|
||||
SET contacts.sharing = true, contacts.receiving = true
|
||||
WHERE contacts.pending = false
|
||||
SQL
|
||||
)
|
||||
|
|
@ -43,11 +44,12 @@ SQL
|
|||
execute( <<SQL
|
||||
UPDATE contacts
|
||||
SET contacts.pending = false
|
||||
WHERE contacts.mutual = true
|
||||
WHERE contacts.receiving = true AND contacts.sharing = true
|
||||
SQL
|
||||
)
|
||||
|
||||
remove_column :contacts, :mutual
|
||||
remove_column :contacts, :sharing
|
||||
remove_column :contacts, :receiving
|
||||
|
||||
remove_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 "updated_at"
|
||||
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
|
||||
|
||||
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)
|
||||
unless contact.receiving?
|
||||
contact.dispatch_request
|
||||
|
||||
if contact.sharing?
|
||||
contact.mutual = true
|
||||
end
|
||||
contact.receiving = true
|
||||
end
|
||||
|
||||
contact.aspects << aspect
|
||||
contact.save
|
||||
|
||||
|
|
@ -27,10 +25,10 @@ module Diaspora
|
|||
def remove_contact(contact, opts={:force => false})
|
||||
posts = contact.posts.all
|
||||
|
||||
if !contact.mutual || opts[:force]
|
||||
if !contact.mutual? || opts[:force]
|
||||
contact.destroy
|
||||
else
|
||||
contact.update_attributes(:mutual => false)
|
||||
contact.update_attributes(:sharing => false)
|
||||
end
|
||||
|
||||
posts.each do |p|
|
||||
|
|
|
|||
|
|
@ -5,11 +5,13 @@ module HelperMethods
|
|||
def connect_users(user1, aspect1, user2, aspect2)
|
||||
user1.contacts.create!(:person => user2.person,
|
||||
:aspects => [aspect1],
|
||||
:mutual => true)
|
||||
:sharing => true,
|
||||
:receiving => true)
|
||||
|
||||
user2.contacts.create!(:person => user1.person,
|
||||
:aspects => [aspect2],
|
||||
:mutual => true)
|
||||
:sharing => true,
|
||||
:receiving => true)
|
||||
end
|
||||
|
||||
def stub_success(address = 'abc@example.com', opts = {})
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ describe Contact do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
context 'validations' do
|
||||
let(:contact){Contact.new}
|
||||
|
||||
it 'requires a user' do
|
||||
|
|
@ -44,6 +44,20 @@ describe Contact do
|
|||
contact.person = person
|
||||
contact.should_not be_valid
|
||||
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
|
||||
|
||||
describe '#contacts' do
|
||||
|
|
@ -99,7 +113,6 @@ describe Contact do
|
|||
@contact.contacts.should == []
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'requesting' do
|
||||
|
|
@ -132,38 +145,4 @@ describe Contact do
|
|||
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
|
||||
|
|
|
|||
|
|
@ -86,6 +86,12 @@ describe Request do
|
|||
}.from(false).to(true)
|
||||
|
||||
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
|
||||
|
||||
context 'xml' do
|
||||
|
|
|
|||
|
|
@ -28,9 +28,9 @@ describe Diaspora::UserModules::Connecting do
|
|||
end
|
||||
|
||||
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.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
|
||||
|
||||
|
|
@ -83,8 +83,8 @@ describe Diaspora::UserModules::Connecting do
|
|||
|
||||
it 'does set mutual on share-back request' do
|
||||
eve.share_with(alice.person, eve.aspects.first)
|
||||
|
||||
alice.share_with(eve.person, alice.aspects.first)
|
||||
|
||||
alice.contacts.find_by_person_id(eve.person.id).should be_mutual
|
||||
end
|
||||
|
||||
|
|
@ -116,10 +116,10 @@ describe Diaspora::UserModules::Connecting do
|
|||
alice.share_with(eve.person, alice.aspects.first)
|
||||
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")
|
||||
|
||||
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)
|
||||
|
||||
contact.should_not_receive(:dispatch_request)
|
||||
|
|
@ -127,6 +127,11 @@ describe Diaspora::UserModules::Connecting do
|
|||
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
|
||||
notification = Factory.create(:notification, :target => eve.person)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue