1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-01-02 01:17:43 +03:00

clarify HasOrgProfileReadme vs HasUserProfileReadme, fix tests

This commit is contained in:
wxiaoguang 2024-12-30 21:56:43 +08:00
parent 93e7a010c3
commit ae3627fd2a
9 changed files with 83 additions and 85 deletions

View File

@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/renderhelper"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
@ -21,9 +22,7 @@ import (
"code.gitea.io/gitea/services/context"
)
const (
tplOrgHome templates.TplName = "org/home"
)
const tplOrgHome templates.TplName = "org/home"
// Home show organization home page
func Home(ctx *context.Context) {
@ -110,29 +109,13 @@ func home(ctx *context.Context, viewRepositories bool) {
ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull
ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0
err = shared_user.RenderOrgHeader(ctx)
prepareResult, err := shared_user.PrepareOrgHeader(ctx)
if err != nil {
ctx.ServerError("RenderOrgHeader", err)
ctx.ServerError("PrepareOrgHeader", err)
return
}
isBothProfilesExist := ctx.Data["HasPublicProfileReadme"] == true && ctx.Data["HasPrivateProfileReadme"] == true
isViewerMember := ctx.FormString("view_as")
ctx.Data["IsViewerMember"] = isViewerMember == "member"
viewAsPrivate := isViewerMember == "member"
if !isBothProfilesExist {
if !prepareOrgProfileReadme(ctx, prepareOrgProfileReadmeOptions{viewRepositories: viewRepositories}) {
if !prepareOrgProfileReadme(ctx, prepareOrgProfileReadmeOptions{viewRepositories: viewRepositories, viewAsPrivate: true}) {
ctx.Data["PageIsViewRepositories"] = true
}
}
} else {
if !prepareOrgProfileReadme(ctx, prepareOrgProfileReadmeOptions{viewRepositories: viewRepositories, viewAsPrivate: viewAsPrivate}) {
ctx.Data["PageIsViewRepositories"] = true
}
}
ctx.Data["HasPrivateProfileReadme"] = viewRepositories || !prepareOrgProfileReadme(ctx, prepareResult)
var (
repos []*repo_model.Repository
@ -171,36 +154,45 @@ func home(ctx *context.Context, viewRepositories bool) {
ctx.HTML(http.StatusOK, tplOrgHome)
}
type prepareOrgProfileReadmeOptions struct {
viewRepositories bool
viewAsPrivate bool
}
func prepareOrgProfileReadme(ctx *context.Context, prepareResult *shared_user.PrepareOrgHeaderResult) bool {
viewAs := ctx.FormString("view_as")
viewAsMember := viewAs == "member"
func prepareOrgProfileReadme(ctx *context.Context, opts prepareOrgProfileReadmeOptions) bool {
profileRepoName := util.Iif(opts.viewAsPrivate, shared_user.RepoNameProfilePrivate, shared_user.RepoNameProfile)
profileDbRepo, profileReadme := shared_user.FindOwnerProfileReadme(ctx, ctx.Doer, profileRepoName)
if opts.viewAsPrivate {
ctx.Data["HasPrivateProfileReadme"] = profileReadme != nil
var profileRepo *repo_model.Repository
var readmeBlob *git.Blob
if viewAsMember {
if prepareResult.ProfilePrivateReadmeBlob != nil {
profileRepo, readmeBlob = prepareResult.ProfilePrivateRepo, prepareResult.ProfilePrivateReadmeBlob
} else {
profileRepo, readmeBlob = prepareResult.ProfilePublicRepo, prepareResult.ProfilePublicReadmeBlob
viewAsMember = false
}
} else {
ctx.Data["HasPublicProfileReadme"] = profileReadme != nil
if prepareResult.ProfilePublicReadmeBlob != nil {
profileRepo, readmeBlob = prepareResult.ProfilePublicRepo, prepareResult.ProfilePublicReadmeBlob
} else {
profileRepo, readmeBlob = prepareResult.ProfilePrivateRepo, prepareResult.ProfilePrivateReadmeBlob
viewAsMember = true
}
}
if profileReadme == nil || opts.viewRepositories {
if readmeBlob == nil {
return false
}
if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
log.Error("failed to GetBlobContent for %s profile readme: %v", profileRepoName, 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 for %s profile readme: %v", profileRepoName, err)
} else {
ctx.Data["ProfileReadmeContent"] = profileContent
}
readmeBytes, err := readmeBlob.GetBlobContent(setting.UI.MaxDisplayFileSize)
if err != nil {
log.Error("failed to GetBlobContent for %q profile (view as %q) readme: %v", ctx.ContextUser.Name, viewAs, err)
return false
}
rctx := renderhelper.NewRenderContextRepoFile(ctx, profileRepo, renderhelper.RepoFileOptions{
CurrentRefPath: path.Join("branch", util.PathEscapeSegments(profileRepo.DefaultBranch)),
})
ctx.Data["ProfileReadmeContent"], err = markdown.RenderString(rctx, readmeBytes)
if err != nil {
log.Error("failed to GetBlobContent for %q profile (view as %q) readme: %v", ctx.ContextUser.Name, viewAs, err)
return false
}
ctx.Data["IsViewingOrgAsMember"] = viewAsMember
return true
}

View File

@ -54,9 +54,9 @@ func Members(ctx *context.Context) {
return
}
err = shared_user.RenderOrgHeader(ctx)
_, err = shared_user.PrepareOrgHeader(ctx)
if err != nil {
ctx.ServerError("RenderOrgHeader", err)
ctx.ServerError("PrepareOrgHeader", err)
return
}

View File

@ -58,9 +58,9 @@ func Teams(ctx *context.Context) {
}
ctx.Data["Teams"] = ctx.Org.Teams
err := shared_user.RenderOrgHeader(ctx)
_, err := shared_user.PrepareOrgHeader(ctx)
if err != nil {
ctx.ServerError("RenderOrgHeader", err)
ctx.ServerError("PrepareOrgHeader", err)
return
}

View File

@ -142,7 +142,7 @@ func RenderUserHeader(ctx *context.Context) {
prepareContextForCommonProfile(ctx)
_, profileReadmeBlob := FindOwnerProfileReadme(ctx, ctx.Doer)
ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil
ctx.Data["HasUserProfileReadme"] = profileReadmeBlob != nil
}
func LoadHeaderCount(ctx *context.Context) error {
@ -184,18 +184,24 @@ const (
RepoNameProfile = ".profile"
)
func RenderOrgHeader(ctx *context.Context) error {
if err := LoadHeaderCount(ctx); err != nil {
return err
type PrepareOrgHeaderResult struct {
ProfilePublicRepo *repo_model.Repository
ProfilePublicReadmeBlob *git.Blob
ProfilePrivateRepo *repo_model.Repository
ProfilePrivateReadmeBlob *git.Blob
HasOrgProfileReadme bool
}
func PrepareOrgHeader(ctx *context.Context) (result *PrepareOrgHeaderResult, err error) {
if err = LoadHeaderCount(ctx); err != nil {
return nil, err
}
// FIXME: only do database query, do not open it
_, profileReadmeBlob := FindOwnerProfileReadme(ctx, ctx.Doer)
ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil
// FIXME: only do database query, do not open it
_, profileReadmeBlob = FindOwnerProfileReadme(ctx, ctx.Doer, RepoNameProfilePrivate)
ctx.Data["HasPrivateProfileReadme"] = profileReadmeBlob != nil
return nil
result = &PrepareOrgHeaderResult{}
result.ProfilePublicRepo, result.ProfilePublicReadmeBlob = FindOwnerProfileReadme(ctx, ctx.Doer)
result.ProfilePrivateRepo, result.ProfilePrivateReadmeBlob = FindOwnerProfileReadme(ctx, ctx.Doer, RepoNameProfilePrivate)
result.HasOrgProfileReadme = result.ProfilePublicReadmeBlob != nil || result.ProfilePrivateReadmeBlob != nil
ctx.Data["HasOrgProfileReadme"] = result.HasOrgProfileReadme // many pages need it to show the "overview" tab
ctx.Data["ShowOrgProfileReadmeSelector"] = result.ProfilePublicReadmeBlob != nil && result.ProfilePrivateReadmeBlob != nil
return result, nil
}

View File

@ -95,7 +95,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
}
}
ctx.Data["TabName"] = tab
ctx.Data["HasPublicProfileReadme"] = profileReadme != nil
ctx.Data["HasUserProfileReadme"] = profileReadme != nil
page := ctx.FormInt("page")
if page <= 0 {

View File

@ -6,7 +6,7 @@
<div class="ui mobile reversed stackable grid">
<div class="ui {{if .ShowMemberAndTeamTab}}eleven wide{{end}} column">
{{if .ProfileReadmeContent}}
<div id="readme_profile" class="markup">{{.ProfileReadmeContent}}</div>
<div id="readme_profile" class="markup" data-profile-view-as-member="{{.IsViewingOrgAsMember}}">{{.ProfileReadmeContent}}</div>
{{end}}
{{template "shared/repo_search" .}}
{{template "explore/repo_list" .}}
@ -25,26 +25,26 @@
<div class="divider"></div>
{{end}}
{{if and .ShowMemberAndTeamTab .HasPublicProfileReadme .HasPrivateProfileReadme}}
{{if and .ShowMemberAndTeamTab .ShowOrgProfileReadmeSelector}}
<div class="tw-my-4">
<div id="org-home-view-as-dropdown" class="ui dropdown jump">
<span class="text">
{{svg "octicon-eye"}}
View as: {{if not .IsViewerMember}}{{ctx.Locale.Tr "settings.visibility.public"}}{{else}}{{ctx.Locale.Tr "org.members.member"}}{{end}}
View as: {{if not .IsViewingOrgAsMember}}{{ctx.Locale.Tr "settings.visibility.public"}}{{else}}{{ctx.Locale.Tr "org.members.member"}}{{end}}
</span>
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="menu">
{{/* TODO: does it really need to use CurrentURL with query parameters? Why not construct a new link with clear parameters */}}
<a href="{{QueryBuild $.CurrentURL "view_as" "public"}}" class="item {{if not .IsViewerMember}}selected{{end}}">
{{svg "octicon-check" 14 (Iif (not .IsViewerMember) "" "tw-invisible")}} {{ctx.Locale.Tr "settings.visibility.public"}}
<a href="{{QueryBuild $.CurrentURL "view_as" "public"}}" class="item {{if not .IsViewingOrgAsMember}}selected{{end}}">
{{svg "octicon-check" 14 (Iif (not .IsViewingOrgAsMember) "" "tw-invisible")}} {{ctx.Locale.Tr "settings.visibility.public"}}
</a>
<a href="{{QueryBuild $.CurrentURL "view_as" "member"}}" class="item {{if .IsViewerMember}}selected{{end}}">
{{svg "octicon-check" 14 (Iif .IsViewerMember "" "tw-invisible")}} {{ctx.Locale.Tr "org.members.member"}}
<a href="{{QueryBuild $.CurrentURL "view_as" "member"}}" class="item {{if .IsViewingOrgAsMember}}selected{{end}}">
{{svg "octicon-check" 14 (Iif .IsViewingOrgAsMember "" "tw-invisible")}} {{ctx.Locale.Tr "org.members.member"}}
</a>
</div>
</div>
<div class="tw-my-2">
{{if .IsViewerMember}}{{ctx.Locale.Tr "org.view_as_member_hint"}}{{else}}{{ctx.Locale.Tr "org.view_as_public_hint"}}{{end}}
{{if .IsViewingOrgAsMember}}{{ctx.Locale.Tr "org.view_as_member_hint"}}{{else}}{{ctx.Locale.Tr "org.view_as_public_hint"}}{{end}}
</div>
</div>
{{end}}

View File

@ -1,12 +1,12 @@
<div class="ui container">
<overflow-menu class="ui secondary pointing tabular borderless menu tw-mb-4">
<div class="overflow-menu-items">
{{if or .HasPublicProfileReadme .HasPrivateProfileReadme}}
{{if .HasOrgProfileReadme}}
<a class="{{if .PageIsViewOverview}}active {{end}}item" href="{{$.Org.HomeLink}}">
{{svg "octicon-info"}} {{ctx.Locale.Tr "user.overview"}}
</a>
{{end}}
<a class="{{if .PageIsViewRepositories}}active {{end}}item" href="{{$.Org.HomeLink}}{{if or .HasPublicProfileReadme .HasPrivateProfileReadme}}/-/repositories{{end}}">
<a class="{{if .PageIsViewRepositories}}active {{end}}item" href="{{$.Org.HomeLink}}{{if .HasOrgProfileReadme}}/-/repositories{{end}}">
{{svg "octicon-repo"}} {{ctx.Locale.Tr "user.repositories"}}
{{if .RepoCount}}
<div class="ui small label">{{.RepoCount}}</div>

View File

@ -1,6 +1,6 @@
<overflow-menu class="ui secondary pointing tabular borderless menu">
<div class="overflow-menu-items">
{{if and .HasPublicProfileReadme .ContextUser.IsIndividual}}
{{if and .HasUserProfileReadme .ContextUser.IsIndividual}}
<a class="{{if eq .TabName "overview"}}active {{end}}item" href="{{.ContextUser.HomeLink}}?tab=overview">
{{svg "octicon-info"}} {{ctx.Locale.Tr "user.overview"}}
</a>

View File

@ -69,7 +69,7 @@ func testOrgProfile(t *testing.T, u *url.URL) {
resp := MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
profileDivs := htmlDoc.doc.Find("#user-content-public")
profileDivs := htmlDoc.doc.Find("[data-profile-view-as-member=false]")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv := htmlDoc.doc.Find("#org-home-view-as-dropdown")
@ -81,7 +81,7 @@ func testOrgProfile(t *testing.T, u *url.URL) {
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown")
@ -93,7 +93,7 @@ func testOrgProfile(t *testing.T, u *url.URL) {
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown")
@ -103,14 +103,14 @@ func testOrgProfile(t *testing.T, u *url.URL) {
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-private")
profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=true]")
assert.EqualValues(t, 1, profileDivs.Length())
req = NewRequest(t, "GET", "/org3?view_as=public")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]")
assert.EqualValues(t, 1, profileDivs.Length())
// PART 2: Each org has either one of private pr public profile
@ -121,7 +121,7 @@ func testOrgProfile(t *testing.T, u *url.URL) {
req = NewRequest(t, "GET", "/org41")
resp = MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown")
assert.EqualValues(t, 0, dropDownDiv.Length())
@ -129,9 +129,9 @@ func testOrgProfile(t *testing.T, u *url.URL) {
req = NewRequest(t, "GET", "/org42")
resp = MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]")
assert.EqualValues(t, 0, profileDivs.Length())
profileDivs = htmlDoc.doc.Find("#user-content-public")
profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]")
assert.EqualValues(t, 0, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown")
assert.EqualValues(t, 0, dropDownDiv.Length())
@ -140,7 +140,7 @@ func testOrgProfile(t *testing.T, u *url.URL) {
req = NewRequest(t, "GET", "/org41")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown")
assert.EqualValues(t, 0, dropDownDiv.Length())
@ -148,7 +148,7 @@ func testOrgProfile(t *testing.T, u *url.URL) {
req = NewRequest(t, "GET", "/org42")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-private")
profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=true]")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown")
assert.EqualValues(t, 0, dropDownDiv.Length())