diff --git a/app/channel.go b/app/channel.go index afa3ecc35d..3af6a1a419 100644 --- a/app/channel.go +++ b/app/channel.go @@ -1842,8 +1842,7 @@ func (a *App) MarkChannelsAsViewed(channelIds []string, userId string, currentSe } } } else if notify == model.USER_NOTIFY_MENTION || channel.Type == model.CHANNEL_DIRECT { - if result := <-a.Srv.Store.User().GetUnreadCountForChannel(userId, channelId); result.Err == nil { - count := result.Data.(int64) + if count, err := a.Srv.Store.User().GetUnreadCountForChannel(userId, channelId); err == nil { if count > 0 { channelsToClearPushNotifications = append(channelsToClearPushNotifications, channelId) } diff --git a/store/sqlstore/user_store.go b/store/sqlstore/user_store.go index ed86bd5dbc..a5b2e12726 100644 --- a/store/sqlstore/user_store.go +++ b/store/sqlstore/user_store.go @@ -1143,14 +1143,12 @@ func (us SqlUserStore) GetUnreadCount(userId string) (int64, error) { return count, nil } -func (us SqlUserStore) GetUnreadCountForChannel(userId string, channelId string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - if count, err := us.GetReplica().SelectInt("SELECT SUM(CASE WHEN c.Type = 'D' THEN (c.TotalMsgCount - cm.MsgCount) ELSE cm.MentionCount END) FROM Channels c INNER JOIN ChannelMembers cm ON c.Id = cm.ChannelId AND cm.ChannelId = :ChannelId AND cm.UserId = :UserId", map[string]interface{}{"ChannelId": channelId, "UserId": userId}); err != nil { - result.Err = model.NewAppError("SqlUserStore.GetMentionCountForChannel", "store.sql_user.get_unread_count_for_channel.app_error", nil, err.Error(), http.StatusInternalServerError) - } else { - result.Data = count - } - }) +func (us SqlUserStore) GetUnreadCountForChannel(userId string, channelId string) (int64, *model.AppError) { + count, err := us.GetReplica().SelectInt("SELECT SUM(CASE WHEN c.Type = 'D' THEN (c.TotalMsgCount - cm.MsgCount) ELSE cm.MentionCount END) FROM Channels c INNER JOIN ChannelMembers cm ON c.Id = cm.ChannelId AND cm.ChannelId = :ChannelId AND cm.UserId = :UserId", map[string]interface{}{"ChannelId": channelId, "UserId": userId}) + if err != nil { + return 0, model.NewAppError("SqlUserStore.GetMentionCountForChannel", "store.sql_user.get_unread_count_for_channel.app_error", nil, err.Error(), http.StatusInternalServerError) + } + return count, nil } func (us SqlUserStore) GetAnyUnreadPostCountForChannel(userId string, channelId string) (int64, *model.AppError) { diff --git a/store/store.go b/store/store.go index 6c72ea2fa8..707eacc0a8 100644 --- a/store/store.go +++ b/store/store.go @@ -287,7 +287,7 @@ type UserStore interface { PermanentDelete(userId string) *model.AppError AnalyticsActiveCount(time int64) (int64, *model.AppError) GetUnreadCount(userId string) (int64, error) - GetUnreadCountForChannel(userId string, channelId string) StoreChannel + GetUnreadCountForChannel(userId string, channelId string) (int64, *model.AppError) GetAnyUnreadPostCountForChannel(userId string, channelId string) (int64, *model.AppError) GetRecentlyActiveUsersForTeam(teamId string, offset, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) GetNewUsersForTeam(teamId string, offset, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) diff --git a/store/storetest/mocks/CommandWebhookStore.go b/store/storetest/mocks/CommandWebhookStore.go index 80b9530a63..c388f2386d 100644 --- a/store/storetest/mocks/CommandWebhookStore.go +++ b/store/storetest/mocks/CommandWebhookStore.go @@ -4,8 +4,8 @@ package mocks -import "github.com/stretchr/testify/mock" -import "github.com/mattermost/mattermost-server/model" +import mock "github.com/stretchr/testify/mock" +import model "github.com/mattermost/mattermost-server/model" // CommandWebhookStore is an autogenerated mock type for the CommandWebhookStore type type CommandWebhookStore struct { diff --git a/store/storetest/mocks/UserStore.go b/store/storetest/mocks/UserStore.go index a69d614528..785907231f 100644 --- a/store/storetest/mocks/UserStore.go +++ b/store/storetest/mocks/UserStore.go @@ -786,19 +786,26 @@ func (_m *UserStore) GetUnreadCount(userId string) (int64, error) { } // GetUnreadCountForChannel provides a mock function with given fields: userId, channelId -func (_m *UserStore) GetUnreadCountForChannel(userId string, channelId string) store.StoreChannel { +func (_m *UserStore) GetUnreadCountForChannel(userId string, channelId string) (int64, *model.AppError) { ret := _m.Called(userId, channelId) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { + var r0 int64 + if rf, ok := ret.Get(0).(func(string, string) int64); ok { r0 = rf(userId, channelId) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).(int64) + } + + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string, string) *model.AppError); ok { + r1 = rf(userId, channelId) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) } } - return r0 + return r0, r1 } // GetUsersBatchForIndexing provides a mock function with given fields: startTime, endTime, limit diff --git a/store/storetest/user_store.go b/store/storetest/user_store.go index 4a5bf41a02..a4fb552b9b 100644 --- a/store/storetest/user_store.go +++ b/store/storetest/user_store.go @@ -1950,12 +1950,14 @@ func testUserUnreadCount(t *testing.T, ss store.Store) { t.Fatal("should have 3 unread messages") } - badge = (<-ss.User().GetUnreadCountForChannel(u2.Id, c1.Id)).Data.(int64) + badge, unreadCountErr = ss.User().GetUnreadCountForChannel(u2.Id, c1.Id) + require.Nil(t, unreadCountErr) if badge != 1 { t.Fatal("should have 1 unread messages for that channel") } - badge = (<-ss.User().GetUnreadCountForChannel(u2.Id, c2.Id)).Data.(int64) + badge, unreadCountErr = ss.User().GetUnreadCountForChannel(u2.Id, c2.Id) + require.Nil(t, unreadCountErr) if badge != 2 { t.Fatal("should have 2 unread messages for that channel") }