diff --git a/app/api/helpers.rb b/app/api/helpers.rb index e306bea..56dcb68 100644 --- a/app/api/helpers.rb +++ b/app/api/helpers.rb @@ -10,6 +10,9 @@ module Api include Noosfero::Plugin::HotSpot include ForgotPasswordHelper include SearchTermHelper + include ProfileImageHelper + include Noosfero::Gravatar + include ThemeLoaderHelper def set_locale I18n.locale = (params[:lang] || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en') diff --git a/app/api/v1/people.rb b/app/api/v1/people.rb index d12626d..767fd4f 100644 --- a/app/api/v1/people.rb +++ b/app/api/v1/people.rb @@ -108,6 +108,23 @@ module Api end present output end + + desc "Return the person profile picture (you can optionally pass a 'size' parameter)" + get ":id/icon" do + person = environment.people.find(params[:id]) + + size = params[:size] || :portrait + image = profile_icon(person, size.to_sym) + output = {} + + unless image.match(/^\/\/www\.gravatar\.com/).nil? + output[:icon] = 'https:' + image + else + output[:icon] = request.url.gsub(/\/api\/.*/, '') + image + end + + present output + end end resource :profiles do diff --git a/app/helpers/theme_loader_helper.rb b/app/helpers/theme_loader_helper.rb index 10d8014..e2e0be2 100644 --- a/app/helpers/theme_loader_helper.rb +++ b/app/helpers/theme_loader_helper.rb @@ -2,7 +2,7 @@ module ThemeLoaderHelper def current_theme @current_theme ||= begin - if session[:user_theme] + if !(defined?(session)).nil? && session[:user_theme] session[:user_theme] else # utility for developers: set the theme to 'random' in development mode and @@ -14,7 +14,7 @@ module ThemeLoaderHelper elsif Rails.env.development? && respond_to?(:params) && params[:user_theme] && File.exists?(Rails.root.join('public/designs/themes', params[:user_theme])) params[:user_theme] else - if profile && !profile.theme.nil? + if defined?(profile) && profile && !profile.theme.nil? profile.theme elsif environment environment.theme @@ -34,7 +34,7 @@ module ThemeLoaderHelper end def theme_path - if session[:user_theme] + if !(defined?(session)).nil? && session[:user_theme] '/user_themes/' + current_theme elsif session[:theme] '/designs/themes/' + session[:theme] diff --git a/test/api/people_test.rb b/test/api/people_test.rb index 01d7f08..dad9b7a 100644 --- a/test/api/people_test.rb +++ b/test/api/people_test.rb @@ -369,6 +369,44 @@ class PeopleTest < ActiveSupport::TestCase assert_equal "www.blog.org", json['person']['additional_data']['Custom Blog'] end + should 'return portrait icon if size is not provided and there is a profile image' do + img = Image.create!(uploaded_data: fixture_file_upload('/files/rails.png', 'image/png')) + profile = fast_create(Person, image_id: img.id) + + get "/api/v1/people/#{profile.id}/icon?#{params.to_query}" + assert_equal 200, last_response.status + json = JSON.parse(last_response.body) + assert_match /^https?:\/\/.*portrait\.png$/, json['icon'] + end + + should 'return icon in provided size if there is a profile image' do + img = Image.create!(uploaded_data: fixture_file_upload('/files/rails.png', 'image/png')) + profile = fast_create(Person, image_id: img.id) + + get "/api/v1/people/#{profile.id}/icon?#{params.to_query}&size=big" + assert_equal 200, last_response.status + json = JSON.parse(last_response.body) + assert_match /^https?:\/\/.*big\.png$/, json['icon'] + end + + should 'return icon from gravatar without size if there is no profile image' do + profile = create_user('test-user').person + + get "/api/v1/people/#{profile.id}/icon?#{params.to_query}" + assert_equal 200, last_response.status + json = JSON.parse(last_response.body) + assert_match /^https:\/\/www\.gravatar\.com.*size=64/, json['icon'] + end + + should 'return icon from gravatar with size if there is no profile image' do + profile = create_user('test-user').person + + get "/api/v1/people/#{profile.id}/icon?#{params.to_query}&size=big" + assert_equal 200, last_response.status + json = JSON.parse(last_response.body) + assert_match /^https:\/\/www\.gravatar\.com.*size=150/, json['icon'] + end + PERSON_ATTRIBUTES = %w(vote_count comments_count articles_count following_articles_count) PERSON_ATTRIBUTES.map do |attribute| -- libgit2 0.21.2