From 5388d7b1de190df08d954ec882152c70b682e34c Mon Sep 17 00:00:00 2001 From: Artur Galyamov Date: Fri, 2 Sep 2022 20:32:44 +0500 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=B2=D0=BE=D0=B4=D1=8F=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D1=80=D0=B5=D1=81=D1=83=D1=80=D1=81=D1=8B=20?= =?UTF-8?q?=D0=B8=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=B5=D1=81=D1=8B=20?= =?UTF-8?q?=D1=82=D0=B0=D0=BC=20=D0=B3=D0=B4=D0=B5=20=D0=BD=D1=83=D0=B6?= =?UTF-8?q?=D0=BD=D0=BE=20#106?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GetRenderedTextField не вынесен в отдельный сервис, т.к. получается циклический импорт. Из-за того, что modules.Context связан с более нижележащим уровнем modeuls. --- routers/web/explore/user.go | 29 +++++++++++++++++------------ routers/web/user/profile.go | 39 +++++++++++++++++++++++---------------- templates/explore/interests.tmpl | 6 +++--- templates/explore/resources.tmpl | 6 +++--- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index ccfce1a3ff..cecb4578eb 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -6,8 +6,7 @@ package explore import ( "bytes" - "code.gitea.io/gitea/modules/markup" - "code.gitea.io/gitea/modules/markup/markdown" + "code.gitea.io/gitea/routers/web/user" "net/http" "code.gitea.io/gitea/models/db" @@ -80,20 +79,26 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, ctx.Data["Keyword"] = opts.Keyword ctx.Data["Total"] = count ctx.Data["Users"] = users - var renderedCompetences = make(map[int64]string) - for _, user := range users { - renderedCompetences[user.ID], err = markdown.RenderString(&markup.RenderContext{ - URLPrefix: ctx.Repo.RepoLink, - Metas: map[string]string{"mode": "document"}, - GitRepo: ctx.Repo.GitRepo, - Ctx: ctx, - }, user.Competences) + var renderedContent = make(map[int64]string) + var contentFieldName string + switch opts.Kind { + case user_model.ByCompetence: + contentFieldName = "Competences" + case user_model.ByResource: + contentFieldName = "Resources" + case user_model.ByInterest: + contentFieldName = "Interests" + default: + contentFieldName = "Description" + } + for _, curUser := range users { + renderedContent[curUser.ID], err = user.GetRenderedTextField(ctx, curUser, contentFieldName) if err != nil { - ctx.ServerError("RenderCompetences", err) + ctx.ServerError("RenderContent", err) return } } - ctx.Data["RenderedCompetences"] = renderedCompetences + ctx.Data["RenderedContent"] = renderedContent ctx.Data["UsersTwoFaStatus"] = user_model.UserList(users).GetTwoFaStatus() ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 2ad333a6af..90fafb871e 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -159,28 +159,39 @@ func Profile(ctx *context.Context) { ctx.Data["HeatmapData"] = data } - var renderErr error + var ( + renderErr error + content string + ) - renderErr = getRenderedTextField(ctx, ctxUser, "Description") - if renderErr != nil { + content, renderErr = GetRenderedTextField(ctx, ctxUser, "Description") + if renderErr == nil { + ctx.Data["RenderedDescription"] = content + } else { ctx.ServerError("RenderString", renderErr) return } - renderErr = getRenderedTextField(ctx, ctxUser, "Competences") - if renderErr != nil { + content, renderErr = GetRenderedTextField(ctx, ctxUser, "Competences") + if renderErr == nil { + ctx.Data["RenderedCompetences"] = content + } else { ctx.ServerError("RenderString", renderErr) return } - renderErr = getRenderedTextField(ctx, ctxUser, "Resources") - if renderErr != nil { + content, renderErr = GetRenderedTextField(ctx, ctxUser, "Resources") + if renderErr == nil { + ctx.Data["RenderedResources"] = content + } else { ctx.ServerError("RenderString", renderErr) return } - renderErr = getRenderedTextField(ctx, ctxUser, "Interests") - if renderErr != nil { + content, renderErr = GetRenderedTextField(ctx, ctxUser, "Interests") + if renderErr == nil { + ctx.Data["RenderedInterests"] = content + } else { ctx.ServerError("RenderString", renderErr) return } @@ -391,13 +402,13 @@ func Action(ctx *context.Context) { ctx.RedirectToFirst(ctx.FormString("redirect_to"), u.HomeLink()) } -func getTextField(user *user_model.User, fieldName string) string { +func getTextField(user interface{}, fieldName string) string { reflectedObj := reflect.ValueOf(user) dynamicField := reflect.Indirect(reflectedObj).FieldByName(fieldName) return dynamicField.String() } -func getRenderedTextField(ctx *context.Context, ctxUser *user_model.User, fieldName string) error { +func GetRenderedTextField(ctx *context.Context, ctxUser interface{}, fieldName string) (string, error) { var err error = nil var content string fieldVal := getTextField(ctxUser, fieldName) @@ -408,10 +419,6 @@ func getRenderedTextField(ctx *context.Context, ctxUser *user_model.User, fieldN GitRepo: ctx.Repo.GitRepo, Ctx: ctx, }, fieldVal) - if err == nil { - renderedFieldName := fmt.Sprintf("Rendered%s", fieldName) - ctx.Data[renderedFieldName] = content - } } - return err + return content, err } diff --git a/templates/explore/interests.tmpl b/templates/explore/interests.tmpl index 91fece10f9..6f87437518 100644 --- a/templates/explore/interests.tmpl +++ b/templates/explore/interests.tmpl @@ -20,9 +20,9 @@ {{.Email}} {{end}} {{svg "octicon-clock"}} {{$.i18n.Tr "user.join_on"}} {{.CreatedUnix.FormatShort}} - {{if .Competences}} -

Компетенции:

-
{{(index $.RenderedCompetences .ID)|Str2html}}
+ {{if .Interests}} +

{{$.i18n.Tr "explore.interests"}}:

+
{{(index $.RenderedContent .ID)|Str2html}}
{{end}} diff --git a/templates/explore/resources.tmpl b/templates/explore/resources.tmpl index 91fece10f9..fc0acff672 100644 --- a/templates/explore/resources.tmpl +++ b/templates/explore/resources.tmpl @@ -20,9 +20,9 @@ {{.Email}} {{end}} {{svg "octicon-clock"}} {{$.i18n.Tr "user.join_on"}} {{.CreatedUnix.FormatShort}} - {{if .Competences}} -

Компетенции:

-
{{(index $.RenderedCompetences .ID)|Str2html}}
+ {{if .Resources}} +

{{$.i18n.Tr "explore.resources"}}:

+
{{(index $.RenderedContent .ID)|Str2html}}
{{end}}