Commit 57ac8d09079918715d8932a10a29632d3c2fcfd2

Authored by Caio Almeida
Committed by Larissa Reis
1 parent 66f71956

Ticket #262: Adding API endpoint to get profile icon

app/api/helpers.rb
... ... @@ -10,6 +10,9 @@ module Api
10 10 include Noosfero::Plugin::HotSpot
11 11 include ForgotPasswordHelper
12 12 include SearchTermHelper
  13 + include ProfileImageHelper
  14 + include Noosfero::Gravatar
  15 + include ThemeLoaderHelper
13 16  
14 17 def set_locale
15 18 I18n.locale = (params[:lang] || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en')
... ...
app/api/v1/people.rb
... ... @@ -108,6 +108,23 @@ module Api
108 108 end
109 109 present output
110 110 end
  111 +
  112 + desc "Return the person profile picture (you can optionally pass a 'size' parameter)"
  113 + get ":id/icon" do
  114 + person = environment.people.find(params[:id])
  115 +
  116 + size = params[:size] || :portrait
  117 + image = profile_icon(person, size.to_sym)
  118 + output = {}
  119 +
  120 + unless image.match(/^\/\/www\.gravatar\.com/).nil?
  121 + output[:icon] = 'https:' + image
  122 + else
  123 + output[:icon] = request.url.gsub(/\/api\/.*/, '') + image
  124 + end
  125 +
  126 + present output
  127 + end
111 128 end
112 129  
113 130 resource :profiles do
... ...
app/helpers/theme_loader_helper.rb
... ... @@ -2,7 +2,7 @@ module ThemeLoaderHelper
2 2 def current_theme
3 3 @current_theme ||=
4 4 begin
5   - if session[:user_theme]
  5 + if !(defined?(session)).nil? && session[:user_theme]
6 6 session[:user_theme]
7 7 else
8 8 # utility for developers: set the theme to 'random' in development mode and
... ... @@ -14,7 +14,7 @@ module ThemeLoaderHelper
14 14 elsif Rails.env.development? && respond_to?(:params) && params[:user_theme] && File.exists?(Rails.root.join('public/designs/themes', params[:user_theme]))
15 15 params[:user_theme]
16 16 else
17   - if profile && !profile.theme.nil?
  17 + if defined?(profile) && profile && !profile.theme.nil?
18 18 profile.theme
19 19 elsif environment
20 20 environment.theme
... ... @@ -34,7 +34,7 @@ module ThemeLoaderHelper
34 34 end
35 35  
36 36 def theme_path
37   - if session[:user_theme]
  37 + if !(defined?(session)).nil? && session[:user_theme]
38 38 '/user_themes/' + current_theme
39 39 elsif session[:theme]
40 40 '/designs/themes/' + session[:theme]
... ...
test/api/people_test.rb
... ... @@ -369,6 +369,44 @@ class PeopleTest < ActiveSupport::TestCase
369 369 assert_equal "www.blog.org", json['person']['additional_data']['Custom Blog']
370 370 end
371 371  
  372 + should 'return portrait icon if size is not provided and there is a profile image' do
  373 + img = Image.create!(uploaded_data: fixture_file_upload('/files/rails.png', 'image/png'))
  374 + profile = fast_create(Person, image_id: img.id)
  375 +
  376 + get "/api/v1/people/#{profile.id}/icon?#{params.to_query}"
  377 + assert_equal 200, last_response.status
  378 + json = JSON.parse(last_response.body)
  379 + assert_match /^https?:\/\/.*portrait\.png$/, json['icon']
  380 + end
  381 +
  382 + should 'return icon in provided size if there is a profile image' do
  383 + img = Image.create!(uploaded_data: fixture_file_upload('/files/rails.png', 'image/png'))
  384 + profile = fast_create(Person, image_id: img.id)
  385 +
  386 + get "/api/v1/people/#{profile.id}/icon?#{params.to_query}&size=big"
  387 + assert_equal 200, last_response.status
  388 + json = JSON.parse(last_response.body)
  389 + assert_match /^https?:\/\/.*big\.png$/, json['icon']
  390 + end
  391 +
  392 + should 'return icon from gravatar without size if there is no profile image' do
  393 + profile = create_user('test-user').person
  394 +
  395 + get "/api/v1/people/#{profile.id}/icon?#{params.to_query}"
  396 + assert_equal 200, last_response.status
  397 + json = JSON.parse(last_response.body)
  398 + assert_match /^https:\/\/www\.gravatar\.com.*size=64/, json['icon']
  399 + end
  400 +
  401 + should 'return icon from gravatar with size if there is no profile image' do
  402 + profile = create_user('test-user').person
  403 +
  404 + get "/api/v1/people/#{profile.id}/icon?#{params.to_query}&size=big"
  405 + assert_equal 200, last_response.status
  406 + json = JSON.parse(last_response.body)
  407 + assert_match /^https:\/\/www\.gravatar\.com.*size=150/, json['icon']
  408 + end
  409 +
372 410 PERSON_ATTRIBUTES = %w(vote_count comments_count articles_count following_articles_count)
373 411  
374 412 PERSON_ATTRIBUTES.map do |attribute|
... ...