Monorepo for Tangled
at e11751cd3366e288734165632af69f7bd32c96dd 1520 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 HasProfile bool 527 FollowStatus models.FollowStatus 528 Punchcard *models.Punchcard 529 Profile *models.Profile 530 Stats ProfileStats 531 Active string 532} 533 534type ProfileStats struct { 535 RepoCount int64 536 StarredCount int64 537 StringCount int64 538 FollowersCount int64 539 FollowingCount int64 540} 541 542func (p *ProfileCard) GetTabs() [][]any { 543 tabs := [][]any{ 544 {"overview", "overview", "square-chart-gantt", nil}, 545 {"repos", "repos", "book-marked", p.Stats.RepoCount}, 546 {"starred", "starred", "star", p.Stats.StarredCount}, 547 {"strings", "strings", "line-squiggle", p.Stats.StringCount}, 548 } 549 550 return tabs 551} 552 553type ProfileOverviewParams struct { 554 LoggedInUser *oauth.MultiAccountUser 555 Repos []models.Repo 556 CollaboratingRepos []models.Repo 557 ProfileTimeline *models.ProfileTimeline 558 Card *ProfileCard 559 Active string 560} 561 562func (p *Pages) ProfileOverview(w io.Writer, params ProfileOverviewParams) error { 563 params.Active = "overview" 564 return p.executeProfile("user/overview", w, params) 565} 566 567type ProfileReposParams struct { 568 LoggedInUser *oauth.MultiAccountUser 569 Repos []models.Repo 570 Card *ProfileCard 571 Active string 572} 573 574func (p *Pages) ProfileRepos(w io.Writer, params ProfileReposParams) error { 575 params.Active = "repos" 576 return p.executeProfile("user/repos", w, params) 577} 578 579type ProfileStarredParams struct { 580 LoggedInUser *oauth.MultiAccountUser 581 Repos []models.Repo 582 Card *ProfileCard 583 Active string 584} 585 586func (p *Pages) ProfileStarred(w io.Writer, params ProfileStarredParams) error { 587 params.Active = "starred" 588 return p.executeProfile("user/starred", w, params) 589} 590 591type ProfileStringsParams struct { 592 LoggedInUser *oauth.MultiAccountUser 593 Strings []models.String 594 Card *ProfileCard 595 Active string 596} 597 598func (p *Pages) ProfileStrings(w io.Writer, params ProfileStringsParams) error { 599 params.Active = "strings" 600 return p.executeProfile("user/strings", w, params) 601} 602 603type FollowCard struct { 604 UserDid string 605 LoggedInUser *oauth.MultiAccountUser 606 FollowStatus models.FollowStatus 607 FollowersCount int64 608 FollowingCount int64 609 Profile *models.Profile 610} 611 612type ProfileFollowersParams struct { 613 LoggedInUser *oauth.MultiAccountUser 614 Followers []FollowCard 615 Card *ProfileCard 616 Active string 617} 618 619func (p *Pages) ProfileFollowers(w io.Writer, params ProfileFollowersParams) error { 620 params.Active = "overview" 621 return p.executeProfile("user/followers", w, params) 622} 623 624type ProfileFollowingParams struct { 625 LoggedInUser *oauth.MultiAccountUser 626 Following []FollowCard 627 Card *ProfileCard 628 Active string 629} 630 631func (p *Pages) ProfileFollowing(w io.Writer, params ProfileFollowingParams) error { 632 params.Active = "overview" 633 return p.executeProfile("user/following", w, params) 634} 635 636type FollowFragmentParams struct { 637 UserDid string 638 FollowStatus models.FollowStatus 639 FollowersCount int64 640} 641 642func (p *Pages) FollowFragment(w io.Writer, params FollowFragmentParams) error { 643 return p.executePlain("user/fragments/follow-oob", w, params) 644} 645 646type EditBioParams struct { 647 LoggedInUser *oauth.MultiAccountUser 648 Profile *models.Profile 649} 650 651func (p *Pages) EditBioFragment(w io.Writer, params EditBioParams) error { 652 return p.executePlain("user/fragments/editBio", w, params) 653} 654 655type EditPinsParams struct { 656 LoggedInUser *oauth.MultiAccountUser 657 Profile *models.Profile 658 AllRepos []PinnedRepo 659} 660 661type PinnedRepo struct { 662 IsPinned bool 663 models.Repo 664} 665 666func (p *Pages) EditPinsFragment(w io.Writer, params EditPinsParams) error { 667 return p.executePlain("user/fragments/editPins", w, params) 668} 669 670type StarBtnFragmentParams struct { 671 IsStarred bool 672 SubjectAt syntax.ATURI 673 StarCount int 674 HxSwapOob bool 675} 676 677func (p *Pages) StarBtnFragment(w io.Writer, params StarBtnFragmentParams) error { 678 params.HxSwapOob = true 679 return p.executePlain("fragments/starBtn", w, params) 680} 681 682type RepoIndexParams struct { 683 LoggedInUser *oauth.MultiAccountUser 684 RepoInfo repoinfo.RepoInfo 685 Active string 686 TagMap map[string][]string 687 CommitsTrunc []types.Commit 688 TagsTrunc []*types.TagReference 689 BranchesTrunc []types.Branch 690 // ForkInfo *types.ForkInfo 691 HTMLReadme template.HTML 692 Raw bool 693 EmailToDid map[string]string 694 VerifiedCommits commitverify.VerifiedCommits 695 Languages []types.RepoLanguageDetails 696 Pipelines map[string]models.Pipeline 697 NeedsKnotUpgrade bool 698 types.RepoIndexResponse 699} 700 701func (p *Pages) RepoIndexPage(w io.Writer, params RepoIndexParams) error { 702 params.Active = "overview" 703 if params.IsEmpty { 704 return p.executeRepo("repo/empty", w, params) 705 } 706 707 if params.NeedsKnotUpgrade { 708 return p.executeRepo("repo/needsUpgrade", w, params) 709 } 710 711 p.rctx.RepoInfo = params.RepoInfo 712 p.rctx.RepoInfo.Ref = params.Ref 713 p.rctx.RendererType = markup.RendererTypeRepoMarkdown 714 715 if params.ReadmeFileName != "" { 716 ext := filepath.Ext(params.ReadmeFileName) 717 switch ext { 718 case ".md", ".markdown", ".mdown", ".mkdn", ".mkd": 719 params.Raw = false 720 htmlString := p.rctx.RenderMarkdown(params.Readme) 721 sanitized := p.rctx.SanitizeDefault(htmlString) 722 params.HTMLReadme = template.HTML(sanitized) 723 default: 724 params.Raw = true 725 } 726 } 727 728 return p.executeRepo("repo/index", w, params) 729} 730 731type RepoLogParams struct { 732 LoggedInUser *oauth.MultiAccountUser 733 RepoInfo repoinfo.RepoInfo 734 TagMap map[string][]string 735 Active string 736 EmailToDid map[string]string 737 VerifiedCommits commitverify.VerifiedCommits 738 Pipelines map[string]models.Pipeline 739 740 types.RepoLogResponse 741} 742 743func (p *Pages) RepoLog(w io.Writer, params RepoLogParams) error { 744 params.Active = "overview" 745 return p.executeRepo("repo/log", w, params) 746} 747 748type RepoCommitParams struct { 749 LoggedInUser *oauth.MultiAccountUser 750 RepoInfo repoinfo.RepoInfo 751 Active string 752 EmailToDid map[string]string 753 Pipeline *models.Pipeline 754 DiffOpts types.DiffOpts 755 756 // singular because it's always going to be just one 757 VerifiedCommit commitverify.VerifiedCommits 758 759 types.RepoCommitResponse 760} 761 762func (p *Pages) RepoCommit(w io.Writer, params RepoCommitParams) error { 763 params.Active = "overview" 764 return p.executeRepo("repo/commit", w, params) 765} 766 767type RepoTreeParams struct { 768 LoggedInUser *oauth.MultiAccountUser 769 RepoInfo repoinfo.RepoInfo 770 Active string 771 BreadCrumbs [][]string 772 TreePath string 773 Raw bool 774 HTMLReadme template.HTML 775 EmailToDid map[string]string 776 LastCommitInfo *types.LastCommitInfo 777 types.RepoTreeResponse 778} 779 780type RepoTreeStats struct { 781 NumFolders uint64 782 NumFiles uint64 783} 784 785func (r RepoTreeParams) TreeStats() RepoTreeStats { 786 numFolders, numFiles := 0, 0 787 for _, f := range r.Files { 788 if !f.IsFile() { 789 numFolders += 1 790 } else if f.IsFile() { 791 numFiles += 1 792 } 793 } 794 795 return RepoTreeStats{ 796 NumFolders: uint64(numFolders), 797 NumFiles: uint64(numFiles), 798 } 799} 800 801func (p *Pages) RepoTree(w io.Writer, params RepoTreeParams) error { 802 params.Active = "overview" 803 804 p.rctx.RepoInfo = params.RepoInfo 805 p.rctx.RepoInfo.Ref = params.Ref 806 p.rctx.RendererType = markup.RendererTypeRepoMarkdown 807 808 if params.ReadmeFileName != "" { 809 ext := filepath.Ext(params.ReadmeFileName) 810 switch ext { 811 case ".md", ".markdown", ".mdown", ".mkdn", ".mkd": 812 params.Raw = false 813 htmlString := p.rctx.RenderMarkdown(params.Readme) 814 sanitized := p.rctx.SanitizeDefault(htmlString) 815 params.HTMLReadme = template.HTML(sanitized) 816 default: 817 params.Raw = true 818 } 819 } 820 821 return p.executeRepo("repo/tree", w, params) 822} 823 824type RepoBranchesParams struct { 825 LoggedInUser *oauth.MultiAccountUser 826 RepoInfo repoinfo.RepoInfo 827 Active string 828 types.RepoBranchesResponse 829} 830 831func (p *Pages) RepoBranches(w io.Writer, params RepoBranchesParams) error { 832 params.Active = "overview" 833 return p.executeRepo("repo/branches", w, params) 834} 835 836type RepoTagsParams struct { 837 LoggedInUser *oauth.MultiAccountUser 838 RepoInfo repoinfo.RepoInfo 839 Active string 840 types.RepoTagsResponse 841 ArtifactMap map[plumbing.Hash][]models.Artifact 842 DanglingArtifacts []models.Artifact 843} 844 845func (p *Pages) RepoTags(w io.Writer, params RepoTagsParams) error { 846 params.Active = "overview" 847 return p.executeRepo("repo/tags", w, params) 848} 849 850type RepoTagParams struct { 851 LoggedInUser *oauth.MultiAccountUser 852 RepoInfo repoinfo.RepoInfo 853 Active string 854 types.RepoTagResponse 855 ArtifactMap map[plumbing.Hash][]models.Artifact 856 DanglingArtifacts []models.Artifact 857} 858 859func (p *Pages) RepoTag(w io.Writer, params RepoTagParams) error { 860 params.Active = "overview" 861 return p.executeRepo("repo/tag", w, params) 862} 863 864type RepoArtifactParams struct { 865 LoggedInUser *oauth.MultiAccountUser 866 RepoInfo repoinfo.RepoInfo 867 Artifact models.Artifact 868} 869 870func (p *Pages) RepoArtifactFragment(w io.Writer, params RepoArtifactParams) error { 871 return p.executePlain("repo/fragments/artifact", w, params) 872} 873 874type RepoBlobParams struct { 875 LoggedInUser *oauth.MultiAccountUser 876 RepoInfo repoinfo.RepoInfo 877 Active string 878 BreadCrumbs [][]string 879 BlobView models.BlobView 880 EmailToDid map[string]string 881 LastCommitInfo *types.LastCommitInfo 882 *tangled.RepoBlob_Output 883} 884 885func (p *Pages) RepoBlob(w io.Writer, params RepoBlobParams) error { 886 switch params.BlobView.ContentType { 887 case models.BlobContentTypeMarkup: 888 p.rctx.RepoInfo = params.RepoInfo 889 } 890 891 params.Active = "overview" 892 return p.executeRepo("repo/blob", w, params) 893} 894 895type Collaborator struct { 896 Did string 897 Role string 898} 899 900type RepoSettingsParams struct { 901 LoggedInUser *oauth.MultiAccountUser 902 RepoInfo repoinfo.RepoInfo 903 Collaborators []Collaborator 904 Active string 905 Branches []types.Branch 906 Spindles []string 907 CurrentSpindle string 908 Secrets []*tangled.RepoListSecrets_Secret 909 910 // TODO: use repoinfo.roles 911 IsCollaboratorInviteAllowed bool 912} 913 914func (p *Pages) RepoSettings(w io.Writer, params RepoSettingsParams) error { 915 params.Active = "settings" 916 return p.executeRepo("repo/settings", w, params) 917} 918 919type RepoGeneralSettingsParams struct { 920 LoggedInUser *oauth.MultiAccountUser 921 RepoInfo repoinfo.RepoInfo 922 Labels []models.LabelDefinition 923 DefaultLabels []models.LabelDefinition 924 SubscribedLabels map[string]struct{} 925 ShouldSubscribeAll bool 926 Active string 927 Tab string 928 Branches []types.Branch 929} 930 931func (p *Pages) RepoGeneralSettings(w io.Writer, params RepoGeneralSettingsParams) error { 932 params.Active = "settings" 933 params.Tab = "general" 934 return p.executeRepo("repo/settings/general", w, params) 935} 936 937type RepoAccessSettingsParams struct { 938 LoggedInUser *oauth.MultiAccountUser 939 RepoInfo repoinfo.RepoInfo 940 Active string 941 Tab string 942 Collaborators []Collaborator 943} 944 945func (p *Pages) RepoAccessSettings(w io.Writer, params RepoAccessSettingsParams) error { 946 params.Active = "settings" 947 params.Tab = "access" 948 return p.executeRepo("repo/settings/access", w, params) 949} 950 951type RepoPipelineSettingsParams struct { 952 LoggedInUser *oauth.MultiAccountUser 953 RepoInfo repoinfo.RepoInfo 954 Active string 955 Tab string 956 Spindles []string 957 CurrentSpindle string 958 Secrets []map[string]any 959} 960 961func (p *Pages) RepoPipelineSettings(w io.Writer, params RepoPipelineSettingsParams) error { 962 params.Active = "settings" 963 params.Tab = "pipelines" 964 return p.executeRepo("repo/settings/pipelines", w, params) 965} 966 967type RepoIssuesParams struct { 968 LoggedInUser *oauth.MultiAccountUser 969 RepoInfo repoinfo.RepoInfo 970 Active string 971 Issues []models.Issue 972 IssueCount int 973 LabelDefs map[string]*models.LabelDefinition 974 Page pagination.Page 975 FilteringByOpen bool 976 FilterQuery string 977} 978 979func (p *Pages) RepoIssues(w io.Writer, params RepoIssuesParams) error { 980 params.Active = "issues" 981 return p.executeRepo("repo/issues/issues", w, params) 982} 983 984type RepoSingleIssueParams struct { 985 LoggedInUser *oauth.MultiAccountUser 986 RepoInfo repoinfo.RepoInfo 987 Active string 988 Issue *models.Issue 989 CommentList []models.CommentListItem 990 Backlinks []models.RichReferenceLink 991 LabelDefs map[string]*models.LabelDefinition 992 993 Reactions map[models.ReactionKind]models.ReactionDisplayData 994 UserReacted map[models.ReactionKind]bool 995} 996 997func (p *Pages) RepoSingleIssue(w io.Writer, params RepoSingleIssueParams) error { 998 params.Active = "issues" 999 return p.executeRepo("repo/issues/issue", w, params) 1000} 1001 1002type EditIssueParams struct { 1003 LoggedInUser *oauth.MultiAccountUser 1004 RepoInfo repoinfo.RepoInfo 1005 Issue *models.Issue 1006 Action string 1007} 1008 1009func (p *Pages) EditIssueFragment(w io.Writer, params EditIssueParams) error { 1010 params.Action = "edit" 1011 return p.executePlain("repo/issues/fragments/putIssue", w, params) 1012} 1013 1014type ThreadReactionFragmentParams struct { 1015 ThreadAt syntax.ATURI 1016 Kind models.ReactionKind 1017 Count int 1018 Users []string 1019 IsReacted bool 1020} 1021 1022func (p *Pages) ThreadReactionFragment(w io.Writer, params ThreadReactionFragmentParams) error { 1023 return p.executePlain("repo/fragments/reaction", w, params) 1024} 1025 1026type RepoNewIssueParams struct { 1027 LoggedInUser *oauth.MultiAccountUser 1028 RepoInfo repoinfo.RepoInfo 1029 Issue *models.Issue // existing issue if any -- passed when editing 1030 Active string 1031 Action string 1032} 1033 1034func (p *Pages) RepoNewIssue(w io.Writer, params RepoNewIssueParams) error { 1035 params.Active = "issues" 1036 params.Action = "create" 1037 return p.executeRepo("repo/issues/new", w, params) 1038} 1039 1040type EditIssueCommentParams struct { 1041 LoggedInUser *oauth.MultiAccountUser 1042 RepoInfo repoinfo.RepoInfo 1043 Issue *models.Issue 1044 Comment *models.IssueComment 1045} 1046 1047func (p *Pages) EditIssueCommentFragment(w io.Writer, params EditIssueCommentParams) error { 1048 return p.executePlain("repo/issues/fragments/editIssueComment", w, params) 1049} 1050 1051type ReplyIssueCommentPlaceholderParams struct { 1052 LoggedInUser *oauth.MultiAccountUser 1053 RepoInfo repoinfo.RepoInfo 1054 Issue *models.Issue 1055 Comment *models.IssueComment 1056} 1057 1058func (p *Pages) ReplyIssueCommentPlaceholderFragment(w io.Writer, params ReplyIssueCommentPlaceholderParams) error { 1059 return p.executePlain("repo/issues/fragments/replyIssueCommentPlaceholder", w, params) 1060} 1061 1062type ReplyIssueCommentParams struct { 1063 LoggedInUser *oauth.MultiAccountUser 1064 RepoInfo repoinfo.RepoInfo 1065 Issue *models.Issue 1066 Comment *models.IssueComment 1067} 1068 1069func (p *Pages) ReplyIssueCommentFragment(w io.Writer, params ReplyIssueCommentParams) error { 1070 return p.executePlain("repo/issues/fragments/replyComment", w, params) 1071} 1072 1073type IssueCommentBodyParams struct { 1074 LoggedInUser *oauth.MultiAccountUser 1075 RepoInfo repoinfo.RepoInfo 1076 Issue *models.Issue 1077 Comment *models.IssueComment 1078} 1079 1080func (p *Pages) IssueCommentBodyFragment(w io.Writer, params IssueCommentBodyParams) error { 1081 return p.executePlain("repo/issues/fragments/issueCommentBody", w, params) 1082} 1083 1084type RepoNewPullParams struct { 1085 LoggedInUser *oauth.MultiAccountUser 1086 RepoInfo repoinfo.RepoInfo 1087 Branches []types.Branch 1088 Strategy string 1089 SourceBranch string 1090 TargetBranch string 1091 Title string 1092 Body string 1093 Active string 1094} 1095 1096func (p *Pages) RepoNewPull(w io.Writer, params RepoNewPullParams) error { 1097 params.Active = "pulls" 1098 return p.executeRepo("repo/pulls/new", w, params) 1099} 1100 1101type RepoPullsParams struct { 1102 LoggedInUser *oauth.MultiAccountUser 1103 RepoInfo repoinfo.RepoInfo 1104 Pulls []*models.Pull 1105 Active string 1106 FilteringBy models.PullState 1107 FilterQuery string 1108 Stacks map[string]models.Stack 1109 Pipelines map[string]models.Pipeline 1110 LabelDefs map[string]*models.LabelDefinition 1111 Page pagination.Page 1112 PullCount int 1113} 1114 1115func (p *Pages) RepoPulls(w io.Writer, params RepoPullsParams) error { 1116 params.Active = "pulls" 1117 return p.executeRepo("repo/pulls/pulls", w, params) 1118} 1119 1120type ResubmitResult uint64 1121 1122const ( 1123 ShouldResubmit ResubmitResult = iota 1124 ShouldNotResubmit 1125 Unknown 1126) 1127 1128func (r ResubmitResult) Yes() bool { 1129 return r == ShouldResubmit 1130} 1131func (r ResubmitResult) No() bool { 1132 return r == ShouldNotResubmit 1133} 1134func (r ResubmitResult) Unknown() bool { 1135 return r == Unknown 1136} 1137 1138type RepoSinglePullParams struct { 1139 LoggedInUser *oauth.MultiAccountUser 1140 RepoInfo repoinfo.RepoInfo 1141 Active string 1142 Pull *models.Pull 1143 Stack models.Stack 1144 AbandonedPulls []*models.Pull 1145 Backlinks []models.RichReferenceLink 1146 BranchDeleteStatus *models.BranchDeleteStatus 1147 MergeCheck types.MergeCheckResponse 1148 ResubmitCheck ResubmitResult 1149 Pipelines map[string]models.Pipeline 1150 Diff types.DiffRenderer 1151 DiffOpts types.DiffOpts 1152 ActiveRound int 1153 IsInterdiff bool 1154 1155 Reactions map[models.ReactionKind]models.ReactionDisplayData 1156 UserReacted map[models.ReactionKind]bool 1157 1158 LabelDefs map[string]*models.LabelDefinition 1159} 1160 1161func (p *Pages) RepoSinglePull(w io.Writer, params RepoSinglePullParams) error { 1162 params.Active = "pulls" 1163 return p.executeRepo("repo/pulls/pull", w, params) 1164} 1165 1166type RepoPullPatchParams struct { 1167 LoggedInUser *oauth.MultiAccountUser 1168 RepoInfo repoinfo.RepoInfo 1169 Pull *models.Pull 1170 Stack models.Stack 1171 Diff *types.NiceDiff 1172 Round int 1173 Submission *models.PullSubmission 1174 DiffOpts types.DiffOpts 1175} 1176 1177// this name is a mouthful 1178func (p *Pages) RepoPullPatchPage(w io.Writer, params RepoPullPatchParams) error { 1179 return p.execute("repo/pulls/patch", w, params) 1180} 1181 1182type RepoPullInterdiffParams struct { 1183 LoggedInUser *oauth.MultiAccountUser 1184 RepoInfo repoinfo.RepoInfo 1185 Pull *models.Pull 1186 Round int 1187 Interdiff *patchutil.InterdiffResult 1188 DiffOpts types.DiffOpts 1189} 1190 1191// this name is a mouthful 1192func (p *Pages) RepoPullInterdiffPage(w io.Writer, params RepoPullInterdiffParams) error { 1193 return p.execute("repo/pulls/interdiff", w, params) 1194} 1195 1196type PullPatchUploadParams struct { 1197 RepoInfo repoinfo.RepoInfo 1198} 1199 1200func (p *Pages) PullPatchUploadFragment(w io.Writer, params PullPatchUploadParams) error { 1201 return p.executePlain("repo/pulls/fragments/pullPatchUpload", w, params) 1202} 1203 1204type PullCompareBranchesParams struct { 1205 RepoInfo repoinfo.RepoInfo 1206 Branches []types.Branch 1207 SourceBranch string 1208} 1209 1210func (p *Pages) PullCompareBranchesFragment(w io.Writer, params PullCompareBranchesParams) error { 1211 return p.executePlain("repo/pulls/fragments/pullCompareBranches", w, params) 1212} 1213 1214type PullCompareForkParams struct { 1215 RepoInfo repoinfo.RepoInfo 1216 Forks []models.Repo 1217 Selected string 1218} 1219 1220func (p *Pages) PullCompareForkFragment(w io.Writer, params PullCompareForkParams) error { 1221 return p.executePlain("repo/pulls/fragments/pullCompareForks", w, params) 1222} 1223 1224type PullCompareForkBranchesParams struct { 1225 RepoInfo repoinfo.RepoInfo 1226 SourceBranches []types.Branch 1227 TargetBranches []types.Branch 1228} 1229 1230func (p *Pages) PullCompareForkBranchesFragment(w io.Writer, params PullCompareForkBranchesParams) error { 1231 return p.executePlain("repo/pulls/fragments/pullCompareForksBranches", w, params) 1232} 1233 1234type PullResubmitParams struct { 1235 LoggedInUser *oauth.MultiAccountUser 1236 RepoInfo repoinfo.RepoInfo 1237 Pull *models.Pull 1238 SubmissionId int 1239} 1240 1241func (p *Pages) PullResubmitFragment(w io.Writer, params PullResubmitParams) error { 1242 return p.executePlain("repo/pulls/fragments/pullResubmit", w, params) 1243} 1244 1245type PullActionsParams struct { 1246 LoggedInUser *oauth.MultiAccountUser 1247 RepoInfo repoinfo.RepoInfo 1248 Pull *models.Pull 1249 RoundNumber int 1250 MergeCheck types.MergeCheckResponse 1251 ResubmitCheck ResubmitResult 1252 BranchDeleteStatus *models.BranchDeleteStatus 1253 Stack models.Stack 1254} 1255 1256func (p *Pages) PullActionsFragment(w io.Writer, params PullActionsParams) error { 1257 return p.executePlain("repo/pulls/fragments/pullActions", w, params) 1258} 1259 1260type PullNewCommentParams struct { 1261 LoggedInUser *oauth.MultiAccountUser 1262 RepoInfo repoinfo.RepoInfo 1263 Pull *models.Pull 1264 RoundNumber int 1265} 1266 1267func (p *Pages) PullNewCommentFragment(w io.Writer, params PullNewCommentParams) error { 1268 return p.executePlain("repo/pulls/fragments/pullNewComment", w, params) 1269} 1270 1271type RepoCompareParams 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 Diff *types.NiceDiff 1280 DiffOpts types.DiffOpts 1281 1282 Active string 1283} 1284 1285func (p *Pages) RepoCompare(w io.Writer, params RepoCompareParams) error { 1286 params.Active = "overview" 1287 return p.executeRepo("repo/compare/compare", w, params) 1288} 1289 1290type RepoCompareNewParams struct { 1291 LoggedInUser *oauth.MultiAccountUser 1292 RepoInfo repoinfo.RepoInfo 1293 Forks []models.Repo 1294 Branches []types.Branch 1295 Tags []*types.TagReference 1296 Base string 1297 Head string 1298 1299 Active string 1300} 1301 1302func (p *Pages) RepoCompareNew(w io.Writer, params RepoCompareNewParams) error { 1303 params.Active = "overview" 1304 return p.executeRepo("repo/compare/new", w, params) 1305} 1306 1307type RepoCompareAllowPullParams struct { 1308 LoggedInUser *oauth.MultiAccountUser 1309 RepoInfo repoinfo.RepoInfo 1310 Base string 1311 Head string 1312} 1313 1314func (p *Pages) RepoCompareAllowPullFragment(w io.Writer, params RepoCompareAllowPullParams) error { 1315 return p.executePlain("repo/fragments/compareAllowPull", w, params) 1316} 1317 1318type RepoCompareDiffFragmentParams struct { 1319 Diff types.NiceDiff 1320 DiffOpts types.DiffOpts 1321} 1322 1323func (p *Pages) RepoCompareDiffFragment(w io.Writer, params RepoCompareDiffFragmentParams) error { 1324 return p.executePlain("repo/fragments/diff", w, []any{&params.Diff, &params.DiffOpts}) 1325} 1326 1327type LabelPanelParams struct { 1328 LoggedInUser *oauth.MultiAccountUser 1329 RepoInfo repoinfo.RepoInfo 1330 Defs map[string]*models.LabelDefinition 1331 Subject string 1332 State models.LabelState 1333} 1334 1335func (p *Pages) LabelPanel(w io.Writer, params LabelPanelParams) error { 1336 return p.executePlain("repo/fragments/labelPanel", w, params) 1337} 1338 1339type EditLabelPanelParams struct { 1340 LoggedInUser *oauth.MultiAccountUser 1341 RepoInfo repoinfo.RepoInfo 1342 Defs map[string]*models.LabelDefinition 1343 Subject string 1344 State models.LabelState 1345} 1346 1347func (p *Pages) EditLabelPanel(w io.Writer, params EditLabelPanelParams) error { 1348 return p.executePlain("repo/fragments/editLabelPanel", w, params) 1349} 1350 1351type PipelinesParams struct { 1352 LoggedInUser *oauth.MultiAccountUser 1353 RepoInfo repoinfo.RepoInfo 1354 Pipelines []models.Pipeline 1355 Active string 1356 FilterKind string 1357 Total int64 1358} 1359 1360func (p *Pages) Pipelines(w io.Writer, params PipelinesParams) error { 1361 params.Active = "pipelines" 1362 return p.executeRepo("repo/pipelines/pipelines", w, params) 1363} 1364 1365type LogBlockParams struct { 1366 Id int 1367 Name string 1368 Command string 1369 Collapsed bool 1370 StartTime time.Time 1371} 1372 1373func (p *Pages) LogBlock(w io.Writer, params LogBlockParams) error { 1374 return p.executePlain("repo/pipelines/fragments/logBlock", w, params) 1375} 1376 1377type LogBlockEndParams struct { 1378 Id int 1379 StartTime time.Time 1380 EndTime time.Time 1381} 1382 1383func (p *Pages) LogBlockEnd(w io.Writer, params LogBlockEndParams) error { 1384 return p.executePlain("repo/pipelines/fragments/logBlockEnd", w, params) 1385} 1386 1387type LogLineParams struct { 1388 Id int 1389 Content string 1390} 1391 1392func (p *Pages) LogLine(w io.Writer, params LogLineParams) error { 1393 return p.executePlain("repo/pipelines/fragments/logLine", w, params) 1394} 1395 1396type WorkflowParams struct { 1397 LoggedInUser *oauth.MultiAccountUser 1398 RepoInfo repoinfo.RepoInfo 1399 Pipeline models.Pipeline 1400 Workflow string 1401 LogUrl string 1402 Active string 1403} 1404 1405func (p *Pages) Workflow(w io.Writer, params WorkflowParams) error { 1406 params.Active = "pipelines" 1407 return p.executeRepo("repo/pipelines/workflow", w, params) 1408} 1409 1410type PutStringParams struct { 1411 LoggedInUser *oauth.MultiAccountUser 1412 Action string 1413 1414 // this is supplied in the case of editing an existing string 1415 String models.String 1416} 1417 1418func (p *Pages) PutString(w io.Writer, params PutStringParams) error { 1419 return p.execute("strings/put", w, params) 1420} 1421 1422type StringsDashboardParams struct { 1423 LoggedInUser *oauth.MultiAccountUser 1424 Card ProfileCard 1425 Strings []models.String 1426} 1427 1428func (p *Pages) StringsDashboard(w io.Writer, params StringsDashboardParams) error { 1429 return p.execute("strings/dashboard", w, params) 1430} 1431 1432type StringTimelineParams struct { 1433 LoggedInUser *oauth.MultiAccountUser 1434 Strings []models.String 1435} 1436 1437func (p *Pages) StringsTimeline(w io.Writer, params StringTimelineParams) error { 1438 return p.execute("strings/timeline", w, params) 1439} 1440 1441type SingleStringParams struct { 1442 LoggedInUser *oauth.MultiAccountUser 1443 ShowRendered bool 1444 RenderToggle bool 1445 RenderedContents template.HTML 1446 String *models.String 1447 Stats models.StringStats 1448 IsStarred bool 1449 StarCount int 1450 Owner identity.Identity 1451} 1452 1453func (p *Pages) SingleString(w io.Writer, params SingleStringParams) error { 1454 return p.execute("strings/string", w, params) 1455} 1456 1457func (p *Pages) Home(w io.Writer, params TimelineParams) error { 1458 return p.execute("timeline/home", w, params) 1459} 1460 1461func (p *Pages) Static() http.Handler { 1462 if p.dev { 1463 return http.StripPrefix("/static/", http.FileServer(http.Dir("appview/pages/static"))) 1464 } 1465 1466 sub, err := fs.Sub(p.embedFS, "static") 1467 if err != nil { 1468 p.logger.Error("no static dir found? that's crazy", "err", err) 1469 panic(err) 1470 } 1471 // Custom handler to apply Cache-Control headers for font files 1472 return Cache(http.StripPrefix("/static/", http.FileServer(http.FS(sub)))) 1473} 1474 1475func Cache(h http.Handler) http.Handler { 1476 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 1477 path := strings.Split(r.URL.Path, "?")[0] 1478 1479 if strings.HasSuffix(path, ".css") { 1480 // on day for css files 1481 w.Header().Set("Cache-Control", "public, max-age=86400") 1482 } else { 1483 w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") 1484 } 1485 h.ServeHTTP(w, r) 1486 }) 1487} 1488 1489func (p *Pages) CssContentHash() string { 1490 cssFile, err := p.embedFS.Open("static/tw.css") 1491 if err != nil { 1492 slog.Debug("Error opening CSS file", "err", err) 1493 return "" 1494 } 1495 defer cssFile.Close() 1496 1497 hasher := sha256.New() 1498 if _, err := io.Copy(hasher, cssFile); err != nil { 1499 slog.Debug("Error hashing CSS file", "err", err) 1500 return "" 1501 } 1502 1503 return hex.EncodeToString(hasher.Sum(nil))[:8] // Use first 8 chars of hash 1504} 1505 1506func (p *Pages) Error500(w io.Writer) error { 1507 return p.execute("errors/500", w, nil) 1508} 1509 1510func (p *Pages) Error404(w io.Writer) error { 1511 return p.execute("errors/404", w, nil) 1512} 1513 1514func (p *Pages) ErrorKnot404(w io.Writer) error { 1515 return p.execute("errors/knot404", w, nil) 1516} 1517 1518func (p *Pages) Error503(w io.Writer) error { 1519 return p.execute("errors/503", w, nil) 1520}