From c79bea9808139bc66ef52cf90dc9619581ce9f81 Mon Sep 17 00:00:00 2001 From: Artur Galyamov Date: Mon, 22 Aug 2022 16:51:54 +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=D0=BF=D0=BE=D0=BB=D1=8F=20=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=BF=D0=B5=D1=82=D0=B5=D0=BD=D1=86=D0=B8=D0=B8,=20=D1=80?= =?UTF-8?q?=D0=B5=D1=81=D1=83=D1=80=D1=81=D1=8B=20=D0=B8=20=D0=B8=D0=BD?= =?UTF-8?q?=D1=82=D0=B5=D1=80=D0=B5=D1=81=D1=8B=20=D0=B2=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=84=D0=B8=D0=BB=D0=B5=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F=20#9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit решено использовать рефлексию, для того, чтобы код был более читабельным. Хотя для MVP это избыточно. --- routers/web/user/profile.go | 62 ++++++++++++++++++++++++++++++++++++--------- templates/user/profile.tmpl | 15 +++++++++++ 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 40fc44ed14..2ad333a6af 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -9,6 +9,7 @@ import ( "fmt" "net/http" "path" + "reflect" "strings" "code.gitea.io/gitea/models" @@ -158,18 +159,30 @@ func Profile(ctx *context.Context) { ctx.Data["HeatmapData"] = data } - if len(ctxUser.Description) != 0 { - content, err := markdown.RenderString(&markup.RenderContext{ - URLPrefix: ctx.Repo.RepoLink, - Metas: map[string]string{"mode": "document"}, - GitRepo: ctx.Repo.GitRepo, - Ctx: ctx, - }, ctxUser.Description) - if err != nil { - ctx.ServerError("RenderString", err) - return - } - ctx.Data["RenderedDescription"] = content + var renderErr error + + renderErr = getRenderedTextField(ctx, ctxUser, "Description") + if renderErr != nil { + ctx.ServerError("RenderString", renderErr) + return + } + + renderErr = getRenderedTextField(ctx, ctxUser, "Competences") + if renderErr != nil { + ctx.ServerError("RenderString", renderErr) + return + } + + renderErr = getRenderedTextField(ctx, ctxUser, "Resources") + if renderErr != nil { + ctx.ServerError("RenderString", renderErr) + return + } + + renderErr = getRenderedTextField(ctx, ctxUser, "Interests") + if renderErr != nil { + ctx.ServerError("RenderString", renderErr) + return } showPrivate := ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == ctxUser.ID) @@ -377,3 +390,28 @@ func Action(ctx *context.Context) { // FIXME: We should check this URL and make sure that it's a valid Gitea URL ctx.RedirectToFirst(ctx.FormString("redirect_to"), u.HomeLink()) } + +func getTextField(user *user_model.User, 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 { + var err error = nil + var content string + fieldVal := getTextField(ctxUser, fieldName) + if len(fieldVal) != 0 { + content, err = markdown.RenderString(&markup.RenderContext{ + URLPrefix: ctx.Repo.RepoLink, + Metas: map[string]string{"mode": "document"}, + GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, + }, fieldVal) + if err == nil { + renderedFieldName := fmt.Sprintf("Rendered%s", fieldName) + ctx.Data[renderedFieldName] = content + } + } + return err +} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index d14a86fd17..0ce7c9af40 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -39,6 +39,21 @@
{{$.RenderedDescription|Str2html}}
{{end}} + {{if $.RenderedCompetences}} +
  • +
    {{$.RenderedCompetences|Str2html}}
    +
  • + {{end}} + {{if $.RenderedResources}} +
  • +
    {{$.RenderedResources|Str2html}}
    +
  • + {{end}} + {{if $.RenderedInterests}} +
  • +
    {{$.RenderedInterests|Str2html}}
    +
  • + {{end}} {{range .OpenIDs}} {{if .Show}}