[MM15037] Stop sending notifications to user whose Oauth bot posted (#15953)

Co-authored-by: Cam Graff <cameron@woodridgesoftware.com>
This commit is contained in:
camgraff
2021-01-26 09:33:44 -06:00
committed by GitHub
parent 4b4fc58e7f
commit 02523cb186
3 changed files with 60 additions and 1 deletions

View File

@@ -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

View File

@@ -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) {

View File

@@ -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"] != ""
}