diff --git a/app/models/article.rb b/app/models/article.rb index 04516a7..58eeba6 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -701,6 +701,11 @@ class Article < ActiveRecord::Base person ? person.id : nil end + #FIXME make this test + def author_custom_image(size = :icon) + author ? author.profile_custom_image(size) : nil + end + def version_license(version_number = nil) return license if version_number.nil? profile.environment.licenses.find_by_id(get_version(version_number).license_id) diff --git a/app/models/comment.rb b/app/models/comment.rb index d22d978..d9d2e65 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -65,6 +65,11 @@ class Comment < ActiveRecord::Base author ? author.url : nil end + #FIXME make this test + def author_custom_image(size = :icon) + author ? author.profile_custom_image(size) : nil + end + def url article.view_url.merge(:anchor => anchor) end diff --git a/app/models/profile.rb b/app/models/profile.rb index 7338329..d4637ea 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -935,6 +935,13 @@ private :generate_url, :url_options image.public_filename(:icon) if image.present? end + #FIXME make this test + def profile_custom_image(size = :icon) + image_path = profile_custom_icon if size == :icon + image_path ||= image.public_filename(size) if image.present? + image_path + end + def jid(options = {}) domain = options[:domain] || environment.default_hostname "#{identifier}@#{domain}" diff --git a/lib/api/entities.rb b/lib/api/entities.rb index ed5ff0d..4c21c4d 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -2,17 +2,34 @@ module API module Entities class Article < Grape::Entity expose :id, :name, :body, :created_at -# expose :is_admin?, as: :is_admin -# expose :avatar_url do |user, options| -# if user.avatar.present? -# user.avatar.url -# end -# end + expose :author do |article, options| + { + :id => article.author_id, + :name => article.author_name, + :icon_url => article.author_custom_image(:icon), + :minor_url => article.author_custom_image(:minor), + :portrait_url => article.author_custom_image(:portrait), + :thumb_url => article.author_custom_image(:thumb), + } + end + end class Comment < Grape::Entity - expose :author_id, :body, :title, :created_at + expose :body, :title, :created_at, :id + + expose :author do |comment, options| + { + :id => comment.author_id, + :name => comment.author_name, + :icon_url => comment.author_custom_image(:icon), + :minor_url => comment.author_custom_image(:minor), + :portrait_url => comment.author_custom_image(:portrait), + :thumb_url => comment.author_custom_image(:thumb), + } + end + end class User < Grape::Entity diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 853908d..4137a06 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -13,6 +13,16 @@ module API @current_user = nil end + def environment + @environment + end + + def limit + limit = params[:limit].to_i + limit = default_limit if limit <= 0 + limit + end + #FIXME see if its needed # def paginate(relation) # per_page = params[:per_page].to_i @@ -105,5 +115,27 @@ module API error!({'message' => message, :code => status}, status) end + protected + + def detect_stuff_by_domain + @domain = Domain.find_by_name(request.host) + if @domain.nil? + @environment = Environment.default + if @environment.nil? && Rails.env.development? + # This should only happen in development ... + @environment = Environment.create!(:name => "Noosfero", :is_default => true) + end + else + @environment = @domain.environment + end + end + + private + + def default_limit + 20 + end + + end end diff --git a/lib/api/session.rb b/lib/api/session.rb index c00486d..0260c2f 100644 --- a/lib/api/session.rb +++ b/lib/api/session.rb @@ -11,7 +11,6 @@ module API # Example Request: # POST /login?login=some&password=pass get "/login" do -environment = nil #FIXME load the correct environment create a method in helper user ||= User.authenticate(params[:login], params[:password], environment) return unauthorized! unless user diff --git a/lib/api/v1/articles.rb b/lib/api/v1/articles.rb index 9a49cf2..c1eaab0 100644 --- a/lib/api/v1/articles.rb +++ b/lib/api/v1/articles.rb @@ -2,44 +2,55 @@ module API module V1 class Articles < Grape::API + before { detect_stuff_by_domain } before { authenticate! } resource :articles do - #FIXME See if it's possible to use pagination instead of DateTime control. see a way to use this pagination logic genericaly + helpers do + def default_from_date + @article_created_at ||= Article.first.created_at + @article_created_at + end + end + + # Collect comments from articles + # + # Parameters: + # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created + # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected + # limit - amount of comments returned. The default value is 20 + # + # Example Request: + # GET /articles?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10&type=Blog get do - first_update = DateTime.parse(params[:first_update]) if params[:first_update] - last_update = DateTime.parse(params[:last_update]) if params[:last_update] + from_date = DateTime.parse(params[:from]) if params[:from] + until_date = DateTime.parse(params[:until]) if params[:until] - if first_update.nil? - begin_date = Article.first.created_at - end_date = last_update.nil? ? DateTime.now : last_update + if from_date.nil? + begin_period = default_from_date + end_period = until_date.nil? ? DateTime.now : until_date else - begin_date = first_update - end_date = DateTime.now + begin_period = from_date + end_period = DateTime.now end - limit = params[:limit].to_i - limit = 20 if limit == 0 conditions = {} conditions[:type] = params[:content_type] if params[:content_type] #FIXME validate type - conditions[:created_at] = begin_date...end_date - present Article.find(:all, :conditions => conditions, :offset => (first_update.nil? ? 0 : 1), :limit => limit, :order => "created_at DESC"), :with => Entities::Article + conditions[:created_at] = begin_period...end_period + present environment.articles.find(:all, :conditions => conditions, :offset => (from_date.nil? ? 0 : 1), :limit => limit, :order => "created_at DESC"), :with => Entities::Article end - #FIXME load article with environment context get ':id' do - present Article.find(params[:id]), :with => Entities::Article + present environment.articles.find(params[:id]), :with => Entities::Article end - #FIXME load article with environment context get ':id/children' do - present Article.find(params[:id]).children, :with => Entities::Article + present environment.articles.find(params[:id]).children, :with => Entities::Article end - #FIXME load article with environment context get ':id/children/:child_id' do - present Article.find(params[:id]).children.find(params[:child_id]), :with => Entities::Article + present environment.articles.find(params[:id]).children.find(params[:child_id]), :with => Entities::Article end diff --git a/lib/api/v1/comments.rb b/lib/api/v1/comments.rb index 3b9f386..df6000f 100644 --- a/lib/api/v1/comments.rb +++ b/lib/api/v1/comments.rb @@ -1,22 +1,32 @@ module API module V1 - class Comments < Grape::API - - before { authenticate! } - - resource :articles do - #FIXME make the pagination - #FIXME put it on environment context - get ":id/comments" do - present Article.find(params[:id]).comments, :with => Entities::Comment + class Comments < Grape::API + + before { detect_stuff_by_domain } + before { authenticate! } + + resource :articles do + # Collect comments from articles + # + # Parameters: + # reference_id - comment id used as reference to collect comment + # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected + # limit - amount of comments returned. The default value is 20 + # + # Example Request: + # GET /articles/12/comments?oldest&limit=10&reference_id=23 + get ":id/comments" do + conditions = {} + conditions = ["id #{params.key?(:oldest) ? '<' : '>'} ?", params[:reference_id]] if params[:reference_id] + present environment.articles.find(params[:id]).comments.find(:all, :conditions => conditions, :limit => limit), :with => Entities::Comment + end + + get ":id/comments/:comment_id" do + present environment.articles.find(params[:id]).comments.find(params[:comment_id]), :with => Entities::Comment + end + end - - get ":id/comments/:comment_id" do - present Article.find(params[:id]).comments.find(params[:comment_id]), :with => Entities::Comment - end - + end - - end end end -- libgit2 0.21.2