Monorepo for Tangled
tangled.org
1package pages
2
3import (
4 "bytes"
5 "crypto/sha256"
6 "embed"
7 "encoding/hex"
8 "fmt"
9 "html/template"
10 "io"
11 "io/fs"
12 "log"
13 "net/http"
14 "os"
15 "path/filepath"
16 "strings"
17
18 "tangled.sh/tangled.sh/core/appview/commitverify"
19 "tangled.sh/tangled.sh/core/appview/config"
20 "tangled.sh/tangled.sh/core/appview/db"
21 "tangled.sh/tangled.sh/core/appview/oauth"
22 "tangled.sh/tangled.sh/core/appview/pages/markup"
23 "tangled.sh/tangled.sh/core/appview/pages/repoinfo"
24 "tangled.sh/tangled.sh/core/appview/pagination"
25 "tangled.sh/tangled.sh/core/patchutil"
26 "tangled.sh/tangled.sh/core/types"
27
28 "github.com/alecthomas/chroma/v2"
29 chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
30 "github.com/alecthomas/chroma/v2/lexers"
31 "github.com/alecthomas/chroma/v2/styles"
32 "github.com/bluesky-social/indigo/atproto/syntax"
33 "github.com/go-git/go-git/v5/plumbing"
34 "github.com/go-git/go-git/v5/plumbing/object"
35 "github.com/microcosm-cc/bluemonday"
36)
37
38//go:embed templates/* static
39var Files embed.FS
40
41type Pages struct {
42 t map[string]*template.Template
43 avatar config.AvatarConfig
44 dev bool
45 embedFS embed.FS
46 templateDir string // Path to templates on disk for dev mode
47 rctx *markup.RenderContext
48}
49
50func NewPages(config *config.Config) *Pages {
51 // initialized with safe defaults, can be overriden per use
52 rctx := &markup.RenderContext{
53 IsDev: config.Core.Dev,
54 CamoUrl: config.Camo.Host,
55 CamoSecret: config.Camo.SharedSecret,
56 }
57
58 p := &Pages{
59 t: make(map[string]*template.Template),
60 dev: config.Core.Dev,
61 avatar: config.Avatar,
62 embedFS: Files,
63 rctx: rctx,
64 templateDir: "appview/pages",
65 }
66
67 // Initial load of all templates
68 p.loadAllTemplates()
69
70 return p
71}
72
73func (p *Pages) loadAllTemplates() {
74 templates := make(map[string]*template.Template)
75 var fragmentPaths []string
76
77 // Use embedded FS for initial loading
78 // First, collect all fragment paths
79 err := fs.WalkDir(p.embedFS, "templates", func(path string, d fs.DirEntry, err error) error {
80 if err != nil {
81 return err
82 }
83 if d.IsDir() {
84 return nil
85 }
86 if !strings.HasSuffix(path, ".html") {
87 return nil
88 }
89 if !strings.Contains(path, "fragments/") {
90 return nil
91 }
92 name := strings.TrimPrefix(path, "templates/")
93 name = strings.TrimSuffix(name, ".html")
94 tmpl, err := template.New(name).
95 Funcs(p.funcMap()).
96 ParseFS(p.embedFS, path)
97 if err != nil {
98 log.Fatalf("setting up fragment: %v", err)
99 }
100 templates[name] = tmpl
101 fragmentPaths = append(fragmentPaths, path)
102 log.Printf("loaded fragment: %s", name)
103 return nil
104 })
105 if err != nil {
106 log.Fatalf("walking template dir for fragments: %v", err)
107 }
108
109 // Then walk through and setup the rest of the templates
110 err = fs.WalkDir(p.embedFS, "templates", func(path string, d fs.DirEntry, err error) error {
111 if err != nil {
112 return err
113 }
114 if d.IsDir() {
115 return nil
116 }
117 if !strings.HasSuffix(path, "html") {
118 return nil
119 }
120 // Skip fragments as they've already been loaded
121 if strings.Contains(path, "fragments/") {
122 return nil
123 }
124 // Skip layouts
125 if strings.Contains(path, "layouts/") {
126 return nil
127 }
128 name := strings.TrimPrefix(path, "templates/")
129 name = strings.TrimSuffix(name, ".html")
130 // Add the page template on top of the base
131 allPaths := []string{}
132 allPaths = append(allPaths, "templates/layouts/*.html")
133 allPaths = append(allPaths, fragmentPaths...)
134 allPaths = append(allPaths, path)
135 tmpl, err := template.New(name).
136 Funcs(p.funcMap()).
137 ParseFS(p.embedFS, allPaths...)
138 if err != nil {
139 return fmt.Errorf("setting up template: %w", err)
140 }
141 templates[name] = tmpl
142 log.Printf("loaded template: %s", name)
143 return nil
144 })
145 if err != nil {
146 log.Fatalf("walking template dir: %v", err)
147 }
148
149 log.Printf("total templates loaded: %d", len(templates))
150 p.t = templates
151}
152
153// loadTemplateFromDisk loads a template from the filesystem in dev mode
154func (p *Pages) loadTemplateFromDisk(name string) error {
155 if !p.dev {
156 return nil
157 }
158
159 log.Printf("reloading template from disk: %s", name)
160
161 // Find all fragments first
162 var fragmentPaths []string
163 err := filepath.WalkDir(filepath.Join(p.templateDir, "templates"), func(path string, d fs.DirEntry, err error) error {
164 if err != nil {
165 return err
166 }
167 if d.IsDir() {
168 return nil
169 }
170 if !strings.HasSuffix(path, ".html") {
171 return nil
172 }
173 if !strings.Contains(path, "fragments/") {
174 return nil
175 }
176 fragmentPaths = append(fragmentPaths, path)
177 return nil
178 })
179 if err != nil {
180 return fmt.Errorf("walking disk template dir for fragments: %w", err)
181 }
182
183 // Find the template path on disk
184 templatePath := filepath.Join(p.templateDir, "templates", name+".html")
185 if _, err := os.Stat(templatePath); os.IsNotExist(err) {
186 return fmt.Errorf("template not found on disk: %s", name)
187 }
188
189 // Create a new template
190 tmpl := template.New(name).Funcs(p.funcMap())
191
192 // Parse layouts
193 layoutGlob := filepath.Join(p.templateDir, "templates", "layouts", "*.html")
194 layouts, err := filepath.Glob(layoutGlob)
195 if err != nil {
196 return fmt.Errorf("finding layout templates: %w", err)
197 }
198
199 // Create paths for parsing
200 allFiles := append(layouts, fragmentPaths...)
201 allFiles = append(allFiles, templatePath)
202
203 // Parse all templates
204 tmpl, err = tmpl.ParseFiles(allFiles...)
205 if err != nil {
206 return fmt.Errorf("parsing template files: %w", err)
207 }
208
209 // Update the template in the map
210 p.t[name] = tmpl
211 log.Printf("template reloaded from disk: %s", name)
212 return nil
213}
214
215func (p *Pages) executeOrReload(templateName string, w io.Writer, base string, params any) error {
216 // In dev mode, reload the template from disk before executing
217 if p.dev {
218 if err := p.loadTemplateFromDisk(templateName); err != nil {
219 log.Printf("warning: failed to reload template %s from disk: %v", templateName, err)
220 // Continue with the existing template
221 }
222 }
223
224 tmpl, exists := p.t[templateName]
225 if !exists {
226 return fmt.Errorf("template not found: %s", templateName)
227 }
228
229 if base == "" {
230 return tmpl.Execute(w, params)
231 } else {
232 return tmpl.ExecuteTemplate(w, base, params)
233 }
234}
235
236func (p *Pages) execute(name string, w io.Writer, params any) error {
237 return p.executeOrReload(name, w, "layouts/base", params)
238}
239
240func (p *Pages) executePlain(name string, w io.Writer, params any) error {
241 return p.executeOrReload(name, w, "", params)
242}
243
244func (p *Pages) executeRepo(name string, w io.Writer, params any) error {
245 return p.executeOrReload(name, w, "layouts/repobase", params)
246}
247
248type LoginParams struct {
249}
250
251func (p *Pages) Login(w io.Writer, params LoginParams) error {
252 return p.executePlain("user/login", w, params)
253}
254
255type TimelineParams struct {
256 LoggedInUser *oauth.User
257 Timeline []db.TimelineEvent
258 DidHandleMap map[string]string
259}
260
261func (p *Pages) Timeline(w io.Writer, params TimelineParams) error {
262 return p.execute("timeline", w, params)
263}
264
265type SettingsParams struct {
266 LoggedInUser *oauth.User
267 PubKeys []db.PublicKey
268 Emails []db.Email
269}
270
271func (p *Pages) Settings(w io.Writer, params SettingsParams) error {
272 return p.execute("settings", w, params)
273}
274
275type KnotsParams struct {
276 LoggedInUser *oauth.User
277 Registrations []db.Registration
278}
279
280func (p *Pages) Knots(w io.Writer, params KnotsParams) error {
281 return p.execute("knots", w, params)
282}
283
284type KnotParams struct {
285 LoggedInUser *oauth.User
286 DidHandleMap map[string]string
287 Registration *db.Registration
288 Members []string
289 IsOwner bool
290}
291
292func (p *Pages) Knot(w io.Writer, params KnotParams) error {
293 return p.execute("knot", w, params)
294}
295
296type SpindlesParams struct {
297 LoggedInUser *oauth.User
298 Spindles []db.Spindle
299}
300
301func (p *Pages) Spindles(w io.Writer, params SpindlesParams) error {
302 return p.execute("spindles/index", w, params)
303}
304
305type SpindleListingParams struct {
306 db.Spindle
307}
308
309func (p *Pages) SpindleListing(w io.Writer, params SpindleListingParams) error {
310 return p.executePlain("spindles/fragments/spindleListing", w, params)
311}
312
313type SpindleDashboardParams struct {
314 LoggedInUser *oauth.User
315 Spindle db.Spindle
316 Members []string
317 Repos map[string][]db.Repo
318 DidHandleMap map[string]string
319}
320
321func (p *Pages) SpindleDashboard(w io.Writer, params SpindleDashboardParams) error {
322 return p.execute("spindles/dashboard", w, params)
323}
324
325type NewRepoParams struct {
326 LoggedInUser *oauth.User
327 Knots []string
328}
329
330func (p *Pages) NewRepo(w io.Writer, params NewRepoParams) error {
331 return p.execute("repo/new", w, params)
332}
333
334type ForkRepoParams struct {
335 LoggedInUser *oauth.User
336 Knots []string
337 RepoInfo repoinfo.RepoInfo
338}
339
340func (p *Pages) ForkRepo(w io.Writer, params ForkRepoParams) error {
341 return p.execute("repo/fork", w, params)
342}
343
344type ProfilePageParams struct {
345 LoggedInUser *oauth.User
346 Repos []db.Repo
347 CollaboratingRepos []db.Repo
348 ProfileTimeline *db.ProfileTimeline
349 Card ProfileCard
350 Punchcard db.Punchcard
351
352 DidHandleMap map[string]string
353}
354
355type ProfileCard struct {
356 UserDid string
357 UserHandle string
358 FollowStatus db.FollowStatus
359 AvatarUri string
360 Followers int
361 Following int
362
363 Profile *db.Profile
364}
365
366func (p *Pages) ProfilePage(w io.Writer, params ProfilePageParams) error {
367 return p.execute("user/profile", w, params)
368}
369
370type ReposPageParams struct {
371 LoggedInUser *oauth.User
372 Repos []db.Repo
373 Card ProfileCard
374
375 DidHandleMap map[string]string
376}
377
378func (p *Pages) ReposPage(w io.Writer, params ReposPageParams) error {
379 return p.execute("user/repos", w, params)
380}
381
382type FollowFragmentParams struct {
383 UserDid string
384 FollowStatus db.FollowStatus
385}
386
387func (p *Pages) FollowFragment(w io.Writer, params FollowFragmentParams) error {
388 return p.executePlain("user/fragments/follow", w, params)
389}
390
391type EditBioParams struct {
392 LoggedInUser *oauth.User
393 Profile *db.Profile
394}
395
396func (p *Pages) EditBioFragment(w io.Writer, params EditBioParams) error {
397 return p.executePlain("user/fragments/editBio", w, params)
398}
399
400type EditPinsParams struct {
401 LoggedInUser *oauth.User
402 Profile *db.Profile
403 AllRepos []PinnedRepo
404 DidHandleMap map[string]string
405}
406
407type PinnedRepo struct {
408 IsPinned bool
409 db.Repo
410}
411
412func (p *Pages) EditPinsFragment(w io.Writer, params EditPinsParams) error {
413 return p.executePlain("user/fragments/editPins", w, params)
414}
415
416type RepoActionsFragmentParams struct {
417 IsStarred bool
418 RepoAt syntax.ATURI
419 Stats db.RepoStats
420}
421
422func (p *Pages) RepoActionsFragment(w io.Writer, params RepoActionsFragmentParams) error {
423 return p.executePlain("repo/fragments/repoActions", w, params)
424}
425
426type RepoDescriptionParams struct {
427 RepoInfo repoinfo.RepoInfo
428}
429
430func (p *Pages) EditRepoDescriptionFragment(w io.Writer, params RepoDescriptionParams) error {
431 return p.executePlain("repo/fragments/editRepoDescription", w, params)
432}
433
434func (p *Pages) RepoDescriptionFragment(w io.Writer, params RepoDescriptionParams) error {
435 return p.executePlain("repo/fragments/repoDescription", w, params)
436}
437
438type RepoIndexParams struct {
439 LoggedInUser *oauth.User
440 RepoInfo repoinfo.RepoInfo
441 Active string
442 TagMap map[string][]string
443 CommitsTrunc []*object.Commit
444 TagsTrunc []*types.TagReference
445 BranchesTrunc []types.Branch
446 ForkInfo *types.ForkInfo
447 HTMLReadme template.HTML
448 Raw bool
449 EmailToDidOrHandle map[string]string
450 VerifiedCommits commitverify.VerifiedCommits
451 Languages []types.RepoLanguageDetails
452 Pipelines map[string]db.Pipeline
453 types.RepoIndexResponse
454}
455
456func (p *Pages) RepoIndexPage(w io.Writer, params RepoIndexParams) error {
457 params.Active = "overview"
458 if params.IsEmpty {
459 return p.executeRepo("repo/empty", w, params)
460 }
461
462 p.rctx.RepoInfo = params.RepoInfo
463 p.rctx.RendererType = markup.RendererTypeRepoMarkdown
464
465 if params.ReadmeFileName != "" {
466 var htmlString string
467 ext := filepath.Ext(params.ReadmeFileName)
468 switch ext {
469 case ".md", ".markdown", ".mdown", ".mkdn", ".mkd":
470 htmlString = p.rctx.RenderMarkdown(params.Readme)
471 params.Raw = false
472 params.HTMLReadme = template.HTML(p.rctx.Sanitize(htmlString))
473 default:
474 htmlString = string(params.Readme)
475 params.Raw = true
476 params.HTMLReadme = template.HTML(bluemonday.NewPolicy().Sanitize(htmlString))
477 }
478 }
479
480 return p.executeRepo("repo/index", w, params)
481}
482
483type RepoLogParams struct {
484 LoggedInUser *oauth.User
485 RepoInfo repoinfo.RepoInfo
486 TagMap map[string][]string
487 types.RepoLogResponse
488 Active string
489 EmailToDidOrHandle map[string]string
490 VerifiedCommits commitverify.VerifiedCommits
491 Pipelines map[string]db.Pipeline
492}
493
494func (p *Pages) RepoLog(w io.Writer, params RepoLogParams) error {
495 params.Active = "overview"
496 return p.executeRepo("repo/log", w, params)
497}
498
499type RepoCommitParams struct {
500 LoggedInUser *oauth.User
501 RepoInfo repoinfo.RepoInfo
502 Active string
503 EmailToDidOrHandle map[string]string
504 Pipeline *db.Pipeline
505
506 // singular because it's always going to be just one
507 VerifiedCommit commitverify.VerifiedCommits
508
509 types.RepoCommitResponse
510}
511
512func (p *Pages) RepoCommit(w io.Writer, params RepoCommitParams) error {
513 params.Active = "overview"
514 return p.executeRepo("repo/commit", w, params)
515}
516
517type RepoTreeParams struct {
518 LoggedInUser *oauth.User
519 RepoInfo repoinfo.RepoInfo
520 Active string
521 BreadCrumbs [][]string
522 BaseTreeLink string
523 BaseBlobLink string
524 types.RepoTreeResponse
525}
526
527type RepoTreeStats struct {
528 NumFolders uint64
529 NumFiles uint64
530}
531
532func (r RepoTreeParams) TreeStats() RepoTreeStats {
533 numFolders, numFiles := 0, 0
534 for _, f := range r.Files {
535 if !f.IsFile {
536 numFolders += 1
537 } else if f.IsFile {
538 numFiles += 1
539 }
540 }
541
542 return RepoTreeStats{
543 NumFolders: uint64(numFolders),
544 NumFiles: uint64(numFiles),
545 }
546}
547
548func (p *Pages) RepoTree(w io.Writer, params RepoTreeParams) error {
549 params.Active = "overview"
550 return p.execute("repo/tree", w, params)
551}
552
553type RepoBranchesParams struct {
554 LoggedInUser *oauth.User
555 RepoInfo repoinfo.RepoInfo
556 Active string
557 types.RepoBranchesResponse
558}
559
560func (p *Pages) RepoBranches(w io.Writer, params RepoBranchesParams) error {
561 params.Active = "overview"
562 return p.executeRepo("repo/branches", w, params)
563}
564
565type RepoTagsParams struct {
566 LoggedInUser *oauth.User
567 RepoInfo repoinfo.RepoInfo
568 Active string
569 types.RepoTagsResponse
570 ArtifactMap map[plumbing.Hash][]db.Artifact
571 DanglingArtifacts []db.Artifact
572}
573
574func (p *Pages) RepoTags(w io.Writer, params RepoTagsParams) error {
575 params.Active = "overview"
576 return p.executeRepo("repo/tags", w, params)
577}
578
579type RepoArtifactParams struct {
580 LoggedInUser *oauth.User
581 RepoInfo repoinfo.RepoInfo
582 Artifact db.Artifact
583}
584
585func (p *Pages) RepoArtifactFragment(w io.Writer, params RepoArtifactParams) error {
586 return p.executePlain("repo/fragments/artifact", w, params)
587}
588
589type RepoBlobParams struct {
590 LoggedInUser *oauth.User
591 RepoInfo repoinfo.RepoInfo
592 Active string
593 BreadCrumbs [][]string
594 ShowRendered bool
595 RenderToggle bool
596 RenderedContents template.HTML
597 types.RepoBlobResponse
598}
599
600func (p *Pages) RepoBlob(w io.Writer, params RepoBlobParams) error {
601 var style *chroma.Style = styles.Get("catpuccin-latte")
602
603 if params.ShowRendered {
604 switch markup.GetFormat(params.Path) {
605 case markup.FormatMarkdown:
606 p.rctx.RepoInfo = params.RepoInfo
607 p.rctx.RendererType = markup.RendererTypeRepoMarkdown
608 htmlString := p.rctx.RenderMarkdown(params.Contents)
609 params.RenderedContents = template.HTML(p.rctx.Sanitize(htmlString))
610 }
611 }
612
613 if params.Lines < 5000 {
614 c := params.Contents
615 formatter := chromahtml.New(
616 chromahtml.InlineCode(false),
617 chromahtml.WithLineNumbers(true),
618 chromahtml.WithLinkableLineNumbers(true, "L"),
619 chromahtml.Standalone(false),
620 chromahtml.WithClasses(true),
621 )
622
623 lexer := lexers.Get(filepath.Base(params.Path))
624 if lexer == nil {
625 lexer = lexers.Fallback
626 }
627
628 iterator, err := lexer.Tokenise(nil, c)
629 if err != nil {
630 return fmt.Errorf("chroma tokenize: %w", err)
631 }
632
633 var code bytes.Buffer
634 err = formatter.Format(&code, style, iterator)
635 if err != nil {
636 return fmt.Errorf("chroma format: %w", err)
637 }
638
639 params.Contents = code.String()
640 }
641
642 params.Active = "overview"
643 return p.executeRepo("repo/blob", w, params)
644}
645
646type Collaborator struct {
647 Did string
648 Handle string
649 Role string
650}
651
652type RepoSettingsParams struct {
653 LoggedInUser *oauth.User
654 RepoInfo repoinfo.RepoInfo
655 Collaborators []Collaborator
656 Active string
657 Branches []types.Branch
658 Spindles []string
659 CurrentSpindle string
660 // TODO: use repoinfo.roles
661 IsCollaboratorInviteAllowed bool
662}
663
664func (p *Pages) RepoSettings(w io.Writer, params RepoSettingsParams) error {
665 params.Active = "settings"
666 return p.executeRepo("repo/settings", w, params)
667}
668
669type RepoIssuesParams struct {
670 LoggedInUser *oauth.User
671 RepoInfo repoinfo.RepoInfo
672 Active string
673 Issues []db.Issue
674 DidHandleMap map[string]string
675 Page pagination.Page
676 FilteringByOpen bool
677}
678
679func (p *Pages) RepoIssues(w io.Writer, params RepoIssuesParams) error {
680 params.Active = "issues"
681 return p.executeRepo("repo/issues/issues", w, params)
682}
683
684type RepoSingleIssueParams struct {
685 LoggedInUser *oauth.User
686 RepoInfo repoinfo.RepoInfo
687 Active string
688 Issue db.Issue
689 Comments []db.Comment
690 IssueOwnerHandle string
691 DidHandleMap map[string]string
692
693 OrderedReactionKinds []db.ReactionKind
694 Reactions map[db.ReactionKind]int
695 UserReacted map[db.ReactionKind]bool
696
697 State string
698}
699
700type ThreadReactionFragmentParams struct {
701 ThreadAt syntax.ATURI
702 Kind db.ReactionKind
703 Count int
704 IsReacted bool
705}
706
707func (p *Pages) ThreadReactionFragment(w io.Writer, params ThreadReactionFragmentParams) error {
708 return p.executePlain("repo/fragments/reaction", w, params)
709}
710
711func (p *Pages) RepoSingleIssue(w io.Writer, params RepoSingleIssueParams) error {
712 params.Active = "issues"
713 if params.Issue.Open {
714 params.State = "open"
715 } else {
716 params.State = "closed"
717 }
718 return p.execute("repo/issues/issue", w, params)
719}
720
721type RepoNewIssueParams struct {
722 LoggedInUser *oauth.User
723 RepoInfo repoinfo.RepoInfo
724 Active string
725}
726
727func (p *Pages) RepoNewIssue(w io.Writer, params RepoNewIssueParams) error {
728 params.Active = "issues"
729 return p.executeRepo("repo/issues/new", w, params)
730}
731
732type EditIssueCommentParams struct {
733 LoggedInUser *oauth.User
734 RepoInfo repoinfo.RepoInfo
735 Issue *db.Issue
736 Comment *db.Comment
737}
738
739func (p *Pages) EditIssueCommentFragment(w io.Writer, params EditIssueCommentParams) error {
740 return p.executePlain("repo/issues/fragments/editIssueComment", w, params)
741}
742
743type SingleIssueCommentParams struct {
744 LoggedInUser *oauth.User
745 DidHandleMap map[string]string
746 RepoInfo repoinfo.RepoInfo
747 Issue *db.Issue
748 Comment *db.Comment
749}
750
751func (p *Pages) SingleIssueCommentFragment(w io.Writer, params SingleIssueCommentParams) error {
752 return p.executePlain("repo/issues/fragments/issueComment", w, params)
753}
754
755type RepoNewPullParams struct {
756 LoggedInUser *oauth.User
757 RepoInfo repoinfo.RepoInfo
758 Branches []types.Branch
759 Strategy string
760 SourceBranch string
761 TargetBranch string
762 Title string
763 Body string
764 Active string
765}
766
767func (p *Pages) RepoNewPull(w io.Writer, params RepoNewPullParams) error {
768 params.Active = "pulls"
769 return p.executeRepo("repo/pulls/new", w, params)
770}
771
772type RepoPullsParams struct {
773 LoggedInUser *oauth.User
774 RepoInfo repoinfo.RepoInfo
775 Pulls []*db.Pull
776 Active string
777 DidHandleMap map[string]string
778 FilteringBy db.PullState
779 Stacks map[string]db.Stack
780}
781
782func (p *Pages) RepoPulls(w io.Writer, params RepoPullsParams) error {
783 params.Active = "pulls"
784 return p.executeRepo("repo/pulls/pulls", w, params)
785}
786
787type ResubmitResult uint64
788
789const (
790 ShouldResubmit ResubmitResult = iota
791 ShouldNotResubmit
792 Unknown
793)
794
795func (r ResubmitResult) Yes() bool {
796 return r == ShouldResubmit
797}
798func (r ResubmitResult) No() bool {
799 return r == ShouldNotResubmit
800}
801func (r ResubmitResult) Unknown() bool {
802 return r == Unknown
803}
804
805type RepoSinglePullParams struct {
806 LoggedInUser *oauth.User
807 RepoInfo repoinfo.RepoInfo
808 Active string
809 DidHandleMap map[string]string
810 Pull *db.Pull
811 Stack db.Stack
812 AbandonedPulls []*db.Pull
813 MergeCheck types.MergeCheckResponse
814 ResubmitCheck ResubmitResult
815 Pipelines map[string]db.Pipeline
816
817 OrderedReactionKinds []db.ReactionKind
818 Reactions map[db.ReactionKind]int
819 UserReacted map[db.ReactionKind]bool
820}
821
822func (p *Pages) RepoSinglePull(w io.Writer, params RepoSinglePullParams) error {
823 params.Active = "pulls"
824 return p.executeRepo("repo/pulls/pull", w, params)
825}
826
827type RepoPullPatchParams struct {
828 LoggedInUser *oauth.User
829 DidHandleMap map[string]string
830 RepoInfo repoinfo.RepoInfo
831 Pull *db.Pull
832 Stack db.Stack
833 Diff *types.NiceDiff
834 Round int
835 Submission *db.PullSubmission
836 OrderedReactionKinds []db.ReactionKind
837}
838
839// this name is a mouthful
840func (p *Pages) RepoPullPatchPage(w io.Writer, params RepoPullPatchParams) error {
841 return p.execute("repo/pulls/patch", w, params)
842}
843
844type RepoPullInterdiffParams struct {
845 LoggedInUser *oauth.User
846 DidHandleMap map[string]string
847 RepoInfo repoinfo.RepoInfo
848 Pull *db.Pull
849 Round int
850 Interdiff *patchutil.InterdiffResult
851 OrderedReactionKinds []db.ReactionKind
852}
853
854// this name is a mouthful
855func (p *Pages) RepoPullInterdiffPage(w io.Writer, params RepoPullInterdiffParams) error {
856 return p.execute("repo/pulls/interdiff", w, params)
857}
858
859type PullPatchUploadParams struct {
860 RepoInfo repoinfo.RepoInfo
861}
862
863func (p *Pages) PullPatchUploadFragment(w io.Writer, params PullPatchUploadParams) error {
864 return p.executePlain("repo/pulls/fragments/pullPatchUpload", w, params)
865}
866
867type PullCompareBranchesParams struct {
868 RepoInfo repoinfo.RepoInfo
869 Branches []types.Branch
870 SourceBranch string
871}
872
873func (p *Pages) PullCompareBranchesFragment(w io.Writer, params PullCompareBranchesParams) error {
874 return p.executePlain("repo/pulls/fragments/pullCompareBranches", w, params)
875}
876
877type PullCompareForkParams struct {
878 RepoInfo repoinfo.RepoInfo
879 Forks []db.Repo
880 Selected string
881}
882
883func (p *Pages) PullCompareForkFragment(w io.Writer, params PullCompareForkParams) error {
884 return p.executePlain("repo/pulls/fragments/pullCompareForks", w, params)
885}
886
887type PullCompareForkBranchesParams struct {
888 RepoInfo repoinfo.RepoInfo
889 SourceBranches []types.Branch
890 TargetBranches []types.Branch
891}
892
893func (p *Pages) PullCompareForkBranchesFragment(w io.Writer, params PullCompareForkBranchesParams) error {
894 return p.executePlain("repo/pulls/fragments/pullCompareForksBranches", w, params)
895}
896
897type PullResubmitParams struct {
898 LoggedInUser *oauth.User
899 RepoInfo repoinfo.RepoInfo
900 Pull *db.Pull
901 SubmissionId int
902}
903
904func (p *Pages) PullResubmitFragment(w io.Writer, params PullResubmitParams) error {
905 return p.executePlain("repo/pulls/fragments/pullResubmit", w, params)
906}
907
908type PullActionsParams struct {
909 LoggedInUser *oauth.User
910 RepoInfo repoinfo.RepoInfo
911 Pull *db.Pull
912 RoundNumber int
913 MergeCheck types.MergeCheckResponse
914 ResubmitCheck ResubmitResult
915 Stack db.Stack
916}
917
918func (p *Pages) PullActionsFragment(w io.Writer, params PullActionsParams) error {
919 return p.executePlain("repo/pulls/fragments/pullActions", w, params)
920}
921
922type PullNewCommentParams struct {
923 LoggedInUser *oauth.User
924 RepoInfo repoinfo.RepoInfo
925 Pull *db.Pull
926 RoundNumber int
927}
928
929func (p *Pages) PullNewCommentFragment(w io.Writer, params PullNewCommentParams) error {
930 return p.executePlain("repo/pulls/fragments/pullNewComment", w, params)
931}
932
933type RepoCompareParams struct {
934 LoggedInUser *oauth.User
935 RepoInfo repoinfo.RepoInfo
936 Forks []db.Repo
937 Branches []types.Branch
938 Tags []*types.TagReference
939 Base string
940 Head string
941 Diff *types.NiceDiff
942
943 Active string
944}
945
946func (p *Pages) RepoCompare(w io.Writer, params RepoCompareParams) error {
947 params.Active = "overview"
948 return p.executeRepo("repo/compare/compare", w, params)
949}
950
951type RepoCompareNewParams struct {
952 LoggedInUser *oauth.User
953 RepoInfo repoinfo.RepoInfo
954 Forks []db.Repo
955 Branches []types.Branch
956 Tags []*types.TagReference
957 Base string
958 Head string
959
960 Active string
961}
962
963func (p *Pages) RepoCompareNew(w io.Writer, params RepoCompareNewParams) error {
964 params.Active = "overview"
965 return p.executeRepo("repo/compare/new", w, params)
966}
967
968type RepoCompareAllowPullParams struct {
969 LoggedInUser *oauth.User
970 RepoInfo repoinfo.RepoInfo
971 Base string
972 Head string
973}
974
975func (p *Pages) RepoCompareAllowPullFragment(w io.Writer, params RepoCompareAllowPullParams) error {
976 return p.executePlain("repo/fragments/compareAllowPull", w, params)
977}
978
979type RepoCompareDiffParams struct {
980 LoggedInUser *oauth.User
981 RepoInfo repoinfo.RepoInfo
982 Diff types.NiceDiff
983}
984
985func (p *Pages) RepoCompareDiff(w io.Writer, params RepoCompareDiffParams) error {
986 return p.executePlain("repo/fragments/diff", w, []any{params.RepoInfo.FullName, ¶ms.Diff})
987}
988
989type PipelinesParams struct {
990 LoggedInUser *oauth.User
991 RepoInfo repoinfo.RepoInfo
992 Pipelines []db.Pipeline
993 Active string
994}
995
996func (p *Pages) Pipelines(w io.Writer, params PipelinesParams) error {
997 params.Active = "pipelines"
998 return p.executeRepo("repo/pipelines/pipelines", w, params)
999}
1000
1001type LogBlockParams struct {
1002 Id int
1003 Name string
1004 Command string
1005 Collapsed bool
1006}
1007
1008func (p *Pages) LogBlock(w io.Writer, params LogBlockParams) error {
1009 return p.executePlain("repo/pipelines/fragments/logBlock", w, params)
1010}
1011
1012type LogLineParams struct {
1013 Id int
1014 Content string
1015}
1016
1017func (p *Pages) LogLine(w io.Writer, params LogLineParams) error {
1018 return p.executePlain("repo/pipelines/fragments/logLine", w, params)
1019}
1020
1021type WorkflowParams struct {
1022 LoggedInUser *oauth.User
1023 RepoInfo repoinfo.RepoInfo
1024 Pipeline db.Pipeline
1025 Workflow string
1026 LogUrl string
1027 Active string
1028}
1029
1030func (p *Pages) Workflow(w io.Writer, params WorkflowParams) error {
1031 params.Active = "pipelines"
1032 return p.executeRepo("repo/pipelines/workflow", w, params)
1033}
1034
1035func (p *Pages) Static() http.Handler {
1036 if p.dev {
1037 return http.StripPrefix("/static/", http.FileServer(http.Dir("appview/pages/static")))
1038 }
1039
1040 sub, err := fs.Sub(Files, "static")
1041 if err != nil {
1042 log.Fatalf("no static dir found? that's crazy: %v", err)
1043 }
1044 // Custom handler to apply Cache-Control headers for font files
1045 return Cache(http.StripPrefix("/static/", http.FileServer(http.FS(sub))))
1046}
1047
1048func Cache(h http.Handler) http.Handler {
1049 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1050 path := strings.Split(r.URL.Path, "?")[0]
1051
1052 if strings.HasSuffix(path, ".css") {
1053 // on day for css files
1054 w.Header().Set("Cache-Control", "public, max-age=86400")
1055 } else {
1056 w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
1057 }
1058 h.ServeHTTP(w, r)
1059 })
1060}
1061
1062func CssContentHash() string {
1063 cssFile, err := Files.Open("static/tw.css")
1064 if err != nil {
1065 log.Printf("Error opening CSS file: %v", err)
1066 return ""
1067 }
1068 defer cssFile.Close()
1069
1070 hasher := sha256.New()
1071 if _, err := io.Copy(hasher, cssFile); err != nil {
1072 log.Printf("Error hashing CSS file: %v", err)
1073 return ""
1074 }
1075
1076 return hex.EncodeToString(hasher.Sum(nil))[:8] // Use first 8 chars of hash
1077}
1078
1079func (p *Pages) Error500(w io.Writer) error {
1080 return p.execute("errors/500", w, nil)
1081}
1082
1083func (p *Pages) Error404(w io.Writer) error {
1084 return p.execute("errors/404", w, nil)
1085}
1086
1087func (p *Pages) Error503(w io.Writer) error {
1088 return p.execute("errors/503", w, nil)
1089}