2021-09-13 21:36:36 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-03 19:33:59 +01:00
|
|
|
"errors"
|
2021-09-13 21:36:36 +02:00
|
|
|
"fmt"
|
|
|
|
|
2021-12-10 16:46:37 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/config"
|
2021-09-13 21:36:36 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/notify/notifymentions"
|
2021-12-10 16:46:37 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/notify/notifysubscriptions"
|
2021-09-13 21:36:36 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/notify/plugindelivery"
|
2022-04-07 17:42:32 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/permissions"
|
2021-12-10 16:46:37 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
|
|
|
"github.com/mattermost/focalboard/server/ws"
|
2021-09-13 21:36:36 +02:00
|
|
|
|
|
|
|
pluginapi "github.com/mattermost/mattermost-plugin-api"
|
2022-03-26 00:05:56 +01:00
|
|
|
apierrors "github.com/mattermost/mattermost-plugin-api/errors"
|
2021-09-13 21:36:36 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/model"
|
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
botUsername = "boards"
|
|
|
|
botDisplayname = "Boards"
|
|
|
|
botDescription = "Created by Boards plugin."
|
|
|
|
)
|
|
|
|
|
2021-12-10 16:46:37 +01:00
|
|
|
type notifyBackendParams struct {
|
2022-04-07 17:42:32 +02:00
|
|
|
cfg *config.Configuration
|
|
|
|
client *pluginapi.Client
|
|
|
|
permissions permissions.PermissionsService
|
2022-04-14 00:09:55 +02:00
|
|
|
store store.Store
|
|
|
|
wsAdapter ws.Adapter
|
2022-04-07 17:42:32 +02:00
|
|
|
serverRoot string
|
|
|
|
logger *mlog.Logger
|
2021-12-10 16:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func createMentionsNotifyBackend(params notifyBackendParams) (*notifymentions.Backend, error) {
|
2022-04-14 00:09:55 +02:00
|
|
|
delivery, err := createDelivery(params.client, params.serverRoot)
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:09:55 +02:00
|
|
|
backendParams := notifymentions.BackendParams{
|
|
|
|
Store: params.store,
|
|
|
|
Permissions: params.permissions,
|
|
|
|
Delivery: delivery,
|
|
|
|
WSAdapter: params.wsAdapter,
|
|
|
|
Logger: params.logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
backend := notifymentions.New(backendParams)
|
2021-12-10 16:46:37 +01:00
|
|
|
|
|
|
|
return backend, nil
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:09:55 +02:00
|
|
|
func createSubscriptionsNotifyBackend(params notifyBackendParams) (*notifysubscriptions.Backend, error) {
|
|
|
|
delivery, err := createDelivery(params.client, params.serverRoot)
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backendParams := notifysubscriptions.BackendParams{
|
|
|
|
ServerRoot: params.serverRoot,
|
2022-04-14 00:09:55 +02:00
|
|
|
Store: params.store,
|
2022-04-07 17:42:32 +02:00
|
|
|
Permissions: params.permissions,
|
2021-12-10 16:46:37 +01:00
|
|
|
Delivery: delivery,
|
2022-04-14 00:09:55 +02:00
|
|
|
WSAdapter: params.wsAdapter,
|
2021-12-10 16:46:37 +01:00
|
|
|
Logger: params.logger,
|
|
|
|
NotifyFreqCardSeconds: params.cfg.NotifyFreqCardSeconds,
|
|
|
|
NotifyFreqBoardSeconds: params.cfg.NotifyFreqBoardSeconds,
|
|
|
|
}
|
|
|
|
backend := notifysubscriptions.New(backendParams)
|
|
|
|
|
|
|
|
return backend, nil
|
|
|
|
}
|
|
|
|
|
2022-04-14 00:09:55 +02:00
|
|
|
func createDelivery(client *pluginapi.Client, serverRoot string) (*plugindelivery.PluginDelivery, error) {
|
2021-09-13 21:36:36 +02:00
|
|
|
bot := &model.Bot{
|
|
|
|
Username: botUsername,
|
|
|
|
DisplayName: botDisplayname,
|
|
|
|
Description: botDescription,
|
|
|
|
}
|
|
|
|
botID, err := client.Bot.EnsureBot(bot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to ensure %s bot: %w", botDisplayname, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginAPI := &pluginAPIAdapter{client: client}
|
|
|
|
|
2022-04-14 00:09:55 +02:00
|
|
|
return plugindelivery.New(botID, serverRoot, pluginAPI), nil
|
2021-09-13 21:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type pluginAPIAdapter struct {
|
|
|
|
client *pluginapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (da *pluginAPIAdapter) GetDirectChannel(userID1, userID2 string) (*model.Channel, error) {
|
|
|
|
return da.client.Channel.GetDirect(userID1, userID2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (da *pluginAPIAdapter) CreatePost(post *model.Post) error {
|
|
|
|
return da.client.Post.CreatePost(post)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (da *pluginAPIAdapter) GetUserByID(userID string) (*model.User, error) {
|
|
|
|
return da.client.User.Get(userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (da *pluginAPIAdapter) GetUserByUsername(name string) (*model.User, error) {
|
|
|
|
return da.client.User.GetByUsername(name)
|
|
|
|
}
|
2021-09-20 23:32:47 +02:00
|
|
|
|
|
|
|
func (da *pluginAPIAdapter) GetTeamMember(teamID string, userID string) (*model.TeamMember, error) {
|
|
|
|
return da.client.Team.GetMember(teamID, userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (da *pluginAPIAdapter) GetChannelByID(channelID string) (*model.Channel, error) {
|
|
|
|
return da.client.Channel.Get(channelID)
|
|
|
|
}
|
2021-12-03 19:33:59 +01:00
|
|
|
|
|
|
|
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 {
|
2022-03-26 00:05:56 +01:00
|
|
|
return errors.Is(err, apierrors.ErrNotFound)
|
2021-12-03 19:33:59 +01:00
|
|
|
}
|