diff --git a/routers/web/repo/competence.go b/routers/web/repo/competence.go index 4ad4f175a2..274f2a807d 100644 --- a/routers/web/repo/competence.go +++ b/routers/web/repo/competence.go @@ -1,13 +1,10 @@ package repo import ( - repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/markup" - "code.gitea.io/gitea/modules/markup/markdown" + "code.gitea.io/gitea/services/trust_props" "net/http" - "regexp" ) const ( @@ -35,34 +32,14 @@ func ToggleCompetences(ctx *context.Context) { return } - repo := ctx.Repo.Repository - toggledContent := ctx.FormString("content") - - content, err := markdown.RenderString(&markup.RenderContext{ - URLPrefix: ctx.FormString("context"), // FIXME: <- IS THIS SAFE ? - Metas: ctx.Repo.Repository.ComposeMetas(), - GitRepo: ctx.Repo.GitRepo, - Ctx: ctx, - }, toggledContent) - if err != nil { - ctx.ServerError("RenderString", err) - return - } - - repo.Competences, err = trimSearchLinks(toggledContent) - if err != nil { - ctx.ServerError("trimSearchLinks", err) - return - } - - err = repo_model.UpdateRepositoryCols(repo, "competences") + toggledContent, err := trust_props.SaveRepoAndRenderContent(ctx, "competences") if err != nil { ctx.ServerError("ToggleCompetences", err) return } responseBody := map[string]interface{}{ - "content": content, + "content": toggledContent, } ctx.JSON( @@ -70,12 +47,3 @@ func ToggleCompetences(ctx *context.Context) { responseBody, ) } - -func trimSearchLinks(content string) (string, error) { - regExp, err := regexp.Compile(`\s*\[найти\]\(.+\s*\)`) - if err != nil { - return "", err - } - trimmedSearchLink := regExp.ReplaceAllString(content, "") - return trimmedSearchLink, nil -} diff --git a/routers/web/repo/resource.go b/routers/web/repo/resource.go index a5ca592219..1bfe4998d3 100644 --- a/routers/web/repo/resource.go +++ b/routers/web/repo/resource.go @@ -4,6 +4,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/routers/web/user" + "code.gitea.io/gitea/services/trust_props" "fmt" "net/http" "regexp" @@ -29,6 +30,28 @@ func Resources(ctx *context.Context) { ctx.HTML(http.StatusOK, tplResources) } +func ToggleResources(ctx *context.Context) { + if !ctx.IsSigned { + ctx.Error(http.StatusForbidden) + return + } + + toggledContent, err := trust_props.SaveRepoAndRenderContent(ctx, "resources") + if err != nil { + ctx.ServerError("ToggleCompetences", err) + return + } + + responseBody := map[string]interface{}{ + "content": toggledContent, + } + + ctx.JSON( + http.StatusOK, + responseBody, + ) +} + func GetRenderedTrustPropsWithSearchLinks(ctx *context.Context, repo interface{}, fieldName string) (string, string, error) { trustProps := user.GetTextField(repo, fieldName) diff --git a/routers/web/web.go b/routers/web/web.go index bca09e950c..d5451e853a 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -891,6 +891,7 @@ func RegisterRoutes(m *web.Route) { m.Get("/{type:issues|pulls}", repo.Issues) m.Get("/issues_tree", repo.IssuesTree) m.Get("/resources", repo.Resources) + m.Post("/resources", repo.ToggleResources) m.Get("/competences", repo.Competences) m.Post("/competences", repo.ToggleCompetences) m.Get("/{type:issues|pulls}/{index}", repo.ViewIssue) diff --git a/services/trust_props/trust_props.go b/services/trust_props/trust_props.go new file mode 100644 index 0000000000..9f635f4b97 --- /dev/null +++ b/services/trust_props/trust_props.go @@ -0,0 +1,58 @@ +package trust_props + +import ( + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/markup" + "code.gitea.io/gitea/modules/markup/markdown" + "reflect" + "regexp" + "strings" +) + +func SaveRepoAndRenderContent(ctx *context.Context, colName string) (string, error) { + repo := ctx.Repo.Repository + toggledContent := ctx.FormString("content") + + content, err := markdown.RenderString(&markup.RenderContext{ + URLPrefix: ctx.FormString("context"), // FIXME: <- IS THIS SAFE ? + Metas: ctx.Repo.Repository.ComposeMetas(), + GitRepo: ctx.Repo.GitRepo, + Ctx: ctx, + }, toggledContent) + if err != nil { + return "", err + } + + trimmedToggledContent, err := trimSearchLinks(toggledContent) + if err != nil { + return "", err + } + fieldName := upCase(colName) + reflectedRepo := reflect.ValueOf(repo) + reflectedRepo.Elem().FieldByName(fieldName).SetString(trimmedToggledContent) + err = repo_model.UpdateRepositoryCols(repo, colName) + if err != nil { + return "", err + } + + return content, nil +} + +func trimSearchLinks(content string) (string, error) { + regExp, err := regexp.Compile(`\s*\[найти\]\(.+\s*\)`) + if err != nil { + return "", err + } + trimmedSearchLink := regExp.ReplaceAllString(content, "") + return trimmedSearchLink, nil +} + +func upCase(str string) string { + ch := rune(str[0]) + ch -= 'a' - 'A' + var upCasedSB strings.Builder + upCasedSB.WriteRune(ch) + upCasedSB.WriteString(str[1:]) + return upCasedSB.String() +} diff --git a/templates/repo/resources/list.tmpl b/templates/repo/resources/list.tmpl index 125cb62d85..6ab050f0af 100644 --- a/templates/repo/resources/list.tmpl +++ b/templates/repo/resources/list.tmpl @@ -3,9 +3,11 @@ {{template "repo/header" .}}