MM-16564 Migrate "User.GetRecentlyActiveUsersForTeam" to Sync by default #11392 (#11399)

Этот коммит содержится в:
lassimus
2019-06-26 15:46:38 -04:00
коммит произвёл Harrison Healey
родитель 8cdf5ffe67
Коммит 8841e3dab9
5 изменённых файлов: 55 добавлений и 53 удалений

Просмотреть файл

@@ -297,12 +297,11 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
} }
func (a *App) GetRecentlyActiveUsersForTeam(teamId string) (map[string]*model.User, *model.AppError) { func (a *App) GetRecentlyActiveUsersForTeam(teamId string) (map[string]*model.User, *model.AppError) {
result := <-a.Srv.Store.User().GetRecentlyActiveUsersForTeam(teamId, 0, 100, nil) users, err := a.Srv.Store.User().GetRecentlyActiveUsersForTeam(teamId, 0, 100, nil)
if result.Err != nil { if err != nil {
return nil, result.Err return nil, err
} }
users := result.Data.([]*model.User)
userMap := make(map[string]*model.User) userMap := make(map[string]*model.User)
for _, user := range users { for _, user := range users {
@@ -313,12 +312,10 @@ func (a *App) GetRecentlyActiveUsersForTeam(teamId string) (map[string]*model.Us
} }
func (a *App) GetRecentlyActiveUsersForTeamPage(teamId string, page, perPage int, asAdmin bool, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) { func (a *App) GetRecentlyActiveUsersForTeamPage(teamId string, page, perPage int, asAdmin bool, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) {
var users []*model.User users, err := a.Srv.Store.User().GetRecentlyActiveUsersForTeam(teamId, page*perPage, perPage, viewRestrictions)
result := <-a.Srv.Store.User().GetRecentlyActiveUsersForTeam(teamId, page*perPage, perPage, viewRestrictions) if err != nil {
if result.Err != nil { return nil, err
return nil, result.Err
} }
users = result.Data.([]*model.User)
return a.sanitizeProfiles(users, asAdmin), nil return a.sanitizeProfiles(users, asAdmin), nil
} }

Просмотреть файл

@@ -787,41 +787,37 @@ type UserWithLastActivityAt struct {
LastActivityAt int64 LastActivityAt int64
} }
func (us SqlUserStore) GetRecentlyActiveUsersForTeam(teamId string, offset, limit int, viewRestrictions *model.ViewUsersRestrictions) store.StoreChannel { func (us SqlUserStore) GetRecentlyActiveUsersForTeam(teamId string, offset, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) {
return store.Do(func(result *store.StoreResult) { query := us.usersQuery.
query := us.usersQuery. Column("s.LastActivityAt").
Column("s.LastActivityAt"). Join("TeamMembers tm ON (tm.UserId = u.Id AND tm.TeamId = ?)", teamId).
Join("TeamMembers tm ON (tm.UserId = u.Id AND tm.TeamId = ?)", teamId). Join("Status s ON (s.UserId = u.Id)").
Join("Status s ON (s.UserId = u.Id)"). OrderBy("s.LastActivityAt DESC").
OrderBy("s.LastActivityAt DESC"). OrderBy("u.Username ASC").
OrderBy("u.Username ASC"). Offset(uint64(offset)).Limit(uint64(limit))
Offset(uint64(offset)).Limit(uint64(limit))
query = applyViewRestrictionsFilter(query, viewRestrictions, true) query = applyViewRestrictionsFilter(query, viewRestrictions, true)
queryString, args, err := query.ToSql() queryString, args, err := query.ToSql()
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlUserStore.GetRecentlyActiveUsers", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlUserStore.GetRecentlyActiveUsers", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
return }
}
var users []*UserWithLastActivityAt var users []*UserWithLastActivityAt
if _, err := us.GetReplica().Select(&users, queryString, args...); err != nil { if _, err := us.GetReplica().Select(&users, queryString, args...); err != nil {
result.Err = model.NewAppError("SqlUserStore.GetRecentlyActiveUsers", "store.sql_user.get_recently_active_users.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlUserStore.GetRecentlyActiveUsers", "store.sql_user.get_recently_active_users.app_error", nil, err.Error(), http.StatusInternalServerError)
return }
}
userList := []*model.User{} userList := []*model.User{}
for _, userWithLastActivityAt := range users { for _, userWithLastActivityAt := range users {
u := userWithLastActivityAt.User u := userWithLastActivityAt.User
u.Sanitize(map[string]bool{}) u.Sanitize(map[string]bool{})
u.LastActivityAt = userWithLastActivityAt.LastActivityAt u.LastActivityAt = userWithLastActivityAt.LastActivityAt
userList = append(userList, &u) userList = append(userList, &u)
} }
result.Data = userList return userList, nil
})
} }
func (us SqlUserStore) GetNewUsersForTeam(teamId string, offset, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) { func (us SqlUserStore) GetNewUsersForTeam(teamId string, offset, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) {

Просмотреть файл

@@ -286,7 +286,7 @@ type UserStore interface {
GetUnreadCount(userId string) (int64, error) GetUnreadCount(userId string) (int64, error)
GetUnreadCountForChannel(userId string, channelId string) StoreChannel GetUnreadCountForChannel(userId string, channelId string) StoreChannel
GetAnyUnreadPostCountForChannel(userId string, channelId string) (int64, *model.AppError) GetAnyUnreadPostCountForChannel(userId string, channelId string) (int64, *model.AppError)
GetRecentlyActiveUsersForTeam(teamId string, offset, limit int, viewRestrictions *model.ViewUsersRestrictions) StoreChannel 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) 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) 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) StoreChannel

