Channel.GetAllChannels to sync by default (#11206)

* Channel.GetAllChannels to sync by default

* Improvements in test of GetAllChannels
This commit is contained in:
Rodrigo Villablanca Vásquez
2019-06-20 10:48:51 -04:00
committed by Jesús Espino
parent 01fa648886
commit b1631026d8
5 changed files with 57 additions and 57 deletions

View File

@@ -1240,11 +1240,7 @@ func (a *App) GetAllChannels(page, perPage int, opts model.ChannelSearchOpts) (*
NotAssociatedToGroup: opts.NotAssociatedToGroup,
IncludeDeleted: opts.IncludeDeleted,
}
result := <-a.Srv.Store.Channel().GetAllChannels(page*perPage, perPage, storeOpts)
if result.Err != nil {
return nil, result.Err
}
return result.Data.(*model.ChannelListWithTeamData), nil
return a.Srv.Store.Channel().GetAllChannels(page*perPage, perPage, storeOpts)
}
func (a *App) GetDeletedChannels(teamId string, offset int, limit int) (*model.ChannelList, *model.AppError) {

View File

@@ -948,45 +948,41 @@ func (s SqlChannelStore) GetChannels(teamId string, userId string, includeDelete
return channels, nil
}
func (s SqlChannelStore) GetAllChannels(offset int, limit int, opts store.ChannelSearchOpts) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
query := s.getQueryBuilder().
Select("c.*, Teams.DisplayName AS TeamDisplayName, Teams.Name AS TeamName, Teams.UpdateAt AS TeamUpdateAt").
From("Channels AS c").
Join("Teams ON Teams.Id = c.TeamId").
Where(sq.Eq{"c.Type": []string{model.CHANNEL_PRIVATE, model.CHANNEL_OPEN}}).
OrderBy("c.DisplayName, Teams.DisplayName").
Limit(uint64(limit)).
Offset(uint64(offset))
func (s SqlChannelStore) GetAllChannels(offset int, limit int, opts store.ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) {
query := s.getQueryBuilder().
Select("c.*, Teams.DisplayName AS TeamDisplayName, Teams.Name AS TeamName, Teams.UpdateAt AS TeamUpdateAt").
From("Channels AS c").
Join("Teams ON Teams.Id = c.TeamId").
Where(sq.Eq{"c.Type": []string{model.CHANNEL_PRIVATE, model.CHANNEL_OPEN}}).
OrderBy("c.DisplayName, Teams.DisplayName").
Limit(uint64(limit)).
Offset(uint64(offset))
if !opts.IncludeDeleted {
query = query.Where(sq.Eq{"c.DeleteAt": int(0)})
}
if !opts.IncludeDeleted {
query = query.Where(sq.Eq{"c.DeleteAt": int(0)})
}
if len(opts.NotAssociatedToGroup) > 0 {
query = query.Where("c.Id NOT IN (SELECT ChannelId FROM GroupChannels WHERE GroupChannels.GroupId = ? AND GroupChannels.DeleteAt = 0)", opts.NotAssociatedToGroup)
}
if len(opts.NotAssociatedToGroup) > 0 {
query = query.Where("c.Id NOT IN (SELECT ChannelId FROM GroupChannels WHERE GroupChannels.GroupId = ? AND GroupChannels.DeleteAt = 0)", opts.NotAssociatedToGroup)
}
if len(opts.ExcludeChannelNames) > 0 {
query = query.Where(fmt.Sprintf("c.Name NOT IN ('%s')", strings.Join(opts.ExcludeChannelNames, "', '")))
}
if len(opts.ExcludeChannelNames) > 0 {
query = query.Where(fmt.Sprintf("c.Name NOT IN ('%s')", strings.Join(opts.ExcludeChannelNames, "', '")))
}
queryString, args, err := query.ToSql()
if err != nil {
result.Err = model.NewAppError("SqlChannelStore.GetAllChannels", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
return
}
queryString, args, err := query.ToSql()
if err != nil {
return nil, model.NewAppError("SqlChannelStore.GetAllChannels", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
}
data := &model.ChannelListWithTeamData{}
_, err = s.GetReplica().Select(data, queryString, args...)
data := &model.ChannelListWithTeamData{}
_, err = s.GetReplica().Select(data, queryString, args...)
if err != nil {
result.Err = model.NewAppError("SqlChannelStore.GetAllChannels", "store.sql_channel.get_all_channels.get.app_error", nil, err.Error(), http.StatusInternalServerError)
return
}
if err != nil {
return nil, model.NewAppError("SqlChannelStore.GetAllChannels", "store.sql_channel.get_all_channels.get.app_error", nil, err.Error(), http.StatusInternalServerError)
}
result.Data = data
})
return data, nil
}
func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset int, limit int) (*model.ChannelList, *model.AppError) {

View File

@@ -148,7 +148,7 @@ type ChannelStore interface {
GetDeletedByName(team_id string, name string) StoreChannel
GetDeleted(team_id string, offset int, limit int) (*model.ChannelList, *model.AppError)
GetChannels(teamId string, userId string, includeDeleted bool) (*model.ChannelList, *model.AppError)
GetAllChannels(page, perPage int, opts ChannelSearchOpts) StoreChannel
GetAllChannels(page, perPage int, opts ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError)
GetMoreChannels(teamId string, userId string, offset int, limit int) (*model.ChannelList, *model.AppError)
GetPublicChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, *model.AppError)
GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*model.ChannelList, *model.AppError)

