* Make UpdateSidebarCategories return the original categories

* MM-20897 Add category muting

* Prevent muting the DMs category

* Fix muted state not being stored in the database

* Address feedback

* Address some feedback

* Fix unit tests

* MM-20897 Mute/unmute channels in the database in bulk

* Satisfy golangci-lint
Этот коммит содержится в:
Harrison Healey
2020-11-16 15:19:01 -05:00
коммит произвёл GitHub
родитель 7f6b43a15f
Коммит 2ebc8ec90f
20 изменённых файлов: 1189 добавлений и 147 удалений

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

@@ -1286,6 +1286,24 @@ func (s *OpenTracingLayerChannelStore) GetMembers(channelId string, offset int,
return result, err
}
func (s *OpenTracingLayerChannelStore) GetMembersByChannelIds(channelIds []string, userId string) (*model.ChannelMembers, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ChannelStore.GetMembersByChannelIds")
s.Root.Store.SetContext(newCtx)
defer func() {
s.Root.Store.SetContext(origCtx)
}()
defer span.Finish()
result, err := s.ChannelStore.GetMembersByChannelIds(channelIds, userId)
if err != nil {
span.LogFields(spanlog.Error(err))
ext.Error.Set(span, true)
}
return result, err
}
func (s *OpenTracingLayerChannelStore) GetMembersByIds(channelId string, userIds []string) (*model.ChannelMembers, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ChannelStore.GetMembersByIds")
@@ -2164,7 +2182,7 @@ func (s *OpenTracingLayerChannelStore) UpdateMultipleMembers(members []*model.Ch
return result, err
}
func (s *OpenTracingLayerChannelStore) UpdateSidebarCategories(userId string, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, error) {
func (s *OpenTracingLayerChannelStore) UpdateSidebarCategories(userId string, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, []*model.SidebarCategoryWithChannels, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ChannelStore.UpdateSidebarCategories")
s.Root.Store.SetContext(newCtx)
@@ -2173,13 +2191,13 @@ func (s *OpenTracingLayerChannelStore) UpdateSidebarCategories(userId string, te
}()
defer span.Finish()
result, err := s.ChannelStore.UpdateSidebarCategories(userId, teamId, categories)
result, resultVar1, err := s.ChannelStore.UpdateSidebarCategories(userId, teamId, categories)
if err != nil {
span.LogFields(spanlog.Error(err))
ext.Error.Set(span, true)
}
return result, err
return result, resultVar1, err
}
func (s *OpenTracingLayerChannelStore) UpdateSidebarCategoryOrder(userId string, teamId string, categoryOrder []string) error {

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

@@ -1390,6 +1390,26 @@ func (s *RetryLayerChannelStore) GetMembers(channelId string, offset int, limit
}
func (s *RetryLayerChannelStore) GetMembersByChannelIds(channelIds []string, userId string) (*model.ChannelMembers, error) {
tries := 0
for {
result, err := s.ChannelStore.GetMembersByChannelIds(channelIds, userId)
if err == nil {
return result, nil
}
if !isRepeatableError(err) {
return result, err
}
tries++
if tries >= 3 {
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
return result, err
}
}
}
func (s *RetryLayerChannelStore) GetMembersByIds(channelId string, userIds []string) (*model.ChannelMembers, error) {
tries := 0
@@ -2298,21 +2318,21 @@ func (s *RetryLayerChannelStore) UpdateMultipleMembers(members []*model.ChannelM
}
func (s *RetryLayerChannelStore) UpdateSidebarCategories(userId string, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, error) {
func (s *RetryLayerChannelStore) UpdateSidebarCategories(userId string, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, []*model.SidebarCategoryWithChannels, error) {
tries := 0
for {
result, err := s.ChannelStore.UpdateSidebarCategories(userId, teamId, categories)
result, resultVar1, err := s.ChannelStore.UpdateSidebarCategories(userId, teamId, categories)
if err == nil {
return result, nil
return result, resultVar1, nil
}
if !isRepeatableError(err) {
return result, err
return result, resultVar1, err
}
tries++
if tries >= 3 {
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
return result, err
return result, resultVar1, err
}
}

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

@@ -2955,27 +2955,30 @@ func (s SqlChannelStore) SearchGroupChannels(userId, term string) (*model.Channe
func (s SqlChannelStore) GetMembersByIds(channelId string, userIds []string) (*model.ChannelMembers, error) {
var dbMembers channelMemberWithSchemeRolesList
props := make(map[string]interface{})
idQuery := ""
for index, userId := range userIds {
if len(idQuery) > 0 {
idQuery += ", "
}
props["userId"+strconv.Itoa(index)] = userId
idQuery += ":userId" + strconv.Itoa(index)
}
keys, props := MapStringsToQueryParams(userIds, "User")
props["ChannelId"] = channelId
if _, err := s.GetReplica().Select(&dbMembers, CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY+"WHERE ChannelMembers.ChannelId = :ChannelId AND ChannelMembers.UserId IN ("+idQuery+")", props); err != nil {
if _, err := s.GetReplica().Select(&dbMembers, CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY+"WHERE ChannelMembers.ChannelId = :ChannelId AND ChannelMembers.UserId IN "+keys, props); err != nil {
return nil, errors.Wrapf(err, "failed to find ChannelMembers with channelId=%s and userId in %v", channelId, userIds)
}
return dbMembers.ToModel(), nil
}
func (s SqlChannelStore) GetMembersByChannelIds(channelIds []string, userId string) (*model.ChannelMembers, error) {
var dbMembers channelMemberWithSchemeRolesList
keys, props := MapStringsToQueryParams(channelIds, "Channel")
props["UserId"] = userId
if _, err := s.GetReplica().Select(&dbMembers, CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY+"WHERE ChannelMembers.UserId = :UserId AND ChannelMembers.ChannelId IN "+keys, props); err != nil {
return nil, errors.Wrapf(err, "failed to find ChannelMembers with userId=%s and channelId in %v", userId, channelIds)
}
return dbMembers.ToModel(), nil
}
func (s SqlChannelStore) GetChannelsByScheme(schemeId string, offset int, limit int) (model.ChannelList, error) {
var channels model.ChannelList
_, err := s.GetReplica().Select(&channels, "SELECT * FROM Channels WHERE SchemeId = :SchemeId ORDER BY DisplayName LIMIT :Limit OFFSET :Offset", map[string]interface{}{"SchemeId": schemeId, "Offset": offset, "Limit": limit})

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

@@ -266,6 +266,7 @@ func (s SqlChannelStore) CreateSidebarCategory(userId, teamId string, newCategor
Sorting: model.SidebarCategorySortDefault,
SortOrder: int64(model.MinimalSidebarSortDistance * len(newOrder)), // first we place it at the end of the list
Type: model.SidebarCategoryCustom,
Muted: newCategory.Muted,
}
if err = transaction.Insert(category); err != nil {
return nil, errors.Wrap(err, "failed to save SidebarCategory")
@@ -605,18 +606,19 @@ func (s SqlChannelStore) UpdateSidebarCategoryOrder(userId, teamId string, categ
return nil
}
func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, error) {
func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, []*model.SidebarCategoryWithChannels, error) {
transaction, err := s.GetMaster().Begin()
if err != nil {
return nil, errors.Wrap(err, "begin_transaction")
return nil, nil, errors.Wrap(err, "begin_transaction")
}
defer finalizeTransaction(transaction)
updatedCategories := []*model.SidebarCategoryWithChannels{}
originalCategories := []*model.SidebarCategoryWithChannels{}
for _, category := range categories {
originalCategory, err2 := s.GetSidebarCategory(category.Id)
if err2 != nil {
return nil, errors.Wrap(err2, "failed to find SidebarCategories")
return nil, nil, errors.Wrap(err2, "failed to find SidebarCategories")
}
// Copy category to avoid modifying an argument
@@ -629,24 +631,28 @@ func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categori
updatedCategory.TeamId = originalCategory.TeamId
updatedCategory.SortOrder = originalCategory.SortOrder
updatedCategory.Type = originalCategory.Type
updatedCategory.Muted = originalCategory.Muted
if updatedCategory.Type != model.SidebarCategoryCustom {
updatedCategory.DisplayName = originalCategory.DisplayName
}
if category.Type != model.SidebarCategoryDirectMessages {
if updatedCategory.Type != model.SidebarCategoryDirectMessages {
updatedCategory.Channels = make([]string, len(category.Channels))
copy(updatedCategory.Channels, category.Channels)
updatedCategory.Muted = category.Muted
}
updateQuery, updateParams, _ := s.getQueryBuilder().
Update("SidebarCategories").
Set("DisplayName", updatedCategory.DisplayName).
Set("Sorting", updatedCategory.Sorting).
Set("Muted", updatedCategory.Muted).
Where(sq.Eq{"Id": updatedCategory.Id}).ToSql()
if _, err = transaction.Exec(updateQuery, updateParams...); err != nil {
return nil, errors.Wrap(err, "failed to update SidebarCategories")
return nil, nil, errors.Wrap(err, "failed to update SidebarCategories")
}
// if we are updating DM category, it's order can't channel order cannot be changed.
@@ -667,11 +673,11 @@ func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categori
).ToSql()
if err2 != nil {
return nil, errors.Wrap(err2, "update_sidebar_catetories_tosql")
return nil, nil, errors.Wrap(err2, "update_sidebar_catetories_tosql")
}
if _, err = transaction.Exec(query, args...); err != nil {
return nil, errors.Wrap(err, "failed to delete SidebarChannels")
return nil, nil, errors.Wrap(err, "failed to delete SidebarChannels")
}
var channels []interface{}
@@ -687,7 +693,7 @@ func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categori
}
if err = transaction.Insert(channels...); err != nil {
return nil, errors.Wrap(err, "failed to save SidebarChannels")
return nil, nil, errors.Wrap(err, "failed to save SidebarChannels")
}
}
@@ -703,7 +709,7 @@ func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categori
).ToSql()
if _, err = transaction.Exec(sql, args...); err != nil {
return nil, errors.Wrap(err, "failed to delete Preferences")
return nil, nil, errors.Wrap(err, "failed to delete Preferences")
}
// And then add the new ones
@@ -716,7 +722,7 @@ func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categori
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Value: "true",
}); err != nil {
return nil, errors.Wrap(err, "failed to save Preference")
return nil, nil, errors.Wrap(err, "failed to save Preference")
}
}
} else {
@@ -729,32 +735,33 @@ func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categori
},
).ToSql()
if nErr != nil {
return nil, errors.Wrap(nErr, "update_sidebar_categories_tosql")
return nil, nil, errors.Wrap(nErr, "update_sidebar_categories_tosql")
}
if _, nErr = transaction.Exec(query, args...); nErr != nil {
return nil, errors.Wrap(nErr, "failed to delete Preferences")
return nil, nil, errors.Wrap(nErr, "failed to delete Preferences")
}
}
updatedCategories = append(updatedCategories, updatedCategory)
originalCategories = append(originalCategories, originalCategory)
}
// Ensure Channels are populated for Channels/Direct Messages category if they change
for i, updatedCategory := range updatedCategories {
populated, nErr := s.completePopulatingCategoryChannelsT(transaction, updatedCategory)
if nErr != nil {
return nil, nErr
return nil, nil, nErr
}
updatedCategories[i] = populated
}
if err = transaction.Commit(); err != nil {
return nil, errors.Wrap(err, "commit_transaction")
return nil, nil, errors.Wrap(err, "commit_transaction")
}
return updatedCategories, nil
return updatedCategories, originalCategories, nil
}
// UpdateSidebarChannelsByPreferences is called when the Preference table is being updated to keep SidebarCategories in sync

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

