358 lines
8.1 kB
1
package pages
2
3
import (
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/*
21
var files embed.FS
22
23
type Pages struct {
24
t map[string]*template.Template
25
}
26
27
func 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
"unescapeHtml": func(s string) template.HTML {
78
return template.HTML(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
96
func 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
137
type LoginParams struct {
138
}
139
140
func (p *Pages) execute(name string, w io.Writer, params any) error {
141
return p.t[name].ExecuteTemplate(w, "layouts/base", params)
142
}
143
144
func (p *Pages) executePlain(name string, w io.Writer, params any) error {
145
return p.t[name].Execute(w, params)
146
}
147
148
func (p *Pages) executeRepo(name string, w io.Writer, params any) error {
149
return p.t[name].ExecuteTemplate(w, "layouts/repobase", params)
150
}
151
152
func (p *Pages) Login(w io.Writer, params LoginParams) error {
153
return p.executePlain("user/login", w, params)
154
}
155
156
type TimelineParams struct {
157
LoggedInUser *auth.User
158
}
159
160
func (p *Pages) Timeline(w io.Writer, params TimelineParams) error {
161
return p.execute("timeline", w, params)
162
}
163
164
type SettingsParams struct {
165
LoggedInUser *auth.User
166
PubKeys []db.PublicKey
167
}
168
169
func (p *Pages) Settings(w io.Writer, params SettingsParams) error {
170
return p.execute("settings/keys", w, params)
171
}
172
173
type KnotsParams struct {
174
LoggedInUser *auth.User
175
Registrations []db.Registration
176
}
177
178
func (p *Pages) Knots(w io.Writer, params KnotsParams) error {
179
return p.execute("knots", w, params)
180
}
181
182
type KnotParams struct {
183
LoggedInUser *auth.User
184
Registration *db.Registration
185
Members []string
186
IsOwner bool
187
}
188
189
func (p *Pages) Knot(w io.Writer, params KnotParams) error {
190
return p.execute("knot", w, params)
191
}
192
193
type NewRepoParams struct {
194
LoggedInUser *auth.User
195
Knots []string
196
}
197
198
func (p *Pages) NewRepo(w io.Writer, params NewRepoParams) error {
199
return p.execute("repo/new", w, params)
200
}
201
202
type ProfilePageParams struct {
203
LoggedInUser *auth.User
204
UserDid string
205
UserHandle string
206
Repos []db.Repo
207
}
208
209
func (p *Pages) ProfilePage(w io.Writer, params ProfilePageParams) error {
210
return p.execute("user/profile", w, params)
211
}
212
213
type RepoInfo struct {
214
Name string
215
OwnerDid string
216
OwnerHandle string
217
Description string
218
SettingsAllowed bool
219
}
220
221
func (r RepoInfo) OwnerWithAt() string {
222
if r.OwnerHandle != "" {
223
return fmt.Sprintf("@%s", r.OwnerHandle)
224
} else {
225
return r.OwnerDid
226
}
227
}
228
229
func (r RepoInfo) FullName() string {
230
return path.Join(r.OwnerWithAt(), r.Name)
231
}
232
233
func (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
247
type RepoIndexParams struct {
248
LoggedInUser *auth.User
249
RepoInfo RepoInfo
250
Active string
251
types.RepoIndexResponse
252
}
253
254
func (p *Pages) RepoIndexPage(w io.Writer, params RepoIndexParams) error {
255
params.Active = "overview"
256
return p.executeRepo("repo/index", w, params)
257
}
258
259
type RepoLogParams struct {
260
LoggedInUser *auth.User
261
RepoInfo RepoInfo
262
types.RepoLogResponse
263
}
264
265
func (p *Pages) RepoLog(w io.Writer, params RepoLogParams) error {
266
return p.execute("repo/log", w, params)
267
}
268
269
type RepoCommitParams struct {
270
LoggedInUser *auth.User
271
RepoInfo RepoInfo
272
types.RepoCommitResponse
273
}
274
275
func (p *Pages) RepoCommit(w io.Writer, params RepoCommitParams) error {
276
return p.executeRepo("repo/commit", w, params)
277
}
278
279
type RepoTreeParams struct {
280
LoggedInUser *auth.User
281
RepoInfo RepoInfo
282
Active string
283
BreadCrumbs [][]string
284
BaseTreeLink string
285
BaseBlobLink string
286
types.RepoTreeResponse
287
}
288
289
func (p *Pages) RepoTree(w io.Writer, params RepoTreeParams) error {
290
params.Active = "overview"
291
return p.execute("repo/tree", w, params)
292
}
293
294
type RepoBranchesParams struct {
295
LoggedInUser *auth.User
296
RepoInfo RepoInfo
297
types.RepoBranchesResponse
298
}
299
300
func (p *Pages) RepoBranches(w io.Writer, params RepoBranchesParams) error {
301
return p.executeRepo("repo/branches", w, params)
302
}
303
304
type RepoTagsParams struct {
305
LoggedInUser *auth.User
306
RepoInfo RepoInfo
307
types.RepoTagsResponse
308
}
309
310
func (p *Pages) RepoTags(w io.Writer, params RepoTagsParams) error {
311
return p.executeRepo("repo/tags", w, params)
312
}
313
314
type RepoBlobParams struct {
315
LoggedInUser *auth.User
316
RepoInfo RepoInfo
317
Active string
318
BreadCrumbs [][]string
319
types.RepoBlobResponse
320
}
321
322
func (p *Pages) RepoBlob(w io.Writer, params RepoBlobParams) error {
323
params.Active = "overview"
324
return p.executeRepo("repo/blob", w, params)
325
}
326
327
type RepoSettingsParams struct {
328
LoggedInUser *auth.User
329
RepoInfo RepoInfo
330
Collaborators [][]string
331
Active string
332
IsCollaboratorInviteAllowed bool
333
}
334
335
func (p *Pages) RepoSettings(w io.Writer, params RepoSettingsParams) error {
336
params.Active = "settings"
337
return p.executeRepo("repo/settings", w, params)
338
}
339
340
func (p *Pages) Static() http.Handler {
341
sub, err := fs.Sub(files, "static")
342
if err != nil {
343
log.Fatalf("no static dir found? that's crazy: %v", err)
344
}
345
return http.StripPrefix("/static/", http.FileServer(http.FS(sub)))
346
}
347
348
func (p *Pages) Error500(w io.Writer) error {
349
return p.execute("errors/500", w, nil)
350
}
351
352
func (p *Pages) Error404(w io.Writer) error {
353
return p.execute("errors/404", w, nil)
354
}
355
356
func (p *Pages) Error503(w io.Writer) error {
357
return p.execute("errors/503", w, nil)
358
}
359