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