Merge branch '3411-expire-collections'
[arvados.git] / services / api / app / controllers / arvados / v1 / groups_controller.rb
index 49f0b02c5fdd84d9a8605ba33b7d3f71fdee0f8d..9fca207dd2140c858a87708d4879ed12fa096919 100644 (file)
@@ -3,15 +3,68 @@ class Arvados::V1::GroupsController < ApplicationController
   def self._contents_requires_parameters
     _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
               },
             })
   end
 
+  def render_404_if_no_object
+    if params[:action] == 'contents'
+      if !params[:uuid]
+        # OK!
+        @object = nil
+        true
+      elsif @object
+        # Project group
+        true
+      elsif (@object = User.where(uuid: params[:uuid]).first)
+        # "Home" pseudo-project
+        true
+      else
+        super
+      end
+    else
+      super
+    end
+  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",
+      :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
     all_objects = []
-    all_available = 0
+    @items_available = 0
 
     # Trick apply_where_limit_order_params into applying suitable
     # per-table values. *_all are the real ones we'll apply to the
@@ -20,51 +73,40 @@ class Arvados::V1::GroupsController < ApplicationController
     offset_all = @offset
     @orders = []
 
-    ArvadosModel.descendants.reject(&:abstract_class?).sort_by(&:to_s).
-      each do |klass|
-      case klass.to_s
-        # We might expect klass==Link etc. here, but we would be
-        # disappointed: when Rails reloads model classes, we get two
-        # distinct classes called Link which do not equal each
-        # other. But we can still rely on klass.to_s to be "Link".
-      when 'ApiClientAuthorization', 'UserAgreement', 'Link'
-        # Do not want.
-      else
-        @objects = klass.readable_by(*@read_users)
-        cond_sql = "#{klass.table_name}.owner_uuid = ?"
-        cond_params = [@object.uuid]
-        if params[:include_linked]
-          cond_sql += " OR #{klass.table_name}.uuid IN (SELECT head_uuid FROM links WHERE link_class=#{klass.sanitize 'name'} AND links.tail_uuid=#{klass.sanitize @object.uuid})"
+    [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
-        @objects = @objects.where(cond_sql, *cond_params).order("#{klass.table_name}.uuid")
-        @limit = limit_all - all_objects.count
-        apply_where_limit_order_params
-        items_available = @objects.
-          except(:limit).except(:offset).
-          count(:id, distinct: true)
-        all_available += items_available
-        @offset = [@offset - items_available, 0].max
-
-        all_objects += @objects.to_a
       end
+
+      @objects = @objects.order("#{klass.table_name}.uuid")
+      @limit = limit_all - all_objects.count
+      apply_where_limit_order_params klass
+      klass_items_available = @objects.
+        except(:limit).except(:offset).
+        count(:id, distinct: true)
+      @items_available += klass_items_available
+      @offset = [@offset - klass_items_available, 0].max
+
+      all_objects += @objects.to_a
     end
-    @objects = all_objects || []
-    @links = Link.where('link_class=? and tail_uuid=?'\
-                        ' and head_uuid in (?)',
-                        'name',
-                        @object.uuid,
-                        @objects.collect(&:uuid))
-    @object_list = {
-      :kind  => "arvados#objectList",
-      :etag => "",
-      :self_link => "",
-      :links => @links.as_api_response(nil),
-      :offset => offset_all,
-      :limit => limit_all,
-      :items_available => all_available,
-      :items => @objects.as_api_response(nil)
-    }
-    render json: @object_list
+
+    @objects = all_objects
+    @limit = limit_all
+    @offset = offset_all
   end
 
 end