summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2015-10-30 10:57:25 -0400
committerJoram Wilander <jwawilander@gmail.com>2015-10-30 10:57:25 -0400
commit256be4c14dfd99fc923d497cc6d7dfe3f4e8e3c5 (patch)
treef85d6a4707eaab904694aa4fc42aca917f03fdc4 /store
parentc2cd58a33f52c6567e39d8ba563425dc06916d41 (diff)
parentbedf85a122359a70cbb21be1219a5d566466273e (diff)
downloadchat-256be4c14dfd99fc923d497cc6d7dfe3f4e8e3c5.tar.gz
chat-256be4c14dfd99fc923d497cc6d7dfe3f4e8e3c5.tar.bz2
chat-256be4c14dfd99fc923d497cc6d7dfe3f4e8e3c5.zip
Merge pull request #1211 from mattermost/PLT-340
PLT-340 allow team directory and open invites
Diffstat (limited to 'store')
-rw-r--r--store/sql_team_store.go90
-rw-r--r--store/sql_team_store_test.go77
-rw-r--r--store/store.go2
3 files changed, 168 insertions, 1 deletions
diff --git a/store/sql_team_store.go b/store/sql_team_store.go
index 8700a9d04..ebf982ec4 100644
--- a/store/sql_team_store.go
+++ b/store/sql_team_store.go
@@ -23,6 +23,7 @@ func NewSqlTeamStore(sqlStore *SqlStore) TeamStore {
table.ColMap("Email").SetMaxSize(128)
table.ColMap("CompanyName").SetMaxSize(64)
table.ColMap("AllowedDomains").SetMaxSize(500)
+ table.ColMap("InviteId").SetMaxSize(32)
}
return s
@@ -31,10 +32,14 @@ func NewSqlTeamStore(sqlStore *SqlStore) TeamStore {
func (s SqlTeamStore) UpgradeSchemaIfNeeded() {
// REMOVE AFTER 1.2 SHIP see PLT-828
s.RemoveColumnIfExists("Teams", "AllowValet")
+ s.CreateColumnIfNotExists("Teams", "InviteId", "varchar(26)", "varchar(26)", "")
+ s.CreateColumnIfNotExists("Teams", "AllowOpenInvite", "tinyint(1)", "boolean", "0")
+ s.CreateColumnIfNotExists("Teams", "AllowTeamListing", "tinyint(1)", "boolean", "0")
}
func (s SqlTeamStore) CreateIndexesIfNotExists() {
s.CreateIndexIfNotExists("idx_teams_name", "Teams", "Name")
+ s.CreateIndexIfNotExists("idx_teams_invite_id", "Teams", "InviteId")
}
func (s SqlTeamStore) Save(team *model.Team) StoreChannel {
@@ -98,6 +103,7 @@ func (s SqlTeamStore) Update(team *model.Team) StoreChannel {
} else {
oldTeam := oldResult.(*model.Team)
team.CreateAt = oldTeam.CreateAt
+ team.UpdateAt = model.GetMillis()
team.Name = oldTeam.Name
if count, err := s.GetMaster().Update(team); err != nil {
@@ -147,7 +153,12 @@ func (s SqlTeamStore) Get(id string) StoreChannel {
} else if obj == nil {
result.Err = model.NewAppError("SqlTeamStore.Get", "We couldn't find the existing team", "id="+id)
} else {
- result.Data = obj.(*model.Team)
+ team := obj.(*model.Team)
+ if len(team.InviteId) == 0 {
+ team.InviteId = team.Id
+ }
+
+ result.Data = team
}
storeChannel <- result
@@ -157,6 +168,35 @@ func (s SqlTeamStore) Get(id string) StoreChannel {
return storeChannel
}
+func (s SqlTeamStore) GetByInviteId(inviteId string) StoreChannel {
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ team := model.Team{}
+
+ if err := s.GetReplica().SelectOne(&team, "SELECT * FROM Teams WHERE Id = :InviteId OR InviteId = :InviteId", map[string]interface{}{"InviteId": inviteId}); err != nil {
+ result.Err = model.NewAppError("SqlTeamStore.GetByInviteId", "We couldn't find the existing team", "inviteId="+inviteId+", "+err.Error())
+ }
+
+ if len(team.InviteId) == 0 {
+ team.InviteId = team.Id
+ }
+
+ if len(inviteId) == 0 || team.InviteId != inviteId {
+ result.Err = model.NewAppError("SqlTeamStore.GetByInviteId", "We couldn't find the existing team", "inviteId="+inviteId)
+ }
+
+ result.Data = &team
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
func (s SqlTeamStore) GetByName(name string) StoreChannel {
storeChannel := make(StoreChannel)
@@ -169,6 +209,10 @@ func (s SqlTeamStore) GetByName(name string) StoreChannel {
result.Err = model.NewAppError("SqlTeamStore.GetByName", "We couldn't find the existing team", "name="+name+", "+err.Error())
}
+ if len(team.InviteId) == 0 {
+ team.InviteId = team.Id
+ }
+
result.Data = &team
storeChannel <- result
@@ -189,6 +233,12 @@ func (s SqlTeamStore) GetTeamsForEmail(email string) StoreChannel {
result.Err = model.NewAppError("SqlTeamStore.GetTeamsForEmail", "We encounted a problem when looking up teams", "email="+email+", "+err.Error())
}
+ for _, team := range data {
+ if len(team.InviteId) == 0 {
+ team.InviteId = team.Id
+ }
+ }
+
result.Data = data
storeChannel <- result
@@ -209,6 +259,44 @@ func (s SqlTeamStore) GetAll() StoreChannel {
result.Err = model.NewAppError("SqlTeamStore.GetAllTeams", "We could not get all teams", err.Error())
}
+ for _, team := range data {
+ if len(team.InviteId) == 0 {
+ team.InviteId = team.Id
+ }
+ }
+
+ result.Data = data
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
+func (s SqlTeamStore) GetAllTeamListing() StoreChannel {
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ query := "SELECT * FROM Teams WHERE AllowTeamListing = 1"
+
+ if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES {
+ query = "SELECT * FROM Teams WHERE AllowTeamListing = true"
+ }
+
+ var data []*model.Team
+ if _, err := s.GetReplica().Select(&data, query); err != nil {
+ result.Err = model.NewAppError("SqlTeamStore.GetAllTeams", "We could not get all teams", err.Error())
+ }
+
+ for _, team := range data {
+ if len(team.InviteId) == 0 {
+ team.InviteId = team.Id
+ }
+ }
+
result.Data = data
storeChannel <- result
diff --git a/store/sql_team_store_test.go b/store/sql_team_store_test.go
index 3d9b4d435..71740f7e7 100644
--- a/store/sql_team_store_test.go
+++ b/store/sql_team_store_test.go
@@ -132,6 +132,54 @@ func TestTeamStoreGetByName(t *testing.T) {
}
}
+func TestTeamStoreGetByIniviteId(t *testing.T) {
+ Setup()
+
+ o1 := model.Team{}
+ o1.DisplayName = "DisplayName"
+ o1.Name = "a" + model.NewId() + "b"
+ o1.Email = model.NewId() + "@nowhere.com"
+ o1.Type = model.TEAM_OPEN
+ o1.InviteId = model.NewId()
+
+ if err := (<-store.Team().Save(&o1)).Err; err != nil {
+ t.Fatal(err)
+ }
+
+ o2 := model.Team{}
+ o2.DisplayName = "DisplayName"
+ o2.Name = "a" + model.NewId() + "b"
+ o2.Email = model.NewId() + "@nowhere.com"
+ o2.Type = model.TEAM_OPEN
+
+ if err := (<-store.Team().Save(&o2)).Err; err != nil {
+ t.Fatal(err)
+ }
+
+ if r1 := <-store.Team().GetByInviteId(o1.InviteId); r1.Err != nil {
+ t.Fatal(r1.Err)
+ } else {
+ if r1.Data.(*model.Team).ToJson() != o1.ToJson() {
+ t.Fatal("invalid returned team")
+ }
+ }
+
+ o2.InviteId = ""
+ <-store.Team().Update(&o2)
+
+ if r1 := <-store.Team().GetByInviteId(o2.Id); r1.Err != nil {
+ t.Fatal(r1.Err)
+ } else {
+ if r1.Data.(*model.Team).Id != o2.Id {
+ t.Fatal("invalid returned team")
+ }
+ }
+
+ if err := (<-store.Team().GetByInviteId("")).Err; err == nil {
+ t.Fatal("Missing id should have failed")
+ }
+}
+
func TestTeamStoreGetForEmail(t *testing.T) {
Setup()
@@ -161,3 +209,32 @@ func TestTeamStoreGetForEmail(t *testing.T) {
t.Fatal(r1.Err)
}
}
+
+func TestAllTeamListing(t *testing.T) {
+ Setup()
+
+ o1 := model.Team{}
+ o1.DisplayName = "DisplayName"
+ o1.Name = "a" + model.NewId() + "b"
+ o1.Email = model.NewId() + "@nowhere.com"
+ o1.Type = model.TEAM_OPEN
+ o1.AllowTeamListing = true
+ Must(store.Team().Save(&o1))
+
+ o2 := model.Team{}
+ o2.DisplayName = "DisplayName"
+ o2.Name = "a" + model.NewId() + "b"
+ o2.Email = model.NewId() + "@nowhere.com"
+ o2.Type = model.TEAM_OPEN
+ Must(store.Team().Save(&o2))
+
+ if r1 := <-store.Team().GetAllTeamListing(); r1.Err != nil {
+ t.Fatal(r1.Err)
+ } else {
+ teams := r1.Data.([]*model.Team)
+
+ if len(teams) == 0 {
+ t.Fatal("failed team listing")
+ }
+ }
+}
diff --git a/store/store.go b/store/store.go
index 42329b036..53a6e053b 100644
--- a/store/store.go
+++ b/store/store.go
@@ -50,6 +50,8 @@ type TeamStore interface {
GetByName(name string) StoreChannel
GetTeamsForEmail(domain string) StoreChannel
GetAll() StoreChannel
+ GetAllTeamListing() StoreChannel
+ GetByInviteId(inviteId string) StoreChannel
}
type ChannelStore interface {