Skip to content

Commit 2969180

Browse files
author
Ben Chang
committed
Use view as to get public and private profile repo
1 parent 2d7e6e9 commit 2969180

File tree

7 files changed

+51
-24
lines changed

7 files changed

+51
-24
lines changed

routers/web/org/home.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,22 @@ func home(ctx *context.Context, viewRepositories bool) {
111111
ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull
112112
ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0
113113

114-
if !prepareOrgProfileReadme(ctx, viewRepositories) {
114+
currentURL := ctx.Req.URL
115+
queryParams := currentURL.Query()
116+
queryParams.Set("view_as", "member")
117+
ctx.Data["QueryForMember"] = queryParams.Encode()
118+
queryParams.Set("view_as", "public")
119+
ctx.Data["QueryForPublic"] = queryParams.Encode()
120+
121+
isViewerMember := ctx.FormString("view_as") == "member"
122+
ctx.Data["IsViewerMember"] = isViewerMember
123+
124+
profileType := "Public"
125+
if isViewerMember {
126+
profileType = "Private"
127+
}
128+
129+
if !prepareOrgProfileReadme(ctx, viewRepositories, profileType) {
115130
ctx.Data["PageIsViewRepositories"] = true
116131
}
117132

@@ -168,28 +183,26 @@ func home(ctx *context.Context, viewRepositories bool) {
168183
ctx.HTML(http.StatusOK, tplOrgHome)
169184
}
170185

171-
func prepareOrgProfileReadme(ctx *context.Context, viewRepositories bool) bool {
172-
profileDbRepo, profileGitRepo, profileReadme, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer)
186+
func prepareOrgProfileReadme(ctx *context.Context, viewRepositories bool, profileType string) bool {
187+
profileDbRepo, profileGitRepo, profileReadme, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer, profileType)
173188
defer profileClose()
174-
ctx.Data["HasProfileReadme"] = profileReadme != nil
189+
ctx.Data[fmt.Sprintf("Has%sProfileReadme", profileType)] = profileReadme != nil
175190

176191
if profileGitRepo == nil || profileReadme == nil || viewRepositories {
177192
return false
178193
}
179194

180195
if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
181-
log.Error("failed to GetBlobContent: %v", err)
196+
log.Error("failed to GetBlobContent for %s profile readme: %v", profileType, err)
182197
} else {
183198
rctx := renderhelper.NewRenderContextRepoFile(ctx, profileDbRepo, renderhelper.RepoFileOptions{
184199
CurrentRefPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)),
185200
})
186201
if profileContent, err := markdown.RenderString(rctx, bytes); err != nil {
187-
log.Error("failed to RenderString: %v", err)
202+
log.Error("failed to RenderString for %s profile readme: %v", profileType, err)
188203
} else {
189-
ctx.Data["ProfileReadme"] = profileContent
204+
ctx.Data[fmt.Sprintf("%sProfileReadme", profileType)] = profileContent
190205
}
191206
}
192-
193-
ctx.Data["PageIsViewOverview"] = true
194207
return true
195208
}

routers/web/shared/user/header.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ func PrepareContextForProfileBigAvatar(ctx *context.Context) {
9393
}
9494
}
9595

96-
func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) {
97-
profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, ".profile")
96+
func FindUserProfileReadme(ctx *context.Context, doer *user_model.User, profileType string) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) {
97+
profileName := ".profile"
98+
if profileType != "Public" {
99+
profileName = ".profile-private"
100+
}
101+
profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, profileName)
98102
if err == nil {
99103
perm, err := access_model.GetUserRepoPermission(ctx, profileDbRepo, doer)
100104
if err == nil && !profileDbRepo.IsEmpty && perm.CanRead(unit.TypeCode) {
@@ -121,9 +125,9 @@ func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profile
121125
func RenderUserHeader(ctx *context.Context) {
122126
prepareContextForCommonProfile(ctx)
123127

124-
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer)
128+
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer, "Public")
125129
defer profileClose()
126-
ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil
130+
ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil
127131
}
128132

