Merge branch 'master' into 8079-api-client-auth-uuid
[arvados.git] / services / api / app / controllers / arvados / v1 / groups_controller.rb
index c1d414e47f1c0694f0cc55055549e87da1c6f16b..eae6dca8c0332ae820fbedbb3965f3112453dfb9 100644 (file)
@@ -1,17 +1,14 @@
 class Arvados::V1::GroupsController < ApplicationController
 
   def self._contents_requires_parameters
-    _index_requires_parameters.
+    params = _index_requires_parameters.
       merge({
               uuid: {
                 type: 'string', required: false, default: nil
               },
-              # include_linked returns name links, which are obsolete, so
-              # remove it when clients have been migrated.
-              include_linked: {
-                type: 'boolean', required: false, default: false
-              },
             })
+    params.delete(:select)
+    params
   end
 
   def render_404_if_no_object
@@ -35,34 +32,21 @@ class Arvados::V1::GroupsController < ApplicationController
   end
 
   def contents
-    # Set @objects:
-    # include_linked returns name links, which are obsolete, so
-    # remove it when clients have been migrated.
-    load_searchable_objects(owner_uuid: @object.andand.uuid,
-                            include_linked: params[:include_linked])
-    sql = 'link_class=? and head_uuid in (?)'
-    sql_params = ['name', @objects.collect(&:uuid)]
-    if @object
-      sql += ' and tail_uuid=?'
-      sql_params << @object.uuid
-    end
-    @links = Link.where sql, *sql_params
-    @object_list = {
-      :kind  => "arvados#objectList",
+    load_searchable_objects
+    send_json({
+      :kind => "arvados#objectList",
       :etag => "",
       :self_link => "",
-      :links => @links.as_api_response(nil),
       :offset => @offset,
       :limit => @limit,
       :items_available => @items_available,
       :items => @objects.as_api_response(nil)
-    }
-    render json: @object_list
+    })
   end
 
   protected
 
-  def load_searchable_objects opts
+  def load_searchable_objects
     all_objects = []
     @items_available = 0
 
@@ -71,37 +55,41 @@ class Arvados::V1::GroupsController < ApplicationController
     # aggregate set.
     limit_all = @limit
     offset_all = @offset
+    # save the orders from the current request as determined by load_param,
+    # but otherwise discard them because we're going to be getting objects
+    # from many models
+    request_orders = @orders.clone
     @orders = []
 
     [Group,
      Job, PipelineInstance, PipelineTemplate,
      Collection,
      Human, Specimen, Trait].each do |klass|
-      @objects = klass.readable_by(*@read_users)
-      if klass == Group
-        @objects = @objects.where(group_class: 'project')
-      end
-      if opts[:owner_uuid]
-        conds = []
-        cond_params = []
-        conds << "#{klass.table_name}.owner_uuid = ?"
-        cond_params << opts[:owner_uuid]
-        if conds.any?
-          cond_sql = '(' + conds.join(') OR (') + ')'
-          @objects = @objects.where(cond_sql, *cond_params)
-        end
+      # If the currently requested orders specifically match the
+      # table_name for the current klass, apply that order.
+      # Otherwise, order by recency.
+      request_order =
+        request_orders.andand.find { |r| r =~ /^#{klass.table_name}\./i } ||
+        klass.default_orders.join(", ")
+
+      @select = nil
+      where_conds = {}
+      where_conds[:owner_uuid] = @object.uuid if @object
+      if klass == Collection
+        @select = klass.selectable_attributes - ["manifest_text"]
+      elsif klass == Group
+        where_conds[:group_class] = "project"
       end
 
-      @objects = @objects.order("#{klass.table_name}.uuid")
+      @objects = klass.readable_by(*@read_users).
+        order(request_order).where(where_conds)
       @limit = limit_all - all_objects.count
-      apply_where_limit_order_params
-      klass_items_available = @objects.
-        except(:limit).except(:offset).
-        count(:id, distinct: true)
+      apply_where_limit_order_params klass
+      klass_object_list = object_list
+      klass_items_available = klass_object_list[:items_available] || 0
       @items_available += klass_items_available
       @offset = [@offset - klass_items_available, 0].max
-
-      all_objects += @objects.to_a
+      all_objects += klass_object_list[:items]
     end
 
     @objects = all_objects