From 2969180fd166d9fe3577cf44e8e14195dcf19f6c Mon Sep 17 00:00:00 2001 From: Ben Chang Date: Mon, 16 Dec 2024 18:05:02 +0800 Subject: [PATCH] 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 f02c08ae76..ad44a07ac7 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 4cb0592b4b..35e253d8de 100644 --- a/routers/web/shared/user/header.go +++ b/routers/web/shared/user/header.go @@ -93,8 +93,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) { @@ -121,9 +125,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 { @@ -165,9 +169,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["HasProfileReadme"] = profileReadmeBlob != nil + ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil + + _, _, profileReadmeBlob, profileClose = FindUserProfileReadme(ctx, ctx.Doer, "Private") + defer profileClose() + ctx.Data["HasPrivateProfileReadme"] = profileReadmeBlob != nil return nil } diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index c41030a5e2..fc9c89633d 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -73,7 +73,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) @@ -95,7 +95,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 { @@ -253,7 +253,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 } } default: // default to "repositories" diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index 4851b69979..e0996de90d 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 29238f8d6b..a9b925ed78 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 275c4e295e..ee28f32ed1 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 cf61bb906a..629e62863a 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}} {{template "shared/repo_search" .}} {{template "explore/repo_list" .}}