internal/httpd/page.go
81 lines · 2299 bytes
7 symbols in this file
1package httpd
2
3import (
4 "net/http"
5
6 "gitbay.org/gitbay/internal/store"
7)
8
9// railPin is one pinned repository in the rail's quick switcher.
10type railPin struct {
11 Owner string
12 Name string
13}
14
15// rail is the viewer's cross-repo state the layout needs: the unread count
16// on the bell and pinned repositories for the quick switcher.
17type rail struct {
18 Unread int
19 Pins []railPin
20}
21
22// basePage is what the layout needs on every page, repo or not. Page
23// structs embed it so the rail and the site name are always in scope.
24type basePage struct {
25 // Site is the instance's display name; Host is the name a command or
26 // URL must use. Anything copy-pasteable takes Host.
27 Site string
28 Host string
29 Viewer string
30 Admin bool // the viewer is an instance admin: the rail shows /admin
31 Rail rail
32 // Theme is stamped on <html> as data-theme: light or dark when the
33 // viewer chose one, empty when the browser's own scheme decides.
34 Theme string
35 // AbuseURL and TermsURL are the operator's footer links, empty when
36 // not configured.
37 AbuseURL string
38 TermsURL string
39}
40
41// anonBase is the layout-wide data with no viewer.
42func (s *Server) anonBase() basePage {
43 return basePage{Site: s.siteName(), Host: s.cfg.SiteHost(), AbuseURL: s.cfg.Web.AbuseURL, TermsURL: s.cfg.Web.TermsURL}
44}
45
46// base builds the layout-wide data for a request that has not already
47// resolved a viewer.
48func (s *Server) base(r *http.Request) basePage {
49 if s.cfg.Web.Mode != "accounts" {
50 return s.anonBase()
51 }
52 return s.baseFor(s.viewer(r))
53}
54
55// baseFor is base for a handler that already holds the viewer, so the
56// session lookup is not repeated.
57func (s *Server) baseFor(viewer store.User) basePage {
58 b := s.anonBase()
59 if viewer.ID == 0 {
60 return b
61 }
62 b.Viewer = viewer.Username
63 b.Admin = viewer.IsAdmin
64 b.Rail = s.railFor(viewer)
65 if theme, err := s.st.Theme(viewer.ID); err == nil && theme != "system" {
66 b.Theme = theme
67 }
68 return b
69}
70
71// railFor collects the viewer's unread count and pinned repositories.
72func (s *Server) railFor(viewer store.User) rail {
73 var rl rail
74 rl.Unread = s.st.UnreadNotices(viewer.ID)
75 if pinned, err := s.st.PinnedRepos(viewer.ID); err == nil {
76 for _, r := range pinned {
77 rl.Pins = append(rl.Pins, railPin{Owner: r.OwnerName, Name: r.Name})
78 }
79 }
80 return rl
81}