internal/httpd/page.go

32a583340a8db2b242ed452180dd239fd118bc26
gitbay/internal/httpd/page.go history · blame · raw

85 lines · 2290 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   string
40	Viewer string
41	Rail   rail
42}
43
44// base builds the layout-wide data for a request that has not already
45// resolved a viewer.
46func (s *Server) base(r *http.Request) basePage {
47	if s.cfg.Web.Mode != "accounts" {
48		return basePage{Site: s.siteName()}
49	}
50	return s.baseFor(s.viewer(r))
51}
52
53// baseFor is base for a handler that already holds the viewer, so the
54// session lookup is not repeated.
55func (s *Server) baseFor(viewer store.User) basePage {
56	b := basePage{Site: s.siteName()}
57	if viewer.ID == 0 {
58		return b
59	}
60	b.Viewer = viewer.Username
61	b.Rail = s.railFor(viewer)
62	return b
63}
64
65// railFor collects the viewer's pinned repositories and review queue,
66// dropping anything they may no longer read.
67func (s *Server) railFor(viewer store.User) rail {
68	var rl rail
69	pinned, _ := s.st.PinnedRepos(viewer.ID)
70	for _, rp := range pinned {
71		grant, _ := s.st.AccessRole(rp.ID, viewer.ID)
72		if policy.CanRead(viewer, rp, grant) {
73			rl.Pinned = append(rl.Pinned, railRepo{Owner: rp.OwnerName, Name: rp.Name})
74		}
75	}
76	queue, _ := s.st.ReviewQueue(viewer.ID)
77	for _, q := range queue {
78		rl.Reviews = append(rl.Reviews, railItem{
79			RepoPath: q.RepoPath,
80			Number:   q.Number,
81			Title:    q.Title,
82		})
83	}
84	return rl
85}