diff --git a/store/sqlstore/channel_store_categories.go b/store/sqlstore/channel_store_categories.go index 0d6063246a..2c0a29305d 100644 --- a/store/sqlstore/channel_store_categories.go +++ b/store/sqlstore/channel_store_categories.go @@ -6,10 +6,9 @@ package sqlstore import ( "fmt" - "github.com/mattermost/mattermost-server/v5/store" - "github.com/mattermost/gorp" "github.com/mattermost/mattermost-server/v5/model" + "github.com/mattermost/mattermost-server/v5/store" sq "github.com/Masterminds/squirrel" "github.com/pkg/errors" @@ -843,6 +842,8 @@ func (s SqlChannelStore) addChannelToFavoritesCategoryT(transaction *gorp.Transa var channel *model.Channel if obj, err := transaction.Get(&model.Channel{}, preference.Name); err != nil { return errors.Wrapf(err, "Failed to get favorited channel with id=%s", preference.Name) + } else if obj == nil { + return store.NewErrNotFound("Channel", preference.Name) } else { channel = obj.(*model.Channel) } diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index 6a24e1babc..d4d7e2d3bf 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -107,6 +107,7 @@ func TestChannelStore(t *testing.T, ss store.Store, s SqlSupplier) { t.Run("GetSidebarCategories", func(t *testing.T) { testGetSidebarCategories(t, ss) }) t.Run("UpdateSidebarCategories", func(t *testing.T) { testUpdateSidebarCategories(t, ss, s) }) t.Run("DeleteSidebarCategory", func(t *testing.T) { testDeleteSidebarCategory(t, ss, s) }) + t.Run("UpdateSidebarChannelsByPreferences", func(t *testing.T) { testUpdateSidebarChannelsByPreferences(t, ss) }) } func testChannelStoreSave(t *testing.T, ss store.Store) { diff --git a/store/storetest/channel_store_categories.go b/store/storetest/channel_store_categories.go index ded22df447..77c0ed0837 100644 --- a/store/storetest/channel_store_categories.go +++ b/store/storetest/channel_store_categories.go @@ -21,6 +21,7 @@ func TestChannelStoreCategories(t *testing.T, ss store.Store, s SqlSupplier) { t.Run("GetSidebarCategories", func(t *testing.T) { testGetSidebarCategories(t, ss) }) t.Run("UpdateSidebarCategories", func(t *testing.T) { testUpdateSidebarCategories(t, ss, s) }) t.Run("DeleteSidebarCategory", func(t *testing.T) { testDeleteSidebarCategory(t, ss, s) }) + t.Run("UpdateSidebarChannelsByPreferences", func(t *testing.T) { testUpdateSidebarChannelsByPreferences(t, ss) }) } func testCreateInitialSidebarCategories(t *testing.T, ss store.Store) { @@ -1676,3 +1677,47 @@ func testDeleteSidebarCategory(t *testing.T, ss store.Store, s SqlSupplier) { assert.NotNil(t, err) }) } + +func testUpdateSidebarChannelsByPreferences(t *testing.T, ss store.Store) { + t.Run("Should be able to update sidebar channels", func(t *testing.T) { + userId := model.NewId() + teamId := model.NewId() + + nErr := ss.Channel().CreateInitialSidebarCategories(userId, teamId) + require.Nil(t, nErr) + + channel, nErr := ss.Channel().Save(&model.Channel{ + Name: "channel", + Type: model.CHANNEL_OPEN, + TeamId: teamId, + }, 10) + require.Nil(t, nErr) + + err := ss.Channel().UpdateSidebarChannelsByPreferences(&model.Preferences{ + model.Preference{ + Name: channel.Id, + Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL, + Value: "true", + }, + }) + assert.NoError(t, err) + }) + + t.Run("Should not panic if channel is not found", func(t *testing.T) { + userId := model.NewId() + teamId := model.NewId() + + nErr := ss.Channel().CreateInitialSidebarCategories(userId, teamId) + assert.Nil(t, nErr) + + require.NotPanics(t, func() { + _ = ss.Channel().UpdateSidebarChannelsByPreferences(&model.Preferences{ + model.Preference{ + Name: "fakeid", + Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL, + Value: "true", + }, + }) + }) + }) +}