From 5507154992eedd323385c37e59b2008586b9aaa0 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Fri, 30 Jun 2017 12:07:23 -0400 Subject: Add some basic sorting support for GET /users endpoint (#6801) --- store/sql_user_store.go | 48 +++++++++++++++++++++++++++++++++++++------- store/sql_user_store_test.go | 17 +++++++++++++++- store/store.go | 3 ++- 3 files changed, 59 insertions(+), 9 deletions(-) (limited to 'store') diff --git a/store/sql_user_store.go b/store/sql_user_store.go index 4aa6f6cfe..ab031ea19 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -756,7 +756,7 @@ type UserWithLastActivityAt struct { LastActivityAt int64 } -func (us SqlUserStore) GetRecentlyActiveUsersForTeam(teamId string) StoreChannel { +func (us SqlUserStore) GetRecentlyActiveUsersForTeam(teamId string, offset, limit int) StoreChannel { storeChannel := make(StoreChannel) @@ -774,21 +774,55 @@ func (us SqlUserStore) GetRecentlyActiveUsersForTeam(teamId string) StoreChannel INNER JOIN Status AS s ON s.UserId = t.UserId WHERE t.TeamId = :TeamId ORDER BY s.LastActivityAt DESC - LIMIT 100 - `, map[string]interface{}{"TeamId": teamId}); err != nil { - result.Err = model.NewLocAppError("SqlUserStore.GetRecentlyActiveUsers", "store.sql_user.get_recently_active_users.app_error", nil, err.Error()) + LIMIT :Limit OFFSET :Offset + `, map[string]interface{}{"TeamId": teamId, "Offset": offset, "Limit": limit}); err != nil { + result.Err = model.NewAppError("SqlUserStore.GetRecentlyActiveUsers", "store.sql_user.get_recently_active_users.app_error", nil, err.Error(), http.StatusInternalServerError) } else { - userMap := make(map[string]*model.User) + userList := []*model.User{} for _, userWithLastActivityAt := range users { u := userWithLastActivityAt.User u.Sanitize(map[string]bool{}) u.LastActivityAt = userWithLastActivityAt.LastActivityAt - userMap[u.Id] = &u + userList = append(userList, &u) } - result.Data = userMap + result.Data = userList + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (us SqlUserStore) GetNewUsersForTeam(teamId string, offset, limit int) StoreChannel { + + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + var users []*model.User + + if _, err := us.GetReplica().Select(&users, ` + SELECT + u.* + FROM Users AS u + INNER JOIN TeamMembers AS t ON u.Id = t.UserId + WHERE t.TeamId = :TeamId + ORDER BY u.CreateAt DESC + LIMIT :Limit OFFSET :Offset + `, map[string]interface{}{"TeamId": teamId, "Offset": offset, "Limit": limit}); err != nil { + result.Err = model.NewAppError("SqlUserStore.GetNewUsersForTeam", "store.sql_user.get_new_users.app_error", nil, err.Error(), http.StatusInternalServerError) + } else { + for _, u := range users { + u.Sanitize(map[string]bool{}) + } + + result.Data = users } storeChannel <- result diff --git a/store/sql_user_store_test.go b/store/sql_user_store_test.go index dc4cd684a..18c4c022c 100644 --- a/store/sql_user_store_test.go +++ b/store/sql_user_store_test.go @@ -1291,7 +1291,22 @@ func TestUserStoreGetRecentlyActiveUsersForTeam(t *testing.T) { tid := model.NewId() Must(store.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u1.Id})) - if r1 := <-store.User().GetRecentlyActiveUsersForTeam(tid); r1.Err != nil { + if r1 := <-store.User().GetRecentlyActiveUsersForTeam(tid, 0, 100); r1.Err != nil { + t.Fatal(r1.Err) + } +} + +func TestUserStoreGetNewUsersForTeam(t *testing.T) { + Setup() + + u1 := &model.User{} + u1.Email = model.NewId() + Must(store.User().Save(u1)) + Must(store.Status().SaveOrUpdate(&model.Status{UserId: u1.Id, Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: model.GetMillis(), ActiveChannel: ""})) + tid := model.NewId() + Must(store.Team().SaveMember(&model.TeamMember{TeamId: tid, UserId: u1.Id})) + + if r1 := <-store.User().GetNewUsersForTeam(tid, 0, 100); r1.Err != nil { t.Fatal(r1.Err) } } diff --git a/store/store.go b/store/store.go index 23c6acd37..9ae5f4b81 100644 --- a/store/store.go +++ b/store/store.go @@ -208,7 +208,8 @@ type UserStore interface { AnalyticsActiveCount(time int64) StoreChannel GetUnreadCount(userId string) StoreChannel GetUnreadCountForChannel(userId string, channelId string) StoreChannel - GetRecentlyActiveUsersForTeam(teamId string) StoreChannel + GetRecentlyActiveUsersForTeam(teamId string, offset, limit int) StoreChannel + GetNewUsersForTeam(teamId string, offset, limit int) StoreChannel Search(teamId string, term string, options map[string]bool) StoreChannel SearchNotInTeam(notInTeamId string, term string, options map[string]bool) StoreChannel SearchInChannel(channelId string, term string, options map[string]bool) StoreChannel -- cgit v1.2.3-1-g7c22