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 удалений

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

@@ -10,6 +10,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"sync"
"unicode"
@@ -23,8 +24,11 @@ import (
const (
CmdCustomStatusTrigger = "status"
usernameSpecialChars = ".-_"
)
var atMentionRegexp = regexp.MustCompile(`\B@[[:alnum:]][[:alnum:]\.\-_:]*`)
type CommandProvider interface {
GetTrigger() string
GetCommand(a *App, T i18n.TranslateFunc) *model.Command
@@ -233,7 +237,7 @@ func (a *App) MentionsToTeamMembers(message, teamID string) model.UserMentionMap
Id string
}
possibleMentions := model.PossibleAtMentions(message)
possibleMentions := possibleAtMentions(message)
mentionChan := make(chan *mentionMapItem, len(possibleMentions))
var wg sync.WaitGroup
@@ -252,8 +256,8 @@ func (a *App) MentionsToTeamMembers(message, teamID string) model.UserMentionMap
// If it's a http.StatusNotFound error, check for usernames in substrings
// without trailing punctuation
if nErr != nil {
trimmed, ok := model.TrimUsernameSpecialChar(mention)
for ; ok; trimmed, ok = model.TrimUsernameSpecialChar(trimmed) {
trimmed, ok := trimUsernameSpecialChar(mention)
for ; ok; trimmed, ok = trimUsernameSpecialChar(trimmed) {
userFromTrimmed, nErr := a.Srv().Store.User().GetByUsername(trimmed)
if nErr != nil && !errors.As(nErr, &nfErr) {
return
@@ -770,3 +774,37 @@ func (a *App) DeleteCommand(commandID string) *model.AppError {
return nil
}
// 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 := model.NormalizeUsername(match[1:])
if !alreadyMentioned[name] && model.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,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
package app
import (
"testing"
@@ -49,7 +49,7 @@ func TestPossibleAtMentions(t *testing.T) {
}
for _, data := range fixture {
actual := PossibleAtMentions(data.message)
actual := possibleAtMentions(data.message)
require.ElementsMatch(t, actual, data.expected)
}
}
@@ -73,7 +73,7 @@ func TestTrimUsernameSpecialChar(t *testing.T) {
}
for _, data := range fixture {
actualString, actualBool := TrimUsernameSpecialChar(data.word)
actualString, actualBool := trimUsernameSpecialChar(data.word)
require.Equal(t, actualBool, data.expectedBool)
if actualBool {
require.Equal(t, actualString, data.expectedString)

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

@@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"net/http"
"regexp"
"strings"
"sync"
"time"
@@ -29,6 +30,8 @@ const (
PageDefault = 0
)
var atMentionPattern = regexp.MustCompile(`\B@`)
func (a *App) CreatePostAsUser(c *request.Context, post *model.Post, currentSessionId string, setOnline bool) (*model.Post, *model.AppError) {
// Check that channel has not been deleted
channel, errCh := a.Srv().Store.Channel().Get(post.ChannelId, true)
@@ -442,7 +445,7 @@ func (a *App) FillInPostProps(post *model.Post, channel *model.Channel) *model.A
post.DelProp("channel_mentions")
}
matched := model.AtMentionPattern.MatchString(post.Message)
matched := atMentionPattern.MatchString(post.Message)
if a.Srv().License() != nil && *a.Srv().License().Features.LDAPGroups && matched && !a.HasPermissionToChannel(post.UserId, post.ChannelId, model.PermissionUseGroupMentions) {
post.AddProp(model.PostPropsGroupHighlightDisabled, true)
}

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

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

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

@@ -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"`