1
0
mirror of https://github.com/mattermost/focalboard.git synced 2025-01-23 18:34:02 +02:00

GH-2469 Add board-bot to team (#3026)

* add bot to team before any mention or subscription notification
This commit is contained in:
Doug Lauder 2022-05-04 16:10:26 -04:00 committed by GitHub
parent fb46097e72
commit 1ff28087f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 7 deletions

View File

@ -121,3 +121,7 @@ func (da *pluginAPIAdapter) GetChannelByID(channelID string) (*model.Channel, er
func (da *pluginAPIAdapter) GetChannelMember(channelID string, userID string) (*model.ChannelMember, error) {
return da.client.Channel.GetMember(channelID, userID)
}
func (da *pluginAPIAdapter) CreateMember(teamID string, userID string) (*model.TeamMember, error) {
return da.client.Team.CreateMember(teamID, userID)
}

View File

@ -12,6 +12,6 @@ import (
// SubscriptionDelivery provides an interface for delivering subscription notifications to other systems, such as
// channels server via plugin API.
type SubscriptionDelivery interface {
SubscriptionDeliverSlackAttachments(subscriberID string, subscriberType model.SubscriberType,
SubscriptionDeliverSlackAttachments(teamID string, subscriberID string, subscriberType model.SubscriberType,
attachments []*mm_model.SlackAttachment) error
}

View File

@ -243,7 +243,7 @@ func (n *notifier) notifySubscribers(hint *model.NotificationHint) error {
mlog.String("subscriber_type", string(sub.SubscriberType)),
)
if err = n.delivery.SubscriptionDeliverSlackAttachments(sub.SubscriberID, sub.SubscriberType, attachments); err != nil {
if err = n.delivery.SubscriptionDeliverSlackAttachments(board.TeamID, sub.SubscriberID, sub.SubscriberType, attachments); err != nil {
merr.Append(fmt.Errorf("cannot deliver notification to subscriber %s [%s]: %w",
sub.SubscriberID, sub.SubscriberType, err))
}

View File

@ -19,7 +19,7 @@ func (pd *PluginDelivery) MentionDeliver(mentionedUser *mm_model.User, extract s
return "", fmt.Errorf("cannot find user: %w", err)
}
channel, err := pd.api.GetDirectChannel(mentionedUser.Id, pd.botID)
channel, err := pd.getDirectChannel(evt.TeamID, mentionedUser.Id, pd.botID)
if err != nil {
return "", fmt.Errorf("cannot get direct channel: %w", err)
}

View File

@ -29,6 +29,10 @@ type PluginAPI interface {
// GetChannelMember gets a channel member by userID.
GetChannelMember(channelID string, userID string) (*mm_model.ChannelMember, error)
// CreateMember adds a user to the specified team. Safe to call if the user is
// already a member of the team.
CreateMember(teamID string, userID string) (*mm_model.TeamMember, error)
}
// PluginDelivery provides ability to send notifications to direct message channels via Mattermost plugin API.
@ -38,6 +42,7 @@ type PluginDelivery struct {
api PluginAPI
}
// New creates a PluginDelivery instance.
func New(botID string, serverRoot string, api PluginAPI) *PluginDelivery {
return &PluginDelivery{
botID: botID,

View File

@ -17,7 +17,7 @@ var (
)
// SubscriptionDeliverSlashAttachments notifies a user that changes were made to a block they are subscribed to.
func (pd *PluginDelivery) SubscriptionDeliverSlackAttachments(subscriberID string, subscriptionType model.SubscriberType,
func (pd *PluginDelivery) SubscriptionDeliverSlackAttachments(teamID string, subscriberID string, subscriptionType model.SubscriberType,
attachments []*mm_model.SlackAttachment) error {
// check subscriber is member of channel
_, err := pd.api.GetUserByID(subscriberID)
@ -29,7 +29,7 @@ func (pd *PluginDelivery) SubscriptionDeliverSlackAttachments(subscriberID strin
return fmt.Errorf("cannot fetch channel member for user %s: %w", subscriberID, err)
}
channelID, err := pd.getDirectChannelID(subscriberID, subscriptionType, pd.botID)
channelID, err := pd.getDirectChannelID(teamID, subscriberID, subscriptionType, pd.botID)
if err != nil {
return err
}
@ -44,14 +44,14 @@ func (pd *PluginDelivery) SubscriptionDeliverSlackAttachments(subscriberID strin
return pd.api.CreatePost(post)
}
func (pd *PluginDelivery) getDirectChannelID(subscriberID string, subscriberType model.SubscriberType, botID string) (string, error) {
func (pd *PluginDelivery) getDirectChannelID(teamID string, subscriberID string, subscriberType model.SubscriberType, botID string) (string, error) {
switch subscriberType {
case model.SubTypeUser:
user, err := pd.api.GetUserByID(subscriberID)
if err != nil {
return "", fmt.Errorf("cannot find user: %w", err)
}
channel, err := pd.api.GetDirectChannel(user.Id, botID)
channel, err := pd.getDirectChannel(teamID, user.Id, botID)
if err != nil {
return "", fmt.Errorf("cannot get direct channel: %w", err)
}
@ -62,3 +62,12 @@ func (pd *PluginDelivery) getDirectChannelID(subscriberID string, subscriberType
return "", ErrUnsupportedSubscriberType
}
}
func (pd *PluginDelivery) getDirectChannel(teamID string, userID string, botID string) (*mm_model.Channel, error) {
// first ensure the bot is a member of the team.
_, err := pd.api.CreateMember(teamID, botID)
if err != nil {
return nil, fmt.Errorf("cannot add bot to team %s: %w", teamID, err)
}
return pd.api.GetDirectChannel(userID, botID)
}

View File

@ -138,3 +138,11 @@ func (m pluginAPIMock) GetChannelByID(channelID string) (*mm_model.Channel, erro
func (m pluginAPIMock) GetChannelMember(channelID string, userID string) (*mm_model.ChannelMember, error) {
return nil, model.NewErrNotFound(userID)
}
func (m pluginAPIMock) CreateMember(teamID string, userID string) (*mm_model.TeamMember, error) {
member := &mm_model.TeamMember{
UserId: userID,
TeamId: teamID,
}
return member, nil
}