diff --git a/api4/channel.go b/api4/channel.go index f0eb96eb95..f7d1396ba4 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -2056,8 +2056,10 @@ func updateCategoriesForTeamForUser(c *Context, w http.ResponseWriter, r *http.R w.Write(model.SidebarCategoriesWithChannelsToJson(categories)) } +// validateUserChannels confirms that the given user is a member of the given channel IDs. Returns an error if the user +// is not a member of any channel or nil if the user is a member of each channel. func validateUserChannels(operationName string, c *Context, teamId, userId string, channelIDs []string) *model.AppError { - channels, err := c.App.GetChannelsForUser(teamId, userId, false, 0) + channels, err := c.App.GetChannelsForUser(teamId, userId, true, 0) if err != nil { return model.NewAppError("Api4."+operationName, "api.invalid_channel", nil, err.Error(), http.StatusBadRequest) } diff --git a/api4/channel_test.go b/api4/channel_test.go index abf79030dd..86c12f6871 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -3996,10 +3996,6 @@ func TestUpdateCategoryForTeamForUser(t *testing.T) { Channels: []string{channelsCategory.Channels[1], channelsCategory.Channels[0], channelsCategory.Channels[4], channelsCategory.Channels[3], channelsCategory.Channels[2]}, } - t.Log("UserId=" + th.BasicUser.Id) - t.Log("TeamId=" + th.BasicTeam.Id) - t.Log("category=" + channelsCategory.Id) - received, resp := th.Client.UpdateSidebarCategoryForTeamForUser(th.BasicUser.Id, th.BasicTeam.Id, channelsCategory.Id, updatedCategory) assert.Nil(t, resp.Error) assert.Equal(t, channelsCategory.Id, received.Id) @@ -4076,4 +4072,33 @@ func TestUpdateCategoryForTeamForUser(t *testing.T) { assert.Equal(t, customCategory.Id, received.Id) assert.Equal(t, updatedCategory.DisplayName, received.DisplayName) }) + + t.Run("should update the channel order of the category even if it contains archived channels", func(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(th.BasicUser.Id, th.BasicTeam.Id, "") + require.Nil(t, resp.Error) + require.Len(t, categories.Categories, 3) + require.Len(t, categories.Order, 3) + + channelsCategory := categories.Categories[1] + require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type) + require.Len(t, channelsCategory.Channels, 5) // Town Square, Off Topic, and the 3 channels created by InitBasic + + // Delete one of the channels + _, resp = th.Client.DeleteChannel(th.BasicChannel.Id) + require.Nil(t, resp.Error) + + // Should still be able to reorder the channels + updatedCategory := &model.SidebarCategoryWithChannels{ + SidebarCategory: channelsCategory.SidebarCategory, + Channels: []string{channelsCategory.Channels[1], channelsCategory.Channels[0], channelsCategory.Channels[4], channelsCategory.Channels[3], channelsCategory.Channels[2]}, + } + + received, resp := th.Client.UpdateSidebarCategoryForTeamForUser(th.BasicUser.Id, th.BasicTeam.Id, channelsCategory.Id, updatedCategory) + require.Nil(t, resp.Error) + assert.Equal(t, channelsCategory.Id, received.Id) + assert.Equal(t, updatedCategory.Channels, received.Channels) + }) }