Deleted RemoveDuplicateTopics function

Signed-off-by: Alexey Terentyev <axifnx@gmail.com>
This commit is contained in:
Alexey Terentyev 2018-06-17 14:48:20 +03:00
parent 6579ddf9d8
commit 630ca5901c
No known key found for this signature in database
GPG Key ID: 60D6C550AEEBB467
4 changed files with 18 additions and 42 deletions

View File

@ -59,22 +59,6 @@ func TopicValidator(topic string) bool {
return len(topic) <= 35 && topicPattern.MatchString(topic)
}
// RemoveDuplicateTopics remove duplicates from topics slice
func RemoveDuplicateTopics(topics []string) []string {
// Map to record duplicates
saved := make(map[string]struct{}, len(topics))
i := 0
for _, v := range topics {
v = strings.TrimSpace(strings.ToLower(v))
if _, ok := saved[v]; !ok {
saved[v] = struct{}{}
topics[i] = v
i++
}
}
return topics[:i]
}
// GetTopicByName retrieves topic by name
func GetTopicByName(name string) (*Topic, error) {
var topic Topic

View File

@ -5,7 +5,6 @@
package models
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
@ -66,17 +65,3 @@ func TestTopicValidator(t *testing.T) {
assert.False(t, TopicValidator("#fifth,test;topic"))
assert.False(t, TopicValidator("#sixth-go+project.topic+with+excess_length"))
}
func TestRemoveDuplicateTopics(t *testing.T) {
topics := []string{"first", "second", "eleventh", "third", "fourth", "fifth", "sixth", "second", "seventh",
"eleventh", "first", "eighth", "ninth", "sixth", "tenth", "eleventh"}
expectedSlice := []string{"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh"}
topics = RemoveDuplicateTopics(topics)
assert.Len(t, topics, 11)
sort.Strings(topics)
sort.Strings(expectedSlice)
assert.EqualValues(t, expectedSlice, topics)
}

View File

@ -2336,8 +2336,9 @@ function initTopicbar() {
}).done(function() {
editDiv.hide();
viewDiv.show();
})
})
}).fail(function(xhr) {
alert(xhr.responseJSON.message)
});
$('#topic_edit .dropdown').dropdown({
allowAdditions: true,

View File

@ -12,7 +12,7 @@ import (
"code.gitea.io/gitea/modules/log"
)
// TopicPost response for creating repository
// TopicsPost response for creating repository
func TopicsPost(ctx *context.Context) {
if ctx.User == nil {
ctx.JSON(403, map[string]interface{}{
@ -27,7 +27,20 @@ func TopicsPost(ctx *context.Context) {
topics = strings.Split(topicsStr, ",")
}
topics = models.RemoveDuplicateTopics(topics)
invalidTopics := make([]string, 0)
i := 0
for _, topic := range topics {
topic = strings.TrimSpace(strings.ToLower(topic))
// ignore empty string
if len(topic) > 0 {
topics[i] = topic
i++
}
if !models.TopicValidator(topic) {
invalidTopics = append(invalidTopics, topic)
}
}
topics = topics[:i]
if len(topics) > 25 {
log.Error(2, "Incorrect number of topics(max 25)")
@ -38,13 +51,6 @@ func TopicsPost(ctx *context.Context) {
return
}
var invalidTopics = make([]string, 0)
for _, topic := range topics {
if !models.TopicValidator(topic) {
invalidTopics = append(invalidTopics, topic)
}
}
if len(invalidTopics) > 0 {
log.Error(2, "Invalid topics: %v", invalidTopics)
ctx.JSON(422, map[string]interface{}{