From a35adb3ae1f7990a30ca93277e893b0cc506dbf3 Mon Sep 17 00:00:00 2001 From: jfrerich Date: Wed, 17 Jul 2019 20:12:29 -0500 Subject: [PATCH] Filter bots out of notifications for members not in channel (#11655) --- app/notification.go | 1 + model/user.go | 11 +++++++++++ model/user_test.go | 9 ++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/notification.go b/app/notification.go index 27d211d580..f9c9dff1c1 100644 --- a/app/notification.go +++ b/app/notification.go @@ -158,6 +158,7 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod } else { outOfChannelMentions = channelMentions } + outOfChannelMentions = outOfChannelMentions.FilterWithoutBots() if channel.Type != model.CHANNEL_GROUP { a.Srv.Go(func() { diff --git a/model/user.go b/model/user.go index 48052f5902..36764310ad 100644 --- a/model/user.go +++ b/model/user.go @@ -161,6 +161,17 @@ func (u UserSlice) IDs() []string { return ids } +func (u UserSlice) FilterWithoutBots() UserSlice { + var matches []*User + + for _, user := range u { + if !user.IsBot { + matches = append(matches, user) + } + } + return UserSlice(matches) +} + func (u UserSlice) FilterByActive(active bool) UserSlice { var matches []*User diff --git a/model/user_test.go b/model/user_test.go index fb3449dd83..0211283dcb 100644 --- a/model/user_test.go +++ b/model/user_test.go @@ -391,9 +391,9 @@ func TestIsValidLocale(t *testing.T) { func TestUserSlice(t *testing.T) { t.Run("FilterByActive", func(t *testing.T) { - user0 := &User{Id: "user0", DeleteAt: 0} - user1 := &User{Id: "user1", DeleteAt: 0} - user2 := &User{Id: "user2", DeleteAt: 1} + user0 := &User{Id: "user0", DeleteAt: 0, IsBot: true} + user1 := &User{Id: "user1", DeleteAt: 0, IsBot: true} + user2 := &User{Id: "user2", DeleteAt: 1, IsBot: false} slice := UserSlice([]*User{user0, user1, user2}) @@ -408,5 +408,8 @@ func TestUserSlice(t *testing.T) { for _, user := range inactiveUsers { assert.True(t, user.DeleteAt != 0) } + + nonBotUsers := slice.FilterWithoutBots() + assert.Equal(t, 1, len(nonBotUsers)) }) }