Merge pull request '[GITEA] Fix wrong link in user and organization profile when using relative url' (#2118) from gusted/forgejo-bp-gt-28617 into v1.21/forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2118
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
This commit is contained in:
Gusted 2024-01-09 20:10:44 +00:00
commit bd3e5109ba
2 changed files with 17 additions and 9 deletions

View file

@ -85,7 +85,7 @@ func PrepareContextForProfileBigAvatar(ctx *context.Context) {
} }
} }
func FindUserProfileReadme(ctx *context.Context) (profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) { func FindUserProfileReadme(ctx *context.Context) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) {
profileDbRepo, err := repo_model.GetRepositoryByName(ctx.ContextUser.ID, ".profile") profileDbRepo, err := repo_model.GetRepositoryByName(ctx.ContextUser.ID, ".profile")
if err == nil && !profileDbRepo.IsEmpty && !profileDbRepo.IsPrivate { if err == nil && !profileDbRepo.IsEmpty && !profileDbRepo.IsPrivate {
if profileGitRepo, err = git.OpenRepository(ctx, profileDbRepo.RepoPath()); err != nil { if profileGitRepo, err = git.OpenRepository(ctx, profileDbRepo.RepoPath()); err != nil {
@ -98,7 +98,7 @@ func FindUserProfileReadme(ctx *context.Context) (profileGitRepo *git.Repository
} }
} }
} }
return profileGitRepo, profileReadmeBlob, func() { return profileDbRepo, profileGitRepo, profileReadmeBlob, func() {
if profileGitRepo != nil { if profileGitRepo != nil {
_ = profileGitRepo.Close() _ = profileGitRepo.Close()
} }
@ -108,7 +108,7 @@ func FindUserProfileReadme(ctx *context.Context) (profileGitRepo *git.Repository
func RenderUserHeader(ctx *context.Context) { func RenderUserHeader(ctx *context.Context) {
prepareContextForCommonProfile(ctx) prepareContextForCommonProfile(ctx)
_, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx) _, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx)
defer profileClose() defer profileClose()
ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil
} }

View file

@ -66,17 +66,17 @@ func userProfile(ctx *context.Context) {
ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data) ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data)
} }
profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx) profileDbRepo, profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx)
defer profileClose() defer profileClose()
showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID) showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
prepareUserProfileTabData(ctx, showPrivate, profileGitRepo, profileReadmeBlob) prepareUserProfileTabData(ctx, showPrivate, profileDbRepo, profileGitRepo, profileReadmeBlob)
// call PrepareContextForProfileBigAvatar later to avoid re-querying the NumFollowers & NumFollowing // call PrepareContextForProfileBigAvatar later to avoid re-querying the NumFollowers & NumFollowing
shared_user.PrepareContextForProfileBigAvatar(ctx) shared_user.PrepareContextForProfileBigAvatar(ctx)
ctx.HTML(http.StatusOK, tplProfile) ctx.HTML(http.StatusOK, tplProfile)
} }
func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileGitRepo *git.Repository, profileReadme *git.Blob) { func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadme *git.Blob) {
// if there is a profile readme, default to "overview" page, otherwise, default to "repositories" page // if there is a profile readme, default to "overview" page, otherwise, default to "repositories" page
// if there is not a profile readme, the overview tab should be treated as the repositories tab // if there is not a profile readme, the overview tab should be treated as the repositories tab
tab := ctx.FormString("tab") tab := ctx.FormString("tab")
@ -235,9 +235,17 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileGi
if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil { if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
log.Error("failed to GetBlobContent: %v", err) log.Error("failed to GetBlobContent: %v", err)
} else { } else {
// Give the URLPrefix to the markdown render for the full link of media element.
// the media link usually be like /[user]/[repoName]/media/branch/[branchName],
// Eg. /Tom/.profile/media/branch/main
// The branch shown on the profile page is the default branch, this need to be in sync with doc, see:
// https://docs.gitea.com/usage/profile-readme
prefix := profileDbRepo.Link() + "/src/branch/" + util.PathEscapeSegments(profileDbRepo.DefaultBranch)
if profileContent, err := markdown.RenderString(&markup.RenderContext{ if profileContent, err := markdown.RenderString(&markup.RenderContext{
Ctx: ctx, Ctx: ctx,
GitRepo: profileGitRepo, GitRepo: profileGitRepo,
URLPrefix: prefix,
Metas: map[string]string{"mode": "document"}, Metas: map[string]string{"mode": "document"},
}, bytes); err != nil { }, bytes); err != nil {
log.Error("failed to RenderString: %v", err) log.Error("failed to RenderString: %v", err)