Migrates Channel.GetChannels to sync by default (#11204)

* Migrates Channel.GetChannels to sync by default

* Some suggestions

* resolved undefined var
Этот коммит содержится в:
Rodrigo Villablanca Vásquez
2019-06-20 03:31:40 -04:00
коммит произвёл Jesús Espino
родитель 3c13b0e4e7
Коммит 327a1c92e8
7 изменённых файлов: 47 добавлений и 51 удалений

Просмотреть файл

@@ -1226,11 +1226,7 @@ func (a *App) GetChannelByNameForTeamName(channelName, teamName string, includeD
} }
func (a *App) GetChannelsForUser(teamId string, userId string, includeDeleted bool) (*model.ChannelList, *model.AppError) { func (a *App) GetChannelsForUser(teamId string, userId string, includeDeleted bool) (*model.ChannelList, *model.AppError) {
result := <-a.Srv.Store.Channel().GetChannels(teamId, userId, includeDeleted) return a.Srv.Store.Channel().GetChannels(teamId, userId, includeDeleted)
if result.Err != nil {
return nil, result.Err
}
return result.Data.(*model.ChannelList), nil
} }
func (a *App) GetAllChannels(page, perPage int, opts model.ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) { func (a *App) GetAllChannels(page, perPage int, opts model.ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) {

Просмотреть файл

@@ -823,14 +823,12 @@ func (a *App) LeaveTeam(team *model.Team, user *model.User, requestorId string)
var channelList *model.ChannelList var channelList *model.ChannelList
if result := <-a.Srv.Store.Channel().GetChannels(team.Id, user.Id, true); result.Err != nil { if channelList, err = a.Srv.Store.Channel().GetChannels(team.Id, user.Id, true); err != nil {
if result.Err.Id == "store.sql_channel.get_channels.not_found.app_error" { if err.Id == "store.sql_channel.get_channels.not_found.app_error" {
channelList = &model.ChannelList{} channelList = &model.ChannelList{}
} else { } else {
return result.Err return err
} }
} else {
channelList = result.Data.(*model.ChannelList)
} }
for _, channel := range *channelList { for _, channel := range *channelList {

Просмотреть файл

@@ -149,21 +149,19 @@ func manualTest(c *web.Context, w http.ResponseWriter, r *http.Request) {
} }
} }
func getChannelID(a *app.App, channelname string, teamid string, userid string) (id string, err bool) { func getChannelID(a *app.App, channelname string, teamid string, userid string) (string, bool) {
// Grab all the channels // Grab all the channels
result := <-a.Srv.Store.Channel().GetChannels(teamid, userid, false) channels, err := a.Srv.Store.Channel().GetChannels(teamid, userid, false)
if result.Err != nil { if err != nil {
mlog.Debug("Unable to get channels") mlog.Debug("Unable to get channels")
return "", false return "", false
} }
data := result.Data.(model.ChannelList) for _, channel := range *channels {
for _, channel := range data {
if channel.Name == channelname { if channel.Name == channelname {
return channel.Id, true return channel.Id, true
} }
} }
mlog.Debug(fmt.Sprintf("Could not find channel: %v, %v possibilities searched", channelname, strconv.Itoa(len(data)))) mlog.Debug(fmt.Sprintf("Could not find channel: %v, %v possibilities searched", channelname, strconv.Itoa(len(*channels))))
return "", false return "", false
} }

Просмотреть файл

@@ -929,27 +929,23 @@ func (s SqlChannelStore) PermanentDeleteMembersByChannel(channelId string) *mode
return nil return nil
} }
func (s SqlChannelStore) GetChannels(teamId string, userId string, includeDeleted bool) store.StoreChannel { func (s SqlChannelStore) GetChannels(teamId string, userId string, includeDeleted bool) (*model.ChannelList, *model.AppError) {
return store.Do(func(result *store.StoreResult) { query := "SELECT Channels.* FROM Channels, ChannelMembers WHERE Id = ChannelId AND UserId = :UserId AND DeleteAt = 0 AND (TeamId = :TeamId OR TeamId = '') ORDER BY DisplayName"
query := "SELECT Channels.* FROM Channels, ChannelMembers WHERE Id = ChannelId AND UserId = :UserId AND DeleteAt = 0 AND (TeamId = :TeamId OR TeamId = '') ORDER BY DisplayName" if includeDeleted {
if includeDeleted { query = "SELECT Channels.* FROM Channels, ChannelMembers WHERE Id = ChannelId AND UserId = :UserId AND (TeamId = :TeamId OR TeamId = '') ORDER BY DisplayName"
query = "SELECT Channels.* FROM Channels, ChannelMembers WHERE Id = ChannelId AND UserId = :UserId AND (TeamId = :TeamId OR TeamId = '') ORDER BY DisplayName" }
} channels := &model.ChannelList{}
data := &model.ChannelList{} _, err := s.GetReplica().Select(channels, query, map[string]interface{}{"TeamId": teamId, "UserId": userId})
_, err := s.GetReplica().Select(data, query, map[string]interface{}{"TeamId": teamId, "UserId": userId})
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlChannelStore.GetChannels", "store.sql_channel.get_channels.get.app_error", nil, "teamId="+teamId+", userId="+userId+", err="+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlChannelStore.GetChannels", "store.sql_channel.get_channels.get.app_error", nil, "teamId="+teamId+", userId="+userId+", err="+err.Error(), http.StatusInternalServerError)
return }
}
if len(*data) == 0 { if len(*channels) == 0 {
result.Err = model.NewAppError("SqlChannelStore.GetChannels", "store.sql_channel.get_channels.not_found.app_error", nil, "teamId="+teamId+", userId="+userId, http.StatusBadRequest) return nil, model.NewAppError("SqlChannelStore.GetChannels", "store.sql_channel.get_channels.not_found.app_error", nil, "teamId="+teamId+", userId="+userId, http.StatusBadRequest)
return }
}
result.Data = data return channels, nil
})
} }
func (s SqlChannelStore) GetAllChannels(offset int, limit int, opts store.ChannelSearchOpts) store.StoreChannel { func (s SqlChannelStore) GetAllChannels(offset int, limit int, opts store.ChannelSearchOpts) store.StoreChannel {

Просмотреть файл

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

Просмотреть файл

@@ -621,9 +621,8 @@ func testChannelStoreDelete(t *testing.T, ss store.Store) {
t.Fatal(err) t.Fatal(err)
} }
cresult := <-ss.Channel().GetChannels(o1.TeamId, m1.UserId, false) list, err := ss.Channel().GetChannels(o1.TeamId, m1.UserId, false)
require.Nil(t, cresult.Err) require.Nil(t, err)
list := cresult.Data.(*model.ChannelList)
if len(*list) != 1 { if len(*list) != 1 {
t.Fatal("invalid number of channels") t.Fatal("invalid number of channels")
@@ -636,14 +635,14 @@ func testChannelStoreDelete(t *testing.T, ss store.Store) {
t.Fatal("invalid number of channels") t.Fatal("invalid number of channels")
} }
cresult = <-ss.Channel().PermanentDelete(o2.Id) cresult := <-ss.Channel().PermanentDelete(o2.Id)
require.Nil(t, cresult.Err) require.Nil(t, cresult.Err)
cresult = <-ss.Channel().GetChannels(o1.TeamId, m1.UserId, false) list, err = ss.Channel().GetChannels(o1.TeamId, m1.UserId, false)
if assert.NotNil(t, cresult.Err) { if assert.NotNil(t, err) {
require.Equal(t, "store.sql_channel.get_channels.not_found.app_error", cresult.Err.Id) require.Equal(t, "store.sql_channel.get_channels.not_found.app_error", err.Id)
} else { } else {
require.Equal(t, &model.ChannelList{}, cresult.Data.(*model.ChannelList)) require.Equal(t, &model.ChannelList{}, list)
} }
if r := <-ss.Channel().PermanentDeleteByTeam(o1.TeamId); r.Err != nil { if r := <-ss.Channel().PermanentDeleteByTeam(o1.TeamId); r.Err != nil {
@@ -1032,8 +1031,8 @@ func testChannelStoreGetChannels(t *testing.T, ss store.Store) {
m3.NotifyProps = model.GetDefaultChannelNotifyProps() m3.NotifyProps = model.GetDefaultChannelNotifyProps()
store.Must(ss.Channel().SaveMember(&m3)) store.Must(ss.Channel().SaveMember(&m3))
cresult := <-ss.Channel().GetChannels(o1.TeamId, m1.UserId, false) list, err := ss.Channel().GetChannels(o1.TeamId, m1.UserId, false)
list := cresult.Data.(*model.ChannelList) require.Nil(t, err)
if (*list)[0].Id != o1.Id { if (*list)[0].Id != o1.Id {
t.Fatal("missing channel") t.Fatal("missing channel")

Просмотреть файл

@@ -468,19 +468,28 @@ func (_m *ChannelStore) GetChannelUnread(channelId string, userId string) (*mode
} }
// GetChannels provides a mock function with given fields: teamId, userId, includeDeleted // GetChannels provides a mock function with given fields: teamId, userId, includeDeleted
func (_m *ChannelStore) GetChannels(teamId string, userId string, includeDeleted bool) store.StoreChannel { func (_m *ChannelStore) GetChannels(teamId string, userId string, includeDeleted bool) (*model.ChannelList, *model.AppError) {
ret := _m.Called(teamId, userId, includeDeleted) ret := _m.Called(teamId, userId, includeDeleted)
var r0 store.StoreChannel var r0 *model.ChannelList
if rf, ok := ret.Get(0).(func(string, string, bool) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, string, bool) *model.ChannelList); ok {
r0 = rf(teamId, userId, includeDeleted) r0 = rf(teamId, userId, includeDeleted)
} else { } else {
if ret.Get(0) != nil { 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, bool) *model.AppError); ok {
r1 = rf(teamId, userId, includeDeleted)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetChannelsBatchForIndexing provides a mock function with given fields: startTime, endTime, limit // GetChannelsBatchForIndexing provides a mock function with given fields: startTime, endTime, limit