MM-52792: Update createPost, updatePost, & patchPost (#24195)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
f3f9a84456
Коммит
b7f1a7f262
@@ -63,15 +63,18 @@ const (
|
||||
|
||||
PropsAddChannelMember = "add_channel_member"
|
||||
|
||||
PostPropsAddedUserId = "addedUserId"
|
||||
PostPropsDeleteBy = "deleteBy"
|
||||
PostPropsOverrideIconURL = "override_icon_url"
|
||||
PostPropsOverrideIconEmoji = "override_icon_emoji"
|
||||
|
||||
PostPropsAddedUserId = "addedUserId"
|
||||
PostPropsDeleteBy = "deleteBy"
|
||||
PostPropsOverrideIconURL = "override_icon_url"
|
||||
PostPropsOverrideIconEmoji = "override_icon_emoji"
|
||||
PostPropsOverrideUsername = "override_username"
|
||||
PostPropsFromWebhook = "from_webhook"
|
||||
PostPropsFromBot = "from_bot"
|
||||
PostPropsFromOAuthApp = "from_oauth_app"
|
||||
PostPropsWebhookDisplayName = "webhook_display_name"
|
||||
PostPropsMentionHighlightDisabled = "mentionHighlightDisabled"
|
||||
PostPropsGroupHighlightDisabled = "disable_group_highlight"
|
||||
|
||||
PostPropsPreviewedPost = "previewed_post"
|
||||
PostPropsPreviewedPost = "previewed_post"
|
||||
|
||||
PostPriorityUrgent = "urgent"
|
||||
PostPropsRequestedAck = "requested_ack"
|
||||
@@ -470,6 +473,39 @@ func (o *Post) SanitizeProps() {
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Post) ContainsIntegrationsReservedProps() []string {
|
||||
return containsIntegrationsReservedProps(o.GetProps())
|
||||
}
|
||||
|
||||
func (o *PostPatch) ContainsIntegrationsReservedProps() []string {
|
||||
if o == nil || o.Props == nil {
|
||||
return nil
|
||||
}
|
||||
return containsIntegrationsReservedProps(*o.Props)
|
||||
}
|
||||
|
||||
func containsIntegrationsReservedProps(props StringInterface) []string {
|
||||
foundProps := []string{}
|
||||
|
||||
if props != nil {
|
||||
reservedProps := []string{
|
||||
PostPropsFromWebhook,
|
||||
PostPropsOverrideUsername,
|
||||
PostPropsWebhookDisplayName,
|
||||
PostPropsOverrideIconURL,
|
||||
PostPropsOverrideIconEmoji,
|
||||
}
|
||||
|
||||
for _, key := range reservedProps {
|
||||
if _, ok := props[key]; ok {
|
||||
foundProps = append(foundProps, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return foundProps
|
||||
}
|
||||
|
||||
func (o *Post) PreSave() {
|
||||
if o.Id == "" {
|
||||
o.Id = NewId()
|
||||
|
||||
@@ -142,6 +142,41 @@ func TestPostSanitizeProps(t *testing.T) {
|
||||
require.NotNil(t, post3.GetProp("attachments"))
|
||||
}
|
||||
|
||||
func TestPost_ContainsIntegrationsReservedProps(t *testing.T) {
|
||||
post1 := &Post{
|
||||
Message: "test",
|
||||
}
|
||||
keys1 := post1.ContainsIntegrationsReservedProps()
|
||||
require.Len(t, keys1, 0)
|
||||
|
||||
post2 := &Post{
|
||||
Message: "test",
|
||||
Props: StringInterface{
|
||||
"from_webhook": "true",
|
||||
"webhook_display_name": "overridden_display_name",
|
||||
"override_username": "overridden_username",
|
||||
"override_icon_url": "a-custom-url",
|
||||
"override_icon_emoji": ":custom_emoji_name:",
|
||||
},
|
||||
}
|
||||
keys2 := post2.ContainsIntegrationsReservedProps()
|
||||
require.Len(t, keys2, 5)
|
||||
}
|
||||
|
||||
func TestPostPatch_ContainsIntegrationsReservedProps(t *testing.T) {
|
||||
postPatch1 := &PostPatch{
|
||||
Props: &StringInterface{
|
||||
"from_webhook": "true",
|
||||
},
|
||||
}
|
||||
keys1 := postPatch1.ContainsIntegrationsReservedProps()
|
||||
require.Len(t, keys1, 1)
|
||||
|
||||
postPatch2 := &PostPatch{}
|
||||
keys2 := postPatch2.ContainsIntegrationsReservedProps()
|
||||
require.Len(t, keys2, 0)
|
||||
}
|
||||
|
||||
func TestPost_AttachmentsEqual(t *testing.T) {
|
||||
post1 := &Post{}
|
||||
post2 := &Post{}
|
||||
|
||||
@@ -214,6 +214,34 @@ func (s *Session) IsOAuthUser() bool {
|
||||
return isOAuthUser
|
||||
}
|
||||
|
||||
func (s *Session) IsBotUser() bool {
|
||||
val, ok := s.Props[SessionPropIsBot]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if val == SessionPropIsBotValue {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Session) IsUserAccessToken() bool {
|
||||
val, ok := s.Props[SessionPropType]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if val == SessionTypeUserAccessToken {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Returns true when session is authenticated as a bot, by personal access token, or is an OAuth app.
|
||||
// Does not indicate other forms of integrations e.g. webhooks, slash commands, etc.
|
||||
func (s *Session) IsIntegration() bool {
|
||||
return s.IsBotUser() || s.IsUserAccessToken() || s.IsOAuth
|
||||
}
|
||||
|
||||
func (s *Session) IsSSOLogin() bool {
|
||||
return s.IsOAuthUser() || s.IsSaml()
|
||||
}
|
||||
|
||||
@@ -133,3 +133,23 @@ func TestSessionIsOAuthUser(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIntegration(t *testing.T) {
|
||||
testCases := []struct {
|
||||
Description string
|
||||
Session Session
|
||||
IsIntegration bool
|
||||
}{
|
||||
{"False on empty props", Session{}, false},
|
||||
{"True when is OAuth App", Session{IsOAuth: true}, true},
|
||||
{"True when session is bot", Session{Props: StringMap{SessionPropIsBot: SessionPropIsBotValue}}, true},
|
||||
{"True when session is user access token", Session{Props: StringMap{SessionPropType: SessionTypeUserAccessToken}}, true},
|
||||
{"Not affected by Props[UserAuthServiceIsOAuth]", Session{Props: StringMap{UserAuthServiceIsOAuth: strconv.FormatBool(true)}}, false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.Description, func(t *testing.T) {
|
||||
require.Equal(t, tc.IsIntegration, tc.Session.IsIntegration())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user