Commit ef06ad0b9b491ab2a8bd8f4767c22720d470abe5

Authored by Leandro Santos
Committed by Rodrigo Souto
1 parent c6b5c34e

expose image urls

app/models/article.rb
@@ -701,6 +701,11 @@ class Article < ActiveRecord::Base @@ -701,6 +701,11 @@ class Article < ActiveRecord::Base
701 person ? person.id : nil 701 person ? person.id : nil
702 end 702 end
703 703
  704 + #FIXME make this test
  705 + def author_custom_image(size = :icon)
  706 + author ? author.profile_custom_image(size) : nil
  707 + end
  708 +
704 def version_license(version_number = nil) 709 def version_license(version_number = nil)
705 return license if version_number.nil? 710 return license if version_number.nil?
706 profile.environment.licenses.find_by_id(get_version(version_number).license_id) 711 profile.environment.licenses.find_by_id(get_version(version_number).license_id)
app/models/comment.rb
@@ -65,6 +65,11 @@ class Comment < ActiveRecord::Base @@ -65,6 +65,11 @@ class Comment < ActiveRecord::Base
65 author ? author.url : nil 65 author ? author.url : nil
66 end 66 end
67 67
  68 + #FIXME make this test
  69 + def author_custom_image(size = :icon)
  70 + author ? author.profile_custom_image(size) : nil
  71 + end
  72 +
68 def url 73 def url
69 article.view_url.merge(:anchor => anchor) 74 article.view_url.merge(:anchor => anchor)
70 end 75 end
app/models/profile.rb
@@ -935,6 +935,13 @@ private :generate_url, :url_options @@ -935,6 +935,13 @@ private :generate_url, :url_options
935 image.public_filename(:icon) if image.present? 935 image.public_filename(:icon) if image.present?
936 end 936 end
937 937
  938 + #FIXME make this test
  939 + def profile_custom_image(size = :icon)
  940 + image_path = profile_custom_icon if size == :icon
  941 + image_path ||= image.public_filename(size) if image.present?
  942 + image_path
  943 + end
  944 +
938 def jid(options = {}) 945 def jid(options = {})
939 domain = options[:domain] || environment.default_hostname 946 domain = options[:domain] || environment.default_hostname
940 "#{identifier}@#{domain}" 947 "#{identifier}@#{domain}"
lib/api/entities.rb
@@ -2,17 +2,34 @@ module API @@ -2,17 +2,34 @@ module API
2 module Entities 2 module Entities
3 class Article < Grape::Entity 3 class Article < Grape::Entity
4 expose :id, :name, :body, :created_at 4 expose :id, :name, :body, :created_at
5 -# expose :is_admin?, as: :is_admin  
6 5
7 -# expose :avatar_url do |user, options|  
8 -# if user.avatar.present?  
9 -# user.avatar.url  
10 -# end  
11 -# end 6 + expose :author do |article, options|
  7 + {
  8 + :id => article.author_id,
  9 + :name => article.author_name,
  10 + :icon_url => article.author_custom_image(:icon),
  11 + :minor_url => article.author_custom_image(:minor),
  12 + :portrait_url => article.author_custom_image(:portrait),
  13 + :thumb_url => article.author_custom_image(:thumb),
  14 + }
  15 + end
  16 +
12 end 17 end
13 18
14 class Comment < Grape::Entity 19 class Comment < Grape::Entity
15 - expose :author_id, :body, :title, :created_at 20 + expose :body, :title, :created_at, :id
  21 +
  22 + expose :author do |comment, options|
  23 + {
  24 + :id => comment.author_id,
  25 + :name => comment.author_name,
  26 + :icon_url => comment.author_custom_image(:icon),
  27 + :minor_url => comment.author_custom_image(:minor),
  28 + :portrait_url => comment.author_custom_image(:portrait),
  29 + :thumb_url => comment.author_custom_image(:thumb),
  30 + }
  31 + end
  32 +
16 end 33 end
17 34
18 class User < Grape::Entity 35 class User < Grape::Entity
lib/api/helpers.rb
@@ -13,6 +13,16 @@ module API @@ -13,6 +13,16 @@ module API
13 @current_user = nil 13 @current_user = nil
14 end 14 end
15 15
  16 + def environment
  17 + @environment
  18 + end
  19 +
  20 + def limit
  21 + limit = params[:limit].to_i
  22 + limit = default_limit if limit <= 0
  23 + limit
  24 + end
  25 +
16 #FIXME see if its needed 26 #FIXME see if its needed
17 # def paginate(relation) 27 # def paginate(relation)
18 # per_page = params[:per_page].to_i 28 # per_page = params[:per_page].to_i
@@ -105,5 +115,27 @@ module API @@ -105,5 +115,27 @@ module API
105 error!({'message' => message, :code => status}, status) 115 error!({'message' => message, :code => status}, status)
106 end 116 end
107 117
  118 + protected
  119 +
  120 + def detect_stuff_by_domain
  121 + @domain = Domain.find_by_name(request.host)
  122 + if @domain.nil?
  123 + @environment = Environment.default
  124 + if @environment.nil? && Rails.env.development?
  125 + # This should only happen in development ...
  126 + @environment = Environment.create!(:name => "Noosfero", :is_default => true)
  127 + end
  128 + else
  129 + @environment = @domain.environment
  130 + end
  131 + end
  132 +
  133 + private
  134 +
  135 + def default_limit
  136 + 20
  137 + end
  138 +
  139 +
