Browse Source

Реализовано переключение состояния флажков ресурсов #128

заодно отрефакторил, вынес общий код в отдельный служебный слой.
Чтобы избежать дублирования и переиспользовать код.
pull/150/head
Artur Galyamov 2 years ago
parent
commit
6631cf0220
  1. 38
      routers/web/repo/competence.go
  2. 23
      routers/web/repo/resource.go
  3. 1
      routers/web/web.go
  4. 58
      services/trust_props/trust_props.go
  5. 4
      templates/repo/resources/list.tmpl

38
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
}

23
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)

1
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)

58
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()
}

4
templates/repo/resources/list.tmpl

@ -3,9 +3,11 @@
{{template "repo/header" .}}
<div class="ui container">
<h1>{{.i18n.Tr "repo.resources"}}</h1>
<div>
<div class="render-content markup trust-props" data-can-edit="true">
{{$.RenderedResources | Str2html}}
</div>
<div class="raw-content hide">{{$.TransformedTrustProps}}</div>
<div class="edit-content-zone hide" data-update-url="{{$.RepoLink}}/resources" data-context="{{.RepoLink}}"></div>
</div>
</div>
{{template "base/footer" .}}

Loading…
Cancel
Save