Skip to content

Commit 784bedd

Browse files
Ben Changchangchaishi
authored andcommitted
Use view as to get public and private profile repo
1 parent a92f505 commit 784bedd

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
@@ -102,8 +102,12 @@ func PrepareContextForProfileBigAvatar(ctx *context.Context) {
102102
}
103103
}
104104

105-
func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) {
106-
profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, ".profile")
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)
107111
if err == nil {
108112
perm, err := access_model.GetUserRepoPermission(ctx, profileDbRepo, doer)
109113
if err == nil && !profileDbRepo.IsEmpty && perm.CanRead(unit.TypeCode) {
@@ -130,9 +134,9 @@ func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profile
130134
func RenderUserHeader(ctx *context.Context) {
131135
prepareContextForCommonProfile(ctx)
132136

133-
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer)
137+
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer, "Public")
134138
defer profileClose()
135-
ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil
139+
ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil
136140
}
137141

138142
func LoadHeaderCount(ctx *context.Context) error {
@@ -174,9 +178,13 @@ func RenderOrgHeader(ctx *context.Context) error {
174178
return err
175179
}
176180

177-
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer)
181+
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer, "Public")
182+
defer profileClose()
183+
ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil
184+
185+
_, _, profileReadmeBlob, profileClose = FindUserProfileReadme(ctx, ctx.Doer, "Private")
178186
defer profileClose()
179-
ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil
187+
ctx.Data["HasPrivateProfileReadme"] = profileReadmeBlob != nil
180188

181189
return nil
182190
}

routers/web/user/profile.go

Lines changed: 3 additions & 3 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)
77+
profileDbRepo, _ /*profileGitRepo*/, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer, "Public")
7878
defer profileClose()
7979

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

101101
page := ctx.FormInt("page")
102102
if page <= 0 {
@@ -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["ProfileReadme"] = profileContent
257+
ctx.Data["PublicProfileReadme"] = profileContent
258258
}
259259
}
260260
case "organizations":

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 if eq .TabName "organizations"}}
3131
{{template "repo/user_cards" .}}
3232
{{else}}

0 commit comments

Comments
 (0)