MM-26410/MM-26825 Improve syncing between favorites category and preferences (#15048)

* MM-26410 Allow moving channels into Favorites when they're favorited in prefs

* MM-26410 Fix management of Favorites category when updating preferences

* MM-26410 Add management of Favorites category when deleting preferences

* Address feedback 1

* Remove WHERE (1=1) from query

* Remove unnecessary sq.Expr

* Rewrite query to use left join

* Remove redundant where statement and add some more tests

* Fix linting issues

* Rename addChannelToFavoritesCategory to addChannelToFavoritesCategory
Этот коммит содержится в:
Harrison Healey
2020-07-22 09:04:40 -04:00
коммит произвёл GitHub
родитель 7602dc0b19
Коммит 14aba9bccb
9 изменённых файлов: 788 добавлений и 49 удалений

Просмотреть файл

@@ -8,6 +8,7 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/v5/model"
@@ -295,6 +296,233 @@ func TestUpdatePreferencesWebsocket(t *testing.T) {
}
}
func TestUpdateSidebarPreferences(t *testing.T) {
t.Run("when favoriting a channel, should add it to the Favorites sidebar category", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
user := th.BasicUser
team1 := th.CreateTeam()
th.LinkUserToTeam(user, team1)
_, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
channel := th.CreateChannelWithClientAndTeam(th.Client, model.CHANNEL_OPEN, team1.Id)
th.AddUserToChannel(user, channel)
// Confirm that the sidebar is populated correctly to begin with
categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
require.Contains(t, categories.Categories[1].Channels, channel.Id)
// Favorite the channel
_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: channel.Id,
Value: "true",
},
})
require.Nil(t, resp.Error)
// Confirm that the channel was added to the Favorites
categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.Contains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
// And unfavorite the channel
_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: channel.Id,
Value: "false",
},
})
require.Nil(t, resp.Error)
// The channel should've been removed from the Favorites
categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
assert.Contains(t, categories.Categories[1].Channels, channel.Id)
})
t.Run("when favoriting a DM channel, should add it to the Favorites sidebar category for all teams", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
user := th.BasicUser
user2 := th.BasicUser2
team1 := th.CreateTeam()
th.LinkUserToTeam(user, team1)
team2 := th.CreateTeam()
th.LinkUserToTeam(user, team2)
dmChannel := th.CreateDmChannel(user2)
// Favorite the channel
_, resp := th.Client.UpdatePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: dmChannel.Id,
Value: "true",
},
})
require.Nil(t, resp.Error)
// Confirm that the channel was added to the Favorites on all teams
categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.Contains(t, categories.Categories[0].Channels, dmChannel.Id)
require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
assert.NotContains(t, categories.Categories[2].Channels, dmChannel.Id)
categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team2.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.Contains(t, categories.Categories[0].Channels, dmChannel.Id)
require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
assert.NotContains(t, categories.Categories[2].Channels, dmChannel.Id)
// And unfavorite the channel
_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: dmChannel.Id,
Value: "false",
},
})
require.Nil(t, resp.Error)
// The channel should've been removed from the Favorites on all teams
categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, dmChannel.Id)
require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
assert.Contains(t, categories.Categories[2].Channels, dmChannel.Id)
categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team2.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, dmChannel.Id)
require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
assert.Contains(t, categories.Categories[2].Channels, dmChannel.Id)
})
t.Run("when favoriting a channel, should not affect other users' favorites categories", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
user := th.BasicUser
user2 := th.BasicUser2
client2 := th.CreateClient()
th.LoginBasic2WithClient(client2)
team1 := th.CreateTeam()
th.LinkUserToTeam(user, team1)
th.LinkUserToTeam(user2, team1)
_, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
_, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
require.Nil(t, resp.Error)
channel := th.CreateChannelWithClientAndTeam(th.Client, model.CHANNEL_OPEN, team1.Id)
th.AddUserToChannel(user, channel)
th.AddUserToChannel(user2, channel)
// Confirm that the sidebar is populated correctly to begin with
categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
require.Contains(t, categories.Categories[1].Channels, channel.Id)
categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
require.Contains(t, categories.Categories[1].Channels, channel.Id)
// Favorite the channel
_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: channel.Id,
Value: "true",
},
})
require.Nil(t, resp.Error)
// Confirm that the channel was not added to Favorites for the second user
categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.NotContains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
assert.Contains(t, categories.Categories[1].Channels, channel.Id)
// Favorite the channel for the second user
_, resp = client2.UpdatePreferences(user2.Id, &model.Preferences{
{
UserId: user2.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: channel.Id,
Value: "true",
},
})
require.Nil(t, resp.Error)
// Confirm that the channel is now in the Favorites for the second user
categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.Contains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
// And unfavorite the channel
_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: channel.Id,
Value: "false",
},
})
require.Nil(t, resp.Error)
// The channel should still be in the second user's favorites
categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.Contains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
})
}
func TestDeletePreferences(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
@@ -396,3 +624,219 @@ func TestDeletePreferencesWebsocket(t *testing.T) {
}
}
}
func TestDeleteSidebarPreferences(t *testing.T) {
t.Run("when removing a favorited channel preference, should remove it from the Favorites sidebar category", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
user := th.BasicUser
team1 := th.CreateTeam()
th.LinkUserToTeam(user, team1)
_, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
channel := th.CreateChannelWithClientAndTeam(th.Client, model.CHANNEL_OPEN, team1.Id)
th.AddUserToChannel(user, channel)
// Confirm that the sidebar is populated correctly to begin with
categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
require.Contains(t, categories.Categories[1].Channels, channel.Id)
// Favorite the channel
_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: channel.Id,
Value: "true",
},
})
require.Nil(t, resp.Error)
// Confirm that the channel was added to the Favorites
categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.Contains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
// And unfavorite the channel by deleting the preference
_, resp = th.Client.DeletePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: channel.Id,
},
})
require.Nil(t, resp.Error)
// The channel should've been removed from the Favorites
categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
assert.Contains(t, categories.Categories[1].Channels, channel.Id)
})
t.Run("when removing a favorited DM preference, should remove it from the Favorites sidebar category", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
user := th.BasicUser
user2 := th.BasicUser2
team1 := th.CreateTeam()
th.LinkUserToTeam(user, team1)
team2 := th.CreateTeam()
th.LinkUserToTeam(user, team2)
dmChannel := th.CreateDmChannel(user2)
// Favorite the channel
_, resp := th.Client.UpdatePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: dmChannel.Id,
Value: "true",
},
})
require.Nil(t, resp.Error)
// Confirm that the channel was added to the Favorites on all teams
categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.Contains(t, categories.Categories[0].Channels, dmChannel.Id)
require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
assert.NotContains(t, categories.Categories[2].Channels, dmChannel.Id)
categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team2.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.Contains(t, categories.Categories[0].Channels, dmChannel.Id)
require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
assert.NotContains(t, categories.Categories[2].Channels, dmChannel.Id)
// And unfavorite the channel by deleting the preference
_, resp = th.Client.DeletePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: dmChannel.Id,
},
})
require.Nil(t, resp.Error)
// The channel should've been removed from the Favorites on all teams
categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, dmChannel.Id)
require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
assert.Contains(t, categories.Categories[2].Channels, dmChannel.Id)
categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team2.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, dmChannel.Id)
require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
assert.Contains(t, categories.Categories[2].Channels, dmChannel.Id)
})
t.Run("when removing a favorited channel preference, should not affect other users' favorites categories", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
user := th.BasicUser
user2 := th.BasicUser2
client2 := th.CreateClient()
th.LoginBasic2WithClient(client2)
team1 := th.CreateTeam()
th.LinkUserToTeam(user, team1)
th.LinkUserToTeam(user2, team1)
_, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
_, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
require.Nil(t, resp.Error)
channel := th.CreateChannelWithClientAndTeam(th.Client, model.CHANNEL_OPEN, team1.Id)
th.AddUserToChannel(user, channel)
th.AddUserToChannel(user2, channel)
// Confirm that the sidebar is populated correctly to begin with
categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
require.Contains(t, categories.Categories[1].Channels, channel.Id)
categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
require.NotContains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
require.Contains(t, categories.Categories[1].Channels, channel.Id)
// Favorite the channel for both users
_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: channel.Id,
Value: "true",
},
})
require.Nil(t, resp.Error)
_, resp = client2.UpdatePreferences(user2.Id, &model.Preferences{
{
UserId: user2.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: channel.Id,
Value: "true",
},
})
require.Nil(t, resp.Error)
// Confirm that the channel is in the Favorites for the second user
categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.Contains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
// And unfavorite the channel for the first user by deleting the preference
_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Name: channel.Id,
Value: "false",
},
})
require.Nil(t, resp.Error)
// The channel should still be in the second user's favorites
categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
require.Nil(t, resp.Error)
require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
assert.Contains(t, categories.Categories[0].Channels, channel.Id)
require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
})
}