108 end 140 end
109 end 141 end
lib/api/session.rb
@@ -11,7 +11,6 @@ module API @@ -11,7 +11,6 @@ module API
11 # Example Request: 11 # Example Request:
12 # POST /login?login=some&password=pass 12 # POST /login?login=some&password=pass
13 get "/login" do 13 get "/login" do
14 -environment = nil #FIXME load the correct environment create a method in helper  
15 user ||= User.authenticate(params[:login], params[:password], environment) 14 user ||= User.authenticate(params[:login], params[:password], environment)
16 15
17 return unauthorized! unless user 16 return unauthorized! unless user
lib/api/v1/articles.rb
@@ -2,44 +2,55 @@ module API @@ -2,44 +2,55 @@ module API
2 module V1 2 module V1
3 class Articles < Grape::API 3 class Articles < Grape::API
4 4
  5 + before { detect_stuff_by_domain }
5 before { authenticate! } 6 before { authenticate! }
6 7
7 resource :articles do 8 resource :articles do
8 9
9 - #FIXME See if it's possible to use pagination instead of DateTime control. see a way to use this pagination logic genericaly 10 + helpers do
  11 + def default_from_date
  12 + @article_created_at ||= Article.first.created_at
  13 + @article_created_at
  14 + end
  15 + end
  16 +
  17 + # Collect comments from articles
  18 + #
  19 + # Parameters:
  20 + # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created
  21 + # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
  22 + # limit - amount of comments returned. The default value is 20
  23 + #
  24 + # Example Request:
  25 + # GET /articles?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10&type=Blog
10 get do 26 get do
11 - first_update = DateTime.parse(params[:first_update]) if params[:first_update]  
12 - last_update = DateTime.parse(params[:last_update]) if params[:last_update] 27 + from_date = DateTime.parse(params[:from]) if params[:from]
  28 + until_date = DateTime.parse(params[:until]) if params[:until]
13 29
14 - if first_update.nil?  
15 - begin_date = Article.first.created_at  
16 - end_date = last_update.nil? ? DateTime.now : last_update 30 + if from_date.nil?
  31 + begin_period = default_from_date
  32 + end_period = until_date.nil? ? DateTime.now : until_date
17 else 33 else
18 - begin_date = first_update  
19 - end_date = DateTime.now 34 + begin_period = from_date
  35 + end_period = DateTime.now
20 end 36 end
21 37
22 - limit = params[:limit].to_i  
23 - limit = 20 if limit == 0  
24 conditions = {} 38 conditions = {}
25 conditions[:type] = params[:content_type] if params[:content_type] #FIXME validate type 39 conditions[:type] = params[:content_type] if params[:content_type] #FIXME validate type
26 - conditions[:created_at] = begin_date...end_date  
27 - present Article.find(:all, :conditions => conditions, :offset => (first_update.nil? ? 0 : 1), :limit => limit, :order => "created_at DESC"), :with => Entities::Article 40 + conditions[:created_at] = begin_period...end_period
  41 + present environment.articles.find(:all, :conditions => conditions, :offset => (from_date.nil? ? 0 : 1), :limit => limit, :order => "created_at DESC"), :with => Entities::Article
28 end 42 end
29 43
30 - #FIXME load article with environment context  
31 get ':id' do 44 get ':id' do
32 - present Article.find(params[:id]), :with => Entities::Article 45 + present environment.articles.find(params[:id]), :with => Entities::Article
33 end 46 end
34 47
35 - #FIXME load article with environment context  
36 get ':id/children' do 48 get ':id/children' do
37 - present Article.find(params[:id]).children, :with => Entities::Article 49 + present environment.articles.find(params[:id]).children, :with => Entities::Article
38 end 50 end
39 51
40 - #FIXME load article with environment context  
41 get ':id/children/:child_id' do 52 get ':id/children/:child_id' do
42 - present Article.find(params[:id]).children.find(params[:child_id]), :with => Entities::Article 53 + present environment.articles.find(params[:id]).children.find(params[:child_id]), :with => Entities::Article
43 end 54 end
44 55
45 56
lib/api/v1/comments.rb
1 module API 1 module API
2 module V1 2 module V1
3 - class Comments < Grape::API  
4 -  
5 - before { authenticate! }  
6 -  
7 - resource :articles do  
8 - #FIXME make the pagination  
9 - #FIXME put it on environment context  
10 - get ":id/comments" do  
11 - present Article.find(params[:id]).comments, :with => Entities::Comment 3 + class Comments < Grape::API
  4 +
  5 + before { detect_stuff_by_domain }
  6 + before { authenticate! }
  7 +
  8 + resource :articles do
  9 + # Collect comments from articles
  10 + #
  11 + # Parameters:
  12 + # reference_id - comment id used as reference to collect comment
  13 + # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
  14 + # limit - amount of comments returned. The default value is 20
  15 + #
  16 + # Example Request:
  17 + # GET /articles/12/comments?oldest&limit=10&reference_id=23
  18 + get ":id/comments" do
  19 + conditions = {}
  20 + conditions = ["id #{params.key?(:oldest) ? '<' : '>'} ?", params[:reference_id]] if params[:reference_id]
  21 + present environment.articles.find(params[:id]).comments.find(:all, :conditions => conditions, :limit => limit), :with => Entities::Comment
  22 + end
  23 +
  24 + get ":id/comments/:comment_id" do
  25 + present environment.articles.find(params[:id]).comments.find(params[:comment_id]), :with => Entities::Comment
  26 + end
  27 +
12 end 28 end
13 -  
14 - get ":id/comments/:comment_id" do  
15 - present Article.find(params[:id]).comments.find(params[:comment_id]), :with => Entities::Comment  
16 - end  
17 - 29 +
18 end 30 end
19 -  
20 - end  
21 end 31 end
22 end 32 end