Skip to content

Commit a47cf32

Browse files
committed
temp
1 parent 0c70525 commit a47cf32

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed

routers/web/org/home.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,16 @@ func home(ctx *context.Context, viewRepositories bool) {
128128
isViewerMember := ctx.FormString("view_as")
129129
ctx.Data["IsViewerMember"] = isViewerMember == "member"
130130

131-
profileType := "Public"
132-
if isViewerMember == "member" {
133-
profileType = "Private"
134-
}
131+
viewAsPrivate := isViewerMember == "member"
135132

136133
if !isBothProfilesExist {
137-
if !prepareOrgProfileReadme(ctx, viewRepositories, "Public") {
138-
if !prepareOrgProfileReadme(ctx, viewRepositories, "Private") {
134+
if !prepareOrgProfileReadme(ctx, prepareOrgProfileReadmeOptions{viewRepositories: viewRepositories}) {
135+
if !prepareOrgProfileReadme(ctx, prepareOrgProfileReadmeOptions{viewRepositories: viewRepositories, viewAsPrivate: true}) {
139136
ctx.Data["PageIsViewRepositories"] = true
140137
}
141138
}
142139
} else {
143-
if !prepareOrgProfileReadme(ctx, viewRepositories, profileType) {
140+
if !prepareOrgProfileReadme(ctx, prepareOrgProfileReadmeOptions{viewRepositories: viewRepositories, viewAsPrivate: viewAsPrivate}) {
144141
ctx.Data["PageIsViewRepositories"] = true
145142
}
146143
}
@@ -182,25 +179,33 @@ func home(ctx *context.Context, viewRepositories bool) {
182179
ctx.HTML(http.StatusOK, tplOrgHome)
183180
}
184181

185-
func prepareOrgProfileReadme(ctx *context.Context, viewRepositories bool, profileType string) bool {
186-
profileDbRepo, profileGitRepo, profileReadme, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer, profileType)
182+
type prepareOrgProfileReadmeOptions struct {
183+
viewRepositories bool
184+
viewAsPrivate bool
185+
}
186+
187+
func prepareOrgProfileReadme(ctx *context.Context, opts prepareOrgProfileReadmeOptions) bool {
188+
profileRepoName := util.Iif(opts.viewAsPrivate, ".profile-private", ".profile")
189+
profileDbRepo, profileGitRepo, profileReadme, profileClose := shared_user.FindOwnerProfileReadme(ctx, ctx.Doer, profileRepoName)
187190
defer profileClose()
188-
ctx.Data[fmt.Sprintf("Has%sProfileReadme", profileType)] = profileReadme != nil
189191

190-
if profileGitRepo == nil || profileReadme == nil || viewRepositories {
192+
// FIXME: need to use fixed keys
193+
// ctx.Data[fmt.Sprintf("Has%sProfileReadme", profileType)] = profileReadme != nil
194+
195+
if profileGitRepo == nil || profileReadme == nil || opts.viewRepositories {
191196
return false
192197
}
193198

194199
if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
195-
log.Error("failed to GetBlobContent for %s profile readme: %v", profileType, err)
200+
log.Error("failed to GetBlobContent for %s profile readme: %v", profileRepoName, err)
196201
} else {
197202
rctx := renderhelper.NewRenderContextRepoFile(ctx, profileDbRepo, renderhelper.RepoFileOptions{
198203
CurrentRefPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)),
199204
})
200205
if profileContent, err := markdown.RenderString(rctx, bytes); err != nil {
201-
log.Error("failed to RenderString for %s profile readme: %v", profileType, err)
206+
log.Error("failed to RenderString for %s profile readme: %v", profileRepoName, err)
202207
} else {
203-
ctx.Data[fmt.Sprintf("%sProfileReadme", profileType)] = profileContent
208+
ctx.Data["ProfileReadmeContent"] = profileContent
204209
}
205210
}
206211
return true

routers/web/shared/user/header.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package user
55

66
import (
7+
"code.gitea.io/gitea/modules/util"
78
"net/url"
89

910
"code.gitea.io/gitea/models/db"
@@ -102,27 +103,24 @@ func PrepareContextForProfileBigAvatar(ctx *context.Context) {
102103
}
103104
}
104105

105-
func FindUserProfileReadme(ctx *context.Context, doer *user_model.User, profileType string) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) {
106-
profileName := ".profile"
107-
if profileType != "Public" {
108-
profileName = ".profile-private"
109-
}
110-
profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, profileName)
106+
func FindOwnerProfileReadme(ctx *context.Context, doer *user_model.User, optProfileRepoName ...string) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) {
107+
profileRepoName := util.OptionalArg(optProfileRepoName, ".profile")
108+
profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, profileRepoName)
111109
if err == nil {
112110
perm, err := access_model.GetUserRepoPermission(ctx, profileDbRepo, doer)
113111
if err == nil && !profileDbRepo.IsEmpty && perm.CanRead(unit.TypeCode) {
114112
if profileGitRepo, err = gitrepo.OpenRepository(ctx, profileDbRepo); err != nil {
115-
log.Error("FindUserProfileReadme failed to OpenRepository: %v", err)
113+
log.Error("FindOwnerProfileReadme failed to OpenRepository: %v", err)
116114
} else {
117115
if commit, err := profileGitRepo.GetBranchCommit(profileDbRepo.DefaultBranch); err != nil {
118-
log.Error("FindUserProfileReadme failed to GetBranchCommit: %v", err)
116+
log.Error("FindOwnerProfileReadme failed to GetBranchCommit: %v", err)
119117
} else {
120118
profileReadmeBlob, _ = commit.GetBlobByPath("README.md")
121119
}
122120
}
123121
}
124122
} else if !repo_model.IsErrRepoNotExist(err) {
125-
log.Error("FindUserProfileReadme failed to GetRepositoryByName: %v", err)
123+
log.Error("FindOwnerProfileReadme failed to GetRepositoryByName: %v", err)
126124
}
127125
return profileDbRepo, profileGitRepo, profileReadmeBlob, func() {
128126
if profileGitRepo != nil {
@@ -134,7 +132,7 @@ func FindUserProfileReadme(ctx *context.Context, doer *user_model.User, profileT
134132
func RenderUserHeader(ctx *context.Context) {
135133
prepareContextForCommonProfile(ctx)
136134

137-
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer, "Public")
135+
_, _, profileReadmeBlob, profileClose := FindOwnerProfileReadme(ctx, ctx.Doer)
138136
defer profileClose()
139137
ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil
140138
}
@@ -178,11 +176,14 @@ func RenderOrgHeader(ctx *context.Context) error {
178176
return err
179177
}
180178

181-
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer, "Public")
179+
// FIXME: only do database query, do not open it
180+
_, _, profileReadmeBlob, profileClose := FindOwnerProfileReadme(ctx, ctx.Doer)
182181
defer profileClose()
183182
ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil
184183

185-
_, _, profileReadmeBlob, profileClose = FindUserProfileReadme(ctx, ctx.Doer, "Private")
184+
// FIXME: only do database query, do not open it
185+
// FIXME: use a const for ".profile-private"
186+
_, _, profileReadmeBlob, profileClose = FindOwnerProfileReadme(ctx, ctx.Doer, ".profile-private")
186187
defer profileClose()
187188
ctx.Data["HasPrivateProfileReadme"] = profileReadmeBlob != nil
188189

routers/web/user/profile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func userProfile(ctx *context.Context) {
7474
ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data)
7575
}
7676

77-
profileDbRepo, _ /*profileGitRepo*/, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer, "Public")
77+
profileDbRepo, _ /*profileGitRepo*/, profileReadmeBlob, profileClose := shared_user.FindOwnerProfileReadme(ctx, ctx.Doer)
7878
defer profileClose()
7979

