MM-55208 allow search prefixes (#28146)
* Ignore at and tilde for username and channel searches. * allow using tilde in search suggestions --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
5d8bf5f97b
Коммит
41bca03270
@@ -1467,9 +1467,11 @@ func (a *App) deletePostFiles(c request.CTX, postID string) {
|
||||
}
|
||||
|
||||
func (a *App) parseAndFetchChannelIdByNameFromInFilter(c request.CTX, channelName, userID, teamID string, includeDeleted bool) (*model.Channel, error) {
|
||||
if strings.HasPrefix(channelName, "@") && strings.Contains(channelName, ",") {
|
||||
cleanChannelName := strings.TrimLeft(channelName, "~")
|
||||
|
||||
if strings.HasPrefix(cleanChannelName, "@") && strings.Contains(cleanChannelName, ",") {
|
||||
var userIDs []string
|
||||
users, err := a.GetUsersByUsernames(strings.Split(channelName[1:], ","), false, nil)
|
||||
users, err := a.GetUsersByUsernames(strings.Split(cleanChannelName[1:], ","), false, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1484,8 +1486,8 @@ func (a *App) parseAndFetchChannelIdByNameFromInFilter(c request.CTX, channelNam
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
if strings.HasPrefix(channelName, "@") && !strings.Contains(channelName, ",") {
|
||||
user, err := a.GetUserByUsername(channelName[1:])
|
||||
if strings.HasPrefix(cleanChannelName, "@") && !strings.Contains(cleanChannelName, ",") {
|
||||
user, err := a.GetUserByUsername(cleanChannelName[1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1496,7 +1498,7 @@ func (a *App) parseAndFetchChannelIdByNameFromInFilter(c request.CTX, channelNam
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
channel, err := a.GetChannelByName(c, channelName, teamID, includeDeleted)
|
||||
channel, err := a.GetChannelByName(c, cleanChannelName, teamID, includeDeleted)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1556,7 +1558,7 @@ func (a *App) convertChannelNamesToChannelIds(c request.CTX, channels []string,
|
||||
|
||||
func (a *App) convertUserNameToUserIds(c request.CTX, usernames []string) []string {
|
||||
for idx, username := range usernames {
|
||||
user, err := a.GetUserByUsername(username)
|
||||
user, err := a.GetUserByUsername(strings.TrimLeft(username, "@"))
|
||||
if err != nil {
|
||||
c.Logger().Warn("error getting user by username", mlog.String("user_name", username), mlog.Err(err))
|
||||
continue
|
||||
|
||||
@@ -1767,6 +1767,48 @@ func TestSearchPostsForUser(t *testing.T) {
|
||||
assert.Equal(t, []string{}, results.Order)
|
||||
es.AssertExpectations(t)
|
||||
})
|
||||
|
||||
t.Run("should return the same results if there is a tilde in the channel name", func(t *testing.T) {
|
||||
th, _ := setup(t, false)
|
||||
defer th.TearDown()
|
||||
|
||||
page := 0
|
||||
|
||||
searchQueryWithPrefix := fmt.Sprintf("in:~%s %s", th.BasicChannel.Name, searchTerm)
|
||||
|
||||
resultsWithPrefix, err := th.App.SearchPostsForUser(th.Context, searchQueryWithPrefix, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage)
|
||||
assert.Nil(t, err)
|
||||
assert.Greater(t, len(resultsWithPrefix.PostList.Posts), 0, "searching using a tilde in front of a channel should return results")
|
||||
searchQueryWithoutPrefix := fmt.Sprintf("in:%s %s", th.BasicChannel.Name, searchTerm)
|
||||
|
||||
resultsWithoutPrefix, err := th.App.SearchPostsForUser(th.Context, searchQueryWithoutPrefix, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, len(resultsWithPrefix.Posts), len(resultsWithoutPrefix.Posts), "searching using a tilde in front of a channel should return the same number of results")
|
||||
for k, v := range resultsWithPrefix.Posts {
|
||||
assert.Equal(t, v, resultsWithoutPrefix.Posts[k], "post at %s was different", k)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should return the same results if there is an 'at' in the user", func(t *testing.T) {
|
||||
th, _ := setup(t, false)
|
||||
defer th.TearDown()
|
||||
|
||||
page := 0
|
||||
|
||||
searchQueryWithPrefix := fmt.Sprintf("from:@%s %s", th.BasicUser.Username, searchTerm)
|
||||
|
||||
resultsWithPrefix, err := th.App.SearchPostsForUser(th.Context, searchQueryWithPrefix, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage)
|
||||
assert.Nil(t, err)
|
||||
assert.Greater(t, len(resultsWithPrefix.PostList.Posts), 0, "searching using a 'at' symbol in front of a channel should return results")
|
||||
searchQueryWithoutPrefix := fmt.Sprintf("from:@%s %s", th.BasicUser.Username, searchTerm)
|
||||
|
||||
resultsWithoutPrefix, err := th.App.SearchPostsForUser(th.Context, searchQueryWithoutPrefix, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, len(resultsWithPrefix.Posts), len(resultsWithoutPrefix.Posts), "searching using an 'at' symbol in front of a channel should return the same number of results")
|
||||
for k, v := range resultsWithPrefix.Posts {
|
||||
assert.Equal(t, v, resultsWithoutPrefix.Posts[k], "post at %s was different", k)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCountMentionsFromPost(t *testing.T) {
|
||||
|
||||
@@ -49,7 +49,10 @@ export default class SearchChannelProvider extends Provider {
|
||||
if (isAtSearch) {
|
||||
channelPrefix = channelPrefix.replace(/^@/, '');
|
||||
}
|
||||
|
||||
const isTildeSearch = channelPrefix.startsWith('~');
|
||||
if (isTildeSearch) {
|
||||
channelPrefix = channelPrefix.replace(/^~/, '');
|
||||
}
|
||||
this.startNewRequest(channelPrefix);
|
||||
|
||||
this.autocompleteChannelsForSearch(
|
||||
|
||||
Ссылка в новой задаче
Block a user