View File

@@ -23,11 +23,10 @@ type SqlSupplier interface {
}
func cleanupChannels(t *testing.T, ss store.Store) {
result := <-ss.Channel().GetAllChannels(0, 100000, store.ChannelSearchOpts{IncludeDeleted: true})
if result.Err != nil {
t.Fatalf("error cleaning all channels: %v", result.Err)
list, err := ss.Channel().GetAllChannels(0, 100000, store.ChannelSearchOpts{IncludeDeleted: true})
if err != nil {
t.Fatalf("error cleaning all channels: %v", err)
}
list := result.Data.(*model.ChannelListWithTeamData)
for _, channel := range *list {
ss.Channel().PermanentDelete(channel.Id)
}
@@ -1152,36 +1151,36 @@ func testChannelStoreGetAllChannels(t *testing.T, ss store.Store, s SqlSupplier)
_, err = ss.Channel().Save(&c5, -1)
require.Nil(t, err)
cresult := <-ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{})
list := cresult.Data.(*model.ChannelListWithTeamData)
list, err := ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{})
require.Nil(t, err)
assert.Len(t, *list, 2)
assert.Equal(t, (*list)[0].Id, c1.Id)
assert.Equal(t, (*list)[0].TeamDisplayName, "Name")
assert.Equal(t, (*list)[1].Id, c3.Id)
assert.Equal(t, (*list)[1].TeamDisplayName, "Name2")
cresult = <-ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{IncludeDeleted: true})
list = cresult.Data.(*model.ChannelListWithTeamData)
list, err = ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{IncludeDeleted: true})
require.Nil(t, err)
assert.Len(t, *list, 3)
assert.Equal(t, (*list)[0].Id, c1.Id)
assert.Equal(t, (*list)[0].TeamDisplayName, "Name")
assert.Equal(t, (*list)[1].Id, c2.Id)
assert.Equal(t, (*list)[2].Id, c3.Id)
cresult = <-ss.Channel().GetAllChannels(0, 1, store.ChannelSearchOpts{IncludeDeleted: true})
list = cresult.Data.(*model.ChannelListWithTeamData)
list, err = ss.Channel().GetAllChannels(0, 1, store.ChannelSearchOpts{IncludeDeleted: true})
require.Nil(t, err)
assert.Len(t, *list, 1)
assert.Equal(t, (*list)[0].Id, c1.Id)
assert.Equal(t, (*list)[0].TeamDisplayName, "Name")
// Not associated to group
cresult = <-ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{NotAssociatedToGroup: group.Id})
list = cresult.Data.(*model.ChannelListWithTeamData)
list, err = ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{NotAssociatedToGroup: group.Id})
require.Nil(t, err)
assert.Len(t, *list, 1)
// Exclude channel names
cresult = <-ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{ExcludeChannelNames: []string{c1.Name}})
list = cresult.Data.(*model.ChannelListWithTeamData)
list, err = ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{ExcludeChannelNames: []string{c1.Name}})
require.Nil(t, err)
assert.Len(t, *list, 1)
// Manually truncate Channels table until testlib can handle cleanups

View File

@@ -254,19 +254,28 @@ func (_m *ChannelStore) GetAllChannelMembersNotifyPropsForChannel(channelId stri
}
// GetAllChannels provides a mock function with given fields: page, perPage, opts
func (_m *ChannelStore) GetAllChannels(page int, perPage int, opts store.ChannelSearchOpts) store.StoreChannel {
func (_m *ChannelStore) GetAllChannels(page int, perPage int, opts store.ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) {
ret := _m.Called(page, perPage, opts)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(int, int, store.ChannelSearchOpts) store.StoreChannel); ok {
var r0 *model.ChannelListWithTeamData
if rf, ok := ret.Get(0).(func(int, int, store.ChannelSearchOpts) *model.ChannelListWithTeamData); ok {
r0 = rf(page, perPage, opts)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).(*model.ChannelListWithTeamData)
}
}
return r0
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(int, int, store.ChannelSearchOpts) *model.AppError); ok {
r1 = rf(page, perPage, opts)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// GetAllChannelsForExportAfter provides a mock function with given fields: limit, afterId