Include repository admin URLs in repo/user view state
[gitorious:mainline.git] / lib / gitorious / view_state / user_repository.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2013 Gitorious AS
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18
19 module Gitorious
20   module ViewState
21     class UserRepository
22       def initialize(app, repository, user)
23         @app = app
24         @repository = repository
25         @user = user
26       end
27
28       def to_json
29         return "{}" if user.nil?
30         JSON.dump({
31           "user" => user_hash,
32           "repository" => repository_hash
33         })
34       end
35
36       private
37       def user_hash
38         {
39           "login" => user.login,
40           "unreadMessageCount" => user.unread_message_count,
41           "dashboardPath" => app.root_path,
42           "profilePath" => app.user_path(user),
43           "editPath" => app.edit_user_path(user),
44           "messagesPath" => app.messages_path,
45           "logoutPath" => app.logout_path
46         }
47       end
48
49       def repository_hash
50         return nil if repository.nil?
51         is_admin = !!app.admin?(user, repository)
52         {
53           "administrator" => is_admin,
54           "watching" => user.watching?(repository),
55           "cloneProtocols" => clone_protocols
56         }.merge(is_admin ? repo_admin_hash : {})
57       end
58
59       def clone_protocols
60         { "protocols" => [] }.tap do |cp|
61           cp["protocols"] << "git" if repository.git_cloning?
62           cp["protocols"] << "http" if repository.http_cloning?
63
64           if repository.display_ssh_url?(user)
65             cp["protocols"] << "ssh"
66             cp["default"] = "ssh"
67           else
68             cp["default"] = repository.default_clone_protocol
69           end
70         end
71       end
72
73       def repo_admin_hash
74         { "admin" => {
75             "editPath" => app.edit_project_repository_path(project, repository),
76             "destroyPath" => app.confirm_delete_project_repository_path(project, repository),
77             "ownershipPath" => app.transfer_ownership_project_repository_path(project, repository),
78             "committershipsPath" => app.project_repository_committerships_path(project, repository)
79           }.tap do |admin|
80             admin["membershipsPath"] = app.project_repository_repository_memberships_path(project, repository) if Gitorious.private_repositories?
81           end
82         }
83       end
84
85       private
86       def app; @app; end
87       def repository; @repository; end
88       def project; @repository.project; end
89       def user; @user; end
90     end
91   end
92 end