8784: Fix test for latest firefox.
[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     params = _index_requires_parameters.
5       merge({
6               uuid: {
7                 type: 'string', required: false, default: nil
8               },
9               recursive: {
10                 type: 'boolean', required: false, description: 'Include contents from child groups recursively.'
11               },
12             })
13     params.delete(:select)
14     params
15   end
16
17   def render_404_if_no_object
18     if params[:action] == 'contents'
19       if !params[:uuid]
20         # OK!
21         @object = nil
22         true
23       elsif @object
24         # Project group
25         true
26       elsif (@object = User.where(uuid: params[:uuid]).first)
27         # "Home" pseudo-project
28         true
29       else
30         super
31       end
32     else
33       super
34     end
35   end
36
37   def contents
38     load_searchable_objects
39     send_json({
40       :kind => "arvados#objectList",
41       :etag => "",
42       :self_link => "",
43       :offset => @offset,
44       :limit => @limit,
45       :items_available => @items_available,
46       :items => @objects.as_api_response(nil)
47     })
48   end
49
50   protected
51
52   def load_searchable_objects
53     all_objects = []
54     @items_available = 0
55
56     # Trick apply_where_limit_order_params into applying suitable
57     # per-table values. *_all are the real ones we'll apply to the
58     # aggregate set.
59     limit_all = @limit
60     offset_all = @offset
61     # save the orders from the current request as determined by load_param,
62     # but otherwise discard them because we're going to be getting objects
63     # from many models
64     request_orders = @orders.clone
65     @orders = []
66
67     request_filters = @filters
68
69     klasses = [Group,
70      Job, PipelineInstance, PipelineTemplate, ContainerRequest, Workflow,
71      Collection,
72      Human, Specimen, Trait]
73
74     table_names = Hash[klasses.collect { |k| [k, k.table_name] }]
75
76     disabled_methods = Rails.configuration.disable_api_methods
77     avail_klasses = table_names.select{|k, t| !disabled_methods.include?(t+'.index')}
78     klasses = avail_klasses.keys
79
80     request_filters.each do |col, op, val|
81       if col.index('.') && !table_names.values.include?(col.split('.', 2)[0])
82         raise ArgumentError.new("Invalid attribute '#{col}' in filter")
83       end
84     end
85
86     wanted_klasses = []
87     request_filters.each do |col,op,val|
88       if op == 'is_a'
89         (val.is_a?(Array) ? val : [val]).each do |type|
90           type = type.split('#')[-1]
91           type[0] = type[0].capitalize
92           wanted_klasses << type
93         end
94       end
95     end
96
97     filter_by_owner = {}
98     if @object
99       if params['recursive']
100         filter_by_owner[:owner_uuid] = [@object.uuid] + @object.descendant_project_uuids
101       else
102         filter_by_owner[:owner_uuid] = @object.uuid
103       end
104     end
105
106     seen_last_class = false
107     klasses.each do |klass|
108       @offset = 0 if seen_last_class  # reset offset for the new next type being processed
109
110       # if current klass is same as params['last_object_class'], mark that fact
111       seen_last_class = true if((params['count'].andand.==('none')) and
112                                 (params['last_object_class'].nil? or
113                                  params['last_object_class'].empty? or
114                                  params['last_object_class'] == klass.to_s))
115
116       # if klasses are specified, skip all other klass types
117       next if wanted_klasses.any? and !wanted_klasses.include?(klass.to_s)
118
119       # don't reprocess klass types that were already seen
120       next if params['count'] == 'none' and !seen_last_class
121
122       # don't process rest of object types if we already have needed number of objects
123       break if params['count'] == 'none' and all_objects.size >= limit_all
124
125       # If the currently requested orders specifically match the
126       # table_name for the current klass, apply that order.
127       # Otherwise, order by recency.
128       request_order =
129         request_orders.andand.find { |r| r =~ /^#{klass.table_name}\./i } ||
130         klass.default_orders.join(", ")
131
132       @select = nil
133       where_conds = filter_by_owner
134       if klass == Collection
135         @select = klass.selectable_attributes - ["manifest_text"]
136       elsif klass == Group
137         where_conds = where_conds.merge(group_class: "project")
138       end
139
140       @filters = request_filters.map do |col, op, val|
141         if !col.index('.')
142           [col, op, val]
143         elsif (col = col.split('.', 2))[0] == klass.table_name
144           [col[1], op, val]
145         else
146           nil
147         end
148       end.compact
149
150       if klass == Collection and params[:include_trash]
151         @objects = klass.unscoped.readable_by(*@read_users).
152           order(request_order).where(where_conds)
153       else
154         @objects = klass.readable_by(*@read_users).
155           order(request_order).where(where_conds)
156       end
157       klass_limit = limit_all - all_objects.count
158       @limit = klass_limit
159       apply_where_limit_order_params klass
160       klass_object_list = object_list(model_class: klass)
161       klass_items_available = klass_object_list[:items_available] || 0
162       @items_available += klass_items_available
163       @offset = [@offset - klass_items_available, 0].max
164       all_objects += klass_object_list[:items]
165
166       if klass_object_list[:limit] < klass_limit
167         # object_list() had to reduce @limit to comply with
168         # max_index_database_read. From now on, we'll do all queries
169         # with limit=0 and just accumulate items_available.
170         limit_all = all_objects.count
171       end
172     end
173
174     @objects = all_objects
175     @limit = limit_all
176     @offset = offset_all
177   end
178
179 end