this repo has no description
1package pages
2
3import (
4 "embed"
5 "fmt"
6 "html/template"
7 "io"
8 "io/fs"
9 "log"
10 "net/http"
11 "path"
12 "strings"
13
14 "github.com/dustin/go-humanize"
15 "github.com/sotangled/tangled/appview/auth"
16 "github.com/sotangled/tangled/appview/db"
17 "github.com/sotangled/tangled/types"
18)
19
20//go:embed templates/* static/*
21var files embed.FS
22
23type Pages struct {
24 t map[string]*template.Template
25}
26
27func funcMap() template.FuncMap {
28 return template.FuncMap{
29 "split": func(s string) []string {
30 return strings.Split(s, "\n")
31 },
32 "add": func(a, b int) int {
33 return a + b
34 },
35 "sub": func(a, b int) int {
36 return a - b
37 },
38 "cond": func(cond interface{}, a, b string) string {
39 if cond == nil {
40 return b
41 }
42
43 if boolean, ok := cond.(bool); boolean && ok {
44 return a
45 }
46
47 return b
48 },
49 "didOrHandle": func(did, handle string) string {
50 if handle != "" {
51 return fmt.Sprintf("@%s", handle)
52 } else {
53 return did
54 }
55 },
56 "assoc": func(values ...string) ([][]string, error) {
57 if len(values)%2 != 0 {
58 return nil, fmt.Errorf("invalid assoc call, must have an even number of arguments")
59 }
60 pairs := make([][]string, 0)
61 for i := 0; i < len(values); i += 2 {
62 pairs = append(pairs, []string{values[i], values[i+1]})
63 }
64 return pairs, nil
65 },
66 "append": func(s []string, values ...string) []string {
67 s = append(s, values...)
68 return s
69 },
70 "timeFmt": humanize.Time,
71 "length": func(v []string) int {
72 return len(v)
73 },
74 "splitN": func(s, sep string, n int) []string {
75 return strings.SplitN(s, sep, n)
76 },
77 "escapeHtml": func(s string) string {
78 return template.HTMLEscapeString(s)
79 },
80 "nl2br": func(text string) template.HTML {
81 return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
82 },
83 "unwrapText": func(text string) string {
84 paragraphs := strings.Split(text, "\n\n")
85
86 for i, p := range paragraphs {
87 lines := strings.Split(p, "\n")
88 paragraphs[i] = strings.Join(lines, " ")
89 }
90
91 return strings.Join(paragraphs, "\n\n")
92 },
93 }
94}
95
96func NewPages() *Pages {
97 templates := make(map[string]*template.Template)
98
99 // Walk through embedded templates directory and parse all .html files
100 err := fs.WalkDir(files, "templates", func(path string, d fs.DirEntry, err error) error {
101 if err != nil {
102 return err
103 }
104
105 if !d.IsDir() && strings.HasSuffix(path, ".html") {
106 name := strings.TrimPrefix(path, "templates/")
107 name = strings.TrimSuffix(name, ".html")
108
109 if !strings.HasPrefix(path, "templates/layouts/") {
110 // Add the page template on top of the base
111 tmpl, err := template.New(name).
112 Funcs(funcMap()).
113 ParseFS(files, "templates/layouts/*.html", path)
114 if err != nil {
115 return fmt.Errorf("setting up template: %w", err)
116 }
117
118 templates[name] = tmpl
119 log.Printf("loaded template: %s", name)
120 }
121
122 return nil
123 }
124 return nil
125 })
126 if err != nil {
127 log.Fatalf("walking template dir: %v", err)
128 }
129
130 log.Printf("total templates loaded: %d", len(templates))
131
132 return &Pages{
133 t: templates,
134 }
135}
136
137type LoginParams struct {
138}
139
140func (p *Pages) execute(name string, w io.Writer, params any) error {
141 return p.t[name].ExecuteTemplate(w, "layouts/base", params)
142}
143
144func (p *Pages) executePlain(name string, w io.Writer, params any) error {
145 return p.t[name].Execute(w, params)
146}
147
148func (p *Pages) executeRepo(name string, w io.Writer, params any) error {
149 return p.t[name].ExecuteTemplate(w, "layouts/repobase", params)
150}
151
152func (p *Pages) Login(w io.Writer, params LoginParams) error {
153 return p.executePlain("user/login", w, params)
154}
155
156type TimelineParams struct {
157 LoggedInUser *auth.User
158}
159
160func (p *Pages) Timeline(w io.Writer, params TimelineParams) error {
161 return p.execute("timeline", w, params)
162}
163
164type SettingsParams struct {
165 LoggedInUser *auth.User
166 PubKeys []db.PublicKey
167}
168
169func (p *Pages) Settings(w io.Writer, params SettingsParams) error {
170 return p.execute("settings/keys", w, params)
171}
172
173type KnotsParams struct {
174 LoggedInUser *auth.User
175 Registrations []db.Registration
176}
177
178func (p *Pages) Knots(w io.Writer, params KnotsParams) error {
179 return p.execute("knots", w, params)
180}
181
182type KnotParams struct {
183 LoggedInUser *auth.User
184 Registration *db.Registration
185 Members []string
186 IsOwner bool
187}
188
189func (p *Pages) Knot(w io.Writer, params KnotParams) error {
190 return p.execute("knot", w, params)
191}
192
193type NewRepoParams struct {
194 LoggedInUser *auth.User
195 Knots []string
196}
197
198func (p *Pages) NewRepo(w io.Writer, params NewRepoParams) error {
199 return p.execute("repo/new", w, params)
200}
201
202type ProfilePageParams struct {
203 LoggedInUser *auth.User
204 UserDid string
205 UserHandle string
206 Repos []db.Repo
207}
208
209func (p *Pages) ProfilePage(w io.Writer, params ProfilePageParams) error {
210 return p.execute("user/profile", w, params)
211}
212
213type RepoInfo struct {
214 Name string
215 OwnerDid string
216 OwnerHandle string
217 Description string
218 SettingsAllowed bool
219}
220
221func (r RepoInfo) OwnerWithAt() string {
222 if r.OwnerHandle != "" {
223 return fmt.Sprintf("@%s", r.OwnerHandle)
224 } else {
225 return r.OwnerDid
226 }
227}
228
229func (r RepoInfo) FullName() string {
230 return path.Join(r.OwnerWithAt(), r.Name)
231}
232
233func (r RepoInfo) GetTabs() [][]string {
234 tabs := [][]string{
235 {"overview", "/"},
236 {"issues", "/issues"},
237 {"pulls", "/pulls"},
238 }
239
240 if r.SettingsAllowed {
241 tabs = append(tabs, []string{"settings", "/settings"})
242 }
243
244 return tabs
245}
246
247type RepoIndexParams struct {
248 LoggedInUser *auth.User
249 RepoInfo RepoInfo
250 Active string
251 types.RepoIndexResponse
252}
253
254func (p *Pages) RepoIndexPage(w io.Writer, params RepoIndexParams) error {
255 params.Active = "overview"
256 return p.executeRepo("repo/index", w, params)
257}
258
259type RepoLogParams struct {
260 LoggedInUser *auth.User
261 RepoInfo RepoInfo
262 types.RepoLogResponse
263}
264
265func (p *Pages) RepoLog(w io.Writer, params RepoLogParams) error {
266 return p.execute("repo/log", w, params)
267}
268
269type RepoCommitParams struct {
270 LoggedInUser *auth.User
271 RepoInfo RepoInfo
272 Active string
273 types.RepoCommitResponse
274}
275
276func (p *Pages) RepoCommit(w io.Writer, params RepoCommitParams) error {
277 params.Active = "overview"
278 return p.executeRepo("repo/commit", w, params)
279}
280
281type RepoTreeParams struct {
282 LoggedInUser *auth.User
283 RepoInfo RepoInfo
284 Active string
285 BreadCrumbs [][]string
286 BaseTreeLink string
287 BaseBlobLink string
288 types.RepoTreeResponse
289}
290
291func (p *Pages) RepoTree(w io.Writer, params RepoTreeParams) error {
292 params.Active = "overview"
293 return p.execute("repo/tree", w, params)
294}
295
296type RepoBranchesParams struct {
297 LoggedInUser *auth.User
298 RepoInfo RepoInfo
299 types.RepoBranchesResponse
300}
301
302func (p *Pages) RepoBranches(w io.Writer, params RepoBranchesParams) error {
303 return p.executeRepo("repo/branches", w, params)
304}
305
306type RepoTagsParams struct {
307 LoggedInUser *auth.User
308 RepoInfo RepoInfo
309 types.RepoTagsResponse
310}
311
312func (p *Pages) RepoTags(w io.Writer, params RepoTagsParams) error {
313 return p.executeRepo("repo/tags", w, params)
314}
315
316type RepoBlobParams struct {
317 LoggedInUser *auth.User
318 RepoInfo RepoInfo
319 Active string
320 BreadCrumbs [][]string
321 types.RepoBlobResponse
322}
323
324func (p *Pages) RepoBlob(w io.Writer, params RepoBlobParams) error {
325 params.Active = "overview"
326 return p.executeRepo("repo/blob", w, params)
327}
328
329type Collaborator struct {
330 Did string
331 Handle string
332 Role string
333}
334
335type RepoSettingsParams struct {
336 LoggedInUser *auth.User
337 RepoInfo RepoInfo
338 Collaborators []Collaborator
339 Active string
340 IsCollaboratorInviteAllowed bool
341}
342
343func (p *Pages) RepoSettings(w io.Writer, params RepoSettingsParams) error {
344 params.Active = "settings"
345 return p.executeRepo("repo/settings", w, params)
346}
347
348func (p *Pages) Static() http.Handler {
349 sub, err := fs.Sub(files, "static")
350 if err != nil {
351 log.Fatalf("no static dir found? that's crazy: %v", err)
352 }
353 return http.StripPrefix("/static/", http.FileServer(http.FS(sub)))
354}
355
356func (p *Pages) Error500(w io.Writer) error {
357 return p.execute("errors/500", w, nil)
358}
359
360func (p *Pages) Error404(w io.Writer) error {
361 return p.execute("errors/404", w, nil)
362}
363
364func (p *Pages) Error503(w io.Writer) error {
365 return p.execute("errors/503", w, nil)
366}