Channel.GetPublicChannelsByIdsForTeam to sync by default (#11209)

This commit is contained in:
Rodrigo Villablanca Vásquez
2019-06-18 17:14:09 -04:00
committed by Gabe Jackson
parent e92ecf6696
commit c8f334ab1c
5 changed files with 59 additions and 56 deletions

View File

@@ -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) {

View File

@@ -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 {

View File

@@ -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

View File

@@ -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")
})
}

View File

@@ -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