From 784bedd1b8967aa5ac3f4c660a037f196604af74 Mon Sep 17 00:00:00 2001 From: Ben Chang Date: Mon, 16 Dec 2024 18:05:02 +0800 Subject: [PATCH 01/22] Use view as to get public and private profile repo --- routers/web/org/home.go | 31 ++++++++++++++++++++--------- routers/web/shared/user/header.go | 20 +++++++++++++------ routers/web/user/profile.go | 6 +++--- templates/org/home.tmpl | 10 ++++++++-- templates/org/menu.tmpl | 4 ++-- templates/user/overview/header.tmpl | 2 +- templates/user/profile.tmpl | 2 +- 7 files changed, 51 insertions(+), 24 deletions(-) diff --git a/routers/web/org/home.go b/routers/web/org/home.go index bdc43acc300db..401b1405e4fe8 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -111,7 +111,22 @@ func home(ctx *context.Context, viewRepositories bool) { ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0 - if !prepareOrgProfileReadme(ctx, viewRepositories) { + currentURL := ctx.Req.URL + queryParams := currentURL.Query() + queryParams.Set("view_as", "member") + ctx.Data["QueryForMember"] = queryParams.Encode() + queryParams.Set("view_as", "public") + ctx.Data["QueryForPublic"] = queryParams.Encode() + + isViewerMember := ctx.FormString("view_as") == "member" + ctx.Data["IsViewerMember"] = isViewerMember + + profileType := "Public" + if isViewerMember { + profileType = "Private" + } + + if !prepareOrgProfileReadme(ctx, viewRepositories, profileType) { ctx.Data["PageIsViewRepositories"] = true } @@ -168,28 +183,26 @@ func home(ctx *context.Context, viewRepositories bool) { ctx.HTML(http.StatusOK, tplOrgHome) } -func prepareOrgProfileReadme(ctx *context.Context, viewRepositories bool) bool { - profileDbRepo, profileGitRepo, profileReadme, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer) +func prepareOrgProfileReadme(ctx *context.Context, viewRepositories bool, profileType string) bool { + profileDbRepo, profileGitRepo, profileReadme, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer, profileType) defer profileClose() - ctx.Data["HasProfileReadme"] = profileReadme != nil + ctx.Data[fmt.Sprintf("Has%sProfileReadme", profileType)] = profileReadme != nil if profileGitRepo == nil || profileReadme == nil || viewRepositories { return false } if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil { - log.Error("failed to GetBlobContent: %v", err) + log.Error("failed to GetBlobContent for %s profile readme: %v", profileType, err) } else { rctx := renderhelper.NewRenderContextRepoFile(ctx, profileDbRepo, renderhelper.RepoFileOptions{ CurrentRefPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)), }) if profileContent, err := markdown.RenderString(rctx, bytes); err != nil { - log.Error("failed to RenderString: %v", err) + log.Error("failed to RenderString for %s profile readme: %v", profileType, err) } else { - ctx.Data["ProfileReadme"] = profileContent + ctx.Data[fmt.Sprintf("%sProfileReadme", profileType)] = profileContent } } - - ctx.Data["PageIsViewOverview"] = true return true } diff --git a/routers/web/shared/user/header.go b/routers/web/shared/user/header.go index d388d2b5d9a6c..9bbbefa108d6a 100644 --- a/routers/web/shared/user/header.go +++ b/routers/web/shared/user/header.go @@ -102,8 +102,12 @@ func PrepareContextForProfileBigAvatar(ctx *context.Context) { } } -func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) { - profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, ".profile") +func FindUserProfileReadme(ctx *context.Context, doer *user_model.User, profileType string) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) { + profileName := ".profile" + if profileType != "Public" { + profileName = ".profile-private" + } + profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, profileName) if err == nil { perm, err := access_model.GetUserRepoPermission(ctx, profileDbRepo, doer) if err == nil && !profileDbRepo.IsEmpty && perm.CanRead(unit.TypeCode) { @@ -130,9 +134,9 @@ func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profile func RenderUserHeader(ctx *context.Context) { prepareContextForCommonProfile(ctx) - _, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer) + _, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer, "Public") defer profileClose() - ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil + ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil } func LoadHeaderCount(ctx *context.Context) error { @@ -174,9 +178,13 @@ func RenderOrgHeader(ctx *context.Context) error { return err } - _, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer) + _, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer, "Public") + defer profileClose() + ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil + + _, _, profileReadmeBlob, profileClose = FindUserProfileReadme(ctx, ctx.Doer, "Private") defer profileClose() - ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil + ctx.Data["HasPrivateProfileReadme"] = profileReadmeBlob != nil return nil } diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 9c014bffdb79b..9fea259001a6c 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -74,7 +74,7 @@ func userProfile(ctx *context.Context) { ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data) } - profileDbRepo, _ /*profileGitRepo*/, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer) + profileDbRepo, _ /*profileGitRepo*/, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer, "Public") defer profileClose() showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID) @@ -96,7 +96,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb } } ctx.Data["TabName"] = tab - ctx.Data["HasProfileReadme"] = profileReadme != nil + ctx.Data["HasPublicProfileReadme"] = profileReadme != nil page := ctx.FormInt("page") if page <= 0 { @@ -254,7 +254,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb if profileContent, err := markdown.RenderString(rctx, bytes); err != nil { log.Error("failed to RenderString: %v", err) } else { - ctx.Data["ProfileReadme"] = profileContent + ctx.Data["PublicProfileReadme"] = profileContent } } case "organizations": diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index 4851b6997967b..e0996de90dfcb 100644 --- a/templates/org/home.tmpl +++ b/templates/org/home.tmpl @@ -5,8 +5,14 @@
- {{if .ProfileReadme}} -
{{.ProfileReadme}}
+ {{if .IsViewerMember}} + {{if .PrivateProfileReadme}} +
{{.PrivateProfileReadme}}
+ {{end}} + {{else}} + {{if .PublicProfileReadme}} +
{{.PublicProfileReadme}}
+ {{end}} {{end}} {{template "shared/repo_search" .}} {{template "explore/repo_list" .}} diff --git a/templates/org/menu.tmpl b/templates/org/menu.tmpl index 29238f8d6bb9a..a9b925ed7826c 100644 --- a/templates/org/menu.tmpl +++ b/templates/org/menu.tmpl @@ -1,12 +1,12 @@
- {{if .HasProfileReadme}} + {{if or .HasPublicProfileReadme .HasPrivateProfileReadme}} {{svg "octicon-info"}} {{ctx.Locale.Tr "user.overview"}} {{end}} - + {{svg "octicon-repo"}} {{ctx.Locale.Tr "user.repositories"}} {{if .RepoCount}}
{{.RepoCount}}
diff --git a/templates/user/overview/header.tmpl b/templates/user/overview/header.tmpl index 275c4e295e447..ee28f32ed156d 100644 --- a/templates/user/overview/header.tmpl +++ b/templates/user/overview/header.tmpl @@ -1,6 +1,6 @@
- {{if and .HasProfileReadme .ContextUser.IsIndividual}} + {{if and .HasPublicProfileReadme .ContextUser.IsIndividual}} {{svg "octicon-info"}} {{ctx.Locale.Tr "user.overview"}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index 2c83ce97cd3df..cfc598eb50fef 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -26,7 +26,7 @@ {{else if eq .TabName "followers"}} {{template "repo/user_cards" .}} {{else if eq .TabName "overview"}} -
{{.ProfileReadme}}
+
{{.PublicProfileReadme}}
{{else if eq .TabName "organizations"}} {{template "repo/user_cards" .}} {{else}} From 3682adb01c6af0ea888301f5375a1403c1ddf5f5 Mon Sep 17 00:00:00 2001 From: Ben Chang Date: Tue, 17 Dec 2024 09:54:20 +0800 Subject: [PATCH 02/22] make query string right --- routers/web/org/home.go | 7 ++++--- templates/org/home.tmpl | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 401b1405e4fe8..b250e1214b52f 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -8,6 +8,7 @@ import ( "net/http" "path" "strings" + html_template "html/template" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/organization" @@ -112,11 +113,11 @@ func home(ctx *context.Context, viewRepositories bool) { ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0 currentURL := ctx.Req.URL - queryParams := currentURL.Query() + queryParams := currentURL.Query() queryParams.Set("view_as", "member") - ctx.Data["QueryForMember"] = queryParams.Encode() + ctx.Data["QueryForMember"] = html_template.URL(queryParams.Encode()) queryParams.Set("view_as", "public") - ctx.Data["QueryForPublic"] = queryParams.Encode() + ctx.Data["QueryForPublic"] = html_template.URL(queryParams.Encode()) isViewerMember := ctx.FormString("view_as") == "member" ctx.Data["IsViewerMember"] = isViewerMember diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index e0996de90dfcb..a8e5d4ebd25ea 100644 --- a/templates/org/home.tmpl +++ b/templates/org/home.tmpl @@ -5,6 +5,14 @@
+ {{if .IsViewerMember}} {{if .PrivateProfileReadme}}
{{.PrivateProfileReadme}}
From ef826a1d0cf59362e475a21f37e140bccfa37e6e Mon Sep 17 00:00:00 2001 From: Ben Chang Date: Tue, 17 Dec 2024 05:46:13 +0000 Subject: [PATCH 03/22] make sure the drop down appears only when criteria are met --- routers/web/org/home.go | 8 +++++++- templates/org/home.tmpl | 6 ++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/routers/web/org/home.go b/routers/web/org/home.go index b250e1214b52f..8c07a06ed1e08 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -5,10 +5,10 @@ package org import ( "fmt" + html_template "html/template" "net/http" "path" "strings" - html_template "html/template" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/organization" @@ -131,6 +131,12 @@ func home(ctx *context.Context, viewRepositories bool) { ctx.Data["PageIsViewRepositories"] = true } + err = shared_user.RenderOrgHeader(ctx) + if err != nil { + ctx.ServerError("RenderOrgHeader", err) + return + } + var ( repos []*repo_model.Repository count int64 diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index a8e5d4ebd25ea..e74cbebd896a2 100644 --- a/templates/org/home.tmpl +++ b/templates/org/home.tmpl @@ -5,16 +5,18 @@
+ {{if and .ShowMemberAndTeamTab .HasPublicProfileReadme .HasPrivateProfileReadme}} + {{end}} {{if .IsViewerMember}} - {{if .PrivateProfileReadme}} + {{if and .ShowMemberAndTeamTab .PrivateProfileReadme}}
{{.PrivateProfileReadme}}
{{end}} {{else}} From 6496b3c1289d47cb92f89251385085358c59679c Mon Sep 17 00:00:00 2001 From: changchaishi Date: Fri, 20 Dec 2024 08:10:55 +0000 Subject: [PATCH 04/22] Add hint for public and private profile repo name --- options/locale/locale_en-US.ini | 2 ++ templates/repo/create.tmpl | 2 ++ web_src/js/features/repo-create.ts | 18 ++++++++++++++++++ web_src/js/index.ts | 2 ++ 4 files changed, 24 insertions(+) create mode 100644 web_src/js/features/repo-create.ts diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 16d894aa266d5..decd006914c5c 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1015,6 +1015,8 @@ new_repo_helper = A repository contains all project files, including revision hi owner = Owner owner_helper = Some organizations may not show up in the dropdown due to a maximum repository count limit. repo_name = Repository Name +repo_name_public_profile_hint=.profile is a special repository that you can use to add a README.md to your personal or organization publiv profile. Make sure it's public and initialize it with a README to get started. +repo_name_private_profile_hint=.profile-private is a special repository that you can use to add a README.md to your organization member profile. Make sure it's private and initialize it with a README to get started. repo_name_helper = Good repository names use short, memorable and unique keywords. repo_size = Repository Size template = Template diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index 2e1de244ea19c..3a7e618b73666 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -44,6 +44,8 @@
+ + {{ctx.Locale.Tr "repo.repo_name_helper"}}
diff --git a/web_src/js/features/repo-create.ts b/web_src/js/features/repo-create.ts new file mode 100644 index 0000000000000..bc16cad9fa032 --- /dev/null +++ b/web_src/js/features/repo-create.ts @@ -0,0 +1,18 @@ +const repoName = document.querySelector('#repo_name'); +const repoPublicHint = document.querySelector('#repo_name_public_profile_hint'); +const repoPrivateHint = document.querySelector('#repo_name_private_profile_hint'); + +export function initRepoCreate() { + repoName?.addEventListener('input', () => { + if (repoName?.value === '.profile') { + repoPublicHint.style.display = 'inline-block'; + } else { + repoPublicHint.style.display = 'none'; + } + if (repoName?.value === '.profile-private') { + repoPrivateHint.style.display = 'inline-block'; + } else { + repoPrivateHint.style.display = 'none'; + } + }); +} diff --git a/web_src/js/index.ts b/web_src/js/index.ts index 51d8c96fbdfbf..06b629008d6fc 100644 --- a/web_src/js/index.ts +++ b/web_src/js/index.ts @@ -35,6 +35,7 @@ import {initRepoEllipsisButton, initCommitStatuses} from './features/repo-commit import {initRepoTopicBar} from './features/repo-home.ts'; import {initAdminCommon} from './features/admin/common.ts'; import {initRepoTemplateSearch} from './features/repo-template.ts'; +import {initRepoCreate} from './features/repo-create.ts'; import {initRepoCodeView} from './features/repo-code.ts'; import {initSshKeyFormParser} from './features/sshkey-helper.ts'; import {initUserSettings} from './features/user-settings.ts'; @@ -173,6 +174,7 @@ onDomReady(() => { initRepoActivityTopAuthorsChart, initRepoArchiveLinks, initRepoBranchButton, + initRepoCreate, initRepoCodeView, initBranchSelectorTabs, initRepoEllipsisButton, From 4b3a8ac3f05ccc45e55f40339013f34714583427 Mon Sep 17 00:00:00 2001 From: changchaishi Date: Fri, 20 Dec 2024 08:26:16 +0000 Subject: [PATCH 05/22] fix that drop down manu shows in repository tab --- templates/org/home.tmpl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index e74cbebd896a2..345a65028d12f 100644 --- a/templates/org/home.tmpl +++ b/templates/org/home.tmpl @@ -5,15 +5,17 @@
- {{if and .ShowMemberAndTeamTab .HasPublicProfileReadme .HasPrivateProfileReadme}} -