8080
showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
@@ -254,7 +254,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
254254
if profileContent, err := markdown.RenderString(rctx, bytes); err != nil {
255255
log.Error("failed to RenderString: %v", err)
256256
} else {
257-
ctx.Data["PublicProfileReadme"] = profileContent
257+
ctx.Data["ProfileReadmeContent"] = profileContent
258258
}
259259
}
260260
case "organizations":

templates/org/home.tmpl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="ui container">
66
<div class="ui mobile reversed stackable grid">
77
<div class="ui {{if .ShowMemberAndTeamTab}}eleven wide{{end}} column">
8-
{{if or .PublicProfileReadme .PrivateProfileReadme}}
8+
{{if or .ProfileReadmeContent}}
99
{{if and .ShowMemberAndTeamTab .HasPublicProfileReadme .HasPrivateProfileReadme}}
1010
<div class="ui small secondary filter menu">
1111
<div id="profile_view_as_dropdown" class="item ui small dropdown jump" style="padding-bottom: 1em;">
@@ -19,11 +19,8 @@
1919
</div>
2020
{{end}}
2121
{{end}}
22-
{{if .PrivateProfileReadme}}
23-
<div id="readme_profile" class="markup">{{.PrivateProfileReadme}}</div>
24-
{{end}}
25-
{{if .PublicProfileReadme}}
26-
<div id="readme_profile" class="markup">{{.PublicProfileReadme}}</div>
22+
{{if .ProfileReadmeContent}}
23+
<div id="readme_profile" class="markup">{{.ProfileReadmeContent}}</div>
2724
{{end}}
2825
{{template "shared/repo_search" .}}
2926
{{template "explore/repo_list" .}}

templates/user/profile.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{{else if eq .TabName "followers"}}
2727
{{template "repo/user_cards" .}}
2828
{{else if eq .TabName "overview"}}
29-
<div id="readme_profile" class="markup">{{.PublicProfileReadme}}</div>
29+
<div id="readme_profile" class="markup">{{.ProfileReadme}}</div>
3030
{{else if eq .TabName "organizations"}}
3131
{{template "repo/user_cards" .}}
3232
{{else}}

0 commit comments

Comments
 (0)