internal/httpd/page.go
88 lines · 2478 bytes
1package httpd
2
3import (
4 "net/http"
5
6 "gitbay.org/gitbay/internal/policy"
7 "gitbay.org/gitbay/internal/store"
8)
9
10// railRepo is one pinned repository in the rail.
11type railRepo struct {
12 Owner string
13 Name string
14}
15
16// railItem is one merge request in the rail's review queue.
17type railItem struct {
18 RepoPath string
19 Number int64
20 Title string
21}
22
23// rail is the cross-repo state in the left rail: the repositories and the
24// review work a viewer moves between. A repo's own navigation lives in its
25// header tabs, not here — the rail holds only what changes as you switch
26// context, which is what earns it the width.
27type rail struct {
28 Pinned []railRepo
29 Reviews []railItem
30}
31
32// Empty reports whether the rail has nothing to show beyond the global
33// links, so the template can skip its group headings.
34func (r rail) Empty() bool { return len(r.Pinned) == 0 && len(r.Reviews) == 0 }
35
36// basePage is what the layout needs on every page, repo or not. Page
37// structs embed it so the rail and the site name are always in scope.
38type basePage struct {
39 // Site is the instance's display name; Host is the name a command or
40 // URL must use. Anything copy-pasteable takes Host.
41 Site string
42 Host string
43 Viewer string
44 Rail rail
45}
46
47// base builds the layout-wide data for a request that has not already
48// resolved a viewer.
49func (s *Server) base(r *http.Request) basePage {
50 if s.cfg.Web.Mode != "accounts" {
51 return basePage{Site: s.siteName(), Host: s.cfg.SiteHost()}
52 }
53 return s.baseFor(s.viewer(r))
54}
55
56// baseFor is base for a handler that already holds the viewer, so the
57// session lookup is not repeated.
58func (s *Server) baseFor(viewer store.User) basePage {
59 b := basePage{Site: s.siteName(), Host: s.cfg.SiteHost()}
60 if viewer.ID == 0 {
61 return b
62 }
63 b.Viewer = viewer.Username
64 b.Rail = s.railFor(viewer)
65 return b
66}
67
68// railFor collects the viewer's pinned repositories and review queue,
69// dropping anything they may no longer read.
70func (s *Server) railFor(viewer store.User) rail {
71 var rl rail
72 pinned, _ := s.st.PinnedRepos(viewer.ID)
73 for _, rp := range pinned {
74 grant, _ := s.st.AccessRole(rp.ID, viewer.ID)
75 if policy.CanRead(viewer, rp, grant) {
76 rl.Pinned = append(rl.Pinned, railRepo{Owner: rp.OwnerName, Name: rp.Name})
77 }
78 }
79 queue, _ := s.st.ReviewQueue(viewer.ID)
80 for _, q := range queue {
81 rl.Reviews = append(rl.Reviews, railItem{
82 RepoPath: q.RepoPath,
83 Number: q.Number,
84 Title: q.Title,
85 })
86 }
87 return rl
88}