diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb index 2db4646a0..9cd57f398 100644 --- a/app/controllers/services_controller.rb +++ b/app/controllers/services_controller.rb @@ -28,6 +28,10 @@ class ServicesController < ApplicationController current_user.update_profile(current_user.person.profile.from_omniauth_hash(user)) + pp "YAY" + debugger + Resque.enqueue(Jobs::FetchProfilePhoto, current_user.id, service.id) + flash[:notice] = I18n.t 'services.create.success' if current_user.getting_started redirect_to getting_started_path diff --git a/app/models/jobs/fetch_profile_photo.rb b/app/models/jobs/fetch_profile_photo.rb new file mode 100644 index 000000000..13648bc95 --- /dev/null +++ b/app/models/jobs/fetch_profile_photo.rb @@ -0,0 +1,26 @@ +# Copyright (c) 2010-2011, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + + +module Jobs + class FetchProfilePhoto < Base + @queue = :photos + def self.perform(user_id, service_id) + user = User.find(user_id) + service = Service.find(service_id) + + @photo = Photo.new + @photo.author = user.person + @photo.diaspora_handle = user.person.diaspora_handle + @photo.random_string = ActiveSupport::SecureRandom.hex(10) + @photo.remote_unprocessed_image_url = service.profile_photo_url + @photo.save! + + profile_params = {:image_url => @photo.url(:thumb_large), + :image_url_medium => @photo.url(:thumb_medium), + :image_url_small => @photo.url(:thumb_small)} + user.update_profile(profile_params) + end + end +end diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb index 12c7d9585..86e6b0d9f 100644 --- a/app/models/services/twitter.rb +++ b/app/models/services/twitter.rb @@ -33,4 +33,8 @@ class Services::Twitter < Service def public_message(post, url) super(post, MAX_CHARACTERS, url) end + + def profile_photo_url + "http://api.twitter.com/1/users/profile_image?screen_name=#{nickname}&size=bigger" + end end diff --git a/spec/controllers/services_controller_spec.rb b/spec/controllers/services_controller_spec.rb index 5369fa4c9..4c639b46d 100644 --- a/spec/controllers/services_controller_spec.rb +++ b/spec/controllers/services_controller_spec.rb @@ -64,6 +64,17 @@ describe ServicesController do post :create, :provider => 'twitter' @user.reload.services.first.class.name.should == "Services::Twitter" end + + it 'queues a job to save user photo' do + request.env['omniauth.auth'] = omniauth_auth + + post :create, :provider => 'twitter' + + #service_stub = stub.as_null_object + Services::Twitter.any_instance.stub(:profile_photo_url).and_return("http://api.service.com/profile_photo.jpeg") + #Services::Twitter.should_receive(:new).and_return(service_stub) + Resque.should_receive(:enqueue).with(Jobs::FetchProfilePhoto, @user.id, "http://api.service.com/profile_photo.jpeg") + end end describe '#destroy' do diff --git a/spec/models/services/twitter_spec.rb b/spec/models/services/twitter_spec.rb index 98766e2b6..b479420e3 100644 --- a/spec/models/services/twitter_spec.rb +++ b/spec/models/services/twitter_spec.rb @@ -27,4 +27,12 @@ describe Services::Twitter do @service.post(@post, url) end end + + describe "#profile_photo_url" do + it 'returns the bigger profile photo' do + @service.nickname = "joindiaspora" + @service.profile_photo_url.should == + "http://api.twitter.com/1/users/profile_image?screen_name=joindiaspora&size=bigger" + end + end end