@@ -20,6 +20,7 @@ import (
const (
CURRENT_SCHEMA_VERSION = VERSION_5_29_0
VERSION_5_30_0 = "5.30.0"
VERSION_5_29_0 = "5.29.0"
VERSION_5_28_1 = "5.28.1"
VERSION_5_28_0 = "5.28.0"
@@ -866,15 +867,6 @@ func upgradeDatabaseToVersion5281(sqlStore SqlStore) {
}
}
func upgradeDatabaseToVersion530(sqlStore SqlStore) {
// if shouldPerformUpgrade(sqlStore, VERSION_5_29_0, VERSION_5_30_0) {
sqlStore.CreateColumnIfNotExistsNoDefault("FileInfo", "Content", "longtext", "text")
// saveSchemaVersion(sqlStore, VERSION_5_30_0)
// }
}
func precheckMigrationToVersion528(sqlStore SqlStore) error {
teamsQuery, _, err := sqlStore.getQueryBuilder().Select(`COALESCE(SUM(CASE
WHEN CHAR_LENGTH(SchemeId) > 26 THEN 1
@@ -948,3 +940,14 @@ func upgradeDatabaseToVersion529(sqlStore SqlStore) {
saveSchemaVersion(sqlStore, VERSION_5_29_0)
}
}
func upgradeDatabaseToVersion530(sqlStore SqlStore) {
// if shouldPerformUpgrade(sqlStore, VERSION_5_29_0, VERSION_5_30_0) {
sqlStore.CreateColumnIfNotExistsNoDefault("FileInfo", "Content", "longtext", "text")
sqlStore.CreateColumnIfNotExists("SidebarCategories", "Muted", "tinyint(1)", "boolean", "0")
// saveSchemaVersion(sqlStore, VERSION_5_30_0)
// }
}

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

@@ -207,6 +207,7 @@ type ChannelStore interface {
SearchMore(userId string, teamId string, term string) (*model.ChannelList, error)
SearchGroupChannels(userId, term string) (*model.ChannelList, error)
GetMembersByIds(channelId string, userIds []string) (*model.ChannelMembers, error)
GetMembersByChannelIds(channelIds []string, userId string) (*model.ChannelMembers, error)
AnalyticsDeletedTypeCount(teamId string, channelType string) (int64, error)
GetChannelUnread(channelId, userId string) (*model.ChannelUnread, error)
ClearCaches()
@@ -221,7 +222,7 @@ type ChannelStore interface {
GetSidebarCategoryOrder(userId, teamId string) ([]string, error)
CreateSidebarCategory(userId, teamId string, newCategory *model.SidebarCategoryWithChannels) (*model.SidebarCategoryWithChannels, error)
UpdateSidebarCategoryOrder(userId, teamId string, categoryOrder []string) error
UpdateSidebarCategories(userId, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, error)
UpdateSidebarCategories(userId, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, []*model.SidebarCategoryWithChannels, error)
UpdateSidebarChannelsByPreferences(preferences *model.Preferences) error
DeleteSidebarChannelsByPreferences(preferences *model.Preferences) error
DeleteSidebarCategory(categoryId string) error

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

@@ -84,6 +84,7 @@ func TestChannelStore(t *testing.T, ss store.Store, s SqlSupplier) {
t.Run("SearchForUserInTeam", func(t *testing.T) { testChannelStoreSearchForUserInTeam(t, ss) })
t.Run("SearchAllChannels", func(t *testing.T) { testChannelStoreSearchAllChannels(t, ss) })
t.Run("GetMembersByIds", func(t *testing.T) { testChannelStoreGetMembersByIds(t, ss) })
t.Run("GetMembersByChannelIds", func(t *testing.T) { testChannelStoreGetMembersByChannelIds(t, ss) })
t.Run("SearchGroupChannels", func(t *testing.T) { testChannelStoreSearchGroupChannels(t, ss) })
t.Run("AnalyticsDeletedTypeCount", func(t *testing.T) { testChannelStoreAnalyticsDeletedTypeCount(t, ss) })
t.Run("GetPinnedPosts", func(t *testing.T) { testChannelStoreGetPinnedPosts(t, ss) })
@@ -5546,6 +5547,64 @@ func testChannelStoreGetMembersByIds(t *testing.T, ss store.Store) {
require.NotNil(t, nErr, "empty user ids - should have failed")
}
func testChannelStoreGetMembersByChannelIds(t *testing.T, ss store.Store) {
userId := model.NewId()
// Create a couple channels and add the user to them
channel1, err := ss.Channel().Save(&model.Channel{
TeamId: model.NewId(),
DisplayName: model.NewId(),
Name: model.NewId(),
Type: model.CHANNEL_OPEN,
}, -1)
require.Nil(t, err)
channel2, err := ss.Channel().Save(&model.Channel{
TeamId: model.NewId(),
DisplayName: model.NewId(),
Name: model.NewId(),
Type: model.CHANNEL_OPEN,
}, -1)
require.Nil(t, err)
_, err = ss.Channel().SaveMember(&model.ChannelMember{
ChannelId: channel1.Id,
UserId: userId,
NotifyProps: model.GetDefaultChannelNotifyProps(),
})
require.Nil(t, err)
_, err = ss.Channel().SaveMember(&model.ChannelMember{
ChannelId: channel2.Id,
UserId: userId,
NotifyProps: model.GetDefaultChannelNotifyProps(),
})
require.Nil(t, err)
t.Run("should return the user's members for the given channels", func(t *testing.T) {
result, nErr := ss.Channel().GetMembersByChannelIds([]string{channel1.Id, channel2.Id}, userId)
require.Nil(t, nErr)
assert.Len(t, *result, 2)
assert.Equal(t, userId, (*result)[0].UserId)
assert.True(t, (*result)[0].ChannelId == channel1.Id || (*result)[1].ChannelId == channel1.Id)
assert.Equal(t, userId, (*result)[1].UserId)
assert.True(t, (*result)[0].ChannelId == channel2.Id || (*result)[1].ChannelId == channel2.Id)
})
t.Run("should not error or return anything for invalid channel IDs", func(t *testing.T) {
result, nErr := ss.Channel().GetMembersByChannelIds([]string{model.NewId(), model.NewId()}, userId)
require.Nil(t, nErr)
assert.Len(t, *result, 0)
})
t.Run("should not error or return anything for invalid user IDs", func(t *testing.T) {
result, nErr := ss.Channel().GetMembersByChannelIds([]string{channel1.Id, channel2.Id}, model.NewId())
require.Nil(t, nErr)
assert.Len(t, *result, 0)
})
}
func testChannelStoreSearchGroupChannels(t *testing.T, ss store.Store) {
// Users
u1 := &model.User{}

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

@@ -478,7 +478,7 @@ func testCreateSidebarCategory(t *testing.T, ss store.Store) {
// Assign them to categories
favoritesCategory.Channels = []string{channel1.Id}
channelsCategory.Channels = []string{channel2.Id}
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
favoritesCategory,
channelsCategory,
})
@@ -683,7 +683,7 @@ func testGetSidebarCategory(t *testing.T, ss store.Store, s SqlSupplier) {
require.Nil(t, nErr)
// And assign one to another category
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: favoritesCategory.SidebarCategory,
Channels: []string{channel2.Id},
@@ -884,7 +884,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
dmsCategory := initialCategories.Categories[2]
// And then update one of them
updated, err := ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
updated, _, err := ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
channelsCategory,
})
require.Nil(t, err)
@@ -917,7 +917,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
dmsCategory := initialCategories.Categories[2]
// And then update them
updatedCategories, err := ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
updatedCategories, _, err := ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
favoritesCategory,
channelsCategory,
dmsCategory,
@@ -980,7 +980,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
},
}
updatedCategories, err := ss.Channel().UpdateSidebarCategories(userId, teamId, categoriesToUpdate)
updatedCategories, _, err := ss.Channel().UpdateSidebarCategories(userId, teamId, categoriesToUpdate)
assert.Nil(t, err)
assert.NotEqual(t, "Favorites", categoriesToUpdate[0].DisplayName)
@@ -1022,7 +1022,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
require.Nil(t, nErr)
// Assign it to favorites
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: favoritesCategory.SidebarCategory,
Channels: []string{channel.Id},
@@ -1039,7 +1039,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
channelsCategory := categories.Categories[1]
require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type)
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: channelsCategory.SidebarCategory,
Channels: []string{channel.Id},
@@ -1087,7 +1087,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
assert.Nil(t, nErr)
// Assign it to favorites
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: favoritesCategory.SidebarCategory,
Channels: []string{dmChannel.Id},
@@ -1104,7 +1104,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
dmsCategory := categories.Categories[2]
require.Equal(t, model.SidebarCategoryDirectMessages, dmsCategory.Type)
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: dmsCategory.SidebarCategory,
Channels: []string{dmChannel.Id},
@@ -1162,7 +1162,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
assert.Nil(t, nErr)
// Assign it to favorites on the first team. The favorites preference gets set for all teams.
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: favoritesCategory.SidebarCategory,
Channels: []string{dmChannel.Id},
@@ -1176,7 +1176,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
assert.Equal(t, "true", res.Value)
// Assign it to favorites on the second team. The favorites preference is already set.
updated, err := ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
updated, _, err := ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: favoritesCategory2.SidebarCategory,
Channels: []string{dmChannel.Id},
@@ -1191,7 +1191,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
assert.Equal(t, "true", res.Value)
// Remove it from favorites on the first team. This clears the favorites preference for all teams.
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: favoritesCategory.SidebarCategory,
Channels: []string{},
@@ -1204,7 +1204,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
assert.Nil(t, res)
// Remove it from favorites on the second team. The favorites preference was already deleted.
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId2, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId2, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: favoritesCategory2.SidebarCategory,
Channels: []string{},
@@ -1268,7 +1268,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
require.Nil(t, nErr)
// Have user1 favorite it
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: favoritesCategory.SidebarCategory,
Channels: []string{channel.Id},
@@ -1290,7 +1290,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
assert.Nil(t, res)
// And user2 favorite it
_, err = ss.Channel().UpdateSidebarCategories(userId2, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId2, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: favoritesCategory2.SidebarCategory,
Channels: []string{channel.Id},
@@ -1313,7 +1313,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
assert.Equal(t, "true", res.Value)
// And then user1 unfavorite it
_, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: channelsCategory.SidebarCategory,
Channels: []string{channel.Id},
@@ -1335,7 +1335,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
assert.Equal(t, "true", res.Value)
// And finally user2 favorite it
_, err = ss.Channel().UpdateSidebarCategories(userId2, teamId, []*model.SidebarCategoryWithChannels{
_, _, err = ss.Channel().UpdateSidebarCategories(userId2, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: channelsCategory2.SidebarCategory,
Channels: []string{channel.Id},
@@ -1416,7 +1416,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
},
}
updatedCategories, nErr := ss.Channel().UpdateSidebarCategories(userId, teamId, categoriesToUpdate)
updatedCategories, _, nErr := ss.Channel().UpdateSidebarCategories(userId, teamId, categoriesToUpdate)
assert.Nil(t, nErr)
// The channels should still exist in the category because they would otherwise be orphaned
@@ -1470,7 +1470,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
},
}
updatedCategories, err := ss.Channel().UpdateSidebarCategories(userId, teamId, categoriesToUpdate)
updatedCategories, _, err := ss.Channel().UpdateSidebarCategories(userId, teamId, categoriesToUpdate)
assert.Nil(t, err)
assert.Equal(t, dmsCategory.Id, updatedCategories[0].Id)
assert.Equal(t, []string{}, updatedCategories[0].Channels)
@@ -1497,7 +1497,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
},
}
updatedCategories, err = ss.Channel().UpdateSidebarCategories(userId, teamId, categoriesToUpdate)
updatedCategories, _, err = ss.Channel().UpdateSidebarCategories(userId, teamId, categoriesToUpdate)
assert.Nil(t, err)
assert.Equal(t, dmsCategory.Id, updatedCategories[0].Id)
assert.Equal(t, []string{dmChannel.Id}, updatedCategories[0].Channels)
@@ -1545,7 +1545,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
require.Nil(t, nErr)
// Move the channel one way
updatedCategories, nErr := ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
updatedCategories, _, nErr := ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: channelsCategory.SidebarCategory,
Channels: []string{},
@@ -1561,7 +1561,7 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
assert.Equal(t, []string{channel.Id}, updatedCategories[1].Channels)
// And then the other
updatedCategories, nErr = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
updatedCategories, _, nErr = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: channelsCategory.SidebarCategory,
Channels: []string{channel.Id},
@@ -1575,6 +1575,77 @@ func testUpdateSidebarCategories(t *testing.T, ss store.Store, s SqlSupplier) {
assert.Equal(t, []string{channel.Id}, updatedCategories[0].Channels)
assert.Equal(t, []string{}, updatedCategories[1].Channels)
})
t.Run("should correctly return the original categories that were modified", func(t *testing.T) {
userId := model.NewId()
teamId := model.NewId()
// Join a channel
channel, nErr := ss.Channel().Save(&model.Channel{
Name: "channel",
Type: model.CHANNEL_OPEN,
TeamId: teamId,
}, 10)
require.Nil(t, nErr)
_, err := ss.Channel().SaveMember(&model.ChannelMember{
UserId: userId,
ChannelId: channel.Id,
NotifyProps: model.GetDefaultChannelNotifyProps(),
})
require.Nil(t, err)
// And then create the initial categories so that Channels includes the channel
nErr = ss.Channel().CreateInitialSidebarCategories(userId, teamId)
require.Nil(t, nErr)
initialCategories, nErr := ss.Channel().GetSidebarCategories(userId, teamId)
require.Nil(t, nErr)
channelsCategory := initialCategories.Categories[1]
require.Equal(t, []string{channel.Id}, channelsCategory.Channels)
customCategory, nErr := ss.Channel().CreateSidebarCategory(userId, teamId, &model.SidebarCategoryWithChannels{
SidebarCategory: model.SidebarCategory{
DisplayName: "originalName",
},
})
require.Nil(t, nErr)
// Rename the custom category
updatedCategories, originalCategories, nErr := ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: customCategory.Id,
DisplayName: "updatedName",
},
},
})
require.Nil(t, nErr)
require.Equal(t, len(updatedCategories), len(originalCategories))
assert.Equal(t, "originalName", originalCategories[0].DisplayName)
assert.Equal(t, "updatedName", updatedCategories[0].DisplayName)
// Move a channel
updatedCategories, originalCategories, nErr = ss.Channel().UpdateSidebarCategories(userId, teamId, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: channelsCategory.SidebarCategory,
Channels: []string{},
},
{
SidebarCategory: customCategory.SidebarCategory,
Channels: []string{channel.Id},
},
})
require.Nil(t, nErr)
require.Equal(t, len(updatedCategories), len(originalCategories))
require.Equal(t, updatedCategories[0].Id, originalCategories[0].Id)
require.Equal(t, updatedCategories[1].Id, originalCategories[1].Id)
assert.Equal(t, []string{channel.Id}, originalCategories[0].Channels)
assert.Equal(t, []string{}, updatedCategories[0].Channels)
assert.Equal(t, []string{}, originalCategories[1].Channels)
assert.Equal(t, []string{channel.Id}, updatedCategories[1].Channels)
})
}
func testDeleteSidebarCategory(t *testing.T, ss store.Store, s SqlSupplier) {

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

@@ -934,6 +934,29 @@ func (_m *ChannelStore) GetMembers(channelId string, offset int, limit int) (*mo
return r0, r1
}
// GetMembersByChannelIds provides a mock function with given fields: channelIds, userId
func (_m *ChannelStore) GetMembersByChannelIds(channelIds []string, userId string) (*model.ChannelMembers, error) {
ret := _m.Called(channelIds, userId)
var r0 *model.ChannelMembers
if rf, ok := ret.Get(0).(func([]string, string) *model.ChannelMembers); ok {
r0 = rf(channelIds, userId)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.ChannelMembers)
}
}
var r1 error
if rf, ok := ret.Get(1).(func([]string, string) error); ok {
r1 = rf(channelIds, userId)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// GetMembersByIds provides a mock function with given fields: channelId, userIds
func (_m *ChannelStore) GetMembersByIds(channelId string, userIds []string) (*model.ChannelMembers, error) {
ret := _m.Called(channelId, userIds)
@@ -1859,7 +1882,7 @@ func (_m *ChannelStore) UpdateMultipleMembers(members []*model.ChannelMember) ([
}
// UpdateSidebarCategories provides a mock function with given fields: userId, teamId, categories
func (_m *ChannelStore) UpdateSidebarCategories(userId string, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, error) {
func (_m *ChannelStore) UpdateSidebarCategories(userId string, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, []*model.SidebarCategoryWithChannels, error) {
ret := _m.Called(userId, teamId, categories)
var r0 []*model.SidebarCategoryWithChannels
@@ -1871,14 +1894,23 @@ func (_m *ChannelStore) UpdateSidebarCategories(userId string, teamId string, ca
}
}
var r1 error
if rf, ok := ret.Get(1).(func(string, string, []*model.SidebarCategoryWithChannels) error); ok {
var r1 []*model.SidebarCategoryWithChannels
if rf, ok := ret.Get(1).(func(string, string, []*model.SidebarCategoryWithChannels) []*model.SidebarCategoryWithChannels); ok {
r1 = rf(userId, teamId, categories)
} else {
r1 = ret.Error(1)
if ret.Get(1) != nil {
r1 = ret.Get(1).([]*model.SidebarCategoryWithChannels)
}
}
return r0, r1
var r2 error
if rf, ok := ret.Get(2).(func(string, string, []*model.SidebarCategoryWithChannels) error); ok {
r2 = rf(userId, teamId, categories)
} else {
r2 = ret.Error(2)
}
return r0, r1, r2
}
// UpdateSidebarCategoryOrder provides a mock function with given fields: userId, teamId, categoryOrder

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

@@ -1191,6 +1191,22 @@ func (s *TimerLayerChannelStore) GetMembers(channelId string, offset int, limit
return result, err
}
func (s *TimerLayerChannelStore) GetMembersByChannelIds(channelIds []string, userId string) (*model.ChannelMembers, error) {
start := timemodule.Now()
result, err := s.ChannelStore.GetMembersByChannelIds(channelIds, userId)
elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
if s.Root.Metrics != nil {
success := "false"
if err == nil {
success = "true"
}
s.Root.Metrics.ObserveStoreMethodDuration("ChannelStore.GetMembersByChannelIds", success, elapsed)
}
return result, err
}
func (s *TimerLayerChannelStore) GetMembersByIds(channelId string, userIds []string) (*model.ChannelMembers, error) {
start := timemodule.Now()
@@ -2000,10 +2016,10 @@ func (s *TimerLayerChannelStore) UpdateMultipleMembers(members []*model.ChannelM
return result, err
}
func (s *TimerLayerChannelStore) UpdateSidebarCategories(userId string, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, error) {
func (s *TimerLayerChannelStore) UpdateSidebarCategories(userId string, teamId string, categories []*model.SidebarCategoryWithChannels) ([]*model.SidebarCategoryWithChannels, []*model.SidebarCategoryWithChannels, error) {
start := timemodule.Now()
result, err := s.ChannelStore.UpdateSidebarCategories(userId, teamId, categories)
result, resultVar1, err := s.ChannelStore.UpdateSidebarCategories(userId, teamId, categories)
elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
if s.Root.Metrics != nil {
@@ -2013,7 +2029,7 @@ func (s *TimerLayerChannelStore) UpdateSidebarCategories(userId string, teamId s
}
s.Root.Metrics.ObserveStoreMethodDuration("ChannelStore.UpdateSidebarCategories", success, elapsed)
}
return result, err
return result, resultVar1, err
}
func (s *TimerLayerChannelStore) UpdateSidebarCategoryOrder(userId string, teamId string, categoryOrder []string) error {