|
4 | 4 | package user
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "code.gitea.io/gitea/models/db" |
| 8 | + "code.gitea.io/gitea/models/organization" |
7 | 9 | repo_model "code.gitea.io/gitea/models/repo"
|
| 10 | + user_model "code.gitea.io/gitea/models/user" |
8 | 11 | "code.gitea.io/gitea/modules/context"
|
9 | 12 | "code.gitea.io/gitea/modules/git"
|
| 13 | + "code.gitea.io/gitea/modules/log" |
| 14 | + "code.gitea.io/gitea/modules/markup" |
| 15 | + "code.gitea.io/gitea/modules/markup/markdown" |
10 | 16 | "code.gitea.io/gitea/modules/setting"
|
11 | 17 | )
|
12 | 18 |
|
13 |
| -func RenderUserHeader(ctx *context.Context) { |
14 |
| - ctx.Data["IsProjectEnabled"] = true |
| 19 | +// prepareContextForCommonProfile store some common data into context data for user's profile related pages (including the nav menu) |
| 20 | +// It is designed to be fast and safe to be called multiple times in one request |
| 21 | +func prepareContextForCommonProfile(ctx *context.Context) { |
15 | 22 | ctx.Data["IsPackageEnabled"] = setting.Packages.Enabled
|
16 | 23 | ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
17 | 24 | ctx.Data["ContextUser"] = ctx.ContextUser
|
18 |
| - tab := ctx.FormString("tab") |
19 |
| - ctx.Data["TabName"] = tab |
20 |
| - repo, err := repo_model.GetRepositoryByName(ctx.ContextUser.ID, ".profile") |
21 |
| - if err == nil && !repo.IsEmpty { |
22 |
| - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) |
| 25 | + ctx.Data["EnableFeed"] = setting.Other.EnableFeed |
| 26 | + ctx.Data["FeedURL"] = ctx.ContextUser.HomeLink() |
| 27 | +} |
| 28 | + |
| 29 | +// PrepareContextForProfileBigAvatar set the context for big avatar view on the profile page |
| 30 | +func PrepareContextForProfileBigAvatar(ctx *context.Context) { |
| 31 | + prepareContextForCommonProfile(ctx) |
| 32 | + |
| 33 | + ctx.Data["IsFollowing"] = ctx.Doer != nil && user_model.IsFollowing(ctx.Doer.ID, ctx.ContextUser.ID) |
| 34 | + ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail && ctx.ContextUser.Email != "" && ctx.IsSigned && !ctx.ContextUser.KeepEmailPrivate |
| 35 | + |
| 36 | + // Show OpenID URIs |
| 37 | + openIDs, err := user_model.GetUserOpenIDs(ctx.ContextUser.ID) |
| 38 | + if err != nil { |
| 39 | + ctx.ServerError("GetUserOpenIDs", err) |
| 40 | + return |
| 41 | + } |
| 42 | + ctx.Data["OpenIDs"] = openIDs |
| 43 | + |
| 44 | + if len(ctx.ContextUser.Description) != 0 { |
| 45 | + content, err := markdown.RenderString(&markup.RenderContext{ |
| 46 | + URLPrefix: ctx.Repo.RepoLink, |
| 47 | + Metas: map[string]string{"mode": "document"}, |
| 48 | + GitRepo: ctx.Repo.GitRepo, |
| 49 | + Ctx: ctx, |
| 50 | + }, ctx.ContextUser.Description) |
23 | 51 | if err != nil {
|
24 |
| - ctx.ServerError("OpenRepository", err) |
| 52 | + ctx.ServerError("RenderString", err) |
25 | 53 | return
|
26 | 54 | }
|
27 |
| - defer gitRepo.Close() |
28 |
| - commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch) |
29 |
| - if err != nil { |
30 |
| - ctx.ServerError("GetBranchCommit", err) |
31 |
| - return |
| 55 | + ctx.Data["RenderedDescription"] = content |
| 56 | + } |
| 57 | + |
| 58 | + showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID) |
| 59 | + orgs, err := organization.FindOrgs(organization.FindOrgOptions{ |
| 60 | + UserID: ctx.ContextUser.ID, |
| 61 | + IncludePrivate: showPrivate, |
| 62 | + }) |
| 63 | + if err != nil { |
| 64 | + ctx.ServerError("FindOrgs", err) |
| 65 | + return |
| 66 | + } |
| 67 | + ctx.Data["Orgs"] = orgs |
| 68 | + ctx.Data["HasOrgsVisible"] = organization.HasOrgsVisible(orgs, ctx.Doer) |
| 69 | + |
| 70 | + badges, _, err := user_model.GetUserBadges(ctx, ctx.ContextUser) |
| 71 | + if err != nil { |
| 72 | + ctx.ServerError("GetUserBadges", err) |
| 73 | + return |
| 74 | + } |
| 75 | + ctx.Data["Badges"] = badges |
| 76 | + |
| 77 | + // in case the numbers are already provided by other functions, no need to query again (which is slow) |
| 78 | + if _, ok := ctx.Data["NumFollowers"]; !ok { |
| 79 | + _, ctx.Data["NumFollowers"], _ = user_model.GetUserFollowers(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{PageSize: 1, Page: 1}) |
| 80 | + } |
| 81 | + if _, ok := ctx.Data["NumFollowing"]; !ok { |
| 82 | + _, ctx.Data["NumFollowing"], _ = user_model.GetUserFollowing(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{PageSize: 1, Page: 1}) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func FindUserProfileReadme(ctx *context.Context) (profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) { |
| 87 | + profileDbRepo, err := repo_model.GetRepositoryByName(ctx.ContextUser.ID, ".profile") |
| 88 | + if err == nil && !profileDbRepo.IsEmpty { |
| 89 | + if profileGitRepo, err = git.OpenRepository(ctx, profileDbRepo.RepoPath()); err != nil { |
| 90 | + log.Error("FindUserProfileReadme failed to OpenRepository: %v", err) |
| 91 | + } else { |
| 92 | + if commit, err := profileGitRepo.GetBranchCommit(profileDbRepo.DefaultBranch); err != nil { |
| 93 | + log.Error("FindUserProfileReadme failed to GetBranchCommit: %v", err) |
| 94 | + } else { |
| 95 | + profileReadmeBlob, _ = commit.GetBlobByPath("README.md") |
| 96 | + } |
32 | 97 | }
|
33 |
| - blob, err := commit.GetBlobByPath("README.md") |
34 |
| - if err == nil && blob != nil { |
35 |
| - ctx.Data["ProfileReadme"] = true |
| 98 | + } |
| 99 | + return profileGitRepo, profileReadmeBlob, func() { |
| 100 | + if profileGitRepo != nil { |
| 101 | + _ = profileGitRepo.Close() |
36 | 102 | }
|
37 | 103 | }
|
38 | 104 | }
|
| 105 | + |
| 106 | +func RenderUserHeader(ctx *context.Context) { |
| 107 | + prepareContextForCommonProfile(ctx) |
| 108 | + |
| 109 | + _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx) |
| 110 | + defer profileClose() |
| 111 | + ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil |
| 112 | +} |
0 commit comments