MM-21987 Resolve mentions in slash commands (#13762)

* Create infrastructure to manage mentions

Two new files have been added (along with their tests); namely:

- model/at_mentions.go: utilities to parse and manage mentions; for the moment,
it just contains a regex and a couple of functions to parse possible mentions
and to post-process them, but it can be extended in the future.
- model/mention_map.go: it contains two new types (UserMentionMap and
ChannelMentionMap) that both have FromURLValues and ToURLValues. These types
can be used when adding the mentions to the payload of the plugin slash
commands.

* Extend custom commands payload with mentions

Two couples of new fields are added to the payload; namely:

- user_mentions and user_mentions_ids: two aligned arrays of the same length
containing all the different @-mentions found in the command: the i-th element
of user_mentions_ids is the user identifier of the i-th element of
user_mentions.
- channel_mentions and channel_mentions_ids: two aligned arrays of the same
length containing all the different ~-mentions found in the command: the i-th
element of channel_mentions_ids is the channel identifier of the i-th element
of channel_mentions.

* Fix shadowing of variables and redundant return

* Fix shadowing of variable

* Address review comments (HT @lieut-data)

- Improvements in mentionsToTeamMembers and mentionsToPublicChannels:
	- Scope implementation details inside the functions.
	- Improve goroutines synchronization by using a sync.WaitGroup.
	- Retry lookup of username only if the returned error is http.StatusCode,
	  so we can return early if the error is more severe.
- Invert check in PossibleAtMentions to improve readability.
- Make user and channel mention keys private to the module.
- Allow the specification of an empty map of mentions in
(Channel|User)MentionsFromURLValues when both mentions keys are absent.
- Replace custom functions in tests with require.Equal on maps.

* Test functions to parse mentions from messages

* Extend plugin commands payload with mentions

* Add functions to CommandArgs to add mentions

The functions make sure that the maps are initialized before adding any value.

* Address review comments (HT @lieut-data)

- Adds a mlog.Warn to avoid burying the error when the user is not found.
- Improve readability in loop populating the mention map by moving the
initialization of the map closer to the loop and by iterating over the channel
itself, not over its length.

* File was not gofmt-ed with -s

* Close channel when all goroutines are finished

* Again, all code should be checked with gofmt -s

* Refactor code out of a goroutine

This change helps improve the readability of the code and does not affect its
overall performance. Less complexity is always better.

* Close channel and iterate over its range

Adapt mentionsToPublicChannels to have the same structure in the management
of the mentions channel as in mentionsToTeamMembers.

* Adapt mentionsToTeamMembers to new App

Commit 17523fa changed the App structure, making the *Server field
private, which is now accessed through the Srv() function.

Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Alejandro García Montoro
2020-03-11 11:50:12 +01:00
коммит произвёл GitHub
родитель 5d928b4f94
Коммит 2bec92a404
10 изменённых файлов: 892 добавлений и 10 удалений

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

@@ -4,6 +4,7 @@
package app
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
@@ -383,3 +384,174 @@ func TestDoCommandRequest(t *testing.T) {
close(done)
})
}
func TestMentionsToTeamMembers(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
otherTeam := th.CreateTeam()
otherUser := th.CreateUser()
th.LinkUserToTeam(otherUser, otherTeam)
fixture := []struct {
message string
inTeam string
expectedMap model.UserMentionMap
}{
{
fmt.Sprintf(""),
th.BasicTeam.Id,
model.UserMentionMap{},
},
{
fmt.Sprintf("/trigger"),
th.BasicTeam.Id,
model.UserMentionMap{},
},
{
fmt.Sprintf("/trigger 0 mentions"),
th.BasicTeam.Id,
model.UserMentionMap{},
},
{
fmt.Sprintf("/trigger 1 valid user @%s", th.BasicUser.Username),
th.BasicTeam.Id,
model.UserMentionMap{th.BasicUser.Username: th.BasicUser.Id},
},
{
fmt.Sprintf("/trigger 2 valid users @%s @%s",
th.BasicUser.Username, th.BasicUser2.Username,
),
th.BasicTeam.Id,
model.UserMentionMap{
th.BasicUser.Username: th.BasicUser.Id,
th.BasicUser2.Username: th.BasicUser2.Id,
},
},
{
fmt.Sprintf("/trigger 1 user from another team @%s", otherUser.Username),
th.BasicTeam.Id,
model.UserMentionMap{},
},
{
fmt.Sprintf("/trigger 2 valid users + 1 from another team @%s @%s @%s",
th.BasicUser.Username, th.BasicUser2.Username, otherUser.Username,
),
th.BasicTeam.Id,
model.UserMentionMap{
th.BasicUser.Username: th.BasicUser.Id,
th.BasicUser2.Username: th.BasicUser2.Id,
},
},
{
fmt.Sprintf("/trigger a valid channel ~%s", th.BasicChannel.Name),
th.BasicTeam.Id,
model.UserMentionMap{},
},
{
fmt.Sprintf("/trigger channel and mentions ~%s @%s",
th.BasicChannel.Name, th.BasicUser.Username),
th.BasicTeam.Id,
model.UserMentionMap{th.BasicUser.Username: th.BasicUser.Id},
},
{
fmt.Sprintf("/trigger repeated users @%s @%s @%s",
th.BasicUser.Username, th.BasicUser2.Username, th.BasicUser.Username),
th.BasicTeam.Id,
model.UserMentionMap{
th.BasicUser.Username: th.BasicUser.Id,
th.BasicUser2.Username: th.BasicUser2.Id,
},
},
}
for _, data := range fixture {
actualMap := th.App.mentionsToTeamMembers(data.message, data.inTeam)
require.Equal(t, actualMap, data.expectedMap)
}
}
func TestMentionsToPublicChannels(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
otherPublicChannel := th.CreateChannel(th.BasicTeam)
privateChannel := th.CreatePrivateChannel(th.BasicTeam)
fixture := []struct {
message string
inTeam string
expectedMap model.ChannelMentionMap
}{
{
fmt.Sprintf(""),
th.BasicTeam.Id,
model.ChannelMentionMap{},
},
{
fmt.Sprintf("/trigger"),
th.BasicTeam.Id,
model.ChannelMentionMap{},
},
{
fmt.Sprintf("/trigger 0 mentions"),
th.BasicTeam.Id,
model.ChannelMentionMap{},
},
{
fmt.Sprintf("/trigger 1 public channel ~%s", th.BasicChannel.Name),
th.BasicTeam.Id,
model.ChannelMentionMap{th.BasicChannel.Name: th.BasicChannel.Id},
},
{
fmt.Sprintf("/trigger 2 public channels ~%s ~%s",
th.BasicChannel.Name, otherPublicChannel.Name,
),
th.BasicTeam.Id,
model.ChannelMentionMap{
th.BasicChannel.Name: th.BasicChannel.Id,
otherPublicChannel.Name: otherPublicChannel.Id,
},
},
{
fmt.Sprintf("/trigger 1 private channel ~%s", privateChannel.Name),
th.BasicTeam.Id,
model.ChannelMentionMap{},
},
{
fmt.Sprintf("/trigger 2 public channel + 1 private ~%s ~%s ~%s",
th.BasicChannel.Name, otherPublicChannel.Name, privateChannel.Name,
),
th.BasicTeam.Id,
model.ChannelMentionMap{
th.BasicChannel.Name: th.BasicChannel.Id,
otherPublicChannel.Name: otherPublicChannel.Id,
},
},
{
fmt.Sprintf("/trigger a valid user @%s", th.BasicUser.Username),
th.BasicTeam.Id,
model.ChannelMentionMap{},
},
{
fmt.Sprintf("/trigger channel and mentions ~%s @%s",
th.BasicChannel.Name, th.BasicUser.Username),
th.BasicTeam.Id,
model.ChannelMentionMap{th.BasicChannel.Name: th.BasicChannel.Id},
},
{
fmt.Sprintf("/trigger repeated channels ~%s ~%s ~%s",
th.BasicChannel.Name, otherPublicChannel.Name, th.BasicChannel.Name),
th.BasicTeam.Id,
model.ChannelMentionMap{
th.BasicChannel.Name: th.BasicChannel.Id,
otherPublicChannel.Name: otherPublicChannel.Id,
},
},
}
for _, data := range fixture {
actualMap := th.App.mentionsToPublicChannels(data.message, data.inTeam)
require.Equal(t, actualMap, data.expectedMap)
}
}