129133
func LoadHeaderCount(ctx *context.Context) error {
@@ -165,9 +169,13 @@ func RenderOrgHeader(ctx *context.Context) error {
165169
return err
166170
}
167171

168-
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer)
172+
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer, "Public")
173+
defer profileClose()
174+
ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil
175+
176+
_, _, profileReadmeBlob, profileClose = FindUserProfileReadme(ctx, ctx.Doer, "Private")
169177
defer profileClose()
170-
ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil
178+
ctx.Data["HasPrivateProfileReadme"] = profileReadmeBlob != nil
171179

172180
return nil
173181
}

routers/web/user/profile.go

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

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

7979
showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
@@ -95,7 +95,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
9595
}
9696
}
9797
ctx.Data["TabName"] = tab
98-
ctx.Data["HasProfileReadme"] = profileReadme != nil
98+
ctx.Data["HasPublicProfileReadme"] = profileReadme != nil
9999

100100
page := ctx.FormInt("page")
101101
if page <= 0 {
@@ -253,7 +253,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
253253
if profileContent, err := markdown.RenderString(rctx, bytes); err != nil {
254254
log.Error("failed to RenderString: %v", err)
255255
} else {
256-
ctx.Data["ProfileReadme"] = profileContent
256+
ctx.Data["PublicProfileReadme"] = profileContent
257257
}
258258
}
259259
default: // default to "repositories"

templates/org/home.tmpl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
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 .ProfileReadme}}
9-
<div id="readme_profile" class="markup">{{.ProfileReadme}}</div>
8+
{{if .IsViewerMember}}
9+
{{if .PrivateProfileReadme}}
10+
<div id="readme_profile" class="markup">{{.PrivateProfileReadme}}</div>
11+
{{end}}
12+
{{else}}
13+
{{if .PublicProfileReadme}}
14+
<div id="readme_profile" class="markup">{{.PublicProfileReadme}}</div>
15+
{{end}}
1016
{{end}}
1117
{{template "shared/repo_search" .}}
1218
{{template "explore/repo_list" .}}

templates/org/menu.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<div class="ui container">
22
<overflow-menu class="ui secondary pointing tabular borderless menu tw-mb-4">
33
<div class="overflow-menu-items">
4-
{{if .HasProfileReadme}}
4+
{{if or .HasPublicProfileReadme .HasPrivateProfileReadme}}
55
<a class="{{if .PageIsViewOverview}}active {{end}}item" href="{{$.Org.HomeLink}}">
66
{{svg "octicon-info"}} {{ctx.Locale.Tr "user.overview"}}
77
</a>
88
{{end}}
9-
<a class="{{if .PageIsViewRepositories}}active {{end}}item" href="{{$.Org.HomeLink}}{{if .HasProfileReadme}}/-/repositories{{end}}">
9+
<a class="{{if .PageIsViewRepositories}}active {{end}}item" href="{{$.Org.HomeLink}}{{if or .HasPublicProfileReadme .HasPrivateProfileReadme}}/-/repositories{{end}}">
1010
{{svg "octicon-repo"}} {{ctx.Locale.Tr "user.repositories"}}
1111
{{if .RepoCount}}
1212
<div class="ui small label">{{.RepoCount}}</div>

templates/user/overview/header.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<overflow-menu class="ui secondary pointing tabular borderless menu">
22
<div class="overflow-menu-items">
3-
{{if and .HasProfileReadme .ContextUser.IsIndividual}}
3+
{{if and .HasPublicProfileReadme .ContextUser.IsIndividual}}
44
<a class="{{if eq .TabName "overview"}}active {{end}}item" href="{{.ContextUser.HomeLink}}?tab=overview">
55
{{svg "octicon-info"}} {{ctx.Locale.Tr "user.overview"}}
66
</a>

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">{{.ProfileReadme}}</div>
29+
<div id="readme_profile" class="markup">{{.PublicProfileReadme}}</div>
3030
{{else}}
3131
{{template "shared/repo_search" .}}
3232
{{template "explore/repo_list" .}}

0 commit comments

Comments
 (0)