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 types.RepoIndexResponse
254}
255
256func (p *Pages) RepoIndexPage(w io.Writer, params RepoIndexParams) error {
257 params.Active = "overview"
258 return p.executeRepo("repo/index", w, params)
259}
260
261type RepoLogParams struct {
262 LoggedInUser *auth.User
263 RepoInfo RepoInfo
264 types.RepoLogResponse
265}
266
267func (p *Pages) RepoLog(w io.Writer, params RepoLogParams) error {
268 return p.execute("repo/log", w, params)
269}
270
271type RepoCommitParams struct {
272 LoggedInUser *auth.User
273 RepoInfo RepoInfo
274 types.RepoCommitResponse
275}
276
277func (p *Pages) RepoCommit(w io.Writer, params RepoCommitParams) error {
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 RepoSettingsParams struct {
330 LoggedInUser *auth.User
331 RepoInfo RepoInfo
332 Collaborators [][]string
333 Active string
334 IsCollaboratorInviteAllowed bool
335}
336
337func (p *Pages) RepoSettings(w io.Writer, params RepoSettingsParams) error {
338 params.Active = "settings"
339 return p.executeRepo("repo/settings", w, params)
340}
341
342func (p *Pages) Static() http.Handler {
343 sub, err := fs.Sub(files, "static")
344 if err != nil {
345 log.Fatalf("no static dir found? that's crazy: %v", err)
346 }
347 return http.StripPrefix("/static/", http.FileServer(http.FS(sub)))
348}
349
350func (p *Pages) Error500(w io.Writer) error {
351 return p.execute("errors/500", w, nil)
352}
353
354func (p *Pages) Error404(w io.Writer) error {
355 return p.execute("errors/404", w, nil)
356}
357
358func (p *Pages) Error503(w io.Writer) error {
359 return p.execute("errors/503", w, nil)
360}