diff --git a/app/channel.go b/app/channel.go index 3c6527c745..359c6110fb 100644 --- a/app/channel.go +++ b/app/channel.go @@ -1258,11 +1258,7 @@ func (a *App) GetChannelsUserNotIn(teamId string, userId string, offset int, lim } func (a *App) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*model.ChannelList, *model.AppError) { - result := <-a.Srv.Store.Channel().GetPublicChannelsByIdsForTeam(teamId, channelIds) - if result.Err != nil { - return nil, result.Err - } - return result.Data.(*model.ChannelList), nil + return a.Srv.Store.Channel().GetPublicChannelsByIdsForTeam(teamId, channelIds) } func (a *App) GetPublicChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, *model.AppError) { diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index 4b470863c0..8c0318314f 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -1066,47 +1066,45 @@ func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, lim }) } -func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - props := make(map[string]interface{}) - props["teamId"] = teamId +func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*model.ChannelList, *model.AppError) { + props := make(map[string]interface{}) + props["teamId"] = teamId - idQuery := "" + idQuery := "" - for index, channelId := range channelIds { - if len(idQuery) > 0 { - idQuery += ", " - } - - props["channelId"+strconv.Itoa(index)] = channelId - idQuery += ":channelId" + strconv.Itoa(index) + for index, channelId := range channelIds { + if len(idQuery) > 0 { + idQuery += ", " } - data := &model.ChannelList{} - _, err := s.GetReplica().Select(data, ` - SELECT - Channels.* - FROM - Channels - JOIN - PublicChannels pc ON (pc.Id = Channels.Id) - WHERE - pc.TeamId = :teamId - AND pc.DeleteAt = 0 - AND pc.Id IN (`+idQuery+`) - ORDER BY pc.DisplayName + props["channelId"+strconv.Itoa(index)] = channelId + idQuery += ":channelId" + strconv.Itoa(index) + } + + data := &model.ChannelList{} + _, err := s.GetReplica().Select(data, ` + SELECT + Channels.* + FROM + Channels + JOIN + PublicChannels pc ON (pc.Id = Channels.Id) + WHERE + pc.TeamId = :teamId + AND pc.DeleteAt = 0 + AND pc.Id IN (`+idQuery+`) + ORDER BY pc.DisplayName `, props) - if err != nil { - result.Err = model.NewAppError("SqlChannelStore.GetPublicChannelsByIdsForTeam", "store.sql_channel.get_channels_by_ids.get.app_error", nil, err.Error(), http.StatusInternalServerError) - } + if err != nil { + return nil, model.NewAppError("SqlChannelStore.GetPublicChannelsByIdsForTeam", "store.sql_channel.get_channels_by_ids.get.app_error", nil, err.Error(), http.StatusInternalServerError) + } - if len(*data) == 0 { - result.Err = model.NewAppError("SqlChannelStore.GetPublicChannelsByIdsForTeam", "store.sql_channel.get_channels_by_ids.not_found.app_error", nil, "", http.StatusNotFound) - } + if len(*data) == 0 { + return nil, model.NewAppError("SqlChannelStore.GetPublicChannelsByIdsForTeam", "store.sql_channel.get_channels_by_ids.not_found.app_error", nil, "", http.StatusNotFound) + } - result.Data = data - }) + return data, nil } type channelIdWithCountAndUpdateAt struct { diff --git a/store/store.go b/store/store.go index bae858d58d..4306ebc265 100644 --- a/store/store.go +++ b/store/store.go @@ -151,7 +151,7 @@ type ChannelStore interface { GetAllChannels(page, perPage int, opts ChannelSearchOpts) StoreChannel GetMoreChannels(teamId string, userId string, offset int, limit int) (*model.ChannelList, *model.AppError) GetPublicChannelsForTeam(teamId string, offset int, limit int) StoreChannel - GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) StoreChannel + GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*model.ChannelList, *model.AppError) GetChannelCounts(teamId string, userId string) (*model.ChannelCounts, *model.AppError) GetTeamChannels(teamId string) (*model.ChannelList, *model.AppError) GetAll(teamId string) StoreChannel diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index d45c15dc9e..c99fa27a97 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -1450,15 +1450,15 @@ func testChannelStoreGetPublicChannelsByIdsForTeam(t *testing.T, ss store.Store) require.Nil(t, err) t.Run("oc1 by itself should be found as a public channel in the team", func(t *testing.T) { - result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id}) - require.Nil(t, result.Err) - require.Equal(t, &model.ChannelList{&oc1}, result.Data.(*model.ChannelList)) + list, channelErr := ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id}) + require.Nil(t, channelErr) + require.Equal(t, &model.ChannelList{&oc1}, list) }) t.Run("only oc1, among others, should be found as a public channel in the team", func(t *testing.T) { - result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id, oc2.Id, model.NewId(), pc3.Id}) - require.Nil(t, result.Err) - require.Equal(t, &model.ChannelList{&oc1}, result.Data.(*model.ChannelList)) + list, channelErr := ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id, oc2.Id, model.NewId(), pc3.Id}) + require.Nil(t, channelErr) + require.Equal(t, &model.ChannelList{&oc1}, list) }) // oc4 is another public channel on the team @@ -1485,15 +1485,15 @@ func testChannelStoreGetPublicChannelsByIdsForTeam(t *testing.T, ss store.Store) require.Nil(t, err, "channel should have been deleted") t.Run("only oc1 and oc4, among others, should be found as a public channel in the team", func(t *testing.T) { - result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id, oc2.Id, model.NewId(), pc3.Id, oc4.Id}) - require.Nil(t, result.Err) - require.Equal(t, &model.ChannelList{&oc1, &oc4}, result.Data.(*model.ChannelList)) + list, err := ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id, oc2.Id, model.NewId(), pc3.Id, oc4.Id}) + require.Nil(t, err) + require.Equal(t, &model.ChannelList{&oc1, &oc4}, list) }) t.Run("random channel id should not be found as a public channel in the team", func(t *testing.T) { - result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{model.NewId()}) - require.NotNil(t, result.Err) - require.Equal(t, result.Err.Id, "store.sql_channel.get_channels_by_ids.not_found.app_error") + _, err := ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{model.NewId()}) + require.NotNil(t, err) + require.Equal(t, err.Id, "store.sql_channel.get_channels_by_ids.not_found.app_error") }) } diff --git a/store/storetest/mocks/ChannelStore.go b/store/storetest/mocks/ChannelStore.go index 578826cd37..c6950d6283 100644 --- a/store/storetest/mocks/ChannelStore.go +++ b/store/storetest/mocks/ChannelStore.go @@ -774,19 +774,28 @@ func (_m *ChannelStore) GetPinnedPosts(channelId string) store.StoreChannel { } // GetPublicChannelsByIdsForTeam provides a mock function with given fields: teamId, channelIds -func (_m *ChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) store.StoreChannel { +func (_m *ChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*model.ChannelList, *model.AppError) { ret := _m.Called(teamId, channelIds) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, []string) store.StoreChannel); ok { + var r0 *model.ChannelList + if rf, ok := ret.Get(0).(func(string, []string) *model.ChannelList); ok { r0 = rf(teamId, channelIds) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).(*model.ChannelList) } } - return r0 + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string, []string) *model.AppError); ok { + r1 = rf(teamId, channelIds) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 } // GetPublicChannelsForTeam provides a mock function with given fields: teamId, offset, limit