diff --git a/app/models/service_user.rb b/app/models/service_user.rb index 226a3fdbbe9b236b864e27991a706c81581f8734..2f1ce222ac88ac84f83bcad79dbdb11810426ff8 100644 --- a/app/models/service_user.rb +++ b/app/models/service_user.rb @@ -1,2 +1,30 @@ class ServiceUser < ActiveRecord::Base + + belongs_to :person + belongs_to :contact + belongs_to :service + belongs_to :request + belongs_to :invitation + + before_save :attach_local_models + + private + def attach_local_models + service_for_uid = Services::Facebook.where(:type => service.type.to_s, :uid => self.uid).first + if !service_for_uid.blank? && (service_for_uid.user.person.profile.searchable) + self.person = service_for_uid.user.person + else + self.person = nil + end + + if self.person + self.contact = self.service.user.contact_for(self.person) + self.request = Request.where(:recipient_id => self.service.user.person.id, + :sender_id => self.person_id).first + end + + self.invitation = Invitation.joins(:recipient).where(:sender_id => self.service.user_id, + :users => {:invitation_service => self.service.provider, + :invitation_identifier => self.uid}).first + end end diff --git a/app/models/services/facebook.rb b/app/models/services/facebook.rb index 519ee6cae420dae3d9b3ef69f75789719ad6fbaa..a24601a5a5ee2e1defe2048015d7dac112a90e72 100644 --- a/app/models/services/facebook.rb +++ b/app/models/services/facebook.rb @@ -68,8 +68,8 @@ class Services::Facebook < Service {:fields => ['name', 'id', 'picture'], :access_token => self.access_token}}) data = JSON.parse(response.body)['data'] data.each{ |p| - ServiceUser.create(:service_id => self.id, :name => p["name"], - :uid => p["id"], :picture_url => p["picture"]) + ServiceUser.find_or_create(:service_id => self.id, :name => p["name"], + :uid => p["id"], :photo_url => p["picture"]) } end end diff --git a/db/migrate/20110318000734_create_service_users.rb b/db/migrate/20110318000734_create_service_users.rb index 30d5d907fdc17560d6d237c75e7b55d1b67c6432..7ac30e69791264f1213b778298d611a9656dfd1f 100644 --- a/db/migrate/20110318000734_create_service_users.rb +++ b/db/migrate/20110318000734_create_service_users.rb @@ -3,15 +3,18 @@ class CreateServiceUsers < ActiveRecord::Migration create_table :service_users do |t| t.string :uid, :null => false t.string :name, :null => false - t.string :picture_url, :null => false + t.string :photo_url, :null => false t.integer :service_id, :null => false t.integer :person_id t.integer :contact_id t.integer :request_id + t.integer :invitation_id + t.timestamps end add_index :service_users, :service_id + add_index :service_users, [:uid, :service_id], :unique => true end def self.down diff --git a/db/schema.rb b/db/schema.rb index e036785f0148c907e0851c80e6ace66f362d4ced..f3032b399db62b2fef7ee247a2784f181d953840 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -274,18 +274,20 @@ ActiveRecord::Schema.define(:version => 20110319172136) do add_index "requests", ["sender_id"], :name => "index_requests_on_sender_id" create_table "service_users", :force => true do |t| - t.string "uid", :null => false - t.string "name", :null => false - t.string "picture_url", :null => false - t.integer "service_id", :null => false + t.string "uid", :null => false + t.string "name", :null => false + t.string "photo_url", :null => false + t.integer "service_id", :null => false t.integer "person_id" t.integer "contact_id" t.integer "request_id" + t.integer "invitation_id" t.datetime "created_at" t.datetime "updated_at" end add_index "service_users", ["service_id"], :name => "index_service_users_on_service_id" + add_index "service_users", ["uid", "service_id"], :name => "index_service_users_on_uid_and_service_id", :unique => true create_table "services", :force => true do |t| t.string "type", :null => false diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 77e4b4022692df18fdcc1f38f0225e383a97797e..b259fc28d8e207bb0bf1ca68ae2d2f77c110c43f 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -46,7 +46,6 @@ namespace :db do desc 'Purge and seed the current RAILS_ENV database using information from db/seeds.rb' task :reset do - puts "Resetting the database for #{Rails.env}".upcase Rake::Task['db:purge'].invoke Rake::Task['db:seed'].invoke diff --git a/spec/models/service_user_spec.rb b/spec/models/service_user_spec.rb index 1138621f0895f91ca103fce139db9c052500aa83..d58da22e749c89a855727eaaa642ddf6c9dc4e37 100644 --- a/spec/models/service_user_spec.rb +++ b/spec/models/service_user_spec.rb @@ -12,6 +12,7 @@ describe ServiceUser do @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.new(:uid => @user2_fb_id, :access_token => "yo") @user2.services << @user2_service @fb_list_hash = <<JSON @@ -20,7 +21,7 @@ describe ServiceUser do { "name": "#{@user2_fb_name}", "id": "#{@user2_fb_id}", - "picture": "http://cdn.fn.com/pic1.jpg" + "picture": "" }, { "name": "Person to Invite", @@ -36,28 +37,32 @@ JSON end context 'lifecycle callbacks' do + before do + @su = ServiceUser.create(:service_id => @service.id, :uid => @user2_fb_id, :name => @user2_fb_name, + :photo_url => @user2_fb_photo_url) + end it 'contains a name' do - su = ServiceUser.new(:service_id => @service.id, :uid => @user2_fb_id) - su.save - su.name.should == @user2_fb_name + @su.name.should == @user2_fb_name end it 'contains a photo url' do - pending + @su.photo_url.should == @user2_fb_photo_url end it 'contains a FB id' do - @service.finder.include?(@user2_fb_id).should be_true + @su.uid.should == @user2_fb_id end it 'contains a diaspora person object' do - @service.finder["#{@user2_fb_id}"][:person].should == @user2.person + @su.person.should == @user2.person end - it 'caches the profile' do - @service.finder["#{@user2_fb_id}"][:person].profile.loaded?.should be_true + it 'queries for the correct service type' do + Services::Facebook.should_receive(:where).with(hash_including({:type => "Services::Facebook"})).and_return([]) + @su.send(:attach_local_models) end it 'does not include the person if the search is disabled' do p = @user2.person.profile p.searchable = false p.save - @service.finder["#{@user2_fb_id}"][:person].should be_nil + @su.save + @su.person.should be_nil end context "request" do @@ -67,36 +72,15 @@ JSON Request.count.should == 1 end it 'contains a request object if one has been sent' do - @service.finder["#{@user2_fb_id}"][:request].should == @request - end - - it 'caches the profile' do - @service.finder["#{@user2_fb_id}"][:request].sender.profile.loaded?.should be_true + @su.save + @su.request.should == @request end - - it 'caches the sender' do - @service.finder["#{@user2_fb_id}"][:request].sender.loaded?.should be_true - end - end it 'contains a contact object if connected' do connect_users(@user, @user.aspects.first, @user2, @user2.aspects.first) - @service.finder["#{@user2_fb_id}"][:contact].should == @user.reload.contact_for(@user2.person) - end - - context 'only local' do - it 'does not return people who are remote' do - @service.finder(:local => true)['abc123'].should be nil - @service.finder(:local => true)["#{@user2_fb_id}"].should_not be_nil - end - end - - context 'only remote' do - it 'does not return people who are remote' do - @service.finder(:remote => true)['abc123'].should_not be nil - @service.finder(:remote => true)["#{@user2_fb_id}"].should be_nil - end + @su.save + @su.contact.should == @user.reload.contact_for(@user2.person) end context 'already invited' do @@ -107,14 +91,16 @@ JSON end it 'contains an invitation if invited' do @inv = Invitation.create(:sender => @user, :recipient => @user2, :aspect => @user.aspects.first) - @service.finder["#{@user2_fb_id}"][:invitation_id].should == @inv.id + @su.save + @su.invitation_id.should == @inv.id end it 'does not find the user with a wrong identifier' do @user2.invitation_identifier = 'dsaofhnadsoifnsdanf' @user2.save @inv = Invitation.create(:sender => @user, :recipient => @user2, :aspect => @user.aspects.first) - @service.finder["#{@user2_fb_id}"][:invitation_id].should be_nil + @su.save + @su.invitation_id.should be_nil end end end diff --git a/spec/models/services/facebook_spec.rb b/spec/models/services/facebook_spec.rb index 414e5161a4aa3f27026ddfe4d7dfa46fec7a0d09..4ce8beb4b22de91a9d43f2488bb15d3f802afa4f 100644 --- a/spec/models/services/facebook_spec.rb +++ b/spec/models/services/facebook_spec.rb @@ -66,5 +66,19 @@ JSON @service.save_friends }.should change(ServiceUser, :count).by(2) end + + context 'only local' do + it 'does not return people who are remote' do + @service.finder(:local => true).should be_empty + @service.finder(:local => true).should_not be_empty + end + end + + context 'only remote' do + it 'does not return people who are remote' do + @service.finder(:remote => true).should_not be_empty + @service.finder(:remote => true).should be_empty + end + end end end