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