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 ```
Этот коммит содержится в:
коммит произвёл
Claudio Costa
родитель
97ccf0bdf6
Коммит
f49b5dc440
@@ -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
|
||||
}
|
||||
|
||||
84
app/command_test.go
Обычный файл
84
app/command_test.go
Обычный файл
@@ -0,0 +1,84 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package app
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user