don't notify @mnetioned user who are not members of channel (#1912)

This commit is contained in:
Doug Lauder 2021-12-03 13:33:59 -05:00 committed by GitHub
parent 8be71b1498
commit 487d59b790
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 0 deletions

View file

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

View file

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

View file

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