Move some variables out of model (#17909)

* Move some variables out of model

There were some stuff that's not necessary to be in model
and can just remain unexported variables.

```release-note
NONE
```

* Add license

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2021-07-13 19:26:20 +05:30
коммит произвёл Claudio Costa
родитель 97ccf0bdf6
Коммит f49b5dc440
6 изменённых файлов: 51 добавлений и 59 удалений

Просмотреть файл

@@ -1,47 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"regexp"
"strings"
)
var atMentionRegexp = regexp.MustCompile(`\B@[[:alnum:]][[:alnum:]\.\-_:]*`)
const usernameSpecialChars = ".-_"
// PossibleAtMentions returns all substrings in message that look like valid @
// mentions.
func PossibleAtMentions(message string) []string {
var names []string
if !strings.Contains(message, "@") {
return names
}
alreadyMentioned := make(map[string]bool)
for _, match := range atMentionRegexp.FindAllString(message, -1) {
name := NormalizeUsername(match[1:])
if !alreadyMentioned[name] && IsValidUsernameAllowRemote(name) {
names = append(names, name)
alreadyMentioned[name] = true
}
}
return names
}
// TrimUsernameSpecialChar tries to remove the last character from word if it
// is a special character for usernames (dot, dash or underscore). If not, it
// returns the same string.
func TrimUsernameSpecialChar(word string) (string, bool) {
len := len(word)
if len > 0 && strings.LastIndexAny(word, usernameSpecialChars) == (len-1) {
return word[:len-1], true
}
return word, false
}

Просмотреть файл

@@ -1,84 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestPossibleAtMentions(t *testing.T) {
fixture := []struct {
message string
expected []string
}{
{
"",
[]string{},
},
{
"@user",
[]string{"user"},
},
{
"@user-with_special.chars @multiple.-_chars",
[]string{"user-with_special.chars", "multiple.-_chars"},
},
{
"@repeated @user @repeated",
[]string{"repeated", "user"},
},
{
"@user1 @user2 @user3",
[]string{"user1", "user2", "user3"},
},
{
"@李",
[]string{},
},
{
"@withfinaldot. @withfinaldash- @withfinalunderscore_",
[]string{
"withfinaldot.",
"withfinaldash-",
"withfinalunderscore_",
},
},
}
for _, data := range fixture {
actual := PossibleAtMentions(data.message)
require.ElementsMatch(t, actual, data.expected)
}
}
func TestTrimUsernameSpecialChar(t *testing.T) {
fixture := []struct {
word string
expectedString string
expectedBool bool
}{
{"user...", "user..", true},
{"user..", "user.", true},
{"user.", "user", true},
{"user--", "user-", true},
{"user-", "user", true},
{"user_.-", "user_.", true},
{"user_.", "user_", true},
{"user_", "user", true},
{"user", "user", false},
{"user.with-inner_chars", "user.with.inner.chars", false},
}
for _, data := range fixture {
actualString, actualBool := TrimUsernameSpecialChar(data.word)
require.Equal(t, actualBool, data.expectedBool)
if actualBool {
require.Equal(t, actualString, data.expectedString)
} else {
require.Equal(t, actualString, data.word)
}
}
}

Просмотреть файл

@@ -18,8 +18,6 @@ const (
var EmojiPattern = regexp.MustCompile(`:[a-zA-Z0-9_+-]+:`)
var ReverseSystemEmojisMap = makeReverseEmojiMap()
type Emoji struct {
Id string `json:"id"`
CreateAt int64 `json:"create_at"`
@@ -51,8 +49,10 @@ func makeReverseEmojiMap() map[string][]string {
return reverseEmojiMap
}
var reverseSystemEmojisMap = makeReverseEmojiMap()
func GetEmojiNameFromUnicode(unicode string) (emojiName string, count int) {
if emojiNames, found := ReverseSystemEmojisMap[unicode]; found {
if emojiNames, found := reverseSystemEmojisMap[unicode]; found {
return emojiNames[0], len(emojiNames)
}

Просмотреть файл

@@ -69,8 +69,6 @@ const (
PostPropsGroupHighlightDisabled = "disable_group_highlight"
)
var AtMentionPattern = regexp.MustCompile(`\B@`)
type Post struct {
Id string `json:"id"`
CreateAt int64 `json:"create_at"`