code.gitea.io/gitea@v1.21.7/routers/web/repo/topic.go (about) 1 // Copyright 2018 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package repo 5 6 import ( 7 "net/http" 8 "strings" 9 10 repo_model "code.gitea.io/gitea/models/repo" 11 "code.gitea.io/gitea/modules/context" 12 "code.gitea.io/gitea/modules/log" 13 ) 14 15 // TopicsPost response for creating repository 16 func TopicsPost(ctx *context.Context) { 17 if ctx.Doer == nil { 18 ctx.JSON(http.StatusForbidden, map[string]any{ 19 "message": "Only owners could change the topics.", 20 }) 21 return 22 } 23 24 topics := make([]string, 0) 25 topicsStr := ctx.FormTrim("topics") 26 if len(topicsStr) > 0 { 27 topics = strings.Split(topicsStr, ",") 28 } 29 30 validTopics, invalidTopics := repo_model.SanitizeAndValidateTopics(topics) 31 32 if len(validTopics) > 25 { 33 ctx.JSON(http.StatusUnprocessableEntity, map[string]any{ 34 "invalidTopics": nil, 35 "message": ctx.Tr("repo.topic.count_prompt"), 36 }) 37 return 38 } 39 40 if len(invalidTopics) > 0 { 41 ctx.JSON(http.StatusUnprocessableEntity, map[string]any{ 42 "invalidTopics": invalidTopics, 43 "message": ctx.Tr("repo.topic.format_prompt"), 44 }) 45 return 46 } 47 48 err := repo_model.SaveTopics(ctx, ctx.Repo.Repository.ID, validTopics...) 49 if err != nil { 50 log.Error("SaveTopics failed: %v", err) 51 ctx.JSON(http.StatusInternalServerError, map[string]any{ 52 "message": "Save topics failed.", 53 }) 54 return 55 } 56 57 ctx.JSON(http.StatusOK, map[string]any{ 58 "status": "ok", 59 }) 60 }