mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
[MM15037] Stop sending notifications to user whose Oauth bot posted (#15953)
Co-authored-by: Cam Graff <cameron@woodridgesoftware.com>
This commit is contained in:
@@ -126,7 +126,14 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
|
||||
if post.RootId != "" && parentPostList != nil {
|
||||
for _, threadPost := range parentPostList.Posts {
|
||||
profile := profileMap[threadPost.UserId]
|
||||
if profile != nil && (profile.NotifyProps[model.COMMENTS_NOTIFY_PROP] == model.COMMENTS_NOTIFY_ANY || (profile.NotifyProps[model.COMMENTS_NOTIFY_PROP] == model.COMMENTS_NOTIFY_ROOT && threadPost.Id == parentPostList.Order[0])) {
|
||||
if profile == nil {
|
||||
continue
|
||||
}
|
||||
// If this is the root post and it was posted by an OAuth bot, don't notify the user
|
||||
if threadPost.Id == parentPostList.Order[0] && threadPost.IsFromOAuthBot() {
|
||||
continue
|
||||
}
|
||||
if profile.NotifyProps[model.COMMENTS_NOTIFY_PROP] == model.COMMENTS_NOTIFY_ANY || (profile.NotifyProps[model.COMMENTS_NOTIFY_PROP] == model.COMMENTS_NOTIFY_ROOT && threadPost.Id == parentPostList.Order[0]) {
|
||||
mentionType := ThreadMention
|
||||
if threadPost.Id == parentPostList.Order[0] {
|
||||
mentionType = CommentMention
|
||||
|
||||
@@ -68,6 +68,53 @@ func TestSendNotifications(t *testing.T) {
|
||||
mentions, err = th.App.SendNotifications(post1, th.BasicTeam, th.BasicChannel, th.BasicUser, nil, true)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, mentions)
|
||||
|
||||
t.Run("replies to post created by OAuth bot should not notify user", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
testUserNotNotified := func(t *testing.T, user *model.User) {
|
||||
rootPost := &model.Post{
|
||||
UserId: user.Id,
|
||||
ChannelId: th.BasicChannel.Id,
|
||||
Message: "a message",
|
||||
Props: model.StringInterface{"from_webhook": "true", "override_username": "a bot"},
|
||||
}
|
||||
|
||||
rootPost, appErr := th.App.CreatePostMissingChannel(rootPost, false)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
childPost := &model.Post{
|
||||
UserId: th.BasicUser2.Id,
|
||||
ChannelId: th.BasicChannel.Id,
|
||||
RootId: rootPost.Id,
|
||||
Message: "a reply",
|
||||
}
|
||||
childPost, appErr = th.App.CreatePostMissingChannel(childPost, false)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
postList := model.PostList{
|
||||
Order: []string{rootPost.Id, childPost.Id},
|
||||
Posts: map[string]*model.Post{rootPost.Id: rootPost, childPost.Id: childPost},
|
||||
}
|
||||
mentions, err = th.App.SendNotifications(childPost, th.BasicTeam, th.BasicChannel, th.BasicUser2, &postList, true)
|
||||
require.Nil(t, err)
|
||||
require.False(t, utils.StringInSlice(user.Id, mentions))
|
||||
}
|
||||
|
||||
th.BasicUser.NotifyProps[model.COMMENTS_NOTIFY_PROP] = model.COMMENTS_NOTIFY_ANY
|
||||
th.BasicUser, err = th.App.UpdateUser(th.BasicUser, false)
|
||||
require.Nil(t, err)
|
||||
t.Run("user wants notifications on all comments", func(t *testing.T) {
|
||||
testUserNotNotified(t, th.BasicUser)
|
||||
})
|
||||
|
||||
th.BasicUser.NotifyProps[model.COMMENTS_NOTIFY_PROP] = model.COMMENTS_NOTIFY_ROOT
|
||||
th.BasicUser, err = th.App.UpdateUser(th.BasicUser, false)
|
||||
require.Nil(t, err)
|
||||
t.Run("user wants notifications on root comment", func(t *testing.T) {
|
||||
testUserNotNotified(t, th.BasicUser)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSendNotificationsWithManyUsers(t *testing.T) {
|
||||
|
||||
@@ -695,3 +695,8 @@ func RewriteImageURLs(message string, f func(string) string) string {
|
||||
|
||||
return string(result)
|
||||
}
|
||||
|
||||
func (o *Post) IsFromOAuthBot() bool {
|
||||
props := o.GetProps()
|
||||
return props["from_webhook"] == "true" && props["override_username"] != ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user