From 9dc14d63b700ce723e25acd9448a32660adc2e1d Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Tue, 28 May 2019 13:04:12 -0400 Subject: [PATCH] Trim leading and trailing spaces for search terms (#10989) --- app/channel.go | 9 +++++++++ app/post.go | 2 +- app/user.go | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/channel.go b/app/channel.go index 87ac50766e..bdc1ad5bd6 100644 --- a/app/channel.go +++ b/app/channel.go @@ -1761,6 +1761,7 @@ func (a *App) UpdateChannelLastViewedAt(channelIds []string, userId string) *mod func (a *App) AutocompleteChannels(teamId string, term string) (*model.ChannelList, *model.AppError) { includeDeleted := *a.Config().TeamSettings.ExperimentalViewArchivedChannels + term = strings.TrimSpace(term) esInterface := a.Elasticsearch license := a.License() @@ -1797,6 +1798,8 @@ func (a *App) AutocompleteChannels(teamId string, term string) (*model.ChannelLi func (a *App) AutocompleteChannelsForSearch(teamId string, userId string, term string) (*model.ChannelList, *model.AppError) { includeDeleted := *a.Config().TeamSettings.ExperimentalViewArchivedChannels + term = strings.TrimSpace(term) + result := <-a.Srv.Store.Channel().AutocompleteInTeamForSearch(teamId, userId, term, includeDeleted) if result.Err != nil { return nil, result.Err @@ -1814,6 +1817,9 @@ func (a *App) SearchAllChannels(term string, opts model.ChannelSearchOpts) (*mod NotAssociatedToGroup: opts.NotAssociatedToGroup, IncludeDeleted: opts.IncludeDeleted, } + + term = strings.TrimSpace(term) + result := <-a.Srv.Store.Channel().SearchAllChannels(term, storeOpts) if result.Err != nil { return nil, result.Err @@ -1824,6 +1830,8 @@ func (a *App) SearchAllChannels(term string, opts model.ChannelSearchOpts) (*mod func (a *App) SearchChannels(teamId string, term string) (*model.ChannelList, *model.AppError) { includeDeleted := *a.Config().TeamSettings.ExperimentalViewArchivedChannels + term = strings.TrimSpace(term) + result := <-a.Srv.Store.Channel().SearchInTeam(teamId, term, includeDeleted) if result.Err != nil { return nil, result.Err @@ -1832,6 +1840,7 @@ func (a *App) SearchChannels(teamId string, term string) (*model.ChannelList, *m } func (a *App) SearchChannelsUserNotIn(teamId string, userId string, term string) (*model.ChannelList, *model.AppError) { + term = strings.TrimSpace(term) result := <-a.Srv.Store.Channel().SearchMore(userId, teamId, term) if result.Err != nil { return nil, result.Err diff --git a/app/post.go b/app/post.go index 8d639620ad..e056f99b45 100644 --- a/app/post.go +++ b/app/post.go @@ -855,7 +855,7 @@ func (a *App) SearchPostsInTeam(teamId string, paramsList []*model.SearchParams) } func (a *App) SearchPostsInTeamForUser(terms string, userId string, teamId string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) { - paramsList := model.ParseSearchParams(terms, timeZoneOffset) + paramsList := model.ParseSearchParams(strings.TrimSpace(terms), timeZoneOffset) includeDeleted := includeDeletedChannels && *a.Config().TeamSettings.ExperimentalViewArchivedChannels esInterface := a.Elasticsearch diff --git a/app/user.go b/app/user.go index 8beecc0c60..2ed65770de 100644 --- a/app/user.go +++ b/app/user.go @@ -1666,6 +1666,7 @@ func (a *App) SearchUsers(props *model.UserSearch, options *model.UserSearchOpti } func (a *App) SearchUsersInChannel(channelId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { + term = strings.TrimSpace(term) result := <-a.Srv.Store.User().SearchInChannel(channelId, term, options) if result.Err != nil { return nil, result.Err @@ -1680,6 +1681,7 @@ func (a *App) SearchUsersInChannel(channelId string, term string, options *model } func (a *App) SearchUsersNotInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { + term = strings.TrimSpace(term) result := <-a.Srv.Store.User().SearchNotInChannel(teamId, channelId, term, options) if result.Err != nil { return nil, result.Err @@ -1696,6 +1698,8 @@ func (a *App) SearchUsersNotInChannel(teamId string, channelId string, term stri func (a *App) SearchUsersInTeam(teamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { var result store.StoreResult + term = strings.TrimSpace(term) + esInterface := a.Elasticsearch license := a.License() if esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch { @@ -1730,6 +1734,7 @@ func (a *App) SearchUsersInTeam(teamId string, term string, options *model.UserS } func (a *App) SearchUsersNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { + term = strings.TrimSpace(term) result := <-a.Srv.Store.User().SearchNotInTeam(notInTeamId, term, options) if result.Err != nil { return nil, result.Err @@ -1744,6 +1749,7 @@ func (a *App) SearchUsersNotInTeam(notInTeamId string, term string, options *mod } func (a *App) SearchUsersWithoutTeam(term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { + term = strings.TrimSpace(term) result := <-a.Srv.Store.User().SearchWithoutTeam(term, options) if result.Err != nil { return nil, result.Err @@ -1760,6 +1766,8 @@ func (a *App) SearchUsersWithoutTeam(term string, options *model.UserSearchOptio func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInChannel, *model.AppError) { var uchan, nuchan store.StoreChannel + term = strings.TrimSpace(term) + esInterface := a.Elasticsearch license := a.License() if esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch { @@ -1820,6 +1828,8 @@ func (a *App) AutocompleteUsersInTeam(teamId string, term string, options *model autocomplete := &model.UserAutocompleteInTeam{} var result store.StoreResult + term = strings.TrimSpace(term) + esInterface := a.Elasticsearch license := a.License() if esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch {