diff options
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_channel_store.go | 35 | ||||
-rw-r--r-- | store/sql_channel_store_test.go | 45 | ||||
-rw-r--r-- | store/store.go | 1 |
3 files changed, 81 insertions, 0 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 001eaaa26..a961781dd 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -13,6 +13,7 @@ import ( "github.com/mattermost/platform/einterfaces" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" + "strconv" ) const ( @@ -1270,3 +1271,37 @@ func (s SqlChannelStore) performSearch(searchQuery string, term string, paramete return result } + +func (s SqlChannelStore) GetMembersByIds(channelId string, userIds []string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + var members model.ChannelMembers + props := make(map[string]interface{}) + idQuery := "" + + for index, userId := range userIds { + if len(idQuery) > 0 { + idQuery += ", " + } + + props["userId"+strconv.Itoa(index)] = userId + idQuery += ":userId" + strconv.Itoa(index) + } + + props["ChannelId"] = channelId + + if _, err := s.GetReplica().Select(&members, "SELECT * FROM ChannelMembers WHERE ChannelId = :ChannelId AND UserId IN ("+idQuery+")", props); err != nil { + result.Err = model.NewLocAppError("SqlChannelStore.GetMembersByIds", "store.sql_channel.get_members_by_ids.app_error", nil, "channelId="+channelId+" "+err.Error()) + } else { + result.Data = members + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index d4bcf6b26..b2a644c15 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -1280,3 +1280,48 @@ func TestChannelStoreSearchInTeam(t *testing.T) { } } } + +func TestChannelStoreGetMembersByIds(t *testing.T) { + Setup() + + o1 := model.Channel{} + o1.TeamId = model.NewId() + o1.DisplayName = "ChannelA" + o1.Name = "a" + model.NewId() + "b" + o1.Type = model.CHANNEL_OPEN + Must(store.Channel().Save(&o1)) + + m1 := &model.ChannelMember{ChannelId: o1.Id, UserId: model.NewId(), NotifyProps: model.GetDefaultChannelNotifyProps()} + Must(store.Channel().SaveMember(m1)) + + if r := <-store.Channel().GetMembersByIds(m1.ChannelId, []string{m1.UserId}); r.Err != nil { + t.Fatal(r.Err) + } else { + rm1 := r.Data.(model.ChannelMembers)[0] + + if rm1.ChannelId != m1.ChannelId { + t.Fatal("bad team id") + } + + if rm1.UserId != m1.UserId { + t.Fatal("bad user id") + } + } + + m2 := &model.ChannelMember{ChannelId: o1.Id, UserId: model.NewId(), NotifyProps: model.GetDefaultChannelNotifyProps()} + Must(store.Channel().SaveMember(m2)) + + if r := <-store.Channel().GetMembersByIds(m1.ChannelId, []string{m1.UserId, m2.UserId, model.NewId()}); r.Err != nil { + t.Fatal(r.Err) + } else { + rm := r.Data.(model.ChannelMembers) + + if len(rm) != 2 { + t.Fatal("return wrong number of results") + } + } + + if r := <-store.Channel().GetMembersByIds(m1.ChannelId, []string{}); r.Err == nil { + t.Fatal("empty user ids - should have failed") + } +} diff --git a/store/store.go b/store/store.go index 40b641002..643fe925a 100644 --- a/store/store.go +++ b/store/store.go @@ -118,6 +118,7 @@ type ChannelStore interface { GetMembersForUser(teamId string, userId string) StoreChannel SearchInTeam(teamId string, term string) StoreChannel SearchMore(userId string, teamId string, term string) StoreChannel + GetMembersByIds(channelId string, userIds []string) StoreChannel } type PostStore interface { |