diff --git a/app/channel.go b/app/channel.go index 4f74352b2b..6b27b20ab7 100644 --- a/app/channel.go +++ b/app/channel.go @@ -1342,11 +1342,10 @@ func (a *App) GetChannelCounts(teamId string, userId string) (*model.ChannelCoun } func (a *App) GetChannelUnread(channelId, userId string) (*model.ChannelUnread, *model.AppError) { - result := <-a.Srv.Store.Channel().GetChannelUnread(channelId, userId) - if result.Err != nil { - return nil, result.Err + channelUnread, err := a.Srv.Store.Channel().GetChannelUnread(channelId, userId) + if err != nil { + return nil, err } - channelUnread := result.Data.(*model.ChannelUnread) if channelUnread.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_MARK_UNREAD_MENTION { channelUnread.MsgCount = 0 diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index a05d9d37c0..94f32e8839 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -682,11 +682,10 @@ func (s SqlChannelStore) updateChannelT(transaction *gorp.Transaction, channel * return result } -func (s SqlChannelStore) GetChannelUnread(channelId, userId string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - var unreadChannel model.ChannelUnread - err := s.GetReplica().SelectOne(&unreadChannel, - `SELECT +func (s SqlChannelStore) GetChannelUnread(channelId, userId string) (*model.ChannelUnread, *model.AppError) { + var unreadChannel model.ChannelUnread + err := s.GetReplica().SelectOne(&unreadChannel, + `SELECT Channels.TeamId TeamId, Channels.Id ChannelId, (Channels.TotalMsgCount - ChannelMembers.MsgCount) MsgCount, ChannelMembers.MentionCount MentionCount, ChannelMembers.NotifyProps NotifyProps FROM Channels, ChannelMembers @@ -695,17 +694,15 @@ func (s SqlChannelStore) GetChannelUnread(channelId, userId string) store.StoreC AND Id = :ChannelId AND UserId = :UserId AND DeleteAt = 0`, - map[string]interface{}{"ChannelId": channelId, "UserId": userId}) + map[string]interface{}{"ChannelId": channelId, "UserId": userId}) - if err != nil { - result.Err = model.NewAppError("SqlChannelStore.GetChannelUnread", "store.sql_channel.get_unread.app_error", nil, "channelId="+channelId+" "+err.Error(), http.StatusInternalServerError) - if err == sql.ErrNoRows { - result.Err.StatusCode = http.StatusNotFound - } - } else { - result.Data = &unreadChannel + if err != nil { + if err == sql.ErrNoRows { + return nil, model.NewAppError("SqlChannelStore.GetChannelUnread", "store.sql_channel.get_unread.app_error", nil, "channelId="+channelId+" "+err.Error(), http.StatusNotFound) } - }) + return nil, model.NewAppError("SqlChannelStore.GetChannelUnread", "store.sql_channel.get_unread.app_error", nil, "channelId="+channelId+" "+err.Error(), http.StatusInternalServerError) + } + return &unreadChannel, nil } func (s SqlChannelStore) InvalidateChannel(id string) { diff --git a/store/store.go b/store/store.go index 0ddf26b420..f3e1513083 100644 --- a/store/store.go +++ b/store/store.go @@ -188,7 +188,7 @@ type ChannelStore interface { SearchMore(userId string, teamId string, term string) StoreChannel GetMembersByIds(channelId string, userIds []string) StoreChannel AnalyticsDeletedTypeCount(teamId string, channelType string) StoreChannel - GetChannelUnread(channelId, userId string) StoreChannel + GetChannelUnread(channelId, userId string) (*model.ChannelUnread, *model.AppError) ClearCaches() GetChannelsByScheme(schemeId string, offset int, limit int) StoreChannel MigrateChannelMembers(fromChannelId string, fromUserId string) StoreChannel diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index f4b2bb1aab..db4d59809c 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -301,10 +301,9 @@ func testGetChannelUnread(t *testing.T, ss store.Store) { store.Must(ss.Channel().SaveMember(cm2)) // Check for Channel 1 - if resp := <-ss.Channel().GetChannelUnread(c1.Id, uid); resp.Err != nil { - t.Fatal(resp.Err) + if ch, err := ss.Channel().GetChannelUnread(c1.Id, uid); err != nil { + t.Fatal(err) } else { - ch := resp.Data.(*model.ChannelUnread) if c1.Id != ch.ChannelId { t.Fatal("wrong channel id") } @@ -327,10 +326,9 @@ func testGetChannelUnread(t *testing.T, ss store.Store) { } // Check for Channel 2 - if resp2 := <-ss.Channel().GetChannelUnread(c2.Id, uid); resp2.Err != nil { - t.Fatal(resp2.Err) + if ch2, err := ss.Channel().GetChannelUnread(c2.Id, uid); err != nil { + t.Fatal(err) } else { - ch2 := resp2.Data.(*model.ChannelUnread) if c2.Id != ch2.ChannelId { t.Fatal("wrong channel id") } diff --git a/store/storetest/mocks/ChannelStore.go b/store/storetest/mocks/ChannelStore.go index 375c6e65ab..826ad813ad 100644 --- a/store/storetest/mocks/ChannelStore.go +++ b/store/storetest/mocks/ChannelStore.go @@ -348,19 +348,28 @@ func (_m *ChannelStore) GetChannelMembersTimezones(channelId string) store.Store } // GetChannelUnread provides a mock function with given fields: channelId, userId -func (_m *ChannelStore) GetChannelUnread(channelId string, userId string) store.StoreChannel { +func (_m *ChannelStore) GetChannelUnread(channelId string, userId string) (*model.ChannelUnread, *model.AppError) { ret := _m.Called(channelId, userId) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { + var r0 *model.ChannelUnread + if rf, ok := ret.Get(0).(func(string, string) *model.ChannelUnread); ok { r0 = rf(channelId, userId) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).(*model.ChannelUnread) } } - return r0 + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string, string) *model.AppError); ok { + r1 = rf(channelId, userId) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 } // GetChannels provides a mock function with given fields: teamId, userId, includeDeleted