summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
authorGeorge Goldberg <george@gberg.me>2017-03-30 02:10:51 +0100
committerJoram Wilander <jwawilander@gmail.com>2017-03-29 21:10:51 -0400
commita4764a5c10ec59820eec7338d97be48d41c1a4d6 (patch)
tree665d61a0b0738b328b9d7514b8732a59787c20ce /store
parent6d6b8134462d9e718f21e76f9c47f73604574c9b (diff)
downloadchat-a4764a5c10ec59820eec7338d97be48d41c1a4d6.tar.gz
chat-a4764a5c10ec59820eec7338d97be48d41c1a4d6.tar.bz2
chat-a4764a5c10ec59820eec7338d97be48d41c1a4d6.zip
PLT-6083: API to get users not in a specific team. (#5888)
Diffstat (limited to 'store')
-rw-r--r--store/sql_user_store.go73
-rw-r--r--store/sql_user_store_test.go134
-rw-r--r--store/store.go2
3 files changed, 209 insertions, 0 deletions
diff --git a/store/sql_user_store.go b/store/sql_user_store.go
index c00e37ed6..092f4abc7 100644
--- a/store/sql_user_store.go
+++ b/store/sql_user_store.go
@@ -1514,3 +1514,76 @@ func (us SqlUserStore) AnalyticsGetSystemAdminCount() StoreChannel {
return storeChannel
}
+
+func (us SqlUserStore) GetProfilesNotInTeam(teamId string, offset int, limit int) StoreChannel {
+
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ var users []*model.User
+
+ if _, err := us.GetReplica().Select(&users, `
+ SELECT
+ u.*
+ FROM Users u
+ LEFT JOIN TeamMembers tm
+ ON tm.UserId = u.Id
+ AND tm.TeamId = :TeamId
+ AND tm.DeleteAt = 0
+ WHERE tm.UserId IS NULL
+ ORDER BY u.Username ASC
+ LIMIT :Limit OFFSET :Offset
+ `, map[string]interface{}{"TeamId": teamId, "Offset": offset, "Limit": limit}); err != nil {
+ result.Err = model.NewLocAppError("SqlUserStore.GetProfilesNotInTeam", "store.sql_user.get_profiles.app_error", nil, err.Error())
+ } else {
+
+ for _, u := range users {
+ u.Password = ""
+ u.AuthData = new(string)
+ *u.AuthData = ""
+ }
+
+ result.Data = users
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
+func (us SqlUserStore) GetEtagForProfilesNotInTeam(teamId string) StoreChannel {
+
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ updateAt, err := us.GetReplica().SelectInt(`
+ SELECT
+ u.UpdateAt
+ FROM Users u
+ LEFT JOIN TeamMembers tm
+ ON tm.UserId = u.Id
+ AND tm.TeamId = :TeamId
+ AND tm.DeleteAt = 0
+ WHERE tm.UserId IS NULL
+ ORDER BY u.UpdateAt DESC
+ LIMIT 1
+ `, map[string]interface{}{"TeamId": teamId})
+
+ if err != nil {
+ result.Data = fmt.Sprintf("%v.%v.%v.%v", model.CurrentVersion, model.GetMillis(), utils.Cfg.PrivacySettings.ShowFullName, utils.Cfg.PrivacySettings.ShowEmailAddress)
+ } else {
+ result.Data = fmt.Sprintf("%v.%v.%v.%v", model.CurrentVersion, updateAt, utils.Cfg.PrivacySettings.ShowFullName, utils.Cfg.PrivacySettings.ShowEmailAddress)
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
diff --git a/store/sql_user_store_test.go b/store/sql_user_store_test.go
index 798988246..73d42dbcd 100644
--- a/store/sql_user_store_test.go
+++ b/store/sql_user_store_test.go
@@ -1632,3 +1632,137 @@ func TestUserStoreAnalyticsGetSystemAdminCount(t *testing.T) {
}
}
}
+
+func TestUserStoreGetProfilesNotInTeam(t *testing.T) {
+ Setup()
+
+ teamId := model.NewId()
+
+ u1 := &model.User{}
+ u1.Email = model.NewId()
+ Must(store.User().Save(u1))
+ Must(store.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}))
+ Must(store.User().UpdateUpdateAt(u1.Id))
+
+ u2 := &model.User{}
+ u2.Email = model.NewId()
+ Must(store.User().Save(u2))
+ Must(store.User().UpdateUpdateAt(u2.Id))
+
+ var initialUsersNotInTeam int
+ var etag1, etag2, etag3 string
+
+ if er1 := <-store.User().GetEtagForProfilesNotInTeam(teamId); er1.Err != nil {
+ t.Fatal(er1.Err)
+ } else {
+ etag1 = er1.Data.(string)
+ }
+
+ if r1 := <-store.User().GetProfilesNotInTeam(teamId, 0, 100000); r1.Err != nil {
+ t.Fatal(r1.Err)
+ } else {
+ users := r1.Data.([]*model.User)
+ initialUsersNotInTeam = len(users)
+ if initialUsersNotInTeam < 1 {
+ t.Fatalf("Should be at least 1 user not in the team")
+ }
+
+ found := false
+ for _, u := range users {
+ if u.Id == u2.Id {
+ found = true
+ }
+ if u.Id == u1.Id {
+ t.Fatalf("Should not have found user1")
+ }
+ }
+
+ if !found {
+ t.Fatal("missing user2")
+ }
+ }
+
+ time.Sleep(time.Millisecond * 10)
+ Must(store.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}))
+ Must(store.User().UpdateUpdateAt(u2.Id))
+
+ if er2 := <-store.User().GetEtagForProfilesNotInTeam(teamId); er2.Err != nil {
+ t.Fatal(er2.Err)
+ } else {
+ etag2 = er2.Data.(string)
+ if etag1 == etag2 {
+ t.Fatalf("etag should have changed")
+ }
+ }
+
+ if r2 := <-store.User().GetProfilesNotInTeam(teamId, 0, 100000); r2.Err != nil {
+ t.Fatal(r2.Err)
+ } else {
+ users := r2.Data.([]*model.User)
+
+ if len(users) != initialUsersNotInTeam-1 {
+ t.Fatalf("Should be one less user not in team")
+ }
+
+ for _, u := range users {
+ if u.Id == u2.Id {
+ t.Fatalf("Should not have found user2")
+ }
+ if u.Id == u1.Id {
+ t.Fatalf("Should not have found user1")
+ }
+ }
+ }
+
+ time.Sleep(time.Millisecond * 10)
+ Must(store.Team().RemoveMember(teamId, u1.Id))
+ Must(store.Team().RemoveMember(teamId, u2.Id))
+ Must(store.User().UpdateUpdateAt(u1.Id))
+ Must(store.User().UpdateUpdateAt(u2.Id))
+
+ if er3 := <-store.User().GetEtagForProfilesNotInTeam(teamId); er3.Err != nil {
+ t.Fatal(er3.Err)
+ } else {
+ etag3 = er3.Data.(string)
+ t.Log(etag3)
+ if etag1 == etag3 || etag3 == etag2 {
+ t.Fatalf("etag should have changed")
+ }
+ }
+
+ if r3 := <-store.User().GetProfilesNotInTeam(teamId, 0, 100000); r3.Err != nil {
+ t.Fatal(r3.Err)
+ } else {
+ users := r3.Data.([]*model.User)
+ found1, found2 := false, false
+ for _, u := range users {
+ if u.Id == u2.Id {
+ found2 = true
+ }
+ if u.Id == u1.Id {
+ found1 = true
+ }
+ }
+
+ if !found1 || !found2 {
+ t.Fatal("missing user1 or user2")
+ }
+ }
+
+ time.Sleep(time.Millisecond * 10)
+ u3 := &model.User{}
+ u3.Email = model.NewId()
+ Must(store.User().Save(u3))
+ Must(store.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u3.Id}))
+ Must(store.User().UpdateUpdateAt(u3.Id))
+
+ if er4 := <-store.User().GetEtagForProfilesNotInTeam(teamId); er4.Err != nil {
+ t.Fatal(er4.Err)
+ } else {
+ etag4 := er4.Data.(string)
+ t.Log(etag4)
+ if etag4 != etag3 {
+ t.Fatalf("etag should be the same")
+ }
+ }
+}
diff --git a/store/store.go b/store/store.go
index 528b26a3f..409280918 100644
--- a/store/store.go
+++ b/store/store.go
@@ -205,6 +205,8 @@ type UserStore interface {
SearchWithoutTeam(term string, options map[string]bool) StoreChannel
AnalyticsGetInactiveUsersCount() StoreChannel
AnalyticsGetSystemAdminCount() StoreChannel
+ GetProfilesNotInTeam(teamId string, offset int, limit int) StoreChannel
+ GetEtagForProfilesNotInTeam(teamId string) StoreChannel
}
type SessionStore interface {