Refactor: func StringInSlice to slices.Contains (#24262)
* Refactor: Use generic to change func StringInSlice to Contains * Refactor: Use generic to change func stringNotInSlice to Contains Make utils.go (dir server/public/utils) and func Contains * Refactor: Move func Contains from channels/utils to public/utils Move func Contains from channels/utils to public/utils Fix import declarations line * Docs: Add a description of the Contains function * Test: add TestContains Add a test code for a Contain function
Этот коммит содержится в:
@@ -14,6 +14,8 @@ import (
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
|
||||
"github.com/mattermost/mattermost/server/v8/channels/app"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/audit"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
@@ -722,7 +724,7 @@ func getUsers(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.SetInvalidParam("role")
|
||||
return
|
||||
}
|
||||
roleValid := utils.StringInSlice(role, roleNamesAll)
|
||||
roleValid := pUtils.Contains(roleNamesAll, role)
|
||||
if !roleValid {
|
||||
c.SetInvalidParam("role")
|
||||
return
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
|
||||
"github.com/mattermost/mattermost/server/v8/channels/audit"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
@@ -85,7 +87,7 @@ func localGetUsers(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.SetInvalidParam("role")
|
||||
return
|
||||
}
|
||||
roleValid := utils.StringInSlice(role, roleNamesAll)
|
||||
roleValid := pUtils.Contains(roleNamesAll, role)
|
||||
if !roleValid {
|
||||
c.SetInvalidParam("role")
|
||||
return
|
||||
|
||||
@@ -15,11 +15,12 @@ import (
|
||||
"github.com/mattermost/mattermost/server/public/plugin"
|
||||
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
|
||||
"github.com/mattermost/mattermost/server/v8/channels/app/request"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/product"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store/sqlstore"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
)
|
||||
|
||||
// channelsWrapper provides an implementation of `product.ChannelService` to be used by products.
|
||||
@@ -1103,7 +1104,7 @@ func (a *App) PatchChannelModerationsForChannel(c request.CTX, channel *model.Ch
|
||||
|
||||
for _, channelModerationPatch := range channelModerationsPatch {
|
||||
permissionModified := *channelModerationPatch.Name
|
||||
if channelModerationPatch.Roles.Guests != nil && utils.StringInSlice(permissionModified, model.ChannelModeratedPermissionsChangedByPatch(guestRole, guestRolePatch)) {
|
||||
if channelModerationPatch.Roles.Guests != nil && pUtils.Contains(model.ChannelModeratedPermissionsChangedByPatch(guestRole, guestRolePatch), permissionModified) {
|
||||
if *channelModerationPatch.Roles.Guests {
|
||||
c.Logger().Info("Permission enabled for guests.", mlog.String("permission", permissionModified), mlog.String("channel_id", channel.Id), mlog.String("channel_name", channel.Name))
|
||||
} else {
|
||||
@@ -1111,7 +1112,7 @@ func (a *App) PatchChannelModerationsForChannel(c request.CTX, channel *model.Ch
|
||||
}
|
||||
}
|
||||
|
||||
if channelModerationPatch.Roles.Members != nil && utils.StringInSlice(permissionModified, model.ChannelModeratedPermissionsChangedByPatch(memberRole, memberRolePatch)) {
|
||||
if channelModerationPatch.Roles.Members != nil && pUtils.Contains(model.ChannelModeratedPermissionsChangedByPatch(memberRole, memberRolePatch), permissionModified) {
|
||||
if *channelModerationPatch.Roles.Members {
|
||||
c.Logger().Info("Permission enabled for members.", mlog.String("permission", permissionModified), mlog.String("channel_id", channel.Id), mlog.String("channel_name", channel.Name))
|
||||
} else {
|
||||
|
||||
@@ -13,8 +13,9 @@ import (
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
)
|
||||
|
||||
func getLicWithSkuShortName(skuShortName string) *model.License {
|
||||
@@ -49,7 +50,7 @@ func TestSendNotifications(t *testing.T) {
|
||||
mentions, err := th.App.SendNotifications(th.Context, post1, th.BasicTeam, th.BasicChannel, th.BasicUser, nil, true)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, mentions)
|
||||
require.True(t, utils.StringInSlice(th.BasicUser2.Id, mentions), "mentions", mentions)
|
||||
require.True(t, pUtils.Contains(mentions, th.BasicUser2.Id), "mentions", mentions)
|
||||
|
||||
t.Run("license is required for group mention", func(t *testing.T) {
|
||||
group := th.CreateGroup()
|
||||
@@ -145,7 +146,7 @@ func TestSendNotifications(t *testing.T) {
|
||||
}
|
||||
mentions, err = th.App.SendNotifications(th.Context, childPost, th.BasicTeam, th.BasicChannel, th.BasicUser2, &postList, true)
|
||||
require.NoError(t, err)
|
||||
require.False(t, utils.StringInSlice(user.Id, mentions))
|
||||
require.False(t, pUtils.Contains(mentions, user.Id))
|
||||
}
|
||||
|
||||
th.BasicUser.NotifyProps[model.CommentsNotifyProp] = model.CommentsNotifyAny
|
||||
@@ -2815,7 +2816,7 @@ func TestReplyPostNotificationsWithCRT(t *testing.T) {
|
||||
}
|
||||
mentions, err := th.App.SendNotifications(th.Context, childPost, th.BasicTeam, th.BasicChannel, th.BasicUser2, &postList, true)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, utils.StringInSlice(user.Id, mentions))
|
||||
assert.False(t, pUtils.Contains(mentions, user.Id))
|
||||
|
||||
membership, err := th.App.GetThreadMembershipForUser(user.Id, rootPost.Id)
|
||||
assert.Error(t, err)
|
||||
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
)
|
||||
|
||||
@@ -46,7 +48,7 @@ func (a *App) AddPublicKey(name string, key io.Reader) *model.AppError {
|
||||
}
|
||||
|
||||
a.UpdateConfig(func(cfg *model.Config) {
|
||||
if !utils.StringInSlice(name, cfg.PluginSettings.SignaturePublicKeyFiles) {
|
||||
if !pUtils.Contains(cfg.PluginSettings.SignaturePublicKeyFiles, name) {
|
||||
cfg.PluginSettings.SignaturePublicKeyFiles = append(cfg.PluginSettings.SignaturePublicKeyFiles, name)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
)
|
||||
@@ -187,13 +189,13 @@ func (a *App) UpdateRole(role *model.Role) (*model.Role, *model.AppError) {
|
||||
|
||||
builtInRolesMinusChannelRoles := append(utils.RemoveStringsFromSlice(model.BuiltInSchemeManagedRoleIDs, builtInChannelRoles...), model.NewSystemRoleIDs...)
|
||||
|
||||
if utils.StringInSlice(savedRole.Name, builtInRolesMinusChannelRoles) {
|
||||
if pUtils.Contains(builtInRolesMinusChannelRoles, savedRole.Name) {
|
||||
return savedRole, nil
|
||||
}
|
||||
|
||||
var roleRetrievalFunc func() ([]*model.Role, *model.AppError)
|
||||
|
||||
if utils.StringInSlice(savedRole.Name, builtInChannelRoles) {
|
||||
if pUtils.Contains(builtInChannelRoles, savedRole.Name) {
|
||||
roleRetrievalFunc = func() ([]*model.Role, *model.AppError) {
|
||||
roles, nErr := a.Srv().Store().Role().AllChannelSchemeRoles()
|
||||
if nErr != nil {
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
)
|
||||
|
||||
type permissionInheritanceTestData struct {
|
||||
@@ -37,7 +37,7 @@ func TestGetRolesByNames(t *testing.T) {
|
||||
require.NotNil(t, actualRole)
|
||||
require.Equal(t, testData.channelRole.Name, actualRole.Name)
|
||||
|
||||
require.Equal(t, testData.shouldHavePermission, utils.StringInSlice(testData.permission.Id, actualRole.Permissions))
|
||||
require.Equal(t, testData.shouldHavePermission, pUtils.Contains(actualRole.Permissions, testData.permission.Id))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestGetRoleByName(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, actualRole)
|
||||
require.Equal(t, testData.channelRole.Name, actualRole.Name)
|
||||
require.Equal(t, testData.shouldHavePermission, utils.StringInSlice(testData.permission.Id, actualRole.Permissions), "row: %+v", testData.truthTableRow)
|
||||
require.Equal(t, testData.shouldHavePermission, pUtils.Contains(actualRole.Permissions, testData.permission.Id), "row: %+v", testData.truthTableRow)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ func TestGetRoleByID(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, actualRole)
|
||||
require.Equal(t, testData.channelRole.Id, actualRole.Id)
|
||||
require.Equal(t, testData.shouldHavePermission, utils.StringInSlice(testData.permission.Id, actualRole.Permissions), "row: %+v", testData.truthTableRow)
|
||||
require.Equal(t, testData.shouldHavePermission, pUtils.Contains(actualRole.Permissions, testData.permission.Id), "row: %+v", testData.truthTableRow)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ func TestGetAllRoles(t *testing.T) {
|
||||
if actualRole.Id == testData.channelRole.Id {
|
||||
require.NotNil(t, actualRole)
|
||||
require.Equal(t, testData.channelRole.Id, actualRole.Id)
|
||||
require.Equal(t, testData.shouldHavePermission, utils.StringInSlice(testData.permission.Id, actualRole.Permissions), "row: %+v", testData.truthTableRow)
|
||||
require.Equal(t, testData.shouldHavePermission, pUtils.Contains(actualRole.Permissions, testData.permission.Id), "row: %+v", testData.truthTableRow)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -6,8 +6,9 @@ package searchtest
|
||||
import (
|
||||
"testing"
|
||||
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -35,12 +36,12 @@ type searchTest struct {
|
||||
func filterTestsByTag(tests []searchTest, tags ...string) []searchTest {
|
||||
filteredTests := []searchTest{}
|
||||
for _, test := range tests {
|
||||
if utils.StringInSlice(EngineAll, test.Tags) {
|
||||
if pUtils.Contains(test.Tags, EngineAll) {
|
||||
filteredTests = append(filteredTests, test)
|
||||
continue
|
||||
}
|
||||
for _, tag := range tags {
|
||||
if utils.StringInSlice(tag, test.Tags) {
|
||||
if pUtils.Contains(test.Tags, tag) {
|
||||
filteredTests = append(filteredTests, test)
|
||||
break
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
)
|
||||
|
||||
func TestGroupStore(t *testing.T, ss store.Store) {
|
||||
@@ -4825,7 +4825,7 @@ func groupTestpUpdateMembersRoleTeam(t *testing.T, ss store.Store) {
|
||||
require.GreaterOrEqual(t, len(members), 4) // sanity check for team membership
|
||||
|
||||
for _, member := range members {
|
||||
if utils.StringInSlice(member.UserId, tt.inUserIDs) {
|
||||
if pUtils.Contains(tt.inUserIDs, member.UserId) {
|
||||
require.True(t, member.SchemeAdmin)
|
||||
} else {
|
||||
require.False(t, member.SchemeAdmin)
|
||||
@@ -4935,7 +4935,7 @@ func groupTestpUpdateMembersRoleChannel(t *testing.T, ss store.Store) {
|
||||
require.GreaterOrEqual(t, len(members), 4) // sanity check for channel membership
|
||||
|
||||
for _, member := range members {
|
||||
if utils.StringInSlice(member.UserId, tt.inUserIDs) {
|
||||
if pUtils.Contains(tt.inUserIDs, member.UserId) {
|
||||
require.True(t, member.SchemeAdmin)
|
||||
} else {
|
||||
require.False(t, member.SchemeAdmin)
|
||||
|
||||
@@ -14,17 +14,9 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
)
|
||||
|
||||
func StringInSlice(a string, slice []string) bool {
|
||||
for _, b := range slice {
|
||||
if b == a {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RemoveStringFromSlice removes the first occurrence of a from slice.
|
||||
func RemoveStringFromSlice(a string, slice []string) []string {
|
||||
for i, str := range slice {
|
||||
@@ -40,7 +32,7 @@ func RemoveStringsFromSlice(slice []string, strings ...string) []string {
|
||||
newSlice := []string{}
|
||||
|
||||
for _, item := range slice {
|
||||
if !StringInSlice(item, strings) {
|
||||
if !pUtils.Contains(strings, item) {
|
||||
newSlice = append(newSlice, item)
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user