diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb index b9ea7f3d6a639749f5f0c37b7dd9d1358fa05c51..f699297dcd24ee5cbd7a6752f746eacf3152343a 100644 --- a/app/controllers/services_controller.rb +++ b/app/controllers/services_controller.rb @@ -51,12 +51,6 @@ class ServicesController < ApplicationController end def inviter - if !AppConfig[:open_invitations] && current_user.invites == 0 - flash[:error] = I18n.t 'invitations.create.no_more' - redirect_to :back - return - end - @uid = params[:uid] if i_id = params[:invitation_id] diff --git a/app/models/person.rb b/app/models/person.rb index e1d63696139d1bb72a771efb60cce4294d510c70..73334daef7c047de3876a4593013da59f9251229 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -43,6 +43,8 @@ class Person < ActiveRecord::Base validates_uniqueness_of :diaspora_handle scope :searchable, joins(:profile).where(:profiles => {:searchable => true}) + scope :remote, where('people.owner_id IS NULL') + scope :local, where('people.owner_id IS NOT NULL') def self.search_query_string(query) query = query.downcase diff --git a/app/models/service_user.rb b/app/models/service_user.rb index 122a98ce2b279ac9f32c0ebfb12c6908d9d6c6e9..a721ae50502befbbf7c08b249dd0f2b91a0800ce 100644 --- a/app/models/service_user.rb +++ b/app/models/service_user.rb @@ -6,6 +6,17 @@ class ServiceUser < ActiveRecord::Base belongs_to :invitation before_save :attach_local_models + scope :with_local_people, joins(:person).merge(Person.local) + scope :with_remote_people, joins(:person).merge(Person.remote) + + + def already_invited? + self.invitation_id.present? + end + + def on_diaspora? + self.person_id.present? + end def attach_local_models service_for_uid = Services::Facebook.where(:type => service.type.to_s, :uid => self.uid).first diff --git a/app/models/services/facebook.rb b/app/models/services/facebook.rb index 146fc882a8b631b2e10f7d40454440161501b5c7..a63ca884c24ed25571632b582842b02df35ba2fe 100644 --- a/app/models/services/facebook.rb +++ b/app/models/services/facebook.rb @@ -21,18 +21,11 @@ class Services::Facebook < Service def finder(opts = {}) Rails.logger.debug("event=friend_finder type=facebook sender_id=#{self.user_id}") - if self.service_users.blank? - self.save_friends - self.service_users.reload - else - Resque.enqueue(Job::UpdateServiceUsers, self.id) - end - person = Person.arel_table - service_user = ServiceUser.arel_table + prevent_service_users_from_being_empty if opts[:local] - ServiceUser.joins(:person).where(:service_id => self.id).where(person[:owner_id].not_eq(nil)).order(:name).all + self.service_users.with_local_people elsif opts[:remote] - ServiceUser.joins(:person).where(:service_id => self.id).where(person[:owner_id].eq(nil)).order(:name).all + self.service_users.with_remote_people else self.service_users end @@ -49,4 +42,15 @@ class Services::Facebook < Service } ServiceUser.import(data, :on_duplicate_key_update => [:updated_at, :contact_id, :person_id, :request_id, :invitation_id, :photo_url, :name]) end + + private + + def prevent_service_users_from_being_empty + if self.service_users.blank? + self.save_friends + self.service_users.reload + else + Resque.enqueue(Job::UpdateServiceUsers, self.id) + end + end end diff --git a/app/views/services/_remote_friend.html.haml b/app/views/services/_remote_friend.html.haml index 720a68ba998237916d598b19691dc7eaad07b316..46983fb25585da6a8339ec073224d2482e279437 100644 --- a/app/views/services/_remote_friend.html.haml +++ b/app/views/services/_remote_friend.html.haml @@ -1,37 +1,28 @@ %li.remote_friend{:id => "uid_" + friend.uid, :uid => friend.uid} .right - -if friend.contact - / TODO(*) add following method in Contact - sharing - - elsif friend.invitation_id + - if friend.already_invited? = t('invitations.new.already_invited') %br = link_to t('.resend'), service_inviter_path(:uid => friend.uid, :provider => 'facebook', :invitation_id => friend.invitation_id) - - elsif friend.person - = link_to t('people.show.start_sharing'), - '#', - :class => 'button' + - elsif friend.on_diaspora? + = link_to(t('people.show.start_sharing'),'#', :class => 'button') - - elsif current_user.invites > 0 - = form_tag service_inviter_path(:provider => 'facebook') do - = select_tag(:aspect_id, options_from_collection_for_select(all_aspects, 'id', 'name')) - = hidden_field_tag :uid, friend.uid - = submit_tag t('.invite') + = form_tag service_inviter_path(:provider => 'facebook') do + = select_tag(:aspect_id, options_from_collection_for_select(all_aspects, 'id', 'name')) + = hidden_field_tag :uid, friend.uid + = submit_tag t('.invite') - - if friend.person + - if friend.on_diaspora? = person_image_link(friend.person) - else = image_tag(friend.photo_url, :class => 'avatar') %h4.name - - if friend.person + - if friend.on_diaspora? = link_to friend.name, person_path(friend.person) - else = friend.name -- unless friend.person.nil? - +- if friend.on_diaspora? %li{:id => "options_" + friend.uid, :class => "share_with hidden", :style => "height:auto"} - - contact = friend.contact - - contact ||= Contact.new(:person => friend.person) - = render 'aspect_memberships/aspect_dropdown', :contact => contact, :person => friend.person, :hang => 'left' + = render 'aspect_memberships/aspect_dropdown', :contact => contact_proxy(friend), :person => friend.person, :hang => 'left' diff --git a/spec/controllers/services_controller_spec.rb b/spec/controllers/services_controller_spec.rb index 06cd95ed814eca5f0a6a20bdd81290de2d8127af..80edbc20865e52c508f66acfef2bc4df21c096cd 100644 --- a/spec/controllers/services_controller_spec.rb +++ b/spec/controllers/services_controller_spec.rb @@ -141,14 +141,7 @@ describe ServicesController do }.should_not change(Invitation, :count) end - it' does not crete an invitation if the user has no invitations' do - @user.invites = 0 - lambda { - put :inviter, @invite_params - }.should_not change(Invitation, :count) - end - - it 'disregares the amount of invites if open_invitations are anabled' do + it 'disregares the amount of invites if open_invitations are enabled' do open_bit = AppConfig[:open_invitations] AppConfig[:open_invitations] = true @user.invites = 0 diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index e47e7aa30e94422759b2131323cd6daa61eed48e..faa3b238cf03a2fe65ac4531d596cca1e2fb6c73 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -11,6 +11,19 @@ describe Person do @person = Factory.create(:person) end + context 'scopes' do + describe '.local' do + it 'returns only local people' do + Person.local =~ [@person] + end + end + + describe '.remote' do + it 'returns only local people' do + Person.remote =~ [@user.person] + end + end + end describe "delegating" do it "delegates last_name to the profile" do @person.last_name.should == @person.profile.last_name diff --git a/spec/models/service_user_spec.rb b/spec/models/service_user_spec.rb index 1880a368c42048464bd512886ab0be0cdd6575e1..8512ee39678c888583bfb1b07a3e1d36c6c45ca9 100644 --- a/spec/models/service_user_spec.rb +++ b/spec/models/service_user_spec.rb @@ -2,6 +2,40 @@ require 'spec_helper' describe ServiceUser do + context 'scope' do + before do + @user = alice + @service = Services::Facebook.new(:access_token => "yeah") + @user.services << @service + @user2 = Factory.create(:user_with_aspect) + @user2_fb_id = '820651' + @user2_fb_name = 'Maxwell Salzberg' + @user2_fb_photo_url = 'http://cdn.fn.com/pic1.jpg' + @user2_service = Services::Facebook.create(:uid => @user2_fb_id, :access_token => "yo", :user_id => @user2.id) + + @su = ServiceUser.new(:service_id => @service.id, :uid => @user2_fb_id, :name => @user2_fb_name,:photo_url => @user2_fb_photo_url) + @su.person = @user2.person + @su.save + end + describe 'with_local_people' do + it 'returns services with local people' do + ServiceUser.with_local_people.count.should == 1 + ServiceUser.with_remote_people.count.should == 0 + end + end + + describe 'with_remote_people' do + it 'returns services with remote people' do + @user2_service.delete + p = @user2.person + p.owner_id = nil + p.save + ServiceUser.with_local_people.count.should == 0 + ServiceUser.with_remote_people.count.should == 1 + end + end + + end describe '#finder' do before do @user = alice diff --git a/spec/models/services/facebook_spec.rb b/spec/models/services/facebook_spec.rb index c5257f2b4545eca7839669412c8a92f2d4004910..fcb1be91639307e392de0acfc8136425ef008b2d 100644 --- a/spec/models/services/facebook_spec.rb +++ b/spec/models/services/facebook_spec.rb @@ -99,20 +99,20 @@ JSON context 'opts' do it 'only local does not return people who are remote' do @service.save_friends - @service.finder(:local => true).each{|su| su.person.should == @user2.person} + @service.finder(:local => true).all.each{|su| su.person.should == @user2.person} end it 'does not return people who are remote' do @service.save_friends - @service.finder(:remote => true).each{|su| su.person.should be_nil} + @service.finder(:remote => true).all.each{|su| su.person.should be_nil} end it 'does not return wrong service objects' do su2 = ServiceUser.create(:service => @user2_service, :uid => @user2_fb_id, :name => @user2_fb_name, :photo_url => @user2_fb_photo_url) su2.person.should == @user2.person - @service.finder(:local => true).each{|su| su.service.should == @service} - @service.finder(:remote => true).each{|su| su.service.should == @service} + @service.finder(:local => true).all.each{|su| su.service.should == @service} + @service.finder(:remote => true).all.each{|su| su.service.should == @service} @service.finder.each{|su| su.service.should == @service} end end