From be43522117e637271509cf24244f554a437c0578 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Thu, 12 Nov 2015 11:25:46 -0500 Subject: Added ChannelExtra.MemberCount field to reflect the actual member count when we truncate the list of members to 20 --- api/channel.go | 7 ++++++- api/channel_test.go | 4 ++++ model/channel_extra.go | 5 +++-- store/sql_channel_store.go | 20 ++++++++++++++++++++ store/sql_channel_store_test.go | 8 ++++---- store/store.go | 1 + web/react/components/channel_header.jsx | 6 +++++- web/react/components/popover_list_members.jsx | 4 ++-- 8 files changed, 45 insertions(+), 10 deletions(-) diff --git a/api/channel.go b/api/channel.go index 44be1cf97..75ca9680d 100644 --- a/api/channel.go +++ b/api/channel.go @@ -707,6 +707,7 @@ func getChannelExtraInfo(c *Context, w http.ResponseWriter, r *http.Request) { scm := Srv.Store.Channel().GetMember(id, c.Session.UserId) ecm := Srv.Store.Channel().GetExtraMembers(id, 20) + ccm := Srv.Store.Channel().GetMemberCount(id) if cmresult := <-scm; cmresult.Err != nil { c.Err = cmresult.Err @@ -714,9 +715,13 @@ func getChannelExtraInfo(c *Context, w http.ResponseWriter, r *http.Request) { } else if ecmresult := <-ecm; ecmresult.Err != nil { c.Err = ecmresult.Err return + } else if ccmresult := <-ccm; ccmresult.Err != nil { + c.Err = ccmresult.Err + return } else { member := cmresult.Data.(model.ChannelMember) extraMembers := ecmresult.Data.([]model.ExtraMember) + memberCount := ccmresult.Data.(int64) if !c.HasPermissionsToTeam(channel.TeamId, "getChannelExtraInfo") { return @@ -732,7 +737,7 @@ func getChannelExtraInfo(c *Context, w http.ResponseWriter, r *http.Request) { return } - data := model.ChannelExtra{Id: channel.Id, Members: extraMembers} + data := model.ChannelExtra{Id: channel.Id, Members: extraMembers, MemberCount: memberCount} w.Header().Set(model.HEADER_ETAG_SERVER, extraEtag) w.Header().Set("Expires", "-1") w.Write([]byte(data.ToJson())) diff --git a/api/channel_test.go b/api/channel_test.go index a41f63b1b..faed387dd 100644 --- a/api/channel_test.go +++ b/api/channel_test.go @@ -677,6 +677,10 @@ func TestGetChannelExtraInfo(t *testing.T) { data := rget.Data.(*model.ChannelExtra) if data.Id != channel1.Id { t.Fatal("couldnt't get extra info") + } else if len(data.Members) != 1 { + t.Fatal("got incorrect members") + } else if data.MemberCount != 1 { + t.Fatal("got incorrect member count") } // diff --git a/model/channel_extra.go b/model/channel_extra.go index c6f0ca192..55da588af 100644 --- a/model/channel_extra.go +++ b/model/channel_extra.go @@ -23,8 +23,9 @@ func (o *ExtraMember) Sanitize(options map[string]bool) { } type ChannelExtra struct { - Id string `json:"id"` - Members []ExtraMember `json:"members"` + Id string `json:"id"` + Members []ExtraMember `json:"members"` + MemberCount int64 `json:"member_count"` } func (o *ChannelExtra) ToJson() string { diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index fc4e19442..a9f99bd67 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -542,6 +542,26 @@ func (s SqlChannelStore) GetMember(channelId string, userId string) StoreChannel return storeChannel } +func (s SqlChannelStore) GetMemberCount(channelId string) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + count, err := s.GetReplica().SelectInt("SELECT count(*) FROM ChannelMembers WHERE ChannelId = :ChannelId", 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 { + result.Data = count + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlChannelStore) GetExtraMembers(channelId string, limit int) StoreChannel { storeChannel := make(StoreChannel) diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index f6a0fb713..8662fcbd3 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -339,15 +339,15 @@ func TestChannelMemberStore(t *testing.T) { t.Fatal("Member update time incorrect") } - members := (<-store.Channel().GetMembers(o1.ChannelId)).Data.([]model.ChannelMember) - if len(members) != 2 { + count := (<-store.Channel().GetMemberCount(o1.ChannelId)).Data.(int64) + if count != 2 { t.Fatal("should have saved 2 members") } Must(store.Channel().RemoveMember(o2.ChannelId, o2.UserId)) - members = (<-store.Channel().GetMembers(o1.ChannelId)).Data.([]model.ChannelMember) - if len(members) != 1 { + count = (<-store.Channel().GetMemberCount(o1.ChannelId)).Data.(int64) + if count != 1 { t.Fatal("should have removed 1 member") } diff --git a/store/store.go b/store/store.go index ce4d90883..13b59b582 100644 --- a/store/store.go +++ b/store/store.go @@ -70,6 +70,7 @@ type ChannelStore interface { UpdateMember(member *model.ChannelMember) StoreChannel GetMembers(channelId string) StoreChannel GetMember(channelId string, userId string) StoreChannel + GetMemberCount(channelId string) StoreChannel RemoveMember(channelId string, userId string) StoreChannel GetExtraMembers(channelId string, limit int) StoreChannel CheckPermissionsTo(teamId string, channelId string, userId string) StoreChannel diff --git a/web/react/components/channel_header.jsx b/web/react/components/channel_header.jsx index 895dc5fe4..dcc3bf87d 100644 --- a/web/react/components/channel_header.jsx +++ b/web/react/components/channel_header.jsx @@ -39,11 +39,14 @@ export default class ChannelHeader extends React.Component { this.state = state; } getStateFromStores() { + const extraInfo = ChannelStore.getCurrentExtraInfo(); + return { channel: ChannelStore.getCurrent(), memberChannel: ChannelStore.getCurrentMember(), memberTeam: UserStore.getCurrentUser(), - users: ChannelStore.getCurrentExtraInfo().members, + users: extraInfo.members, + userCount: extraInfo.member_count, searchVisible: SearchStore.getSearchResults() !== null }; } @@ -373,6 +376,7 @@ export default class ChannelHeader extends React.Component { diff --git a/web/react/components/popover_list_members.jsx b/web/react/components/popover_list_members.jsx index f3c0fa0b4..bd6b6d3bd 100644 --- a/web/react/components/popover_list_members.jsx +++ b/web/react/components/popover_list_members.jsx @@ -69,7 +69,6 @@ export default class PopoverListMembers extends React.Component { render() { let popoverHtml = []; - let count = 0; let countText = '-'; const members = this.props.members; const teamMembers = UserStore.getProfilesUsernameMap(); @@ -147,10 +146,10 @@ export default class PopoverListMembers extends React.Component { ); - count++; } }); + const count = this.props.memberCount; if (count > 20) { countText = '20+'; } else if (count > 0) { @@ -195,5 +194,6 @@ export default class PopoverListMembers extends React.Component { PopoverListMembers.propTypes = { members: React.PropTypes.array.isRequired, + memberCount: React.PropTypes.number.isRequired, channelId: React.PropTypes.string.isRequired }; -- cgit v1.2.3-1-g7c22