From 487d59b790cb2a352d0c6aa20be792c51a889b23 Mon Sep 17 00:00:00 2001 From: Doug Lauder Date: Fri, 3 Dec 2021 13:33:59 -0500 Subject: [PATCH] don't notify @mnetioned user who are not members of channel (#1912) --- mattermost-plugin/server/notifications.go | 9 +++++++++ .../notify/plugindelivery/plugin_delivery.go | 17 +++++++++++++++++ .../services/notify/plugindelivery/user_test.go | 8 ++++++++ 3 files changed, 34 insertions(+) diff --git a/mattermost-plugin/server/notifications.go b/mattermost-plugin/server/notifications.go index 6134982f4..18ef2a6d6 100644 --- a/mattermost-plugin/server/notifications.go +++ b/mattermost-plugin/server/notifications.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "github.com/mattermost/focalboard/server/services/notify" @@ -67,3 +68,11 @@ func (da *pluginAPIAdapter) GetTeamMember(teamID string, userID string) (*model. func (da *pluginAPIAdapter) GetChannelByID(channelID string) (*model.Channel, error) { return da.client.Channel.Get(channelID) } + +func (da *pluginAPIAdapter) GetChannelMember(channelID string, userID string) (*model.ChannelMember, error) { + return da.client.Channel.GetMember(channelID, userID) +} + +func (da *pluginAPIAdapter) IsErrNotFound(err error) bool { + return errors.Is(err, pluginapi.ErrNotFound) +} diff --git a/server/services/notify/plugindelivery/plugin_delivery.go b/server/services/notify/plugindelivery/plugin_delivery.go index 75e4fb3bd..817c5826a 100644 --- a/server/services/notify/plugindelivery/plugin_delivery.go +++ b/server/services/notify/plugindelivery/plugin_delivery.go @@ -30,6 +30,13 @@ type PluginAPI interface { // GetChannelByID gets a Channel by its ID. GetChannelByID(channelID string) (*model.Channel, error) + + // GetChannelMember gets a channel member by userID. + GetChannelMember(channelID string, userID string) (*model.ChannelMember, error) + + // IsErrNotFound returns true if `err` or one of its wrapped children are the `ErrNotFound` + // as defined in the plugin API. + IsErrNotFound(err error) bool } // PluginDelivery provides ability to send notifications to direct message channels via Mattermost plugin API. @@ -64,6 +71,16 @@ func (pd *PluginDelivery) Deliver(mentionUsername string, extract string, evt no } } + // check that user is a member of the channel + _, err = pd.api.GetChannelMember(evt.Workspace, member.UserId) + if err != nil { + if pd.api.IsErrNotFound(err) { + // mentioned user is not a member of the channel; fail silently. + return nil + } + return fmt.Errorf("cannot fetch channel member for user %s: %w", member.UserId, err) + } + author, err := pd.api.GetUserByID(evt.UserID) if err != nil { return fmt.Errorf("cannot find user: %w", err) diff --git a/server/services/notify/plugindelivery/user_test.go b/server/services/notify/plugindelivery/user_test.go index 1577cb857..6be9f5194 100644 --- a/server/services/notify/plugindelivery/user_test.go +++ b/server/services/notify/plugindelivery/user_test.go @@ -140,6 +140,14 @@ func (m pluginAPIMock) GetChannelByID(channelID string) (*mm_model.Channel, erro return nil, ErrNotFound{} } +func (m pluginAPIMock) GetChannelMember(channelID string, userID string) (*mm_model.ChannelMember, error) { + return nil, ErrNotFound{} +} + +func (m pluginAPIMock) IsErrNotFound(err error) bool { + return false +} + type ErrNotFound struct{} func (e ErrNotFound) Error() string {