From f49b5dc44063561679b9369be7ee39618730461e Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Tue, 13 Jul 2021 19:26:20 +0530 Subject: [PATCH] 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 ``` --- app/command.go | 44 +++++++++++++++-- .../command_test.go | 6 +-- app/post.go | 5 +- model/at_mentions.go | 47 ------------------- model/emoji.go | 6 +-- model/post.go | 2 - 6 files changed, 51 insertions(+), 59 deletions(-) rename model/at_mentions_test.go => app/command_test.go (92%) delete mode 100644 model/at_mentions.go diff --git a/app/command.go b/app/command.go index 6b208bb818..dd5eb5ce68 100644 --- a/app/command.go +++ b/app/command.go @@ -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 +} diff --git a/model/at_mentions_test.go b/app/command_test.go similarity index 92% rename from model/at_mentions_test.go rename to app/command_test.go index 39c8251984..f4734aee71 100644 --- a/model/at_mentions_test.go +++ b/app/command_test.go @@ -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) diff --git a/app/post.go b/app/post.go index 8831d0b849..c97737f859 100644 --- a/app/post.go +++ b/app/post.go @@ -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) } diff --git a/model/at_mentions.go b/model/at_mentions.go deleted file mode 100644 index b5bfc0c70c..0000000000 --- a/model/at_mentions.go +++ /dev/null @@ -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 -} diff --git a/model/emoji.go b/model/emoji.go index 3e30237366..380d88d4a6 100644 --- a/model/emoji.go +++ b/model/emoji.go @@ -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) } diff --git a/model/post.go b/model/post.go index 9ac73e0423..79b78ef7d5 100644 --- a/model/post.go +++ b/model/post.go @@ -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"`