this repo has no description
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/slog"
13 "net/http"
14 "os"
15 "path/filepath"
16 "strings"
17 "sync"
18
19 "tangled.org/core/api/tangled"
20 "tangled.org/core/appview/commitverify"
21 "tangled.org/core/appview/config"
22 "tangled.org/core/appview/models"
23 "tangled.org/core/appview/oauth"
24 "tangled.org/core/appview/pages/markup"
25 "tangled.org/core/appview/pages/repoinfo"
26 "tangled.org/core/appview/pagination"
27 "tangled.org/core/idresolver"
28 "tangled.org/core/patchutil"
29 "tangled.org/core/types"
30
31 "github.com/alecthomas/chroma/v2"
32 chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
33 "github.com/alecthomas/chroma/v2/lexers"
34 "github.com/alecthomas/chroma/v2/styles"
35 "github.com/bluesky-social/indigo/atproto/identity"
36 "github.com/bluesky-social/indigo/atproto/syntax"
37 "github.com/go-git/go-git/v5/plumbing"
38 "github.com/go-git/go-git/v5/plumbing/object"
39)
40
41//go:embed templates/* static legal
42var Files embed.FS
43
44type Pages struct {
45 mu sync.RWMutex
46 cache *TmplCache[string, *template.Template]
47
48 avatar config.AvatarConfig
49 resolver *idresolver.Resolver
50 dev bool
51 embedFS fs.FS
52 templateDir string // Path to templates on disk for dev mode
53 rctx *markup.RenderContext
54 logger *slog.Logger
55}
56
57func NewPages(config *config.Config, res *idresolver.Resolver) *Pages {
58 // initialized with safe defaults, can be overriden per use
59 rctx := &markup.RenderContext{
60 IsDev: config.Core.Dev,
61 CamoUrl: config.Camo.Host,
62 CamoSecret: config.Camo.SharedSecret,
63 Sanitizer: markup.NewSanitizer(),
64 }
65
66 p := &Pages{
67 mu: sync.RWMutex{},
68 cache: NewTmplCache[string, *template.Template](),
69 dev: config.Core.Dev,
70 avatar: config.Avatar,
71 rctx: rctx,
72 resolver: res,
73 templateDir: "appview/pages",
74 logger: slog.Default().With("component", "pages"),
75 }
76
77 if p.dev {
78 p.embedFS = os.DirFS(p.templateDir)
79 } else {
80 p.embedFS = Files
81 }
82
83 return p
84}
85
86// reverse of pathToName
87func (p *Pages) nameToPath(s string) string {
88 return "templates/" + s + ".html"
89}
90
91func (p *Pages) fragmentPaths() ([]string, error) {
92 var fragmentPaths []string
93 err := fs.WalkDir(p.embedFS, "templates", func(path string, d fs.DirEntry, err error) error {
94 if err != nil {
95 return err
96 }
97 if d.IsDir() {
98 return nil
99 }
100 if !strings.HasSuffix(path, ".html") {
101 return nil
102 }
103 if !strings.Contains(path, "fragments/") {
104 return nil
105 }
106 fragmentPaths = append(fragmentPaths, path)
107 return nil
108 })
109 if err != nil {
110 return nil, err
111 }
112
113 return fragmentPaths, nil
114}
115
116// parse without memoization
117func (p *Pages) rawParse(stack ...string) (*template.Template, error) {
118 paths, err := p.fragmentPaths()
119 if err != nil {
120 return nil, err
121 }
122 for _, s := range stack {
123 paths = append(paths, p.nameToPath(s))
124 }
125
126 funcs := p.funcMap()
127 top := stack[len(stack)-1]
128 parsed, err := template.New(top).
129 Funcs(funcs).
130 ParseFS(p.embedFS, paths...)
131 if err != nil {
132 return nil, err
133 }
134
135 return parsed, nil
136}
137
138func (p *Pages) parse(stack ...string) (*template.Template, error) {
139 key := strings.Join(stack, "|")
140
141 // never cache in dev mode
142 if cached, exists := p.cache.Get(key); !p.dev && exists {
143 return cached, nil
144 }
145
146 result, err := p.rawParse(stack...)
147 if err != nil {
148 return nil, err
149 }
150
151 p.cache.Set(key, result)
152 return result, nil
153}
154
155func (p *Pages) parseBase(top string) (*template.Template, error) {
156 stack := []string{
157 "layouts/base",
158 top,
159 }
160 return p.parse(stack...)
161}
162
163func (p *Pages) parseRepoBase(top string) (*template.Template, error) {
164 stack := []string{
165 "layouts/base",
166 "layouts/repobase",
167 top,
168 }
169 return p.parse(stack...)
170}
171
172func (p *Pages) parseProfileBase(top string) (*template.Template, error) {
173 stack := []string{
174 "layouts/base",
175 "layouts/profilebase",
176 top,
177 }
178 return p.parse(stack...)
179}
180
181func (p *Pages) executePlain(name string, w io.Writer, params any) error {
182 tpl, err := p.parse(name)
183 if err != nil {
184 return err
185 }
186
187 return tpl.Execute(w, params)
188}
189
190func (p *Pages) execute(name string, w io.Writer, params any) error {
191 tpl, err := p.parseBase(name)
192 if err != nil {
193 return err
194 }
195
196 return tpl.ExecuteTemplate(w, "layouts/base", params)
197}
198
199func (p *Pages) executeRepo(name string, w io.Writer, params any) error {
200 tpl, err := p.parseRepoBase(name)
201 if err != nil {
202 return err
203 }
204
205 return tpl.ExecuteTemplate(w, "layouts/base", params)
206}
207
208func (p *Pages) executeProfile(name string, w io.Writer, params any) error {
209 tpl, err := p.parseProfileBase(name)
210 if err != nil {
211 return err
212 }
213
214 return tpl.ExecuteTemplate(w, "layouts/base", params)
215}
216
217func (p *Pages) Favicon(w io.Writer) error {
218 return p.executePlain("fragments/dolly/silhouette", w, nil)
219}
220
221type LoginParams struct {
222 ReturnUrl string
223}
224
225func (p *Pages) Login(w io.Writer, params LoginParams) error {
226 return p.executePlain("user/login", w, params)
227}
228
229type SignupParams struct {
230 CloudflareSiteKey string
231}
232
233func (p *Pages) Signup(w io.Writer, params SignupParams) error {
234 return p.executePlain("user/signup", w, params)
235}
236
237func (p *Pages) CompleteSignup(w io.Writer) error {
238 return p.executePlain("user/completeSignup", w, nil)
239}
240
241type TermsOfServiceParams struct {
242 LoggedInUser *oauth.User
243 Content template.HTML
244}
245
246func (p *Pages) TermsOfService(w io.Writer, params TermsOfServiceParams) error {
247 filename := "terms.md"
248 filePath := filepath.Join("legal", filename)
249
250 file, err := p.embedFS.Open(filePath)
251 if err != nil {
252 return fmt.Errorf("failed to read %s: %w", filename, err)
253 }
254 defer file.Close()
255
256 markdownBytes, err := io.ReadAll(file)
257 if err != nil {
258 return fmt.Errorf("failed to read %s: %w", filename, err)
259 }
260
261 p.rctx.RendererType = markup.RendererTypeDefault
262 htmlString := p.rctx.RenderMarkdown(string(markdownBytes))
263 sanitized := p.rctx.SanitizeDefault(htmlString)
264 params.Content = template.HTML(sanitized)
265
266 return p.execute("legal/terms", w, params)
267}
268
269type PrivacyPolicyParams struct {
270 LoggedInUser *oauth.User
271 Content template.HTML
272}
273
274func (p *Pages) PrivacyPolicy(w io.Writer, params PrivacyPolicyParams) error {
275 filename := "privacy.md"
276 filePath := filepath.Join("legal", filename)
277
278 file, err := p.embedFS.Open(filePath)
279 if err != nil {
280 return fmt.Errorf("failed to read %s: %w", filename, err)
281 }
282 defer file.Close()
283
284 markdownBytes, err := io.ReadAll(file)
285 if err != nil {
286 return fmt.Errorf("failed to read %s: %w", filename, err)
287 }
288
289 p.rctx.RendererType = markup.RendererTypeDefault
290 htmlString := p.rctx.RenderMarkdown(string(markdownBytes))
291 sanitized := p.rctx.SanitizeDefault(htmlString)
292 params.Content = template.HTML(sanitized)
293
294 return p.execute("legal/privacy", w, params)
295}
296
297type BrandParams struct {
298 LoggedInUser *oauth.User
299}
300
301func (p *Pages) Brand(w io.Writer, params BrandParams) error {
302 return p.execute("brand/brand", w, params)
303}
304
305type TimelineParams struct {
306 LoggedInUser *oauth.User
307 Timeline []models.TimelineEvent
308 Repos []models.Repo
309}
310
311func (p *Pages) Timeline(w io.Writer, params TimelineParams) error {
312 return p.execute("timeline/timeline", w, params)
313}
314
315type UserProfileSettingsParams struct {
316 LoggedInUser *oauth.User
317 Tabs []map[string]any
318 Tab string
319}
320
321func (p *Pages) UserProfileSettings(w io.Writer, params UserProfileSettingsParams) error {
322 return p.execute("user/settings/profile", w, params)
323}
324
325type NotificationsParams struct {
326 LoggedInUser *oauth.User
327 Notifications []*models.NotificationWithEntity
328 UnreadCount int
329 Page pagination.Page
330 Total int64
331}
332
333func (p *Pages) Notifications(w io.Writer, params NotificationsParams) error {
334 return p.execute("notifications/list", w, params)
335}
336
337type NotificationItemParams struct {
338 Notification *models.Notification
339}
340
341func (p *Pages) NotificationItem(w io.Writer, params NotificationItemParams) error {
342 return p.executePlain("notifications/fragments/item", w, params)
343}
344
345type NotificationCountParams struct {
346 Count int64
347}
348
349func (p *Pages) NotificationCount(w io.Writer, params NotificationCountParams) error {
350 return p.executePlain("notifications/fragments/count", w, params)
351}
352
353type UserKeysSettingsParams struct {
354 LoggedInUser *oauth.User
355 PubKeys []models.PublicKey
356 Tabs []map[string]any
357 Tab string
358}
359
360func (p *Pages) UserKeysSettings(w io.Writer, params UserKeysSettingsParams) error {
361 return p.execute("user/settings/keys", w, params)
362}
363
364type UserEmailsSettingsParams struct {
365 LoggedInUser *oauth.User
366 Emails []models.Email
367 Tabs []map[string]any
368 Tab string
369}
370
371func (p *Pages) UserEmailsSettings(w io.Writer, params UserEmailsSettingsParams) error {
372 return p.execute("user/settings/emails", w, params)
373}
374
375type UserNotificationSettingsParams struct {
376 LoggedInUser *oauth.User
377 Preferences *models.NotificationPreferences
378 Tabs []map[string]any
379 Tab string
380}
381
382func (p *Pages) UserNotificationSettings(w io.Writer, params UserNotificationSettingsParams) error {
383 return p.execute("user/settings/notifications", w, params)
384}
385
386type UpgradeBannerParams struct {
387 Registrations []models.Registration
388 Spindles []models.Spindle
389}
390
391func (p *Pages) UpgradeBanner(w io.Writer, params UpgradeBannerParams) error {
392 return p.executePlain("banner", w, params)
393}
394
395type KnotsParams struct {
396 LoggedInUser *oauth.User
397 Registrations []models.Registration
398}
399
400func (p *Pages) Knots(w io.Writer, params KnotsParams) error {
401 return p.execute("knots/index", w, params)
402}
403
404type KnotParams struct {
405 LoggedInUser *oauth.User
406 Registration *models.Registration
407 Members []string
408 Repos map[string][]models.Repo
409 IsOwner bool
410}
411
412func (p *Pages) Knot(w io.Writer, params KnotParams) error {
413 return p.execute("knots/dashboard", w, params)
414}
415
416type KnotListingParams struct {
417 *models.Registration
418}
419
420func (p *Pages) KnotListing(w io.Writer, params KnotListingParams) error {
421 return p.executePlain("knots/fragments/knotListing", w, params)
422}
423
424type SpindlesParams struct {
425 LoggedInUser *oauth.User
426 Spindles []models.Spindle
427}
428
429func (p *Pages) Spindles(w io.Writer, params SpindlesParams) error {
430 return p.execute("spindles/index", w, params)
431}
432
433type SpindleListingParams struct {
434 models.Spindle
435}
436
437func (p *Pages) SpindleListing(w io.Writer, params SpindleListingParams) error {
438 return p.executePlain("spindles/fragments/spindleListing", w, params)
439}
440
441type SpindleDashboardParams struct {
442 LoggedInUser *oauth.User
443 Spindle models.Spindle
444 Members []string
445 Repos map[string][]models.Repo
446}
447
448func (p *Pages) SpindleDashboard(w io.Writer, params SpindleDashboardParams) error {
449 return p.execute("spindles/dashboard", w, params)
450}
451
452type NewRepoParams struct {
453 LoggedInUser *oauth.User
454 Knots []string
455}
456
457func (p *Pages) NewRepo(w io.Writer, params NewRepoParams) error {
458 return p.execute("repo/new", w, params)
459}
460
461type ForkRepoParams struct {
462 LoggedInUser *oauth.User
463 Knots []string
464 RepoInfo repoinfo.RepoInfo
465}
466
467func (p *Pages) ForkRepo(w io.Writer, params ForkRepoParams) error {
468 return p.execute("repo/fork", w, params)
469}
470
471type ProfileCard struct {
472 UserDid string
473 UserHandle string
474 FollowStatus models.FollowStatus
475 Punchcard *models.Punchcard
476 Profile *models.Profile
477 Stats ProfileStats
478 Active string
479}
480
481type ProfileStats struct {
482 RepoCount int64
483 StarredCount int64
484 StringCount int64
485 FollowersCount int64
486 FollowingCount int64
487}
488
489func (p *ProfileCard) GetTabs() [][]any {
490 tabs := [][]any{
491 {"overview", "overview", "square-chart-gantt", nil},
492 {"repos", "repos", "book-marked", p.Stats.RepoCount},
493 {"starred", "starred", "star", p.Stats.StarredCount},
494 {"strings", "strings", "line-squiggle", p.Stats.StringCount},
495 }
496
497 return tabs
498}
499
500type ProfileOverviewParams struct {
501 LoggedInUser *oauth.User
502 Repos []models.Repo
503 CollaboratingRepos []models.Repo
504 ProfileTimeline *models.ProfileTimeline
505 Card *ProfileCard
506 Active string
507}
508
509func (p *Pages) ProfileOverview(w io.Writer, params ProfileOverviewParams) error {
510 params.Active = "overview"
511 return p.executeProfile("user/overview", w, params)
512}
513
514type ProfileReposParams struct {
515 LoggedInUser *oauth.User
516 Repos []models.Repo
517 Card *ProfileCard
518 Active string
519}
520
521func (p *Pages) ProfileRepos(w io.Writer, params ProfileReposParams) error {
522 params.Active = "repos"
523 return p.executeProfile("user/repos", w, params)
524}
525
526type ProfileStarredParams struct {
527 LoggedInUser *oauth.User
528 Repos []models.Repo
529 Card *ProfileCard
530 Active string
531}
532
533func (p *Pages) ProfileStarred(w io.Writer, params ProfileStarredParams) error {
534 params.Active = "starred"
535 return p.executeProfile("user/starred", w, params)
536}
537
538type ProfileStringsParams struct {
539 LoggedInUser *oauth.User
540 Strings []models.String
541 Card *ProfileCard
542 Active string
543}
544
545func (p *Pages) ProfileStrings(w io.Writer, params ProfileStringsParams) error {
546 params.Active = "strings"
547 return p.executeProfile("user/strings", w, params)
548}
549
550type FollowCard struct {
551 UserDid string
552 LoggedInUser *oauth.User
553 FollowStatus models.FollowStatus
554 FollowersCount int64
555 FollowingCount int64
556 Profile *models.Profile
557}
558
559type ProfileFollowersParams struct {
560 LoggedInUser *oauth.User
561 Followers []FollowCard
562 Card *ProfileCard
563 Active string
564}
565
566func (p *Pages) ProfileFollowers(w io.Writer, params ProfileFollowersParams) error {
567 params.Active = "overview"
568 return p.executeProfile("user/followers", w, params)
569}
570
571type ProfileFollowingParams struct {
572 LoggedInUser *oauth.User
573 Following []FollowCard
574 Card *ProfileCard
575 Active string
576}
577
578func (p *Pages) ProfileFollowing(w io.Writer, params ProfileFollowingParams) error {
579 params.Active = "overview"
580 return p.executeProfile("user/following", w, params)
581}
582
583type FollowFragmentParams struct {
584 UserDid string
585 FollowStatus models.FollowStatus
586}
587
588func (p *Pages) FollowFragment(w io.Writer, params FollowFragmentParams) error {
589 return p.executePlain("user/fragments/follow", w, params)
590}
591
592type EditBioParams struct {
593 LoggedInUser *oauth.User
594 Profile *models.Profile
595}
596
597func (p *Pages) EditBioFragment(w io.Writer, params EditBioParams) error {
598 return p.executePlain("user/fragments/editBio", w, params)
599}
600
601type EditPinsParams struct {
602 LoggedInUser *oauth.User
603 Profile *models.Profile
604 AllRepos []PinnedRepo
605}
606
607type PinnedRepo struct {
608 IsPinned bool
609 models.Repo
610}
611
612func (p *Pages) EditPinsFragment(w io.Writer, params EditPinsParams) error {
613 return p.executePlain("user/fragments/editPins", w, params)
614}
615
616type RepoStarFragmentParams struct {
617 IsStarred bool
618 RepoAt syntax.ATURI
619 Stats models.RepoStats
620}
621
622func (p *Pages) RepoStarFragment(w io.Writer, params RepoStarFragmentParams) error {
623 return p.executePlain("repo/fragments/repoStar", w, params)
624}
625
626type RepoDescriptionParams struct {
627 RepoInfo repoinfo.RepoInfo
628}
629
630func (p *Pages) EditRepoDescriptionFragment(w io.Writer, params RepoDescriptionParams) error {
631 return p.executePlain("repo/fragments/editRepoDescription", w, params)
632}
633
634func (p *Pages) RepoDescriptionFragment(w io.Writer, params RepoDescriptionParams) error {
635 return p.executePlain("repo/fragments/repoDescription", w, params)
636}
637
638type RepoIndexParams struct {
639 LoggedInUser *oauth.User
640 RepoInfo repoinfo.RepoInfo
641 Active string
642 TagMap map[string][]string
643 CommitsTrunc []*object.Commit
644 TagsTrunc []*types.TagReference
645 BranchesTrunc []types.Branch
646 // ForkInfo *types.ForkInfo
647 HTMLReadme template.HTML
648 Raw bool
649 EmailToDidOrHandle map[string]string
650 VerifiedCommits commitverify.VerifiedCommits
651 Languages []types.RepoLanguageDetails
652 Pipelines map[string]models.Pipeline
653 NeedsKnotUpgrade bool
654 types.RepoIndexResponse
655}
656
657func (p *Pages) RepoIndexPage(w io.Writer, params RepoIndexParams) error {
658 params.Active = "overview"
659 if params.IsEmpty {
660 return p.executeRepo("repo/empty", w, params)
661 }
662
663 if params.NeedsKnotUpgrade {
664 return p.executeRepo("repo/needsUpgrade", w, params)
665 }
666
667 p.rctx.RepoInfo = params.RepoInfo
668 p.rctx.RepoInfo.Ref = params.Ref
669 p.rctx.RendererType = markup.RendererTypeRepoMarkdown
670
671 if params.ReadmeFileName != "" {
672 ext := filepath.Ext(params.ReadmeFileName)
673 switch ext {
674 case ".md", ".markdown", ".mdown", ".mkdn", ".mkd":
675 params.Raw = false
676 htmlString := p.rctx.RenderMarkdown(params.Readme)
677 sanitized := p.rctx.SanitizeDefault(htmlString)
678 params.HTMLReadme = template.HTML(sanitized)
679 default:
680 params.Raw = true
681 }
682 }
683
684 return p.executeRepo("repo/index", w, params)
685}
686
687type RepoLogParams struct {
688 LoggedInUser *oauth.User
689 RepoInfo repoinfo.RepoInfo
690 TagMap map[string][]string
691 types.RepoLogResponse
692 Active string
693 EmailToDidOrHandle map[string]string
694 VerifiedCommits commitverify.VerifiedCommits
695 Pipelines map[string]models.Pipeline
696}
697
698func (p *Pages) RepoLog(w io.Writer, params RepoLogParams) error {
699 params.Active = "overview"
700 return p.executeRepo("repo/log", w, params)
701}
702
703type RepoCommitParams struct {
704 LoggedInUser *oauth.User
705 RepoInfo repoinfo.RepoInfo
706 Active string
707 EmailToDidOrHandle map[string]string
708 Pipeline *models.Pipeline
709 DiffOpts types.DiffOpts
710
711 // singular because it's always going to be just one
712 VerifiedCommit commitverify.VerifiedCommits
713
714 types.RepoCommitResponse
715}
716
717func (p *Pages) RepoCommit(w io.Writer, params RepoCommitParams) error {
718 params.Active = "overview"
719 return p.executeRepo("repo/commit", w, params)
720}
721
722type RepoTreeParams struct {
723 LoggedInUser *oauth.User
724 RepoInfo repoinfo.RepoInfo
725 Active string
726 BreadCrumbs [][]string
727 TreePath string
728 Raw bool
729 HTMLReadme template.HTML
730 types.RepoTreeResponse
731}
732
733type RepoTreeStats struct {
734 NumFolders uint64
735 NumFiles uint64
736}
737
738func (r RepoTreeParams) TreeStats() RepoTreeStats {
739 numFolders, numFiles := 0, 0
740 for _, f := range r.Files {
741 if !f.IsFile {
742 numFolders += 1
743 } else if f.IsFile {
744 numFiles += 1
745 }
746 }
747
748 return RepoTreeStats{
749 NumFolders: uint64(numFolders),
750 NumFiles: uint64(numFiles),
751 }
752}
753
754func (p *Pages) RepoTree(w io.Writer, params RepoTreeParams) error {
755 params.Active = "overview"
756
757 p.rctx.RepoInfo = params.RepoInfo
758 p.rctx.RepoInfo.Ref = params.Ref
759 p.rctx.RendererType = markup.RendererTypeRepoMarkdown
760
761 if params.ReadmeFileName != "" {
762 ext := filepath.Ext(params.ReadmeFileName)
763 switch ext {
764 case ".md", ".markdown", ".mdown", ".mkdn", ".mkd":
765 params.Raw = false
766 htmlString := p.rctx.RenderMarkdown(params.Readme)
767 sanitized := p.rctx.SanitizeDefault(htmlString)
768 params.HTMLReadme = template.HTML(sanitized)
769 default:
770 params.Raw = true
771 }
772 }
773
774 return p.executeRepo("repo/tree", w, params)
775}
776
777type RepoBranchesParams struct {
778 LoggedInUser *oauth.User
779 RepoInfo repoinfo.RepoInfo
780 Active string
781 types.RepoBranchesResponse
782}
783
784func (p *Pages) RepoBranches(w io.Writer, params RepoBranchesParams) error {
785 params.Active = "overview"
786 return p.executeRepo("repo/branches", w, params)
787}
788
789type RepoTagsParams struct {
790 LoggedInUser *oauth.User
791 RepoInfo repoinfo.RepoInfo
792 Active string
793 types.RepoTagsResponse
794 ArtifactMap map[plumbing.Hash][]models.Artifact
795 DanglingArtifacts []models.Artifact
796}
797
798func (p *Pages) RepoTags(w io.Writer, params RepoTagsParams) error {
799 params.Active = "overview"
800 return p.executeRepo("repo/tags", w, params)
801}
802
803type RepoArtifactParams struct {
804 LoggedInUser *oauth.User
805 RepoInfo repoinfo.RepoInfo
806 Artifact models.Artifact
807}
808
809func (p *Pages) RepoArtifactFragment(w io.Writer, params RepoArtifactParams) error {
810 return p.executePlain("repo/fragments/artifact", w, params)
811}
812
813type RepoBlobParams struct {
814 LoggedInUser *oauth.User
815 RepoInfo repoinfo.RepoInfo
816 Active string
817 Unsupported bool
818 IsImage bool
819 IsVideo bool
820 ContentSrc string
821 BreadCrumbs [][]string
822 ShowRendered bool
823 RenderToggle bool
824 RenderedContents template.HTML
825 *tangled.RepoBlob_Output
826 // Computed fields for template compatibility
827 Contents string
828 Lines int
829 SizeHint uint64
830 IsBinary bool
831}
832
833func (p *Pages) RepoBlob(w io.Writer, params RepoBlobParams) error {
834 var style *chroma.Style = styles.Get("catpuccin-latte")
835
836 if params.ShowRendered {
837 switch markup.GetFormat(params.Path) {
838 case markup.FormatMarkdown:
839 p.rctx.RepoInfo = params.RepoInfo
840 p.rctx.RendererType = markup.RendererTypeRepoMarkdown
841 htmlString := p.rctx.RenderMarkdown(params.Contents)
842 sanitized := p.rctx.SanitizeDefault(htmlString)
843 params.RenderedContents = template.HTML(sanitized)
844 }
845 }
846
847 c := params.Contents
848 formatter := chromahtml.New(
849 chromahtml.InlineCode(false),
850 chromahtml.WithLineNumbers(true),
851 chromahtml.WithLinkableLineNumbers(true, "L"),
852 chromahtml.Standalone(false),
853 chromahtml.WithClasses(true),
854 )
855
856 lexer := lexers.Get(filepath.Base(params.Path))
857 if lexer == nil {
858 lexer = lexers.Fallback
859 }
860
861 iterator, err := lexer.Tokenise(nil, c)
862 if err != nil {
863 return fmt.Errorf("chroma tokenize: %w", err)
864 }
865
866 var code bytes.Buffer
867 err = formatter.Format(&code, style, iterator)
868 if err != nil {
869 return fmt.Errorf("chroma format: %w", err)
870 }
871
872 params.Contents = code.String()
873 params.Active = "overview"
874 return p.executeRepo("repo/blob", w, params)
875}
876
877type Collaborator struct {
878 Did string
879 Handle string
880 Role string
881}
882
883type RepoSettingsParams struct {
884 LoggedInUser *oauth.User
885 RepoInfo repoinfo.RepoInfo
886 Collaborators []Collaborator
887 Active string
888 Branches []types.Branch
889 Spindles []string
890 CurrentSpindle string
891 Secrets []*tangled.RepoListSecrets_Secret
892
893 // TODO: use repoinfo.roles
894 IsCollaboratorInviteAllowed bool
895}
896
897func (p *Pages) RepoSettings(w io.Writer, params RepoSettingsParams) error {
898 params.Active = "settings"
899 return p.executeRepo("repo/settings", w, params)
900}
901
902type RepoGeneralSettingsParams struct {
903 LoggedInUser *oauth.User
904 RepoInfo repoinfo.RepoInfo
905 Labels []models.LabelDefinition
906 DefaultLabels []models.LabelDefinition
907 SubscribedLabels map[string]struct{}
908 ShouldSubscribeAll bool
909 Active string
910 Tabs []map[string]any
911 Tab string
912 Branches []types.Branch
913}
914
915func (p *Pages) RepoGeneralSettings(w io.Writer, params RepoGeneralSettingsParams) error {
916 params.Active = "settings"
917 return p.executeRepo("repo/settings/general", w, params)
918}
919
920type RepoAccessSettingsParams struct {
921 LoggedInUser *oauth.User
922 RepoInfo repoinfo.RepoInfo
923 Active string
924 Tabs []map[string]any
925 Tab string
926 Collaborators []Collaborator
927}
928
929func (p *Pages) RepoAccessSettings(w io.Writer, params RepoAccessSettingsParams) error {
930 params.Active = "settings"
931 return p.executeRepo("repo/settings/access", w, params)
932}
933
934type RepoPipelineSettingsParams struct {
935 LoggedInUser *oauth.User
936 RepoInfo repoinfo.RepoInfo
937 Active string
938 Tabs []map[string]any
939 Tab string
940 Spindles []string
941 CurrentSpindle string
942 Secrets []map[string]any
943}
944
945func (p *Pages) RepoPipelineSettings(w io.Writer, params RepoPipelineSettingsParams) error {
946 params.Active = "settings"
947 return p.executeRepo("repo/settings/pipelines", w, params)
948}
949
950type RepoIssuesParams struct {
951 LoggedInUser *oauth.User
952 RepoInfo repoinfo.RepoInfo
953 Active string
954 Issues []models.Issue
955 LabelDefs map[string]*models.LabelDefinition
956 Page pagination.Page
957 FilteringByOpen bool
958}
959
960func (p *Pages) RepoIssues(w io.Writer, params RepoIssuesParams) error {
961 params.Active = "issues"
962 return p.executeRepo("repo/issues/issues", w, params)
963}
964
965type RepoSingleIssueParams struct {
966 LoggedInUser *oauth.User
967 RepoInfo repoinfo.RepoInfo
968 Active string
969 Issue *models.Issue
970 CommentList []models.CommentListItem
971 LabelDefs map[string]*models.LabelDefinition
972
973 OrderedReactionKinds []models.ReactionKind
974 Reactions map[models.ReactionKind]int
975 UserReacted map[models.ReactionKind]bool
976}
977
978func (p *Pages) RepoSingleIssue(w io.Writer, params RepoSingleIssueParams) error {
979 params.Active = "issues"
980 return p.executeRepo("repo/issues/issue", w, params)
981}
982
983type EditIssueParams struct {
984 LoggedInUser *oauth.User
985 RepoInfo repoinfo.RepoInfo
986 Issue *models.Issue
987 Action string
988}
989
990func (p *Pages) EditIssueFragment(w io.Writer, params EditIssueParams) error {
991 params.Action = "edit"
992 return p.executePlain("repo/issues/fragments/putIssue", w, params)
993}
994
995type ThreadReactionFragmentParams struct {
996 ThreadAt syntax.ATURI
997 Kind models.ReactionKind
998 Count int
999 IsReacted bool
1000}
1001
1002func (p *Pages) ThreadReactionFragment(w io.Writer, params ThreadReactionFragmentParams) error {
1003 return p.executePlain("repo/fragments/reaction", w, params)
1004}
1005
1006type RepoNewIssueParams struct {
1007 LoggedInUser *oauth.User
1008 RepoInfo repoinfo.RepoInfo
1009 Issue *models.Issue // existing issue if any -- passed when editing
1010 Active string
1011 Action string
1012}
1013
1014func (p *Pages) RepoNewIssue(w io.Writer, params RepoNewIssueParams) error {
1015 params.Active = "issues"
1016 params.Action = "create"
1017 return p.executeRepo("repo/issues/new", w, params)
1018}
1019
1020type EditIssueCommentParams struct {
1021 LoggedInUser *oauth.User
1022 RepoInfo repoinfo.RepoInfo
1023 Issue *models.Issue
1024 Comment *models.IssueComment
1025}
1026
1027func (p *Pages) EditIssueCommentFragment(w io.Writer, params EditIssueCommentParams) error {
1028 return p.executePlain("repo/issues/fragments/editIssueComment", w, params)
1029}
1030
1031type ReplyIssueCommentPlaceholderParams struct {
1032 LoggedInUser *oauth.User
1033 RepoInfo repoinfo.RepoInfo
1034 Issue *models.Issue
1035 Comment *models.IssueComment
1036}
1037
1038func (p *Pages) ReplyIssueCommentPlaceholderFragment(w io.Writer, params ReplyIssueCommentPlaceholderParams) error {
1039 return p.executePlain("repo/issues/fragments/replyIssueCommentPlaceholder", w, params)
1040}
1041
1042type ReplyIssueCommentParams struct {
1043 LoggedInUser *oauth.User
1044 RepoInfo repoinfo.RepoInfo
1045 Issue *models.Issue
1046 Comment *models.IssueComment
1047}
1048
1049func (p *Pages) ReplyIssueCommentFragment(w io.Writer, params ReplyIssueCommentParams) error {
1050 return p.executePlain("repo/issues/fragments/replyComment", w, params)
1051}
1052
1053type IssueCommentBodyParams struct {
1054 LoggedInUser *oauth.User
1055 RepoInfo repoinfo.RepoInfo
1056 Issue *models.Issue
1057 Comment *models.IssueComment
1058}
1059
1060func (p *Pages) IssueCommentBodyFragment(w io.Writer, params IssueCommentBodyParams) error {
1061 return p.executePlain("repo/issues/fragments/issueCommentBody", w, params)
1062}
1063
1064type RepoNewPullParams struct {
1065 LoggedInUser *oauth.User
1066 RepoInfo repoinfo.RepoInfo
1067 Branches []types.Branch
1068 Strategy string
1069 SourceBranch string
1070 TargetBranch string
1071 Title string
1072 Body string
1073 Active string
1074}
1075
1076func (p *Pages) RepoNewPull(w io.Writer, params RepoNewPullParams) error {
1077 params.Active = "pulls"
1078 return p.executeRepo("repo/pulls/new", w, params)
1079}
1080
1081type RepoPullsParams struct {
1082 LoggedInUser *oauth.User
1083 RepoInfo repoinfo.RepoInfo
1084 Pulls []*models.Pull
1085 Active string
1086 FilteringBy models.PullState
1087 Stacks map[string]models.Stack
1088 Pipelines map[string]models.Pipeline
1089}
1090
1091func (p *Pages) RepoPulls(w io.Writer, params RepoPullsParams) error {
1092 params.Active = "pulls"
1093 return p.executeRepo("repo/pulls/pulls", w, params)
1094}
1095
1096type ResubmitResult uint64
1097
1098const (
1099 ShouldResubmit ResubmitResult = iota
1100 ShouldNotResubmit
1101 Unknown
1102)
1103
1104func (r ResubmitResult) Yes() bool {
1105 return r == ShouldResubmit
1106}
1107func (r ResubmitResult) No() bool {
1108 return r == ShouldNotResubmit
1109}
1110func (r ResubmitResult) Unknown() bool {
1111 return r == Unknown
1112}
1113
1114type RepoSinglePullParams struct {
1115 LoggedInUser *oauth.User
1116 RepoInfo repoinfo.RepoInfo
1117 Active string
1118 Pull *models.Pull
1119 Stack models.Stack
1120 AbandonedPulls []*models.Pull
1121 MergeCheck types.MergeCheckResponse
1122 ResubmitCheck ResubmitResult
1123 Pipelines map[string]models.Pipeline
1124
1125 OrderedReactionKinds []models.ReactionKind
1126 Reactions map[models.ReactionKind]int
1127 UserReacted map[models.ReactionKind]bool
1128}
1129
1130func (p *Pages) RepoSinglePull(w io.Writer, params RepoSinglePullParams) error {
1131 params.Active = "pulls"
1132 return p.executeRepo("repo/pulls/pull", w, params)
1133}
1134
1135type RepoPullPatchParams struct {
1136 LoggedInUser *oauth.User
1137 RepoInfo repoinfo.RepoInfo
1138 Pull *models.Pull
1139 Stack models.Stack
1140 Diff *types.NiceDiff
1141 Round int
1142 Submission *models.PullSubmission
1143 OrderedReactionKinds []models.ReactionKind
1144 DiffOpts types.DiffOpts
1145}
1146
1147// this name is a mouthful
1148func (p *Pages) RepoPullPatchPage(w io.Writer, params RepoPullPatchParams) error {
1149 return p.execute("repo/pulls/patch", w, params)
1150}
1151
1152type RepoPullInterdiffParams struct {
1153 LoggedInUser *oauth.User
1154 RepoInfo repoinfo.RepoInfo
1155 Pull *models.Pull
1156 Round int
1157 Interdiff *patchutil.InterdiffResult
1158 OrderedReactionKinds []models.ReactionKind
1159 DiffOpts types.DiffOpts
1160}
1161
1162// this name is a mouthful
1163func (p *Pages) RepoPullInterdiffPage(w io.Writer, params RepoPullInterdiffParams) error {
1164 return p.execute("repo/pulls/interdiff", w, params)
1165}
1166
1167type PullPatchUploadParams struct {
1168 RepoInfo repoinfo.RepoInfo
1169}
1170
1171func (p *Pages) PullPatchUploadFragment(w io.Writer, params PullPatchUploadParams) error {
1172 return p.executePlain("repo/pulls/fragments/pullPatchUpload", w, params)
1173}
1174
1175type PullCompareBranchesParams struct {
1176 RepoInfo repoinfo.RepoInfo
1177 Branches []types.Branch
1178 SourceBranch string
1179}
1180
1181func (p *Pages) PullCompareBranchesFragment(w io.Writer, params PullCompareBranchesParams) error {
1182 return p.executePlain("repo/pulls/fragments/pullCompareBranches", w, params)
1183}
1184
1185type PullCompareForkParams struct {
1186 RepoInfo repoinfo.RepoInfo
1187 Forks []models.Repo
1188 Selected string
1189}
1190
1191func (p *Pages) PullCompareForkFragment(w io.Writer, params PullCompareForkParams) error {
1192 return p.executePlain("repo/pulls/fragments/pullCompareForks", w, params)
1193}
1194
1195type PullCompareForkBranchesParams struct {
1196 RepoInfo repoinfo.RepoInfo
1197 SourceBranches []types.Branch
1198 TargetBranches []types.Branch
1199}
1200
1201func (p *Pages) PullCompareForkBranchesFragment(w io.Writer, params PullCompareForkBranchesParams) error {
1202 return p.executePlain("repo/pulls/fragments/pullCompareForksBranches", w, params)
1203}
1204
1205type PullResubmitParams struct {
1206 LoggedInUser *oauth.User
1207 RepoInfo repoinfo.RepoInfo
1208 Pull *models.Pull
1209 SubmissionId int
1210}
1211
1212func (p *Pages) PullResubmitFragment(w io.Writer, params PullResubmitParams) error {
1213 return p.executePlain("repo/pulls/fragments/pullResubmit", w, params)
1214}
1215
1216type PullActionsParams struct {
1217 LoggedInUser *oauth.User
1218 RepoInfo repoinfo.RepoInfo
1219 Pull *models.Pull
1220 RoundNumber int
1221 MergeCheck types.MergeCheckResponse
1222 ResubmitCheck ResubmitResult
1223 Stack models.Stack
1224}
1225
1226func (p *Pages) PullActionsFragment(w io.Writer, params PullActionsParams) error {
1227 return p.executePlain("repo/pulls/fragments/pullActions", w, params)
1228}
1229
1230type PullNewCommentParams struct {
1231 LoggedInUser *oauth.User
1232 RepoInfo repoinfo.RepoInfo
1233 Pull *models.Pull
1234 RoundNumber int
1235}
1236
1237func (p *Pages) PullNewCommentFragment(w io.Writer, params PullNewCommentParams) error {
1238 return p.executePlain("repo/pulls/fragments/pullNewComment", w, params)
1239}
1240
1241type RepoCompareParams struct {
1242 LoggedInUser *oauth.User
1243 RepoInfo repoinfo.RepoInfo
1244 Forks []models.Repo
1245 Branches []types.Branch
1246 Tags []*types.TagReference
1247 Base string
1248 Head string
1249 Diff *types.NiceDiff
1250 DiffOpts types.DiffOpts
1251
1252 Active string
1253}
1254
1255func (p *Pages) RepoCompare(w io.Writer, params RepoCompareParams) error {
1256 params.Active = "overview"
1257 return p.executeRepo("repo/compare/compare", w, params)
1258}
1259
1260type RepoCompareNewParams struct {
1261 LoggedInUser *oauth.User
1262 RepoInfo repoinfo.RepoInfo
1263 Forks []models.Repo
1264 Branches []types.Branch
1265 Tags []*types.TagReference
1266 Base string
1267 Head string
1268
1269 Active string
1270}
1271
1272func (p *Pages) RepoCompareNew(w io.Writer, params RepoCompareNewParams) error {
1273 params.Active = "overview"
1274 return p.executeRepo("repo/compare/new", w, params)
1275}
1276
1277type RepoCompareAllowPullParams struct {
1278 LoggedInUser *oauth.User
1279 RepoInfo repoinfo.RepoInfo
1280 Base string
1281 Head string
1282}
1283
1284func (p *Pages) RepoCompareAllowPullFragment(w io.Writer, params RepoCompareAllowPullParams) error {
1285 return p.executePlain("repo/fragments/compareAllowPull", w, params)
1286}
1287
1288type RepoCompareDiffParams struct {
1289 LoggedInUser *oauth.User
1290 RepoInfo repoinfo.RepoInfo
1291 Diff types.NiceDiff
1292}
1293
1294func (p *Pages) RepoCompareDiff(w io.Writer, params RepoCompareDiffParams) error {
1295 return p.executePlain("repo/fragments/diff", w, []any{params.RepoInfo.FullName, ¶ms.Diff})
1296}
1297
1298type LabelPanelParams struct {
1299 LoggedInUser *oauth.User
1300 RepoInfo repoinfo.RepoInfo
1301 Defs map[string]*models.LabelDefinition
1302 Subject string
1303 State models.LabelState
1304}
1305
1306func (p *Pages) LabelPanel(w io.Writer, params LabelPanelParams) error {
1307 return p.executePlain("repo/fragments/labelPanel", w, params)
1308}
1309
1310type EditLabelPanelParams struct {
1311 LoggedInUser *oauth.User
1312 RepoInfo repoinfo.RepoInfo
1313 Defs map[string]*models.LabelDefinition
1314 Subject string
1315 State models.LabelState
1316}
1317
1318func (p *Pages) EditLabelPanel(w io.Writer, params EditLabelPanelParams) error {
1319 return p.executePlain("repo/fragments/editLabelPanel", w, params)
1320}
1321
1322type PipelinesParams struct {
1323 LoggedInUser *oauth.User
1324 RepoInfo repoinfo.RepoInfo
1325 Pipelines []models.Pipeline
1326 Active string
1327}
1328
1329func (p *Pages) Pipelines(w io.Writer, params PipelinesParams) error {
1330 params.Active = "pipelines"
1331 return p.executeRepo("repo/pipelines/pipelines", w, params)
1332}
1333
1334type LogBlockParams struct {
1335 Id int
1336 Name string
1337 Command string
1338 Collapsed bool
1339}
1340
1341func (p *Pages) LogBlock(w io.Writer, params LogBlockParams) error {
1342 return p.executePlain("repo/pipelines/fragments/logBlock", w, params)
1343}
1344
1345type LogLineParams struct {
1346 Id int
1347 Content string
1348}
1349
1350func (p *Pages) LogLine(w io.Writer, params LogLineParams) error {
1351 return p.executePlain("repo/pipelines/fragments/logLine", w, params)
1352}
1353
1354type WorkflowParams struct {
1355 LoggedInUser *oauth.User
1356 RepoInfo repoinfo.RepoInfo
1357 Pipeline models.Pipeline
1358 Workflow string
1359 LogUrl string
1360 Active string
1361}
1362
1363func (p *Pages) Workflow(w io.Writer, params WorkflowParams) error {
1364 params.Active = "pipelines"
1365 return p.executeRepo("repo/pipelines/workflow", w, params)
1366}
1367
1368type PutStringParams struct {
1369 LoggedInUser *oauth.User
1370 Action string
1371
1372 // this is supplied in the case of editing an existing string
1373 String models.String
1374}
1375
1376func (p *Pages) PutString(w io.Writer, params PutStringParams) error {
1377 return p.execute("strings/put", w, params)
1378}
1379
1380type StringsDashboardParams struct {
1381 LoggedInUser *oauth.User
1382 Card ProfileCard
1383 Strings []models.String
1384}
1385
1386func (p *Pages) StringsDashboard(w io.Writer, params StringsDashboardParams) error {
1387 return p.execute("strings/dashboard", w, params)
1388}
1389
1390type StringTimelineParams struct {
1391 LoggedInUser *oauth.User
1392 Strings []models.String
1393}
1394
1395func (p *Pages) StringsTimeline(w io.Writer, params StringTimelineParams) error {
1396 return p.execute("strings/timeline", w, params)
1397}
1398
1399type SingleStringParams struct {
1400 LoggedInUser *oauth.User
1401 ShowRendered bool
1402 RenderToggle bool
1403 RenderedContents template.HTML
1404 String models.String
1405 Stats models.StringStats
1406 Owner identity.Identity
1407}
1408
1409func (p *Pages) SingleString(w io.Writer, params SingleStringParams) error {
1410 var style *chroma.Style = styles.Get("catpuccin-latte")
1411
1412 if params.ShowRendered {
1413 switch markup.GetFormat(params.String.Filename) {
1414 case markup.FormatMarkdown:
1415 p.rctx.RendererType = markup.RendererTypeRepoMarkdown
1416 htmlString := p.rctx.RenderMarkdown(params.String.Contents)
1417 sanitized := p.rctx.SanitizeDefault(htmlString)
1418 params.RenderedContents = template.HTML(sanitized)
1419 }
1420 }
1421
1422 c := params.String.Contents
1423 formatter := chromahtml.New(
1424 chromahtml.InlineCode(false),
1425 chromahtml.WithLineNumbers(true),
1426 chromahtml.WithLinkableLineNumbers(true, "L"),
1427 chromahtml.Standalone(false),
1428 chromahtml.WithClasses(true),
1429 )
1430
1431 lexer := lexers.Get(filepath.Base(params.String.Filename))
1432 if lexer == nil {
1433 lexer = lexers.Fallback
1434 }
1435
1436 iterator, err := lexer.Tokenise(nil, c)
1437 if err != nil {
1438 return fmt.Errorf("chroma tokenize: %w", err)
1439 }
1440
1441 var code bytes.Buffer
1442 err = formatter.Format(&code, style, iterator)
1443 if err != nil {
1444 return fmt.Errorf("chroma format: %w", err)
1445 }
1446
1447 params.String.Contents = code.String()
1448 return p.execute("strings/string", w, params)
1449}
1450
1451func (p *Pages) Home(w io.Writer, params TimelineParams) error {
1452 return p.execute("timeline/home", w, params)
1453}
1454
1455func (p *Pages) Static() http.Handler {
1456 if p.dev {
1457 return http.StripPrefix("/static/", http.FileServer(http.Dir("appview/pages/static")))
1458 }
1459
1460 sub, err := fs.Sub(Files, "static")
1461 if err != nil {
1462 p.logger.Error("no static dir found? that's crazy", "err", err)
1463 panic(err)
1464 }
1465 // Custom handler to apply Cache-Control headers for font files
1466 return Cache(http.StripPrefix("/static/", http.FileServer(http.FS(sub))))
1467}
1468
1469func Cache(h http.Handler) http.Handler {
1470 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1471 path := strings.Split(r.URL.Path, "?")[0]
1472
1473 if strings.HasSuffix(path, ".css") {
1474 // on day for css files
1475 w.Header().Set("Cache-Control", "public, max-age=86400")
1476 } else {
1477 w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
1478 }
1479 h.ServeHTTP(w, r)
1480 })
1481}
1482
1483func CssContentHash() string {
1484 cssFile, err := Files.Open("static/tw.css")
1485 if err != nil {
1486 slog.Debug("Error opening CSS file", "err", err)
1487 return ""
1488 }
1489 defer cssFile.Close()
1490
1491 hasher := sha256.New()
1492 if _, err := io.Copy(hasher, cssFile); err != nil {
1493 slog.Debug("Error hashing CSS file", "err", err)
1494 return ""
1495 }
1496
1497 return hex.EncodeToString(hasher.Sum(nil))[:8] // Use first 8 chars of hash
1498}
1499
1500func (p *Pages) Error500(w io.Writer) error {
1501 return p.execute("errors/500", w, nil)
1502}
1503
1504func (p *Pages) Error404(w io.Writer) error {
1505 return p.execute("errors/404", w, nil)
1506}
1507
1508func (p *Pages) ErrorKnot404(w io.Writer) error {
1509 return p.execute("errors/knot404", w, nil)
1510}
1511
1512func (p *Pages) Error503(w io.Writer) error {
1513 return p.execute("errors/503", w, nil)
1514}