From 02a9ef3f82b10252c3c7e66f1e8cfcfd8a7e2610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Thu, 23 Sep 2021 14:43:09 +0200 Subject: [PATCH] [MM-38216] Add API endpoint and adapt search to allow multi-team search (#18371) * Add API endpoint and adapt search to allow multi-team search * Refactor handler, refactor sql query to use squirrel, rename app and store functions and add tests * Fix lint * Fix search engines and remove unneeded comments * Fix test * Remove user from channel after test --- api4/post.go | 15 +- api4/post_test.go | 17 ++ app/app_iface.go | 2 +- app/file.go | 2 +- app/opentracing/opentracing_layer.go | 44 ++-- app/plugin_api.go | 2 +- app/post.go | 8 +- app/post_test.go | 14 +- model/client4.go | 16 +- plugin/plugintest/hooks.go | 15 ++ store/opentracinglayer/opentracinglayer.go | 6 +- store/retrylayer/retrylayer.go | 4 +- store/searchlayer/post_layer.go | 8 +- store/searchtest/post_layer.go | 229 ++++++++++++--------- store/sqlstore/channel_store.go | 11 +- store/sqlstore/post_store.go | 218 ++++++++++---------- store/store.go | 2 +- store/storetest/mocks/PostStore.go | 4 +- store/timerlayer/timerlayer.go | 6 +- 19 files changed, 350 insertions(+), 273 deletions(-) diff --git a/api4/post.go b/api4/post.go index 539e6f138d5..5d3dbc055c8 100644 --- a/api4/post.go +++ b/api4/post.go @@ -27,7 +27,8 @@ func (api *API) InitPost() { api.BaseRoutes.ChannelForUser.Handle("/posts/unread", api.APISessionRequired(getPostsForChannelAroundLastUnread)).Methods("GET") - api.BaseRoutes.Team.Handle("/posts/search", api.APISessionRequiredDisableWhenBusy(searchPosts)).Methods("POST") + api.BaseRoutes.Team.Handle("/posts/search", api.APISessionRequiredDisableWhenBusy(searchPostsInTeam)).Methods("POST") + api.BaseRoutes.Posts.Handle("/search", api.APISessionRequiredDisableWhenBusy(searchPostsInAllTeams)).Methods("POST") api.BaseRoutes.Post.Handle("", api.APISessionRequired(updatePost)).Methods("PUT") api.BaseRoutes.Post.Handle("/patch", api.APISessionRequired(patchPost)).Methods("PUT") api.BaseRoutes.PostForUser.Handle("/set_unread", api.APISessionRequired(setPostUnread)).Methods("POST") @@ -473,7 +474,7 @@ func getPostThread(c *Context, w http.ResponseWriter, r *http.Request) { } } -func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) { +func searchPostsInTeam(c *Context, w http.ResponseWriter, r *http.Request) { c.RequireTeamId() if c.Err != nil { return @@ -484,6 +485,14 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) { return } + searchPosts(c, w, r, c.Params.TeamId) +} + +func searchPostsInAllTeams(c *Context, w http.ResponseWriter, r *http.Request) { + searchPosts(c, w, r, "") +} + +func searchPosts(c *Context, w http.ResponseWriter, r *http.Request, teamId string) { var params model.SearchParameter if jsonErr := json.NewDecoder(r.Body).Decode(¶ms); jsonErr != nil { c.Err = model.NewAppError("searchPosts", "api.post.search_posts.invalid_body.app_error", nil, jsonErr.Error(), http.StatusBadRequest) @@ -523,7 +532,7 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) { startTime := time.Now() - results, err := c.App.SearchPostsInTeamForUser(c.AppContext, terms, c.AppContext.Session().UserId, c.Params.TeamId, isOrSearch, includeDeletedChannels, timeZoneOffset, page, perPage) + results, err := c.App.SearchPostsForUser(c.AppContext, terms, c.AppContext.Session().UserId, teamId, isOrSearch, includeDeletedChannels, timeZoneOffset, page, perPage) elapsedTime := float64(time.Since(startTime)) / float64(time.Second) metrics := c.App.Metrics() diff --git a/api4/post_test.go b/api4/post_test.go index 4db75c01df9..f87a0ab7336 100644 --- a/api4/post_test.go +++ b/api4/post_test.go @@ -2201,6 +2201,11 @@ func TestSearchPosts(t *testing.T) { _ = th.CreateMessagePostWithClient(th.Client, archivedChannel, "#hashtag for post3") th.Client.DeleteChannel(archivedChannel.Id) + otherTeam := th.CreateTeam() + channelInOtherTeam := th.CreateChannelWithClientAndTeam(th.Client, model.ChannelTypeOpen, otherTeam.Id) + _ = th.AddUserToChannel(th.BasicUser, channelInOtherTeam) + _ = th.CreateMessagePostWithClient(th.Client, channelInOtherTeam, "search for post 5") + terms := "search" isOrSearch := false timezoneOffset := 5 @@ -2209,6 +2214,18 @@ func TestSearchPosts(t *testing.T) { IsOrSearch: &isOrSearch, TimeZoneOffset: &timezoneOffset, } + allTeamsPosts, _, err := client.SearchPostsWithParams("", &searchParams) + require.NoError(t, err) + require.Len(t, allTeamsPosts.Order, 4, "wrong search along multiple teams") + + terms = "search" + isOrSearch = false + timezoneOffset = 5 + searchParams = model.SearchParameter{ + Terms: &terms, + IsOrSearch: &isOrSearch, + TimeZoneOffset: &timezoneOffset, + } posts, _, err := client.SearchPostsWithParams(th.BasicTeam.Id, &searchParams) require.NoError(t, err) require.Len(t, posts.Order, 3, "wrong search") diff --git a/app/app_iface.go b/app/app_iface.go index f59d0c31833..929bc342cc6 100644 --- a/app/app_iface.go +++ b/app/app_iface.go @@ -953,8 +953,8 @@ type AppIface interface { SearchEngine() *searchengine.Broker SearchFilesInTeamForUser(c *request.Context, terms string, userId string, teamId string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.FileInfoList, *model.AppError) SearchGroupChannels(userID, term string) (model.ChannelList, *model.AppError) + SearchPostsForUser(c *request.Context, terms string, userID string, teamID string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) SearchPostsInTeam(teamID string, paramsList []*model.SearchParams) (*model.PostList, *model.AppError) - SearchPostsInTeamForUser(c *request.Context, terms string, userID string, teamID string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) SearchPrivateTeams(searchOpts *model.TeamSearch) ([]*model.Team, *model.AppError) SearchPublicTeams(searchOpts *model.TeamSearch) ([]*model.Team, *model.AppError) SearchUserAccessTokens(term string) ([]*model.UserAccessToken, *model.AppError) diff --git a/app/file.go b/app/file.go index 5f93c50e9e1..2a2e43721cb 100644 --- a/app/file.go +++ b/app/file.go @@ -1333,7 +1333,7 @@ func (a *App) SearchFilesInTeamForUser(c *request.Context, terms string, userId case errors.As(nErr, &appErr): return nil, appErr default: - return nil, model.NewAppError("SearchPostsInTeamForUser", "app.post.search.app_error", nil, nErr.Error(), http.StatusInternalServerError) + return nil, model.NewAppError("SearchFilesInTeamForUser", "app.post.search.app_error", nil, nErr.Error(), http.StatusInternalServerError) } } diff --git a/app/opentracing/opentracing_layer.go b/app/opentracing/opentracing_layer.go index 7238a27592a..30e0eef82f6 100644 --- a/app/opentracing/opentracing_layer.go +++ b/app/opentracing/opentracing_layer.go @@ -13869,6 +13869,28 @@ func (a *OpenTracingAppLayer) SearchGroupChannels(userID string, term string) (m return resultVar0, resultVar1 } +func (a *OpenTracingAppLayer) SearchPostsForUser(c *request.Context, terms string, userID string, teamID string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page int, perPage int) (*model.PostSearchResults, *model.AppError) { + origCtx := a.ctx + span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SearchPostsForUser") + + a.ctx = newCtx + a.app.Srv().Store.SetContext(newCtx) + defer func() { + a.app.Srv().Store.SetContext(origCtx) + a.ctx = origCtx + }() + + defer span.Finish() + resultVar0, resultVar1 := a.app.SearchPostsForUser(c, terms, userID, teamID, isOrSearch, includeDeletedChannels, timeZoneOffset, page, perPage) + + if resultVar1 != nil { + span.LogFields(spanlog.Error(resultVar1)) + ext.Error.Set(span, true) + } + + return resultVar0, resultVar1 +} + func (a *OpenTracingAppLayer) SearchPostsInTeam(teamID string, paramsList []*model.SearchParams) (*model.PostList, *model.AppError) { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SearchPostsInTeam") @@ -13891,28 +13913,6 @@ func (a *OpenTracingAppLayer) SearchPostsInTeam(teamID string, paramsList []*mod return resultVar0, resultVar1 } -func (a *OpenTracingAppLayer) SearchPostsInTeamForUser(c *request.Context, terms string, userID string, teamID string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page int, perPage int) (*model.PostSearchResults, *model.AppError) { - origCtx := a.ctx - span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SearchPostsInTeamForUser") - - a.ctx = newCtx - a.app.Srv().Store.SetContext(newCtx) - defer func() { - a.app.Srv().Store.SetContext(origCtx) - a.ctx = origCtx - }() - - defer span.Finish() - resultVar0, resultVar1 := a.app.SearchPostsInTeamForUser(c, terms, userID, teamID, isOrSearch, includeDeletedChannels, timeZoneOffset, page, perPage) - - if resultVar1 != nil { - span.LogFields(spanlog.Error(resultVar1)) - ext.Error.Set(span, true) - } - - return resultVar0, resultVar1 -} - func (a *OpenTracingAppLayer) SearchPrivateTeams(searchOpts *model.TeamSearch) ([]*model.Team, *model.AppError) { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SearchPrivateTeams") diff --git a/app/plugin_api.go b/app/plugin_api.go index a66314072e3..6c6b35d3ece 100644 --- a/app/plugin_api.go +++ b/app/plugin_api.go @@ -512,7 +512,7 @@ func (api *PluginAPI) SearchPostsInTeamForUser(teamID string, userID string, sea includeDeletedChannels = *searchParams.IncludeDeletedChannels } - return api.app.SearchPostsInTeamForUser(api.ctx, terms, userID, teamID, isOrSearch, includeDeletedChannels, timeZoneOffset, page, perPage) + return api.app.SearchPostsForUser(api.ctx, terms, userID, teamID, isOrSearch, includeDeletedChannels, timeZoneOffset, page, perPage) } func (api *PluginAPI) AddChannelMember(channelID, userID string) (*model.ChannelMember, *model.AppError) { diff --git a/app/post.go b/app/post.go index 7c7809bff91..aca7b5a1058 100644 --- a/app/post.go +++ b/app/post.go @@ -1307,13 +1307,13 @@ func (a *App) SearchPostsInTeam(teamID string, paramsList []*model.SearchParams) }) } -func (a *App) SearchPostsInTeamForUser(c *request.Context, terms string, userID string, teamID string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) { +func (a *App) SearchPostsForUser(c *request.Context, terms string, userID string, teamID string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) { var postSearchResults *model.PostSearchResults paramsList := model.ParseSearchParams(strings.TrimSpace(terms), timeZoneOffset) includeDeleted := includeDeletedChannels && *a.Config().TeamSettings.ExperimentalViewArchivedChannels if !*a.Config().ServiceSettings.EnablePostSearch { - return nil, model.NewAppError("SearchPostsInTeamForUser", "store.sql_post.search.disabled", nil, fmt.Sprintf("teamId=%v userId=%v", teamID, userID), http.StatusNotImplemented) + return nil, model.NewAppError("SearchPostsForUser", "store.sql_post.search.disabled", nil, fmt.Sprintf("teamId=%v userId=%v", teamID, userID), http.StatusNotImplemented) } finalParamsList := []*model.SearchParams{} @@ -1340,14 +1340,14 @@ func (a *App) SearchPostsInTeamForUser(c *request.Context, terms string, userID return model.MakePostSearchResults(model.NewPostList(), nil), nil } - postSearchResults, nErr := a.Srv().Store.Post().SearchPostsInTeamForUser(finalParamsList, userID, teamID, page, perPage) + postSearchResults, nErr := a.Srv().Store.Post().SearchPostsForUser(finalParamsList, userID, teamID, page, perPage) if nErr != nil { var appErr *model.AppError switch { case errors.As(nErr, &appErr): return nil, appErr default: - return nil, model.NewAppError("SearchPostsInTeamForUser", "app.post.search.app_error", nil, nErr.Error(), http.StatusInternalServerError) + return nil, model.NewAppError("SearchPostsForUser", "app.post.search.app_error", nil, nErr.Error(), http.StatusInternalServerError) } } diff --git a/app/post_test.go b/app/post_test.go index a472ac16c67..411bc7d424e 100644 --- a/app/post_test.go +++ b/app/post_test.go @@ -1195,7 +1195,7 @@ func TestUpdatePost(t *testing.T) { }) } -func TestSearchPostsInTeamForUser(t *testing.T) { +func TestSearchPostsForUser(t *testing.T) { perPage := 5 searchTerm := "searchTerm" @@ -1237,7 +1237,7 @@ func TestSearchPostsInTeamForUser(t *testing.T) { page := 0 - results, err := th.App.SearchPostsInTeamForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) + results, err := th.App.SearchPostsForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) assert.Nil(t, err) assert.Equal(t, []string{ @@ -1257,7 +1257,7 @@ func TestSearchPostsInTeamForUser(t *testing.T) { page := 1 - results, err := th.App.SearchPostsInTeamForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) + results, err := th.App.SearchPostsForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) assert.Nil(t, err) assert.Equal(t, []string{}, results.Order) @@ -1287,7 +1287,7 @@ func TestSearchPostsInTeamForUser(t *testing.T) { th.App.Srv().SearchEngine.ElasticsearchEngine = nil }() - results, err := th.App.SearchPostsInTeamForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) + results, err := th.App.SearchPostsForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) assert.Nil(t, err) assert.Equal(t, resultsPage, results.Order) @@ -1315,7 +1315,7 @@ func TestSearchPostsInTeamForUser(t *testing.T) { th.App.Srv().SearchEngine.ElasticsearchEngine = nil }() - results, err := th.App.SearchPostsInTeamForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) + results, err := th.App.SearchPostsForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) assert.Nil(t, err) assert.Equal(t, resultsPage, results.Order) @@ -1339,7 +1339,7 @@ func TestSearchPostsInTeamForUser(t *testing.T) { th.App.Srv().SearchEngine.ElasticsearchEngine = nil }() - results, err := th.App.SearchPostsInTeamForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) + results, err := th.App.SearchPostsForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) assert.Nil(t, err) assert.Equal(t, []string{ @@ -1371,7 +1371,7 @@ func TestSearchPostsInTeamForUser(t *testing.T) { th.App.Srv().SearchEngine.ElasticsearchEngine = nil }() - results, err := th.App.SearchPostsInTeamForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) + results, err := th.App.SearchPostsForUser(th.Context, searchTerm, th.BasicUser.Id, th.BasicTeam.Id, false, false, 0, page, perPage) assert.Nil(t, err) assert.Equal(t, []string{}, results.Order) diff --git a/model/client4.go b/model/client4.go index 615b1264daf..7bbc98e7d18 100644 --- a/model/client4.go +++ b/model/client4.go @@ -3899,7 +3899,13 @@ func (c *Client4) SearchPostsWithParams(teamId string, params *SearchParameter) if jsonErr != nil { return nil, nil, NewAppError("SearchFilesWithParams", "api.marshal_error", nil, jsonErr.Error(), http.StatusInternalServerError) } - r, err := c.DoAPIPost(c.teamRoute(teamId)+"/posts/search", string(js)) + var route string + if teamId == "" { + route = c.postsRoute() + "/search" + } else { + route = c.teamRoute(teamId) + "/posts/search" + } + r, err := c.DoAPIPost(route, string(js)) if err != nil { return nil, BuildResponse(r), err } @@ -3917,7 +3923,13 @@ func (c *Client4) SearchPostsWithParams(teamId string, params *SearchParameter) // SearchPostsWithMatches returns any posts with matching terms string, including. func (c *Client4) SearchPostsWithMatches(teamId string, terms string, isOrSearch bool) (*PostSearchResults, *Response, error) { requestBody := map[string]interface{}{"terms": terms, "is_or_search": isOrSearch} - r, err := c.DoAPIPost(c.teamRoute(teamId)+"/posts/search", StringInterfaceToJSON(requestBody)) + var route string + if teamId == "" { + route = c.postsRoute() + "/search" + } else { + route = c.teamRoute(teamId) + "/posts/search" + } + r, err := c.DoAPIPost(route, StringInterfaceToJSON(requestBody)) if err != nil { return nil, BuildResponse(r), err } diff --git a/plugin/plugintest/hooks.go b/plugin/plugintest/hooks.go index 7a55bc4030e..daab4a0aa84 100644 --- a/plugin/plugintest/hooks.go +++ b/plugin/plugintest/hooks.go @@ -199,6 +199,16 @@ func (_m *Hooks) OnPluginClusterEvent(c *plugin.Context, ev model.PluginClusterE _m.Called(c, ev) } +// OnWebSocketConnect provides a mock function with given fields: webConnID, userID +func (_m *Hooks) OnWebSocketConnect(webConnID string, userID string) { + _m.Called(webConnID, userID) +} + +// OnWebSocketDisconnect provides a mock function with given fields: webConnID, userID +func (_m *Hooks) OnWebSocketDisconnect(webConnID string, userID string) { + _m.Called(webConnID, userID) +} + // ReactionHasBeenAdded provides a mock function with given fields: c, reaction func (_m *Hooks) ReactionHasBeenAdded(c *plugin.Context, reaction *model.Reaction) { _m.Called(c, reaction) @@ -257,3 +267,8 @@ func (_m *Hooks) UserWillLogIn(c *plugin.Context, user *model.User) string { return r0 } + +// WebSocketMessageHasBeenPosted provides a mock function with given fields: webConnID, userID, req +func (_m *Hooks) WebSocketMessageHasBeenPosted(webConnID string, userID string, req *model.WebSocketRequest) { + _m.Called(webConnID, userID, req) +} diff --git a/store/opentracinglayer/opentracinglayer.go b/store/opentracinglayer/opentracinglayer.go index be7fcc123e3..48d847c0459 100644 --- a/store/opentracinglayer/opentracinglayer.go +++ b/store/opentracinglayer/opentracinglayer.go @@ -5731,16 +5731,16 @@ func (s *OpenTracingLayerPostStore) Search(teamID string, userID string, params return result, err } -func (s *OpenTracingLayerPostStore) SearchPostsInTeamForUser(paramsList []*model.SearchParams, userID string, teamID string, page int, perPage int) (*model.PostSearchResults, error) { +func (s *OpenTracingLayerPostStore) SearchPostsForUser(paramsList []*model.SearchParams, userID string, teamID string, page int, perPage int) (*model.PostSearchResults, error) { origCtx := s.Root.Store.Context() - span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "PostStore.SearchPostsInTeamForUser") + span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "PostStore.SearchPostsForUser") s.Root.Store.SetContext(newCtx) defer func() { s.Root.Store.SetContext(origCtx) }() defer span.Finish() - result, err := s.PostStore.SearchPostsInTeamForUser(paramsList, userID, teamID, page, perPage) + result, err := s.PostStore.SearchPostsForUser(paramsList, userID, teamID, page, perPage) if err != nil { span.LogFields(spanlog.Error(err)) ext.Error.Set(span, true) diff --git a/store/retrylayer/retrylayer.go b/store/retrylayer/retrylayer.go index eb9a59c49c8..69f4b2340f2 100644 --- a/store/retrylayer/retrylayer.go +++ b/store/retrylayer/retrylayer.go @@ -6190,11 +6190,11 @@ func (s *RetryLayerPostStore) Search(teamID string, userID string, params *model } -func (s *RetryLayerPostStore) SearchPostsInTeamForUser(paramsList []*model.SearchParams, userID string, teamID string, page int, perPage int) (*model.PostSearchResults, error) { +func (s *RetryLayerPostStore) SearchPostsForUser(paramsList []*model.SearchParams, userID string, teamID string, page int, perPage int) (*model.PostSearchResults, error) { tries := 0 for { - result, err := s.PostStore.SearchPostsInTeamForUser(paramsList, userID, teamID, page, perPage) + result, err := s.PostStore.SearchPostsForUser(paramsList, userID, teamID, page, perPage) if err == nil { return result, nil } diff --git a/store/searchlayer/post_layer.go b/store/searchlayer/post_layer.go index 610652d75a1..6c30fc20d8a 100644 --- a/store/searchlayer/post_layer.go +++ b/store/searchlayer/post_layer.go @@ -136,7 +136,7 @@ func (s SearchPostStore) PermanentDeleteByChannel(channelID string) error { return err } -func (s SearchPostStore) searchPostsInTeamForUserByEngine(engine searchengine.SearchEngineInterface, paramsList []*model.SearchParams, userId, teamId string, page, perPage int) (*model.PostSearchResults, error) { +func (s SearchPostStore) searchPostsForUserByEngine(engine searchengine.SearchEngineInterface, paramsList []*model.SearchParams, userId, teamId string, page, perPage int) (*model.PostSearchResults, error) { if err := model.IsSearchParamsListValid(paramsList); err != nil { return nil, err } @@ -170,10 +170,10 @@ func (s SearchPostStore) searchPostsInTeamForUserByEngine(engine searchengine.Se return model.MakePostSearchResults(postList, matches), nil } -func (s SearchPostStore) SearchPostsInTeamForUser(paramsList []*model.SearchParams, userId, teamId string, page, perPage int) (*model.PostSearchResults, error) { +func (s SearchPostStore) SearchPostsForUser(paramsList []*model.SearchParams, userId, teamId string, page, perPage int) (*model.PostSearchResults, error) { for _, engine := range s.rootStore.searchEngine.GetActiveEngines() { if engine.IsSearchEnabled() { - results, err := s.searchPostsInTeamForUserByEngine(engine, paramsList, userId, teamId, page, perPage) + results, err := s.searchPostsForUserByEngine(engine, paramsList, userId, teamId, page, perPage) if err != nil { mlog.Warn("Encountered error on SearchPostsInTeamForUser.", mlog.String("search_engine", engine.GetName()), mlog.Err(err)) continue @@ -189,5 +189,5 @@ func (s SearchPostStore) SearchPostsInTeamForUser(paramsList []*model.SearchPara } mlog.Debug("Using database search because no other search engine is available") - return s.PostStore.SearchPostsInTeamForUser(paramsList, userId, teamId, page, perPage) + return s.PostStore.SearchPostsForUser(paramsList, userId, teamId, page, perPage) } diff --git a/store/searchtest/post_layer.go b/store/searchtest/post_layer.go index 7af5b90930d..2df724ab208 100644 --- a/store/searchtest/post_layer.go +++ b/store/searchtest/post_layer.go @@ -262,6 +262,11 @@ var searchPostStoreTests = []searchTest{ Fn: testShouldNotReturnLinksEmbeddedInMarkdown, Tags: []string{EnginePostgres, EngineElasticSearch}, }, + { + Name: "Should search across teams", + Fn: testSearchAcrossTeams, + Tags: []string{EngineAll}, + }, } func TestSearchPostStore(t *testing.T, s store.Store, testEngine *SearchTestEngine) { @@ -289,7 +294,7 @@ func testSearchPostsIncludingDMs(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "test"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -311,13 +316,13 @@ func testSearchPostsWithPagination(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "test"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 1) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 1) require.NoError(t, err) require.Len(t, results.Posts, 1) th.checkPostInSearchResults(t, p2.Id, results.Posts) - results, err = th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 1, 1) + results, err = th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 1, 1) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -332,7 +337,7 @@ func testSearchReturnPinnedAndUnpinned(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "test"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -353,7 +358,7 @@ func testSearchExactPhraseInQuotes(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "\"channel test 1 2 3\""} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -369,7 +374,7 @@ func testSearchEmailAddresses(t *testing.T, th *SearchTestHelper) { t.Run("Should search email addresses enclosed by quotes", func(t *testing.T) { params := &model.SearchParams{Terms: "\"test@test.com\""} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -378,7 +383,7 @@ func testSearchEmailAddresses(t *testing.T, th *SearchTestHelper) { t.Run("Should search email addresses without quotes", func(t *testing.T) { params := &model.SearchParams{Terms: "test@test.com"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -393,7 +398,7 @@ func testSearchMarkdownUnderscores(t *testing.T, th *SearchTestHelper) { t.Run("Should search the start inside the markdown underscore", func(t *testing.T) { params := &model.SearchParams{Terms: "start"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -402,7 +407,7 @@ func testSearchMarkdownUnderscores(t *testing.T, th *SearchTestHelper) { t.Run("Should search a word in the middle of the markdown underscore", func(t *testing.T) { params := &model.SearchParams{Terms: "middle"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -411,7 +416,7 @@ func testSearchMarkdownUnderscores(t *testing.T, th *SearchTestHelper) { t.Run("Should search in the end of the markdown underscore", func(t *testing.T) { params := &model.SearchParams{Terms: "end"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -420,7 +425,7 @@ func testSearchMarkdownUnderscores(t *testing.T, th *SearchTestHelper) { t.Run("Should search inside markdown underscore", func(t *testing.T) { params := &model.SearchParams{Terms: "another"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -438,7 +443,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { t.Run("Should search one word", func(t *testing.T) { params := &model.SearchParams{Terms: "你"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -446,7 +451,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { }) t.Run("Should search two words", func(t *testing.T) { params := &model.SearchParams{Terms: "你好"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -454,7 +459,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { }) t.Run("Should search with wildcard", func(t *testing.T) { params := &model.SearchParams{Terms: "你*"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -469,7 +474,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { t.Run("Should search one word", func(t *testing.T) { params := &model.SearchParams{Terms: "слово"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -477,7 +482,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { }) t.Run("Should search using wildcard", func(t *testing.T) { params := &model.SearchParams{Terms: "слов*"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -494,7 +499,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { t.Run("Should search one word", func(t *testing.T) { params := &model.SearchParams{Terms: "本"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -503,7 +508,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { }) t.Run("Should search two words", func(t *testing.T) { params := &model.SearchParams{Terms: "本木"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -511,7 +516,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { }) t.Run("Should search with wildcard", func(t *testing.T) { params := &model.SearchParams{Terms: "本*"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -529,7 +534,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { t.Run("Should search one word", func(t *testing.T) { params := &model.SearchParams{Terms: "불"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -537,7 +542,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { }) t.Run("Should search two words", func(t *testing.T) { params := &model.SearchParams{Terms: "불다"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -545,7 +550,7 @@ func testSearchNonLatinWords(t *testing.T, th *SearchTestHelper) { }) t.Run("Should search with wildcard", func(t *testing.T) { params := &model.SearchParams{Terms: "불*"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -563,7 +568,7 @@ func testSearchAlternativeSpellings(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "Straße"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -571,7 +576,7 @@ func testSearchAlternativeSpellings(t *testing.T, th *SearchTestHelper) { th.checkPostInSearchResults(t, p2.Id, results.Posts) params = &model.SearchParams{Terms: "Strasse"} - results, err = th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err = th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -587,7 +592,7 @@ func testSearchAlternativeSpellingsAccents(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "café"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -595,7 +600,7 @@ func testSearchAlternativeSpellingsAccents(t *testing.T, th *SearchTestHelper) { th.checkPostInSearchResults(t, p2.Id, results.Posts) params = &model.SearchParams{Terms: "café"} - results, err = th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err = th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -603,7 +608,7 @@ func testSearchAlternativeSpellingsAccents(t *testing.T, th *SearchTestHelper) { th.checkPostInSearchResults(t, p2.Id, results.Posts) params = &model.SearchParams{Terms: "cafe"} - results, err = th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err = th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 0) @@ -621,7 +626,7 @@ func testSearchOrExcludePostsBySpecificUser(t *testing.T, th *SearchTestHelper) Terms: "fromuser", FromUsers: []string{th.User.Id}, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -640,7 +645,7 @@ func testSearchOrExcludePostsInChannel(t *testing.T, th *SearchTestHelper) { Terms: "fromuser", InChannels: []string{th.ChannelBasic.Id}, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -668,7 +673,7 @@ func testSearchOrExcludePostsInDMGM(t *testing.T, th *SearchTestHelper) { Terms: "fromuser", InChannels: []string{direct.Id, group.Id}, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -681,7 +686,7 @@ func testSearchOrExcludePostsInDMGM(t *testing.T, th *SearchTestHelper) { Terms: "fromuser", InChannels: []string{direct.Id}, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -693,7 +698,7 @@ func testSearchOrExcludePostsInDMGM(t *testing.T, th *SearchTestHelper) { Terms: "fromuser", InChannels: []string{group.Id}, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -718,7 +723,7 @@ func testFilterMessagesInSpecificDate(t *testing.T, th *SearchTestHelper) { Terms: "test", OnDate: "2020-03-22", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -729,7 +734,7 @@ func testFilterMessagesInSpecificDate(t *testing.T, th *SearchTestHelper) { Terms: "test", ExcludedDate: "2020-03-22", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -755,7 +760,7 @@ func testFilterMessagesBeforeSpecificDate(t *testing.T, th *SearchTestHelper) { Terms: "test", BeforeDate: "2020-03-23", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -768,7 +773,7 @@ func testFilterMessagesBeforeSpecificDate(t *testing.T, th *SearchTestHelper) { Terms: "test", ExcludedBeforeDate: "2020-03-23", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -793,7 +798,7 @@ func testFilterMessagesAfterSpecificDate(t *testing.T, th *SearchTestHelper) { Terms: "test", AfterDate: "2020-03-23", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -805,7 +810,7 @@ func testFilterMessagesAfterSpecificDate(t *testing.T, th *SearchTestHelper) { Terms: "test", ExcludedAfterDate: "2020-03-23", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -828,7 +833,7 @@ func testFilterMessagesWithATerm(t *testing.T, th *SearchTestHelper) { Terms: "one", ExcludedTerms: "five eight", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -840,7 +845,7 @@ func testFilterMessagesWithATerm(t *testing.T, th *SearchTestHelper) { Terms: "one", ExcludedTerms: "\"eight nine\"", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -863,7 +868,7 @@ func testSearchUsingBooleanOperators(t *testing.T, th *SearchTestHelper) { Terms: "one two", OrTerms: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -876,7 +881,7 @@ func testSearchUsingBooleanOperators(t *testing.T, th *SearchTestHelper) { Terms: "one two", OrTerms: false, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -903,7 +908,7 @@ func testSearchUsingCombinedFilters(t *testing.T, th *SearchTestHelper) { FromUsers: []string{th.User2.Id}, InChannels: []string{th.ChannelPrivate.Id}, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -916,7 +921,7 @@ func testSearchUsingCombinedFilters(t *testing.T, th *SearchTestHelper) { ExcludedUsers: []string{th.User2.Id}, InChannels: []string{th.ChannelPrivate.Id}, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -930,7 +935,7 @@ func testSearchUsingCombinedFilters(t *testing.T, th *SearchTestHelper) { ExcludedAfterDate: "2020-03-11", InChannels: []string{th.ChannelPrivate.Id}, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -942,7 +947,7 @@ func testSearchUsingCombinedFilters(t *testing.T, th *SearchTestHelper) { AfterDate: "2020-03-11", ExcludedChannels: []string{th.ChannelPrivate.Id}, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -965,7 +970,7 @@ func testSearchIgnoringStopWords(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "the search", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -976,7 +981,7 @@ func testSearchIgnoringStopWords(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "a avoid", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -987,7 +992,7 @@ func testSearchIgnoringStopWords(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "in where you", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1020,7 +1025,7 @@ func testSupportStemming(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "search", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1041,7 +1046,7 @@ func testSupportWildcards(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "search*", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1053,7 +1058,7 @@ func testSupportWildcards(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "sear* post", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1073,7 +1078,7 @@ func testNotSupportPrecedingWildcards(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "*earch", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 0) @@ -1089,7 +1094,7 @@ func testSearchDiscardWildcardAlone(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "qwerty *", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1107,7 +1112,7 @@ func testSupportTermsWithDash(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "term-with-dash", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1118,7 +1123,7 @@ func testSupportTermsWithDash(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "\"term-with-dash\"", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1137,7 +1142,7 @@ func testSupportTermsWithUnderscore(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "term_with_underscore", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1148,7 +1153,7 @@ func testSupportTermsWithUnderscore(t *testing.T, th *SearchTestHelper) { params := &model.SearchParams{ Terms: "\"term_with_underscore\"", } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1170,7 +1175,7 @@ func testSearchOrExcludePostsWithHashtags(t *testing.T, th *SearchTestHelper) { Terms: "#hashtag", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1183,7 +1188,7 @@ func testSearchOrExcludePostsWithHashtags(t *testing.T, th *SearchTestHelper) { Terms: "#hashtag", IsHashtag: false, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1209,7 +1214,7 @@ func testSearchHashtagWithMarkdown(t *testing.T, th *SearchTestHelper) { Terms: "#hashtag", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 5) @@ -1232,7 +1237,7 @@ func testSearcWithMultipleHashtags(t *testing.T, th *SearchTestHelper) { Terms: "#hashone #hashtwo", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1245,7 +1250,7 @@ func testSearcWithMultipleHashtags(t *testing.T, th *SearchTestHelper) { IsHashtag: true, OrTerms: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1263,7 +1268,7 @@ func testSearchPostsWithDotsInHashtags(t *testing.T, th *SearchTestHelper) { Terms: "#hashtag.dot", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1284,7 +1289,7 @@ func testSearchHashtagCaseInsensitive(t *testing.T, th *SearchTestHelper) { Terms: "#hashtag", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 3) @@ -1298,7 +1303,7 @@ func testSearchHashtagCaseInsensitive(t *testing.T, th *SearchTestHelper) { Terms: "#HASHTAG", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 3) @@ -1312,7 +1317,7 @@ func testSearchHashtagCaseInsensitive(t *testing.T, th *SearchTestHelper) { Terms: "#HaShTaG", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 3) @@ -1333,7 +1338,7 @@ func testSearchHashtagWithDash(t *testing.T, th *SearchTestHelper) { Terms: "#hashtag-test", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1351,7 +1356,7 @@ func testSearchHashtagWithNumbers(t *testing.T, th *SearchTestHelper) { Terms: "#h4sht4g", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1369,7 +1374,7 @@ func testSearchHashtagWithDots(t *testing.T, th *SearchTestHelper) { Terms: "#hashtag.test", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1387,7 +1392,7 @@ func testSearchHashtagWithUnderscores(t *testing.T, th *SearchTestHelper) { Terms: "#hashtag_test", IsHashtag: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1410,7 +1415,7 @@ func testSearchShouldExcludeSytemMessages(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "test system"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 0) @@ -1426,7 +1431,7 @@ func testSearchShouldBeAbleToMatchByMentions(t *testing.T, th *SearchTestHelper) defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "@testuser"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 3) @@ -1446,7 +1451,7 @@ func testSearchInDeletedOrArchivedChannels(t *testing.T, th *SearchTestHelper) { t.Run("Doesn't include posts in deleted channels", func(t *testing.T) { params := &model.SearchParams{Terms: "message", IncludeDeletedChannels: false} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1456,7 +1461,7 @@ func testSearchInDeletedOrArchivedChannels(t *testing.T, th *SearchTestHelper) { t.Run("Include posts in deleted channels", func(t *testing.T) { params := &model.SearchParams{Terms: "message", IncludeDeletedChannels: true} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 3) @@ -1467,7 +1472,7 @@ func testSearchInDeletedOrArchivedChannels(t *testing.T, th *SearchTestHelper) { t.Run("Include posts in deleted channels using multiple terms", func(t *testing.T) { params := &model.SearchParams{Terms: "message channel", IncludeDeletedChannels: true} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 3) @@ -1482,7 +1487,7 @@ func testSearchInDeletedOrArchivedChannels(t *testing.T, th *SearchTestHelper) { IncludeDeletedChannels: true, OrTerms: true, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 3) @@ -1500,7 +1505,7 @@ func testSearchInDeletedOrArchivedChannels(t *testing.T, th *SearchTestHelper) { Terms: "#hashtag", IncludeDeletedChannels: false, } - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params1, params2}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params1, params2}, th.User.Id, th.Team.Id, 0, 20) require.Nil(t, results) require.Error(t, err) }) @@ -1515,7 +1520,7 @@ func testSearchTermsWithDashes(t *testing.T, th *SearchTestHelper) { t.Run("Search for terms with dash", func(t *testing.T) { params := &model.SearchParams{Terms: "with-dash-term"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1524,7 +1529,7 @@ func testSearchTermsWithDashes(t *testing.T, th *SearchTestHelper) { t.Run("Search for terms with quoted dash", func(t *testing.T) { params := &model.SearchParams{Terms: "\"with-dash-term\""} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1533,7 +1538,7 @@ func testSearchTermsWithDashes(t *testing.T, th *SearchTestHelper) { t.Run("Search for multiple terms with one having dash", func(t *testing.T) { params := &model.SearchParams{Terms: "with-dash-term message"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1542,7 +1547,7 @@ func testSearchTermsWithDashes(t *testing.T, th *SearchTestHelper) { t.Run("Search for multiple OR terms with one having dash", func(t *testing.T) { params := &model.SearchParams{Terms: "with-dash-term message", OrTerms: true} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1560,7 +1565,7 @@ func testSearchTermsWithDots(t *testing.T, th *SearchTestHelper) { t.Run("Search for terms with dots", func(t *testing.T) { params := &model.SearchParams{Terms: "with.dots.term"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1569,7 +1574,7 @@ func testSearchTermsWithDots(t *testing.T, th *SearchTestHelper) { t.Run("Search for terms with quoted dots", func(t *testing.T) { params := &model.SearchParams{Terms: "\"with.dots.term\""} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1578,7 +1583,7 @@ func testSearchTermsWithDots(t *testing.T, th *SearchTestHelper) { t.Run("Search for multiple terms with one having dots", func(t *testing.T) { params := &model.SearchParams{Terms: "with.dots.term message"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1587,7 +1592,7 @@ func testSearchTermsWithDots(t *testing.T, th *SearchTestHelper) { t.Run("Search for multiple OR terms with one having dots", func(t *testing.T) { params := &model.SearchParams{Terms: "with.dots.term message", OrTerms: true} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1605,7 +1610,7 @@ func testSearchTermsWithUnderscores(t *testing.T, th *SearchTestHelper) { t.Run("Search for terms with underscores", func(t *testing.T) { params := &model.SearchParams{Terms: "with_underscores_term"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1614,7 +1619,7 @@ func testSearchTermsWithUnderscores(t *testing.T, th *SearchTestHelper) { t.Run("Search for terms with quoted underscores", func(t *testing.T) { params := &model.SearchParams{Terms: "\"with_underscores_term\""} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1623,7 +1628,7 @@ func testSearchTermsWithUnderscores(t *testing.T, th *SearchTestHelper) { t.Run("Search for multiple terms with one having underscores", func(t *testing.T) { params := &model.SearchParams{Terms: "with_underscores_term message"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1632,7 +1637,7 @@ func testSearchTermsWithUnderscores(t *testing.T, th *SearchTestHelper) { t.Run("Search for multiple OR terms with one having underscores", func(t *testing.T) { params := &model.SearchParams{Terms: "with_underscores_term message", OrTerms: true} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1654,7 +1659,7 @@ func testSearchBotAccountsPosts(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(bot.UserId) params := &model.SearchParams{Terms: "bot"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1673,7 +1678,7 @@ func testSupportStemmingAndWildcards(t *testing.T, th *SearchTestHelper) { t.Run("Should stem appr", func(t *testing.T) { params := &model.SearchParams{Terms: "appr*"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 3) @@ -1684,7 +1689,7 @@ func testSupportStemmingAndWildcards(t *testing.T, th *SearchTestHelper) { t.Run("Should stem approve", func(t *testing.T) { params := &model.SearchParams{Terms: "approve*"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1701,7 +1706,7 @@ func testSupportWildcardOutsideQuotes(t *testing.T, th *SearchTestHelper) { t.Run("Should return results without quotes", func(t *testing.T) { params := &model.SearchParams{Terms: "hell*"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 2) @@ -1711,7 +1716,7 @@ func testSupportWildcardOutsideQuotes(t *testing.T, th *SearchTestHelper) { t.Run("Should return just one result with quotes", func(t *testing.T) { params := &model.SearchParams{Terms: "\"hell\"*"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1730,7 +1735,7 @@ func testHashtagSearchShouldSupportThreeOrMoreCharacters(t *testing.T, th *Searc defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "#123", IsHashtag: true} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1743,21 +1748,21 @@ func testSlashShouldNotBeCharSeparator(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "gamma"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) th.checkPostInSearchResults(t, p1.Id, results.Posts) params = &model.SearchParams{Terms: "beta"} - results, err = th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err = th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) th.checkPostInSearchResults(t, p1.Id, results.Posts) params = &model.SearchParams{Terms: "alpha"} - results, err = th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err = th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1772,7 +1777,7 @@ func testSearchEmailsWithoutQuotes(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "test@test.com"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1787,7 +1792,7 @@ func testSupportSearchInComments(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "reply"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1800,7 +1805,7 @@ func testSupportSearchTermsWithinLinks(t *testing.T, th *SearchTestHelper) { defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "wikipedia"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 1) @@ -1813,8 +1818,26 @@ func testShouldNotReturnLinksEmbeddedInMarkdown(t *testing.T, th *SearchTestHelp defer th.deleteUserPosts(th.User.Id) params := &model.SearchParams{Terms: "wikipedia"} - results, err := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) require.NoError(t, err) require.Len(t, results.Posts, 0) } + +func testSearchAcrossTeams(t *testing.T, th *SearchTestHelper) { + err := th.addUserToChannels(th.User, []string{th.ChannelAnotherTeam.Id}) + require.NoError(t, err) + defer th.Store.Channel().RemoveMember(th.ChannelAnotherTeam.Id, th.User.Id) + + _, err = th.createPost(th.User.Id, th.ChannelAnotherTeam.Id, "text to search", "", model.PostTypeDefault, 0, false) + require.NoError(t, err) + defer th.deleteUserPosts(th.User.Id) + _, err = th.createPost(th.User.Id, th.ChannelBasic.Id, "text to search", "", model.PostTypeDefault, 0, false) + require.NoError(t, err) + + params := &model.SearchParams{Terms: "search"} + results, err := th.Store.Post().SearchPostsForUser([]*model.SearchParams{params}, th.User.Id, "", 0, 20) + require.NoError(t, err) + + require.Len(t, results.Posts, 2) +} diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index 58e8259738f..64c687203a1 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -955,14 +955,17 @@ func (s SqlChannelStore) GetChannels(teamId string, userId string, includeDelete sq.And{ sq.Expr("Id = ChannelId"), sq.Eq{"UserId": userId}, - sq.Or{ - sq.Eq{"TeamId": teamId}, - sq.Eq{"TeamId": ""}, - }, }, ). OrderBy("DisplayName") + if teamId != "" { + query = query.Where(sq.Or{ + sq.Eq{"TeamId": teamId}, + sq.Eq{"TeamId": ""}, + }) + } + if includeDeleted { if lastDeleteAt != 0 { // We filter by non-archived, and archived >= a timestamp. diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index 7455ab225d0..fa43b051b08 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -1472,8 +1472,7 @@ var specialSearchChar = []string{ ":", } -func (s *SqlPostStore) buildCreateDateFilterClause(params *model.SearchParams, queryParams map[string]interface{}) (string, map[string]interface{}) { - searchQuery := "" +func (s *SqlPostStore) buildCreateDateFilterClause(params *model.SearchParams, queryParams map[string]interface{}, builder sq.SelectBuilder) (sq.SelectBuilder, map[string]interface{}) { // handle after: before: on: filters if params.OnDate != "" { onDateStart, onDateEnd := params.GetOnDateMillis() @@ -1481,54 +1480,64 @@ func (s *SqlPostStore) buildCreateDateFilterClause(params *model.SearchParams, q queryParams["OnDateEnd"] = strconv.FormatInt(onDateEnd, 10) // between `on date` start of day and end of day - searchQuery += "AND CreateAt BETWEEN :OnDateStart AND :OnDateEnd " - } else { - - if params.ExcludedDate != "" { - excludedDateStart, excludedDateEnd := params.GetExcludedDateMillis() - queryParams["ExcludedDateStart"] = strconv.FormatInt(excludedDateStart, 10) - queryParams["ExcludedDateEnd"] = strconv.FormatInt(excludedDateEnd, 10) - - searchQuery += "AND CreateAt NOT BETWEEN :ExcludedDateStart AND :ExcludedDateEnd " - } - - if params.AfterDate != "" { - afterDate := params.GetAfterDateMillis() - queryParams["AfterDate"] = strconv.FormatInt(afterDate, 10) - - // greater than `after date` - searchQuery += "AND CreateAt >= :AfterDate " - } - - if params.BeforeDate != "" { - beforeDate := params.GetBeforeDateMillis() - queryParams["BeforeDate"] = strconv.FormatInt(beforeDate, 10) - - // less than `before date` - searchQuery += "AND CreateAt <= :BeforeDate " - } - - if params.ExcludedAfterDate != "" { - afterDate := params.GetExcludedAfterDateMillis() - queryParams["ExcludedAfterDate"] = strconv.FormatInt(afterDate, 10) - - searchQuery += "AND CreateAt < :ExcludedAfterDate " - } - - if params.ExcludedBeforeDate != "" { - beforeDate := params.GetExcludedBeforeDateMillis() - queryParams["ExcludedBeforeDate"] = strconv.FormatInt(beforeDate, 10) - - searchQuery += "AND CreateAt > :ExcludedBeforeDate " - } + builder = builder.Where("CreateAt BETWEEN :OnDateStart AND :OnDateEnd") + return builder, queryParams } - return searchQuery, queryParams + if params.ExcludedDate != "" { + excludedDateStart, excludedDateEnd := params.GetExcludedDateMillis() + queryParams["ExcludedDateStart"] = strconv.FormatInt(excludedDateStart, 10) + queryParams["ExcludedDateEnd"] = strconv.FormatInt(excludedDateEnd, 10) + + builder = builder.Where("CreateAt NOT BETWEEN :ExcludedDateStart AND :ExcludedDateEnd") + } + + if params.AfterDate != "" { + afterDate := params.GetAfterDateMillis() + queryParams["AfterDate"] = strconv.FormatInt(afterDate, 10) + + // greater than `after date` + builder = builder.Where("CreateAt >= :AfterDate") + } + + if params.BeforeDate != "" { + beforeDate := params.GetBeforeDateMillis() + queryParams["BeforeDate"] = strconv.FormatInt(beforeDate, 10) + + // less than `before date` + builder = builder.Where("CreateAt <= :BeforeDate") + } + + if params.ExcludedAfterDate != "" { + afterDate := params.GetExcludedAfterDateMillis() + queryParams["ExcludedAfterDate"] = strconv.FormatInt(afterDate, 10) + + builder = builder.Where("CreateAt < :ExcludedAfterDate") + } + + if params.ExcludedBeforeDate != "" { + beforeDate := params.GetExcludedBeforeDateMillis() + queryParams["ExcludedBeforeDate"] = strconv.FormatInt(beforeDate, 10) + + builder = builder.Where("CreateAt > :ExcludedBeforeDate") + } + + return builder, queryParams } -func (s *SqlPostStore) buildSearchChannelFilterClause(channels []string, paramPrefix string, exclusion bool, queryParams map[string]interface{}, byName bool) (string, map[string]interface{}) { +func (s *SqlPostStore) buildSearchTeamFilterClause(teamId string, queryParams map[string]interface{}, builder sq.SelectBuilder) (sq.SelectBuilder, map[string]interface{}) { + if teamId == "" { + return builder, queryParams + } + + queryParams["TeamId"] = teamId + + return builder.Where("(TeamId = :TeamId OR TeamId = '')"), queryParams +} + +func (s *SqlPostStore) buildSearchChannelFilterClause(channels []string, paramPrefix string, exclusion bool, queryParams map[string]interface{}, byName bool, builder sq.SelectBuilder) (sq.SelectBuilder, map[string]interface{}) { if len(channels) == 0 { - return "", queryParams + return builder, queryParams } clauseSlice := []string{} @@ -1540,15 +1549,15 @@ func (s *SqlPostStore) buildSearchChannelFilterClause(channels []string, paramPr clause := strings.Join(clauseSlice, ", ") if byName { if exclusion { - return "AND Name NOT IN (" + clause + ")", queryParams + return builder.Where("Name NOT IN (" + clause + ")"), queryParams } - return "AND Name IN (" + clause + ")", queryParams + return builder.Where("Name IN (" + clause + ")"), queryParams } if exclusion { - return "AND Id NOT IN (" + clause + ")", queryParams + return builder.Where("Id NOT IN (" + clause + ")"), queryParams } - return "AND Id IN (" + clause + ")", queryParams + return builder.Where("Id IN (" + clause + ")"), queryParams } func (s *SqlPostStore) buildSearchUserFilterClause(users []string, paramPrefix string, exclusion bool, queryParams map[string]interface{}, byUsername bool) (string, map[string]interface{}) { @@ -1574,13 +1583,13 @@ func (s *SqlPostStore) buildSearchUserFilterClause(users []string, paramPrefix s return "AND Id IN (" + clause + ")", queryParams } -func (s *SqlPostStore) buildSearchPostFilterClause(fromUsers []string, excludedUsers []string, queryParams map[string]interface{}, userByUsername bool) (string, map[string]interface{}) { +func (s *SqlPostStore) buildSearchPostFilterClause(fromUsers []string, excludedUsers []string, queryParams map[string]interface{}, userByUsername bool, builder sq.SelectBuilder) (sq.SelectBuilder, map[string]interface{}) { if len(fromUsers) == 0 && len(excludedUsers) == 0 { - return "", queryParams + return builder, queryParams } filterQuery := ` - AND UserId IN ( + UserId IN ( SELECT Id FROM @@ -1598,7 +1607,7 @@ func (s *SqlPostStore) buildSearchPostFilterClause(fromUsers []string, excludedU excludedUserClause, queryParams := s.buildSearchUserFilterClause(excludedUsers, "ExcludedUser", true, queryParams, userByUsername) filterQuery = strings.Replace(filterQuery, "EXCLUDED_USER_FILTER", excludedUserClause, 1) - return filterQuery, queryParams + return builder.Where(filterQuery), queryParams } func (s *SqlPostStore) Search(teamId string, userId string, params *model.SearchParams) (*model.PostList, error) { @@ -1607,7 +1616,6 @@ func (s *SqlPostStore) Search(teamId string, userId string, params *model.Search func (s *SqlPostStore) search(teamId string, userId string, params *model.SearchParams, channelsByName bool, userByUsername bool) (*model.PostList, error) { queryParams := map[string]interface{}{ - "TeamId": teamId, "UserId": userId, } @@ -1619,56 +1627,17 @@ func (s *SqlPostStore) search(teamId string, userId string, params *model.Search return list, nil } - var posts []*model.Post + baseQuery := s.getQueryBuilder().Select( + "*", + "(SELECT COUNT(Posts.Id) FROM Posts WHERE Posts.RootId = (CASE WHEN q2.RootId = '' THEN q2.Id ELSE q2.RootId END) AND Posts.DeleteAt = 0) as ReplyCount", + ).From("Posts q2"). + Where("DeleteAt = 0"). + Where(fmt.Sprintf("Type NOT LIKE '%s%%'", model.PostSystemMessagePrefix)). + OrderByClause("CreateAt DESC"). + Limit(100) - deletedQueryPart := "AND DeleteAt = 0" - if params.IncludeDeletedChannels { - deletedQueryPart = "" - } - - userIdPart := "AND UserId = :UserId" - if params.SearchWithoutUserId { - userIdPart = "" - } - - searchQuery := ` - SELECT - * ,(SELECT COUNT(Posts.Id) FROM Posts WHERE Posts.RootId = (CASE WHEN q2.RootId = '' THEN q2.Id ELSE q2.RootId END) AND Posts.DeleteAt = 0) as ReplyCount - FROM - Posts q2 - WHERE - DeleteAt = 0 - AND Type NOT LIKE '` + model.PostSystemMessagePrefix + `%' - POST_FILTER - AND ChannelId IN ( - SELECT - Id - FROM - Channels, - ChannelMembers - WHERE - Id = ChannelId - AND (TeamId = :TeamId OR TeamId = '') - ` + userIdPart + ` - ` + deletedQueryPart + ` - IN_CHANNEL_FILTER - EXCLUDED_CHANNEL_FILTER) - CREATEDATE_CLAUSE - SEARCH_CLAUSE - ORDER BY CreateAt DESC - LIMIT 100` - - inChannelClause, queryParams := s.buildSearchChannelFilterClause(params.InChannels, "InChannel", false, queryParams, channelsByName) - searchQuery = strings.Replace(searchQuery, "IN_CHANNEL_FILTER", inChannelClause, 1) - - excludedChannelClause, queryParams := s.buildSearchChannelFilterClause(params.ExcludedChannels, "ExcludedChannel", true, queryParams, channelsByName) - searchQuery = strings.Replace(searchQuery, "EXCLUDED_CHANNEL_FILTER", excludedChannelClause, 1) - - postFilterClause, queryParams := s.buildSearchPostFilterClause(params.FromUsers, params.ExcludedUsers, queryParams, userByUsername) - searchQuery = strings.Replace(searchQuery, "POST_FILTER", postFilterClause, 1) - - createDateFilterClause, queryParams := s.buildCreateDateFilterClause(params, queryParams) - searchQuery = strings.Replace(searchQuery, "CREATEDATE_CLAUSE", createDateFilterClause, 1) + baseQuery, queryParams = s.buildSearchPostFilterClause(params.FromUsers, params.ExcludedUsers, queryParams, userByUsername, baseQuery) + baseQuery, queryParams = s.buildCreateDateFilterClause(params, queryParams, baseQuery) termMap := map[string]bool{} terms := params.Terms @@ -1690,7 +1659,6 @@ func (s *SqlPostStore) search(teamId string, userId string, params *model.Search if terms == "" && excludedTerms == "" { // we've already confirmed that we have a channel or user to search for - searchQuery = strings.Replace(searchQuery, "SEARCH_CLAUSE", "", 1) } else if s.DriverName() == model.DatabaseDriverPostgres { // Parse text for wildcards if wildcard, err := regexp.Compile(`\*($| )`); err == nil { @@ -1711,8 +1679,8 @@ func (s *SqlPostStore) search(teamId string, userId string, params *model.Search queryParams["Terms"] = "(" + strings.Join(strings.Fields(terms), " & ") + ")" + excludeClause } - searchClause := fmt.Sprintf("AND to_tsvector('english', %s) @@ to_tsquery('english', :Terms)", searchType) - searchQuery = strings.Replace(searchQuery, "SEARCH_CLAUSE", searchClause, 1) + searchClause := fmt.Sprintf("to_tsvector('english', %s) @@ to_tsquery('english', :Terms)", searchType) + baseQuery = baseQuery.Where(searchClause) } else if s.DriverName() == model.DatabaseDriverMysql { if searchType == "Message" { var err error @@ -1726,8 +1694,8 @@ func (s *SqlPostStore) search(teamId string, userId string, params *model.Search } } - searchClause := fmt.Sprintf("AND MATCH (%s) AGAINST (:Terms IN BOOLEAN MODE)", searchType) - searchQuery = strings.Replace(searchQuery, "SEARCH_CLAUSE", searchClause, 1) + searchClause := fmt.Sprintf("MATCH (%s) AGAINST (:Terms IN BOOLEAN MODE)", searchType) + baseQuery = baseQuery.Where(searchClause) excludeClause := "" if excludedTerms != "" { @@ -1745,7 +1713,37 @@ func (s *SqlPostStore) search(teamId string, userId string, params *model.Search } } - _, err := s.GetSearchReplica().Select(&posts, searchQuery, queryParams) + inQuery := s.getQueryBuilder().Select("Id"). + From("Channels, ChannelMembers"). + Where("Id = ChannelId") + + if !params.IncludeDeletedChannels { + inQuery = inQuery.Where("DeleteAt = 0") + } + + if !params.SearchWithoutUserId { + inQuery = inQuery.Where("UserId = :UserId") + } + + inQuery, queryParams = s.buildSearchTeamFilterClause(teamId, queryParams, inQuery) + inQuery, queryParams = s.buildSearchChannelFilterClause(params.InChannels, "InChannel", false, queryParams, channelsByName, inQuery) + inQuery, queryParams = s.buildSearchChannelFilterClause(params.ExcludedChannels, "ExcludedChannel", true, queryParams, channelsByName, inQuery) + + inQueryClause, _, err := inQuery.ToSql() + if err != nil { + return nil, err + } + + baseQuery = baseQuery.Where(fmt.Sprintf("ChannelId IN (%s)", inQueryClause)) + + searchQuery, _, err := baseQuery.ToSql() + if err != nil { + return nil, err + } + + var posts []*model.Post + + _, err = s.GetSearchReplica().Select(&posts, searchQuery, queryParams) if err != nil { mlog.Warn("Query error searching posts.", mlog.Err(err)) // Don't return the error to the caller as it is of no use to the user. Instead return an empty set of search results. @@ -2288,7 +2286,7 @@ func (s *SqlPostStore) GetDirectPostParentsForExportAfter(limit int, afterId str } //nolint:unparam -func (s *SqlPostStore) SearchPostsInTeamForUser(paramsList []*model.SearchParams, userId, teamId string, page, perPage int) (*model.PostSearchResults, error) { +func (s *SqlPostStore) SearchPostsForUser(paramsList []*model.SearchParams, userId, teamId string, page, perPage int) (*model.PostSearchResults, error) { // Since we don't support paging for DB search, we just return nothing for later pages if page > 0 { return model.MakePostSearchResults(model.NewPostList(), nil), nil diff --git a/store/store.go b/store/store.go index 7fd6f3c58b8..be4cdadfc2d 100644 --- a/store/store.go +++ b/store/store.go @@ -350,7 +350,7 @@ type PostStore interface { GetParentsForExportAfter(limit int, afterID string) ([]*model.PostForExport, error) GetRepliesForExport(parentID string) ([]*model.ReplyForExport, error) GetDirectPostParentsForExportAfter(limit int, afterID string) ([]*model.DirectPostForExport, error) - SearchPostsInTeamForUser(paramsList []*model.SearchParams, userID, teamID string, page, perPage int) (*model.PostSearchResults, error) + SearchPostsForUser(paramsList []*model.SearchParams, userID, teamID string, page, perPage int) (*model.PostSearchResults, error) GetOldestEntityCreationTime() (int64, error) HasAutoResponsePostByUserSince(options model.GetPostsSinceOptions, userId string) (bool, error) GetPostsSinceForSync(options model.GetPostsSinceForSyncOptions, cursor model.GetPostsSinceForSyncCursor, limit int) ([]*model.Post, model.GetPostsSinceForSyncCursor, error) diff --git a/store/storetest/mocks/PostStore.go b/store/storetest/mocks/PostStore.go index 53962be6e18..5b91492e827 100644 --- a/store/storetest/mocks/PostStore.go +++ b/store/storetest/mocks/PostStore.go @@ -867,8 +867,8 @@ func (_m *PostStore) Search(teamID string, userID string, params *model.SearchPa return r0, r1 } -// SearchPostsInTeamForUser provides a mock function with given fields: paramsList, userID, teamID, page, perPage -func (_m *PostStore) SearchPostsInTeamForUser(paramsList []*model.SearchParams, userID string, teamID string, page int, perPage int) (*model.PostSearchResults, error) { +// SearchPostsForUser provides a mock function with given fields: paramsList, userID, teamID, page, perPage +func (_m *PostStore) SearchPostsForUser(paramsList []*model.SearchParams, userID string, teamID string, page int, perPage int) (*model.PostSearchResults, error) { ret := _m.Called(paramsList, userID, teamID, page, perPage) var r0 *model.PostSearchResults diff --git a/store/timerlayer/timerlayer.go b/store/timerlayer/timerlayer.go index 07bf8421d21..40f80990e5f 100644 --- a/store/timerlayer/timerlayer.go +++ b/store/timerlayer/timerlayer.go @@ -5193,10 +5193,10 @@ func (s *TimerLayerPostStore) Search(teamID string, userID string, params *model return result, err } -func (s *TimerLayerPostStore) SearchPostsInTeamForUser(paramsList []*model.SearchParams, userID string, teamID string, page int, perPage int) (*model.PostSearchResults, error) { +func (s *TimerLayerPostStore) SearchPostsForUser(paramsList []*model.SearchParams, userID string, teamID string, page int, perPage int) (*model.PostSearchResults, error) { start := timemodule.Now() - result, err := s.PostStore.SearchPostsInTeamForUser(paramsList, userID, teamID, page, perPage) + result, err := s.PostStore.SearchPostsForUser(paramsList, userID, teamID, page, perPage) elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second) if s.Root.Metrics != nil { @@ -5204,7 +5204,7 @@ func (s *TimerLayerPostStore) SearchPostsInTeamForUser(paramsList []*model.Searc if err == nil { success = "true" } - s.Root.Metrics.ObserveStoreMethodDuration("PostStore.SearchPostsInTeamForUser", success, elapsed) + s.Root.Metrics.ObserveStoreMethodDuration("PostStore.SearchPostsForUser", success, elapsed) } return result, err }