[MM-57356] Make use of go1.21 features (#26620)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3c45c44cb1
Коммит
1e0de8f559
@@ -9,6 +9,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
@@ -18,7 +19,6 @@ import (
|
||||
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
"github.com/mattermost/mattermost/server/public/shared/request"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store/sqlstore"
|
||||
)
|
||||
@@ -1036,7 +1036,7 @@ func (a *App) PatchChannelModerationsForChannel(c request.CTX, channel *model.Ch
|
||||
|
||||
for _, channelModerationPatch := range channelModerationsPatch {
|
||||
permissionModified := *channelModerationPatch.Name
|
||||
if channelModerationPatch.Roles.Guests != nil && pUtils.Contains(model.ChannelModeratedPermissionsChangedByPatch(guestRole, guestRolePatch), permissionModified) {
|
||||
if channelModerationPatch.Roles.Guests != nil && slices.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 {
|
||||
@@ -1044,7 +1044,7 @@ func (a *App) PatchChannelModerationsForChannel(c request.CTX, channel *model.Ch
|
||||
}
|
||||
}
|
||||
|
||||
if channelModerationPatch.Roles.Members != nil && pUtils.Contains(model.ChannelModeratedPermissionsChangedByPatch(memberRole, memberRolePatch), permissionModified) {
|
||||
if channelModerationPatch.Roles.Members != nil && slices.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 {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -17,7 +18,6 @@ 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/app/platform"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
@@ -56,7 +56,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, pUtils.Contains(mentions, th.BasicUser2.Id), "mentions", mentions)
|
||||
require.True(t, slices.Contains(mentions, th.BasicUser2.Id), "mentions", mentions)
|
||||
})
|
||||
|
||||
t.Run("license is required for group mention", func(t *testing.T) {
|
||||
@@ -197,7 +197,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, pUtils.Contains(mentions, user.Id))
|
||||
require.False(t, slices.Contains(mentions, user.Id))
|
||||
}
|
||||
|
||||
var appErr *model.AppError
|
||||
@@ -2831,7 +2831,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, pUtils.Contains(mentions, user.Id))
|
||||
assert.False(t, slices.Contains(mentions, user.Id))
|
||||
|
||||
membership, err := th.App.GetThreadMembershipForUser(user.Id, rootPost.Id)
|
||||
assert.Error(t, err)
|
||||
|
||||
@@ -6,12 +6,12 @@ package platform
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
"github.com/mattermost/mattermost/server/public/utils"
|
||||
"github.com/mattermost/mattermost/server/v8/platform/services/sharedchannel"
|
||||
)
|
||||
|
||||
@@ -59,7 +59,7 @@ func (ps *PlatformService) SharedChannelSyncHandler(event *model.WebSocketEvent)
|
||||
func isEligibleForEvents(syncService SharedChannelServiceIFace, event *model.WebSocketEvent, events []model.WebsocketEventType) bool {
|
||||
return syncServiceEnabled(syncService) &&
|
||||
eventHasChannel(event) &&
|
||||
utils.Contains(events, event.EventType())
|
||||
slices.Contains(events, event.EventType())
|
||||
}
|
||||
|
||||
func eventHasChannel(event *model.WebSocketEvent) bool {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -25,7 +26,6 @@ import (
|
||||
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
"github.com/mattermost/mattermost/server/public/shared/request"
|
||||
"github.com/mattermost/mattermost/server/public/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -877,7 +877,7 @@ func (wc *WebConn) ShouldSendEvent(msg *model.WebSocketEvent) bool {
|
||||
// For typing events, we don't send them to users who don't have
|
||||
// that channel or thread opened.
|
||||
if wc.Platform.Config().FeatureFlags.WebSocketEventScope &&
|
||||
utils.Contains([]model.WebsocketEventType{
|
||||
slices.Contains([]model.WebsocketEventType{
|
||||
model.WebsocketEventTyping,
|
||||
model.WebsocketEventReactionAdded,
|
||||
model.WebsocketEventReactionRemoved,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/crypto/openpgp" //nolint:staticcheck
|
||||
@@ -15,7 +16,6 @@ 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"
|
||||
)
|
||||
@@ -48,7 +48,7 @@ func (a *App) AddPublicKey(name string, key io.Reader) *model.AppError {
|
||||
}
|
||||
|
||||
a.UpdateConfig(func(cfg *model.Config) {
|
||||
if !pUtils.Contains(cfg.PluginSettings.SignaturePublicKeyFiles, name) {
|
||||
if !slices.Contains(cfg.PluginSettings.SignaturePublicKeyFiles, name) {
|
||||
cfg.PluginSettings.SignaturePublicKeyFiles = append(cfg.PluginSettings.SignaturePublicKeyFiles, name)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -9,10 +9,10 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"slices"
|
||||
"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"
|
||||
@@ -189,13 +189,13 @@ func (a *App) UpdateRole(role *model.Role) (*model.Role, *model.AppError) {
|
||||
|
||||
builtInRolesMinusChannelRoles := append(utils.RemoveStringsFromSlice(model.BuiltInSchemeManagedRoleIDs, builtInChannelRoles...), model.NewSystemRoleIDs...)
|
||||
|
||||
if pUtils.Contains(builtInRolesMinusChannelRoles, savedRole.Name) {
|
||||
if slices.Contains(builtInRolesMinusChannelRoles, savedRole.Name) {
|
||||
return savedRole, nil
|
||||
}
|
||||
|
||||
var roleRetrievalFunc func() ([]*model.Role, *model.AppError)
|
||||
|
||||
if pUtils.Contains(builtInChannelRoles, savedRole.Name) {
|
||||
if slices.Contains(builtInChannelRoles, savedRole.Name) {
|
||||
roleRetrievalFunc = func() ([]*model.Role, *model.AppError) {
|
||||
roles, nErr := a.Srv().Store().Role().AllChannelSchemeRoles()
|
||||
if nErr != nil {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"encoding/csv"
|
||||
"io"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -15,7 +16,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
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, pUtils.Contains(actualRole.Permissions, testData.permission.Id))
|
||||
require.Equal(t, testData.shouldHavePermission, slices.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, pUtils.Contains(actualRole.Permissions, testData.permission.Id), "row: %+v", testData.truthTableRow)
|
||||
require.Equal(t, testData.shouldHavePermission, slices.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, pUtils.Contains(actualRole.Permissions, testData.permission.Id), "row: %+v", testData.truthTableRow)
|
||||
require.Equal(t, testData.shouldHavePermission, slices.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, pUtils.Contains(actualRole.Permissions, testData.permission.Id), "row: %+v", testData.truthTableRow)
|
||||
require.Equal(t, testData.shouldHavePermission, slices.Contains(actualRole.Permissions, testData.permission.Id), "row: %+v", testData.truthTableRow)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -419,7 +420,7 @@ func TestSessionsLimit(t *testing.T) {
|
||||
require.Equal(t, maxSessionsLimit, len(gotSessions), "should have maxSessionsLimit number of sessions")
|
||||
|
||||
// Ensure we are retrieving the same sessions.
|
||||
reverse(gotSessions)
|
||||
slices.Reverse(gotSessions)
|
||||
for i, sess := range gotSessions {
|
||||
require.Equal(t, sessions[i].Id, sess.Id)
|
||||
}
|
||||
@@ -440,15 +441,8 @@ func TestSessionsLimit(t *testing.T) {
|
||||
require.Equal(t, maxSessionsLimit, len(gotSessions), "should have maxSessionsLimit number of sessions")
|
||||
|
||||
// Ensure the the oldest sessions were removed first.
|
||||
reverse(gotSessions)
|
||||
slices.Reverse(gotSessions)
|
||||
for i, sess := range gotSessions {
|
||||
require.Equal(t, sessions[i].Id, sess.Id)
|
||||
}
|
||||
}
|
||||
|
||||
// reverse can be replaced by the slices version when we move to 1.21+
|
||||
func reverse[S ~[]E, E any](s S) {
|
||||
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,11 +159,3 @@ func getBrowserName(ua *uasurfer.UserAgent, userAgentString string) string {
|
||||
|
||||
return browserNames[uasurfer.BrowserUnknown]
|
||||
}
|
||||
|
||||
// min should be replaced by to go 1.21 built-in generic function, see MM-57356.
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ package app
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
pUtils "github.com/mattermost/mattermost/server/public/utils"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/app/platform"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -33,7 +33,7 @@ func (h *addMentionsBroadcastHook) Process(msg *platform.HookedWebSocketEvent, w
|
||||
return errors.Wrap(err, "Invalid mentions value passed to addMentionsBroadcastHook")
|
||||
}
|
||||
|
||||
if len(mentions) > 0 && pUtils.Contains[string](mentions, webConn.UserId) {
|
||||
if len(mentions) > 0 && slices.Contains(mentions, webConn.UserId) {
|
||||
// Note that the client expects this field to be stringified
|
||||
msg.Add("mentions", model.ArrayToJSON([]string{webConn.UserId}))
|
||||
}
|
||||
@@ -55,7 +55,7 @@ func (h *addFollowersBroadcastHook) Process(msg *platform.HookedWebSocketEvent,
|
||||
return errors.Wrap(err, "Invalid followers value passed to addFollowersBroadcastHook")
|
||||
}
|
||||
|
||||
if len(followers) > 0 && pUtils.Contains[string](followers, webConn.UserId) {
|
||||
if len(followers) > 0 && slices.Contains(followers, webConn.UserId) {
|
||||
// Note that the client expects this field to be stringified
|
||||
msg.Add("followers", model.ArrayToJSON([]string{webConn.UserId}))
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user