Merge branch '2044-share-button' refs #2766
[arvados.git] / services / api / app / controllers / arvados / v1 / groups_controller.rb
1 class Arvados::V1::GroupsController < ApplicationController
2
3   def self._contents_requires_parameters
4     _index_requires_parameters.
5       merge({
6               include_linked: {
7                 type: 'boolean', required: false, default: false
8               },
9             })
10   end
11
12   def contents
13     all_objects = []
14     all_available = 0
15
16     # Trick apply_where_limit_order_params into applying suitable
17     # per-table values. *_all are the real ones we'll apply to the
18     # aggregate set.
19     limit_all = @limit
20     offset_all = @offset
21     @orders = []
22
23     [Group, Job, PipelineInstance, PipelineTemplate,
24      Human, Specimen, Trait,
25      Collection].each do |klass|
26       @objects = klass.readable_by(*@read_users)
27       cond_sql = "#{klass.table_name}.owner_uuid = ?"
28       cond_params = [@object.uuid]
29       if params[:include_linked]
30         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})"
31       end
32       @objects = @objects.where(cond_sql, *cond_params).order("#{klass.table_name}.uuid")
33       @limit = limit_all - all_objects.count
34       apply_where_limit_order_params
35       items_available = @objects.
36         except(:limit).except(:offset).
37         count(:id, distinct: true)
38       all_available += items_available
39       @offset = [@offset - items_available, 0].max
40
41       all_objects += @objects.to_a
42     end
43     @objects = all_objects || []
44     @links = Link.where('link_class=? and tail_uuid=?'\
45                         ' and head_uuid in (?)',
46                         'name',
47                         @object.uuid,
48                         @objects.collect(&:uuid))
49     @object_list = {
50       :kind  => "arvados#objectList",
51       :etag => "",
52       :self_link => "",
53       :links => @links.as_api_response(nil),
54       :offset => offset_all,
55       :limit => limit_all,
56       :items_available => all_available,
57       :items => @objects.as_api_response(nil)
58     }
59     render json: @object_list
60   end
61
62 end