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 (
|
2022-04-07 17:42:32 +02:00
|
|
|
"errors"
|
2021-12-10 16:46:37 +01:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/services/notify"
|
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/model"
|
|
|
|
)
|
|
|
|
|
2022-04-07 17:42:32 +02:00
|
|
|
var (
|
|
|
|
ErrMentionPermission = errors.New("mention not permitted")
|
|
|
|
)
|
|
|
|
|
2021-12-10 16:46:37 +01:00
|
|
|
// MentionDeliver notifies a user they have been mentioned in a block.
|
|
|
|
func (pd *PluginDelivery) MentionDeliver(mentionUsername string, extract string, evt notify.BlockChangeEvent) (string, error) {
|
2022-04-07 17:42:32 +02:00
|
|
|
user, err := userByUsername(pd.api, mentionUsername)
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
if isErrNotFound(err) {
|
|
|
|
// not really an error; could just be someone typed "@sometext"
|
|
|
|
return "", nil
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("cannot lookup mentioned user: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 17:42:32 +02:00
|
|
|
// make sure mentioned user has permissions to team.
|
|
|
|
if !pd.permissions.HasPermissionToTeam(user.Id, evt.TeamID, model.PermissionViewTeam) {
|
|
|
|
return "", fmt.Errorf("mentioned user %s not member of team %s: %w", user.Id, evt.TeamID, ErrMentionPermission)
|
|
|
|
}
|
|
|
|
|
2021-12-10 16:46:37 +01:00
|
|
|
author, err := pd.api.GetUserByID(evt.ModifiedByID)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot find user: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-04-07 17:42:32 +02:00
|
|
|
channel, err := pd.api.GetDirectChannel(user.Id, pd.botID)
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot get direct channel: %w", err)
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
link := utils.MakeCardLink(pd.serverRoot, evt.Board.TeamID, evt.Board.ID, evt.Card.ID)
|
2021-12-10 16:46:37 +01:00
|
|
|
|
|
|
|
post := &model.Post{
|
|
|
|
UserId: pd.botID,
|
|
|
|
ChannelId: channel.Id,
|
|
|
|
Message: formatMessage(author.Username, extract, evt.Card.Title, link, evt.BlockChanged),
|
|
|
|
}
|
2022-04-07 17:42:32 +02:00
|
|
|
return user.Id, pd.api.CreatePost(post)
|
2021-12-10 16:46:37 +01:00
|
|
|
}
|