Merge branch '12377-workbench-composer-link' refs #12377
[arvados.git] / apps / workbench / app / controllers / repositories_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class RepositoriesController < ApplicationController
6   before_filter :set_share_links, if: -> { defined? @object }
7
8   def index_pane_list
9     %w(repositories help)
10   end
11
12   def show_pane_list
13     if @user_is_manager
14       panes = super | %w(Sharing)
15       panes.insert(panes.length-1, panes.delete_at(panes.index('Advanced'))) if panes.index('Advanced')
16       panes
17     else
18       panes = super
19     end
20     panes.delete('Attributes') if !current_user.is_admin
21     panes
22   end
23
24   def show_tree
25     @commit = params[:commit]
26     @path = params[:path] || ''
27     @subtree = @object.ls_subtree @commit, @path.chomp('/')
28   end
29
30   def show_blob
31     @commit = params[:commit]
32     @path = params[:path]
33     @blobdata = @object.cat_file @commit, @path
34   end
35
36   def show_commit
37     @commit = params[:commit]
38   end
39
40   def all_repos
41     limit = params[:limit].andand.to_i || 100
42     offset = params[:offset].andand.to_i || 0
43     @filters = params[:filters] || []
44
45     if @filters.any?
46       owner_filter = @filters.select do |attr, op, val|
47         (attr == 'owner_uuid')
48       end
49     end
50
51     if !owner_filter.andand.any?
52       filters = @filters + [["owner_uuid", "=", current_user.uuid]]
53       my_repos = Repository.all.order("name ASC").limit(limit).offset(offset).filter(filters).results
54     else      # done fetching all owned repositories
55       my_repos = []
56     end
57
58     if !owner_filter.andand.any?  # if this is next page request, the first page was still fetching "own" repos
59       @filters = @filters.reject do |attr, op, val|
60         (attr == 'owner_uuid') or
61         (attr == 'name') or
62         (attr == 'uuid')
63       end
64     end
65
66     filters = @filters + [["owner_uuid", "!=", current_user.uuid]]
67     other_repos = Repository.all.order("name ASC").limit(limit).offset(offset).filter(filters).results
68
69     @objects = (my_repos + other_repos).first(limit)
70   end
71
72   def find_objects_for_index
73     return if !params[:partial]
74
75     all_repos
76
77     if @objects.any?
78       @next_page_filters = next_page_filters('>=')
79       @next_page_href = url_for(partial: :repositories_rows,
80                                 filters: @next_page_filters.to_json)
81     else
82       @next_page_href = nil
83     end
84   end
85
86   def next_page_href with_params={}
87     @next_page_href
88   end
89
90   def next_page_filters nextpage_operator
91     next_page_filters = @filters.reject do |attr, op, val|
92       (attr == 'owner_uuid') or
93       (attr == 'name' and op == nextpage_operator) or
94       (attr == 'uuid' and op == 'not in')
95     end
96
97     if @objects.any?
98       last_obj = @objects.last
99       next_page_filters += [['name', nextpage_operator, last_obj.name]]
100       next_page_filters += [['uuid', 'not in', [last_obj.uuid]]]
101       # if not-owned, it means we are done with owned repos and fetching other repos
102       next_page_filters += [['owner_uuid', '!=', last_obj.uuid]] if last_obj.owner_uuid != current_user.uuid
103     end
104
105     next_page_filters
106   end
107 end