From 7d08efc335667a0fd1dac79539ecf46ba95ca9a2 Mon Sep 17 00:00:00 2001 From: Sheshagiri Rao Mallipedhi Date: Tue, 18 Jun 2019 00:15:51 +0530 Subject: [PATCH] Migrate Channel.GetChannelsByIds to Sync by default (#11197) * Migrate Channel.GetChannelsByIds to Sync by default * remove <- --- app/channel.go | 8 ++++---- app/user.go | 8 ++++---- store/sqlstore/channel_store.go | 24 ++++++++++-------------- store/store.go | 2 +- store/storetest/channel_store.go | 24 +++++++++++------------- store/storetest/mocks/ChannelStore.go | 19 ++++++++++++++----- 6 files changed, 44 insertions(+), 41 deletions(-) diff --git a/app/channel.go b/app/channel.go index d69b6da10a..dbf9c3a17f 100644 --- a/app/channel.go +++ b/app/channel.go @@ -1759,11 +1759,11 @@ func (a *App) AutocompleteChannels(teamId string, term string) (*model.ChannelLi channelList := model.ChannelList{} if len(channelIds) > 0 { - cresult := <-a.Srv.Store.Channel().GetChannelsByIds(channelIds) - if cresult.Err != nil { - return nil, cresult.Err + channels, err := a.Srv.Store.Channel().GetChannelsByIds(channelIds) + if err != nil { + return nil, err } - for _, c := range cresult.Data.([]*model.Channel) { + for _, c := range channels { if c.DeleteAt > 0 && !includeDeleted { continue } diff --git a/app/user.go b/app/user.go index 4f32304359..397aad2d4e 100644 --- a/app/user.go +++ b/app/user.go @@ -2154,11 +2154,11 @@ func (a *App) getListOfAllowedChannelsForTeam(teamId string, viewRestrictions *m return channelIds, nil } - cresult := <-a.Srv.Store.Channel().GetChannelsByIds(viewRestrictions.Channels) - if cresult.Err != nil { - return nil, cresult.Err + channels, err := a.Srv.Store.Channel().GetChannelsByIds(viewRestrictions.Channels) + if err != nil { + return nil, err } - for _, c := range cresult.Data.([]*model.Channel) { + for _, c := range channels { if c.TeamId == teamId { listOfAllowedChannels = append(listOfAllowedChannels, c.Id) } diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index ea9c0ec724..89fdd39b50 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -1857,22 +1857,18 @@ func (s SqlChannelStore) GetAll(teamId string) store.StoreChannel { }) } -func (s SqlChannelStore) GetChannelsByIds(channelIds []string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - keys, params := MapStringsToQueryParams(channelIds, "Channel") +func (s SqlChannelStore) GetChannelsByIds(channelIds []string) ([]*model.Channel, *model.AppError) { + keys, params := MapStringsToQueryParams(channelIds, "Channel") + query := `SELECT * FROM Channels WHERE Id IN ` + keys + ` ORDER BY Name` - query := `SELECT * FROM Channels WHERE Id IN ` + keys + ` ORDER BY Name` + var channels []*model.Channel + _, err := s.GetReplica().Select(&channels, query, params) - var channels []*model.Channel - _, err := s.GetReplica().Select(&channels, query, params) - - if err != nil { - mlog.Error(fmt.Sprint(err)) - result.Err = model.NewAppError("SqlChannelStore.GetChannelsByIds", "store.sql_channel.get_channels_by_ids.app_error", nil, "", http.StatusInternalServerError) - } else { - result.Data = channels - } - }) + if err != nil { + mlog.Error(fmt.Sprint(err)) + return nil, model.NewAppError("SqlChannelStore.GetChannelsByIds", "store.sql_channel.get_channels_by_ids.app_error", nil, "", http.StatusInternalServerError) + } + return channels, nil } func (s SqlChannelStore) GetForPost(postId string) store.StoreChannel { diff --git a/store/store.go b/store/store.go index 2226fa80f5..21d623916c 100644 --- a/store/store.go +++ b/store/store.go @@ -155,7 +155,7 @@ type ChannelStore interface { GetChannelCounts(teamId string, userId string) (*model.ChannelCounts, *model.AppError) GetTeamChannels(teamId string) StoreChannel GetAll(teamId string) StoreChannel - GetChannelsByIds(channelIds []string) StoreChannel + GetChannelsByIds(channelIds []string) ([]*model.Channel, *model.AppError) GetForPost(postId string) StoreChannel SaveMember(member *model.ChannelMember) StoreChannel UpdateMember(member *model.ChannelMember) StoreChannel diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index d7d88e1e1b..64debfe5e0 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -484,30 +484,28 @@ func testChannelStoreGetChannelsByIds(t *testing.T, ss store.Store) { _, err = ss.Channel().SaveDirectChannel(&o2, &m1, &m2) require.Nil(t, err) - if r1 := <-ss.Channel().GetChannelsByIds([]string{o1.Id, o2.Id}); r1.Err != nil { - t.Fatal(r1.Err) + if r1, err := ss.Channel().GetChannelsByIds([]string{o1.Id, o2.Id}); err != nil { + t.Fatal(err) } else { - cl := r1.Data.([]*model.Channel) - if len(cl) != 2 { - t.Fatal("invalid returned channels, expected 2 and got " + strconv.Itoa(len(cl))) + if len(r1) != 2 { + t.Fatal("invalid returned channels, expected 2 and got " + strconv.Itoa(len(r1))) } - if cl[0].ToJson() != o1.ToJson() { + if r1[0].ToJson() != o1.ToJson() { t.Fatal("invalid returned channel") } - if cl[1].ToJson() != o2.ToJson() { + if r1[1].ToJson() != o2.ToJson() { t.Fatal("invalid returned channel") } } nonexistentId := "abcd1234" - if r2 := <-ss.Channel().GetChannelsByIds([]string{o1.Id, nonexistentId}); r2.Err != nil { - t.Fatal(r2.Err) + if r2, err := ss.Channel().GetChannelsByIds([]string{o1.Id, nonexistentId}); err != nil { + t.Fatal(err) } else { - cl := r2.Data.([]*model.Channel) - if len(cl) != 1 { - t.Fatal("invalid returned channels, expected 1 and got " + strconv.Itoa(len(cl))) + if len(r2) != 1 { + t.Fatal("invalid returned channels, expected 1 and got " + strconv.Itoa(len(r2))) } - if cl[0].ToJson() != o1.ToJson() { + if r2[0].ToJson() != o1.ToJson() { t.Fatal("invalid returned channel") } } diff --git a/store/storetest/mocks/ChannelStore.go b/store/storetest/mocks/ChannelStore.go index 2668fffec5..7472b224fa 100644 --- a/store/storetest/mocks/ChannelStore.go +++ b/store/storetest/mocks/ChannelStore.go @@ -448,19 +448,28 @@ func (_m *ChannelStore) GetChannelsBatchForIndexing(startTime int64, endTime int } // GetChannelsByIds provides a mock function with given fields: channelIds -func (_m *ChannelStore) GetChannelsByIds(channelIds []string) store.StoreChannel { +func (_m *ChannelStore) GetChannelsByIds(channelIds []string) ([]*model.Channel, *model.AppError) { ret := _m.Called(channelIds) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func([]string) store.StoreChannel); ok { + var r0 []*model.Channel + if rf, ok := ret.Get(0).(func([]string) []*model.Channel); ok { r0 = rf(channelIds) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).([]*model.Channel) } } - return r0 + var r1 *model.AppError + if rf, ok := ret.Get(1).(func([]string) *model.AppError); ok { + r1 = rf(channelIds) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 } // GetChannelsByScheme provides a mock function with given fields: schemeId, offset, limit