diff --git a/app/user.go b/app/user.go index d204ffe304..0dc7a71502 100644 --- a/app/user.go +++ b/app/user.go @@ -583,11 +583,7 @@ func (a *App) GetUsersWithoutTeamPage(page int, perPage int, asAdmin bool, viewR } func (a *App) GetUsersWithoutTeam(offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) { - result := <-a.Srv.Store.User().GetProfilesWithoutTeam(offset, limit, viewRestrictions) - if result.Err != nil { - return nil, result.Err - } - return result.Data.([]*model.User), nil + return a.Srv.Store.User().GetProfilesWithoutTeam(offset, limit, viewRestrictions) } // GetTeamGroupUsers returns the users who are associated to the team via GroupTeams and GroupMembers. diff --git a/store/sqlstore/user_store.go b/store/sqlstore/user_store.go index a5b2e12726..073565920e 100644 --- a/store/sqlstore/user_store.go +++ b/store/sqlstore/user_store.go @@ -673,41 +673,37 @@ func (us SqlUserStore) GetProfilesNotInChannel(teamId string, channelId string, return users, nil } -func (us SqlUserStore) GetProfilesWithoutTeam(offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - query := us.usersQuery. - Where(`( - SELECT - COUNT(0) - FROM - TeamMembers - WHERE - TeamMembers.UserId = u.Id - AND TeamMembers.DeleteAt = 0 - ) = 0`). - OrderBy("u.Username ASC"). - Offset(uint64(offset)).Limit(uint64(limit)) +func (us SqlUserStore) GetProfilesWithoutTeam(offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) { + query := us.usersQuery. + Where(`( + SELECT + COUNT(0) + FROM + TeamMembers + WHERE + TeamMembers.UserId = u.Id + AND TeamMembers.DeleteAt = 0 + ) = 0`). + OrderBy("u.Username ASC"). + Offset(uint64(offset)).Limit(uint64(limit)) - query = applyViewRestrictionsFilter(query, viewRestrictions, true) + query = applyViewRestrictionsFilter(query, viewRestrictions, true) - queryString, args, err := query.ToSql() - if err != nil { - result.Err = model.NewAppError("SqlUserStore.GetProfilesWithoutTeam", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError) - return - } + queryString, args, err := query.ToSql() + if err != nil { + return nil, model.NewAppError("SqlUserStore.GetProfilesWithoutTeam", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError) + } - var users []*model.User - if _, err := us.GetReplica().Select(&users, queryString, args...); err != nil { - result.Err = model.NewAppError("SqlUserStore.GetProfilesWithoutTeam", "store.sql_user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError) - return - } + var users []*model.User + if _, err := us.GetReplica().Select(&users, queryString, args...); err != nil { + return nil, model.NewAppError("SqlUserStore.GetProfilesWithoutTeam", "store.sql_user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError) + } - for _, u := range users { - u.Sanitize(map[string]bool{}) - } + for _, u := range users { + u.Sanitize(map[string]bool{}) + } - result.Data = users - }) + return users, nil } func (us SqlUserStore) GetProfilesByUsernames(usernames []string, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) { diff --git a/store/store.go b/store/store.go index 8a518a2c0f..2079827c9e 100644 --- a/store/store.go +++ b/store/store.go @@ -267,7 +267,7 @@ type UserStore interface { GetProfilesInChannelByStatus(channelId string, offset int, limit int) StoreChannel GetAllProfilesInChannel(channelId string, allowFromCache bool) (map[string]*model.User, *model.AppError) GetProfilesNotInChannel(teamId string, channelId string, groupConstrained bool, offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) - GetProfilesWithoutTeam(offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) StoreChannel + GetProfilesWithoutTeam(offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) GetProfilesByUsernames(usernames []string, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) GetAllProfiles(options *model.UserGetOptions) ([]*model.User, *model.AppError) GetProfiles(options *model.UserGetOptions) ([]*model.User, *model.AppError) diff --git a/store/storetest/mocks/UserStore.go b/store/storetest/mocks/UserStore.go index 785907231f..18fec63a68 100644 --- a/store/storetest/mocks/UserStore.go +++ b/store/storetest/mocks/UserStore.go @@ -674,19 +674,28 @@ func (_m *UserStore) GetProfilesNotInTeam(teamId string, groupConstrained bool, } // GetProfilesWithoutTeam provides a mock function with given fields: offset, limit, viewRestrictions -func (_m *UserStore) GetProfilesWithoutTeam(offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) store.StoreChannel { +func (_m *UserStore) GetProfilesWithoutTeam(offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) { ret := _m.Called(offset, limit, viewRestrictions) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(int, int, *model.ViewUsersRestrictions) store.StoreChannel); ok { + var r0 []*model.User + if rf, ok := ret.Get(0).(func(int, int, *model.ViewUsersRestrictions) []*model.User); ok { r0 = rf(offset, limit, viewRestrictions) } 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(int, int, *model.ViewUsersRestrictions) *model.AppError); ok { + r1 = rf(offset, limit, viewRestrictions) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 } // GetRecentlyActiveUsersForTeam provides a mock function with given fields: teamId, offset, limit, viewRestrictions diff --git a/store/storetest/user_store.go b/store/storetest/user_store.go index a4fb552b9b..be925eba1f 100644 --- a/store/storetest/user_store.go +++ b/store/storetest/user_store.go @@ -886,21 +886,21 @@ func testUserStoreGetProfilesWithoutTeam(t *testing.T, ss store.Store) { defer func() { require.Nil(t, ss.Bot().PermanentDelete(u3.Id)) }() t.Run("get, offset 0, limit 100", func(t *testing.T) { - result := <-ss.User().GetProfilesWithoutTeam(0, 100, nil) - require.Nil(t, result.Err) - assert.Equal(t, []*model.User{sanitized(u2), sanitized(u3)}, result.Data.([]*model.User)) + users, err := ss.User().GetProfilesWithoutTeam(0, 100, nil) + require.Nil(t, err) + assert.Equal(t, []*model.User{sanitized(u2), sanitized(u3)}, users) }) t.Run("get, offset 1, limit 1", func(t *testing.T) { - result := <-ss.User().GetProfilesWithoutTeam(1, 1, nil) - require.Nil(t, result.Err) - assert.Equal(t, []*model.User{sanitized(u3)}, result.Data.([]*model.User)) + users, err := ss.User().GetProfilesWithoutTeam(1, 1, nil) + require.Nil(t, err) + assert.Equal(t, []*model.User{sanitized(u3)}, users) }) t.Run("get, offset 2, limit 1", func(t *testing.T) { - result := <-ss.User().GetProfilesWithoutTeam(2, 1, nil) - require.Nil(t, result.Err) - assert.Equal(t, []*model.User{}, result.Data.([]*model.User)) + users, err := ss.User().GetProfilesWithoutTeam(2, 1, nil) + require.Nil(t, err) + assert.Equal(t, []*model.User{}, users) }) }