Просмотреть файл

@@ -576,19 +576,28 @@ func (_m *UserStore) GetProfilesWithoutTeam(offset int, limit int, viewRestricti
} }
// GetRecentlyActiveUsersForTeam provides a mock function with given fields: teamId, offset, limit, viewRestrictions // GetRecentlyActiveUsersForTeam provides a mock function with given fields: teamId, offset, limit, viewRestrictions
func (_m *UserStore) GetRecentlyActiveUsersForTeam(teamId string, offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) store.StoreChannel { func (_m *UserStore) GetRecentlyActiveUsersForTeam(teamId string, offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) {
ret := _m.Called(teamId, offset, limit, viewRestrictions) ret := _m.Called(teamId, offset, limit, viewRestrictions)
var r0 store.StoreChannel var r0 []*model.User
if rf, ok := ret.Get(0).(func(string, int, int, *model.ViewUsersRestrictions) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, int, int, *model.ViewUsersRestrictions) []*model.User); ok {
r0 = rf(teamId, offset, limit, viewRestrictions) r0 = rf(teamId, offset, limit, viewRestrictions)
} else { } else {
if ret.Get(0) != nil { 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, int, int, *model.ViewUsersRestrictions) *model.AppError); ok {
r1 = rf(teamId, offset, limit, viewRestrictions)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetSystemAdminProfiles provides a mock function with given fields: // GetSystemAdminProfiles provides a mock function with given fields:

Просмотреть файл

@@ -1973,29 +1973,29 @@ func testUserStoreGetRecentlyActiveUsersForTeam(t *testing.T, ss store.Store) {
require.Nil(t, ss.Status().SaveOrUpdate(&model.Status{UserId: u3.Id, Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: u3.LastActivityAt, ActiveChannel: ""})) require.Nil(t, ss.Status().SaveOrUpdate(&model.Status{UserId: u3.Id, Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: u3.LastActivityAt, ActiveChannel: ""}))
t.Run("get team 1, offset 0, limit 100", func(t *testing.T) { t.Run("get team 1, offset 0, limit 100", func(t *testing.T) {
result := <-ss.User().GetRecentlyActiveUsersForTeam(teamId, 0, 100, nil) users, err := ss.User().GetRecentlyActiveUsersForTeam(teamId, 0, 100, nil)
require.Nil(t, result.Err) require.Nil(t, err)
assert.Equal(t, []*model.User{ assert.Equal(t, []*model.User{
sanitized(u3), sanitized(u3),
sanitized(u1), sanitized(u1),
sanitized(u2), sanitized(u2),
}, result.Data.([]*model.User)) }, users)
}) })
t.Run("get team 1, offset 0, limit 1", func(t *testing.T) { t.Run("get team 1, offset 0, limit 1", func(t *testing.T) {
result := <-ss.User().GetRecentlyActiveUsersForTeam(teamId, 0, 1, nil) users, err := ss.User().GetRecentlyActiveUsersForTeam(teamId, 0, 1, nil)
require.Nil(t, result.Err) require.Nil(t, err)
assert.Equal(t, []*model.User{ assert.Equal(t, []*model.User{
sanitized(u3), sanitized(u3),
}, result.Data.([]*model.User)) }, users)
}) })
t.Run("get team 1, offset 2, limit 1", func(t *testing.T) { t.Run("get team 1, offset 2, limit 1", func(t *testing.T) {
result := <-ss.User().GetRecentlyActiveUsersForTeam(teamId, 2, 1, nil) users, err := ss.User().GetRecentlyActiveUsersForTeam(teamId, 2, 1, nil)
require.Nil(t, result.Err) require.Nil(t, err)
assert.Equal(t, []*model.User{ assert.Equal(t, []*model.User{
sanitized(u2), sanitized(u2),
}, result.Data.([]*model.User)) }, users)
}) })
} }