diff --git a/app/notification.go b/app/notification.go index 6935c38a5d..2fab4f44fd 100644 --- a/app/notification.go +++ b/app/notification.go @@ -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 diff --git a/app/notification_test.go b/app/notification_test.go index e50d1a174c..f3451255d8 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -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) { diff --git a/model/post.go b/model/post.go index f6da3a9cf3..1823b7e2b5 100644 --- a/model/post.go +++ b/model/post.go @@ -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"] != "" +}