From f8ba59ca9e8e822fbe7ded0d8888013cbd238497 Mon Sep 17 00:00:00 2001 From: Deepak Sah Date: Sat, 29 Jun 2019 03:54:46 +0530 Subject: [PATCH] [MM-16567] Migrate "User.SearchNotInTeam" to Sync by default (#11471) --- app/user.go | 7 +++---- store/sqlstore/user_store.go | 26 ++++++++++++++------------ store/store.go | 2 +- store/storetest/mocks/UserStore.go | 19 ++++++++++++++----- store/storetest/user_store.go | 6 +++--- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/app/user.go b/app/user.go index 16eed8b2e4..45cb840bdd 100644 --- a/app/user.go +++ b/app/user.go @@ -1758,11 +1758,10 @@ func (a *App) SearchUsersInTeam(teamId, term string, options *model.UserSearchOp 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 + users, err := a.Srv.Store.User().SearchNotInTeam(notInTeamId, term, options) + if err != nil { + return nil, err } - users := result.Data.([]*model.User) for _, user := range users { a.SanitizeProfile(user, options.IsAdmin) diff --git a/store/sqlstore/user_store.go b/store/sqlstore/user_store.go index 592379ae14..9c45f958ce 100644 --- a/store/sqlstore/user_store.go +++ b/store/sqlstore/user_store.go @@ -1265,20 +1265,22 @@ func (us SqlUserStore) SearchWithoutTeam(term string, options *model.UserSearchO return result.Data.([]*model.User), nil } -func (us SqlUserStore) SearchNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - query := us.usersQuery. - LeftJoin("TeamMembers tm ON ( tm.UserId = u.Id AND tm.DeleteAt = 0 AND tm.TeamId = ? )", notInTeamId). - Where("tm.UserId IS NULL"). - OrderBy("u.Username ASC"). - Limit(uint64(options.Limit)) +func (us SqlUserStore) SearchNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { + query := us.usersQuery. + LeftJoin("TeamMembers tm ON ( tm.UserId = u.Id AND tm.DeleteAt = 0 AND tm.TeamId = ? )", notInTeamId). + Where("tm.UserId IS NULL"). + OrderBy("u.Username ASC"). + Limit(uint64(options.Limit)) - if options.GroupConstrained { - query = applyTeamGroupConstrainedFilter(query, notInTeamId) - } + if options.GroupConstrained { + query = applyTeamGroupConstrainedFilter(query, notInTeamId) + } - *result = us.performSearch(query, term, options) - }) + result := us.performSearch(query, term, options) + if result.Err != nil { + return nil, result.Err + } + return result.Data.([]*model.User), nil } func (us SqlUserStore) SearchNotInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) store.StoreChannel { diff --git a/store/store.go b/store/store.go index 1dfc0ec3f5..7bcdd6a7b8 100644 --- a/store/store.go +++ b/store/store.go @@ -289,7 +289,7 @@ type UserStore interface { GetRecentlyActiveUsersForTeam(teamId string, offset, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) GetNewUsersForTeam(teamId string, offset, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) Search(teamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) - SearchNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) StoreChannel + SearchNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) SearchInChannel(channelId string, term string, options *model.UserSearchOptions) StoreChannel SearchNotInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) StoreChannel SearchWithoutTeam(term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) diff --git a/store/storetest/mocks/UserStore.go b/store/storetest/mocks/UserStore.go index 9aa9265517..55e695463f 100644 --- a/store/storetest/mocks/UserStore.go +++ b/store/storetest/mocks/UserStore.go @@ -856,19 +856,28 @@ func (_m *UserStore) SearchNotInChannel(teamId string, channelId string, term st } // SearchNotInTeam provides a mock function with given fields: notInTeamId, term, options -func (_m *UserStore) SearchNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) store.StoreChannel { +func (_m *UserStore) SearchNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { ret := _m.Called(notInTeamId, term, options) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, string, *model.UserSearchOptions) store.StoreChannel); ok { + var r0 []*model.User + if rf, ok := ret.Get(0).(func(string, string, *model.UserSearchOptions) []*model.User); ok { r0 = rf(notInTeamId, term, options) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).([]*model.User) } } - return r0 + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string, string, *model.UserSearchOptions) *model.AppError); ok { + r1 = rf(notInTeamId, term, options) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 } // SearchWithoutTeam provides a mock function with given fields: term, options diff --git a/store/storetest/user_store.go b/store/storetest/user_store.go index b234c0d07c..65c3134442 100644 --- a/store/storetest/user_store.go +++ b/store/storetest/user_store.go @@ -3005,13 +3005,13 @@ func testUserStoreSearchNotInTeam(t *testing.T, ss store.Store) { for _, testCase := range testCases { t.Run(testCase.Description, func(t *testing.T) { - result := <-ss.User().SearchNotInTeam( + users, err := ss.User().SearchNotInTeam( testCase.TeamId, testCase.Term, testCase.Options, ) - require.Nil(t, result.Err) - assertUsers(t, testCase.Expected, result.Data.([]*model.User)) + require.Nil(t, err) + assertUsers(t, testCase.Expected, users) }) } }