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