From 27a88b7868da11c19defbad0bfda2d3be0984291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Villablanca=20V=C3=A1squez?= Date: Mon, 17 Jun 2019 10:47:34 -0400 Subject: [PATCH] Channel.GetChannelCounts to sync by default (#11210) --- app/channel.go | 6 +----- store/sqlstore/channel_store.go | 29 ++++++++++++--------------- store/store.go | 2 +- store/storetest/channel_store.go | 3 +-- store/storetest/mocks/ChannelStore.go | 19 +++++++++++++----- 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/app/channel.go b/app/channel.go index 279606d595..f991110af8 100644 --- a/app/channel.go +++ b/app/channel.go @@ -1355,11 +1355,7 @@ func (a *App) GetChannelMemberCount(channelId string) (int64, *model.AppError) { } func (a *App) GetChannelCounts(teamId string, userId string) (*model.ChannelCounts, *model.AppError) { - result := <-a.Srv.Store.Channel().GetChannelCounts(teamId, userId) - if result.Err != nil { - return nil, result.Err - } - return result.Data.(*model.ChannelCounts), nil + return a.Srv.Store.Channel().GetChannelCounts(teamId, userId) } func (a *App) GetChannelUnread(channelId, userId string) (*model.ChannelUnread, *model.AppError) { diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index 7026f3b6ad..f7ea8823a9 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -1115,25 +1115,22 @@ type channelIdWithCountAndUpdateAt struct { UpdateAt int64 } -func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - var data []channelIdWithCountAndUpdateAt - _, err := s.GetReplica().Select(&data, "SELECT Id, TotalMsgCount, UpdateAt FROM Channels WHERE Id IN (SELECT ChannelId FROM ChannelMembers WHERE UserId = :UserId) AND (TeamId = :TeamId OR TeamId = '') AND DeleteAt = 0 ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId, "UserId": userId}) +func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) (*model.ChannelCounts, *model.AppError) { + var data []channelIdWithCountAndUpdateAt + _, err := s.GetReplica().Select(&data, "SELECT Id, TotalMsgCount, UpdateAt FROM Channels WHERE Id IN (SELECT ChannelId FROM ChannelMembers WHERE UserId = :UserId) AND (TeamId = :TeamId OR TeamId = '') AND DeleteAt = 0 ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId, "UserId": userId}) - if err != nil { - result.Err = model.NewAppError("SqlChannelStore.GetChannelCounts", "store.sql_channel.get_channel_counts.get.app_error", nil, "teamId="+teamId+", userId="+userId+", err="+err.Error(), http.StatusInternalServerError) - return - } + if err != nil { + return nil, model.NewAppError("SqlChannelStore.GetChannelCounts", "store.sql_channel.get_channel_counts.get.app_error", nil, "teamId="+teamId+", userId="+userId+", err="+err.Error(), http.StatusInternalServerError) + } - counts := &model.ChannelCounts{Counts: make(map[string]int64), UpdateTimes: make(map[string]int64)} - for i := range data { - v := data[i] - counts.Counts[v.Id] = v.TotalMsgCount - counts.UpdateTimes[v.Id] = v.UpdateAt - } + counts := &model.ChannelCounts{Counts: make(map[string]int64), UpdateTimes: make(map[string]int64)} + for i := range data { + v := data[i] + counts.Counts[v.Id] = v.TotalMsgCount + counts.UpdateTimes[v.Id] = v.UpdateAt + } - result.Data = counts - }) + return counts, nil } func (s SqlChannelStore) GetTeamChannels(teamId string) store.StoreChannel { diff --git a/store/store.go b/store/store.go index 04e4791a65..886231b708 100644 --- a/store/store.go +++ b/store/store.go @@ -152,7 +152,7 @@ type ChannelStore interface { 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 - GetChannelCounts(teamId string, userId string) StoreChannel + GetChannelCounts(teamId string, userId string) (*model.ChannelCounts, *model.AppError) GetTeamChannels(teamId string) StoreChannel GetAll(teamId string) StoreChannel GetChannelsByIds(channelIds []string) StoreChannel diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index 0d51ece824..7233378113 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -1540,8 +1540,7 @@ func testChannelStoreGetChannelCounts(t *testing.T, ss store.Store) { m3.NotifyProps = model.GetDefaultChannelNotifyProps() store.Must(ss.Channel().SaveMember(&m3)) - cresult := <-ss.Channel().GetChannelCounts(o1.TeamId, m1.UserId) - counts := cresult.Data.(*model.ChannelCounts) + counts, _ := ss.Channel().GetChannelCounts(o1.TeamId, m1.UserId) if len(counts.Counts) != 1 { t.Fatal("wrong number of counts") diff --git a/store/storetest/mocks/ChannelStore.go b/store/storetest/mocks/ChannelStore.go index 1de400f9e8..83e38395cd 100644 --- a/store/storetest/mocks/ChannelStore.go +++ b/store/storetest/mocks/ChannelStore.go @@ -316,19 +316,28 @@ func (_m *ChannelStore) GetByNames(team_id string, names []string, allowFromCach } // GetChannelCounts provides a mock function with given fields: teamId, userId -func (_m *ChannelStore) GetChannelCounts(teamId string, userId string) store.StoreChannel { +func (_m *ChannelStore) GetChannelCounts(teamId string, userId string) (*model.ChannelCounts, *model.AppError) { ret := _m.Called(teamId, userId) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { + var r0 *model.ChannelCounts + if rf, ok := ret.Get(0).(func(string, string) *model.ChannelCounts); ok { r0 = rf(teamId, userId) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).(*model.ChannelCounts) } } - return r0 + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string, string) *model.AppError); ok { + r1 = rf(teamId, userId) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 } // GetChannelMembersForExport provides a mock function with given fields: userId, teamId