2021-12-10 16:46:37 +01:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
package plugindelivery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
|
|
|
|
|
|
|
mm_model "github.com/mattermost/mattermost-server/v6/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrUnsupportedSubscriberType = errors.New("invalid subscriber type")
|
|
|
|
)
|
|
|
|
|
|
|
|
// SubscriptionDeliverSlashAttachments notifies a user that changes were made to a block they are subscribed to.
|
2022-05-04 22:10:26 +02:00
|
|
|
func (pd *PluginDelivery) SubscriptionDeliverSlackAttachments(teamID string, subscriberID string, subscriptionType model.SubscriberType,
|
2021-12-10 16:46:37 +01:00
|
|
|
attachments []*mm_model.SlackAttachment) error {
|
|
|
|
// check subscriber is member of channel
|
2022-03-22 15:24:34 +01:00
|
|
|
_, err := pd.api.GetUserByID(subscriberID)
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
2022-04-20 17:02:12 +02:00
|
|
|
if model.IsErrNotFound(err) {
|
2021-12-10 16:46:37 +01:00
|
|
|
// subscriber is not a member of the channel; fail silently.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("cannot fetch channel member for user %s: %w", subscriberID, err)
|
|
|
|
}
|
|
|
|
|
2022-05-04 22:10:26 +02:00
|
|
|
channelID, err := pd.getDirectChannelID(teamID, subscriberID, subscriptionType, pd.botID)
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
post := &mm_model.Post{
|
|
|
|
UserId: pd.botID,
|
|
|
|
ChannelId: channelID,
|
|
|
|
}
|
|
|
|
|
|
|
|
mm_model.ParseSlackAttachment(post, attachments)
|
|
|
|
|
2022-07-18 19:21:57 +02:00
|
|
|
_, err = pd.api.CreatePost(post)
|
|
|
|
return err
|
2021-12-10 16:46:37 +01:00
|
|
|
}
|
|
|
|
|
2022-05-04 22:10:26 +02:00
|
|
|
func (pd *PluginDelivery) getDirectChannelID(teamID string, subscriberID string, subscriberType model.SubscriberType, botID string) (string, error) {
|
2021-12-10 16:46:37 +01:00
|
|
|
switch subscriberType {
|
|
|
|
case model.SubTypeUser:
|
|
|
|
user, err := pd.api.GetUserByID(subscriberID)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot find user: %w", err)
|
|
|
|
}
|
2022-05-04 22:10:26 +02:00
|
|
|
channel, err := pd.getDirectChannel(teamID, user.Id, botID)
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot get direct channel: %w", err)
|
|
|
|
}
|
|
|
|
return channel.Id, nil
|
|
|
|
case model.SubTypeChannel:
|
|
|
|
return subscriberID, nil
|
|
|
|
default:
|
|
|
|
return "", ErrUnsupportedSubscriberType
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 22:10:26 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|