diff options
author | hmhealey <harrisonmhealey@gmail.com> | 2016-01-23 13:13:34 -0500 |
---|---|---|
committer | hmhealey <harrisonmhealey@gmail.com> | 2016-01-23 13:13:44 -0500 |
commit | fc0b422c37f719a350b65e064cde7b506a88052e (patch) | |
tree | 22f05d072d03c343e27c71406c9ca64ac4487b6d /store | |
parent | 1a0b12313bd0af1724df2fc6260ef284acfc5f93 (diff) | |
download | chat-fc0b422c37f719a350b65e064cde7b506a88052e.tar.gz chat-fc0b422c37f719a350b65e064cde7b506a88052e.tar.bz2 chat-fc0b422c37f719a350b65e064cde7b506a88052e.zip |
Changed GetMemberCount to stop including inactive users
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_channel_store.go | 11 | ||||
-rw-r--r-- | store/sql_channel_store_test.go | 106 |
2 files changed, 116 insertions, 1 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 336398ae7..7400df8d2 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -582,7 +582,16 @@ func (s SqlChannelStore) GetMemberCount(channelId string) StoreChannel { go func() { result := StoreResult{} - count, err := s.GetReplica().SelectInt("SELECT count(*) FROM ChannelMembers WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId}) + count, err := s.GetReplica().SelectInt(` + SELECT + count(*) + FROM + ChannelMembers, + Users + WHERE + ChannelMembers.UserId = Users.Id + AND ChannelMembers.ChannelId = :ChannelId + AND Users.DeleteAt = 0`, map[string]interface{}{"ChannelId": channelId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.GetMemberCount", "We couldn't get the channel member count", "channel_id="+channelId+", "+err.Error()) } else { diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index 8b22fbb7a..a3b0c2286 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -750,3 +750,109 @@ func TestChannelStoreIncrementMentionCount(t *testing.T) { t.Fatal("failed to update") } } + +func TestGetMemberCount(t *testing.T) { + Setup() + + teamId := model.NewId() + + c1 := model.Channel{ + TeamId: teamId, + DisplayName: "Channel1", + Name: "a" + model.NewId() + "b", + Type: model.CHANNEL_OPEN, + } + Must(store.Channel().Save(&c1)) + + c2 := model.Channel{ + TeamId: teamId, + DisplayName: "Channel2", + Name: "a" + model.NewId() + "b", + Type: model.CHANNEL_OPEN, + } + Must(store.Channel().Save(&c2)) + + t.Logf("c1.Id = %v", c1.Id) + + u1 := model.User{ + TeamId: teamId, + Email: model.NewId(), + DeleteAt: 0, + } + Must(store.User().Save(&u1)) + + m1 := model.ChannelMember{ + ChannelId: c1.Id, + UserId: u1.Id, + NotifyProps: model.GetDefaultChannelNotifyProps(), + } + Must(store.Channel().SaveMember(&m1)) + + if result := <-store.Channel().GetMemberCount(c1.Id); result.Err != nil { + t.Fatal("failed to get member count: %v", result.Err) + } else if result.Data.(int64) != 1 { + t.Fatal("got incorrect member count %v", result.Data) + } + + u2 := model.User{ + TeamId: teamId, + Email: model.NewId(), + DeleteAt: 0, + } + Must(store.User().Save(&u2)) + + m2 := model.ChannelMember{ + ChannelId: c1.Id, + UserId: u2.Id, + NotifyProps: model.GetDefaultChannelNotifyProps(), + } + Must(store.Channel().SaveMember(&m2)) + + if result := <-store.Channel().GetMemberCount(c1.Id); result.Err != nil { + t.Fatal("failed to get member count: %v", result.Err) + } else if result.Data.(int64) != 2 { + t.Fatal("got incorrect member count %v", result.Data) + } + + // make sure members of other channels aren't counted + u3 := model.User{ + TeamId: teamId, + Email: model.NewId(), + DeleteAt: 0, + } + Must(store.User().Save(&u3)) + + m3 := model.ChannelMember{ + ChannelId: c2.Id, + UserId: u3.Id, + NotifyProps: model.GetDefaultChannelNotifyProps(), + } + Must(store.Channel().SaveMember(&m3)) + + if result := <-store.Channel().GetMemberCount(c1.Id); result.Err != nil { + t.Fatal("failed to get member count: %v", result.Err) + } else if result.Data.(int64) != 2 { + t.Fatal("got incorrect member count %v", result.Data) + } + + // make sure inactive users aren't counted + u4 := model.User{ + TeamId: teamId, + Email: model.NewId(), + DeleteAt: 10000, + } + Must(store.User().Save(&u4)) + + m4 := model.ChannelMember{ + ChannelId: c1.Id, + UserId: u4.Id, + NotifyProps: model.GetDefaultChannelNotifyProps(), + } + Must(store.Channel().SaveMember(&m4)) + + if result := <-store.Channel().GetMemberCount(c1.Id); result.Err != nil { + t.Fatal("failed to get member count: %v", result.Err) + } else if result.Data.(int64) != 2 { + t.Fatal("got incorrect member count %v", result.Data) + } +} |