2021-09-13 21:36:36 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-16 17:53:41 +02:00
|
|
|
"time"
|
2021-09-13 21:36:36 +02:00
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
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"
|
2021-09-13 21:36:36 +02:00
|
|
|
|
|
|
|
pluginapi "github.com/mattermost/mattermost-plugin-api"
|
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
mm_model "github.com/mattermost/mattermost-server/v6/model"
|
2021-09-13 21:36:36 +02:00
|
|
|
|
|
|
|
"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-05-16 17:53:41 +02:00
|
|
|
appAPI *appAPI
|
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{
|
2022-05-16 17:53:41 +02:00
|
|
|
AppAPI: params.appAPI,
|
2022-04-14 00:09:55 +02:00
|
|
|
Permissions: params.permissions,
|
|
|
|
Delivery: delivery,
|
|
|
|
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-05-16 17:53:41 +02:00
|
|
|
AppAPI: params.appAPI,
|
2022-04-07 17:42:32 +02:00
|
|
|
Permissions: params.permissions,
|
2021-12-10 16:46:37 +01:00
|
|
|
Delivery: delivery,
|
|
|
|
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) {
|
2022-05-16 17:53:41 +02:00
|
|
|
bot := &mm_model.Bot{
|
2021-09-13 21:36:36 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
// pluginAPIAdapter provides a simple wrapper around the component based Plugin API
|
|
|
|
// which flattens the API to satisfy an interface.
|
2021-09-13 21:36:36 +02:00
|
|
|
type pluginAPIAdapter struct {
|
|
|
|
client *pluginapi.Client
|
|
|
|
}
|
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
func (da *pluginAPIAdapter) GetDirectChannel(userID1, userID2 string) (*mm_model.Channel, error) {
|
2021-09-13 21:36:36 +02:00
|
|
|
return da.client.Channel.GetDirect(userID1, userID2)
|
|
|
|
}
|
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
func (da *pluginAPIAdapter) CreatePost(post *mm_model.Post) error {
|
2021-09-13 21:36:36 +02:00
|
|
|
return da.client.Post.CreatePost(post)
|
|
|
|
}
|
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
func (da *pluginAPIAdapter) GetUserByID(userID string) (*mm_model.User, error) {
|
2021-09-13 21:36:36 +02:00
|
|
|
return da.client.User.Get(userID)
|
|
|
|
}
|
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
func (da *pluginAPIAdapter) GetUserByUsername(name string) (*mm_model.User, error) {
|
2021-09-13 21:36:36 +02:00
|
|
|
return da.client.User.GetByUsername(name)
|
|
|
|
}
|
2021-09-20 23:32:47 +02:00
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
func (da *pluginAPIAdapter) GetTeamMember(teamID string, userID string) (*mm_model.TeamMember, error) {
|
2021-09-20 23:32:47 +02:00
|
|
|
return da.client.Team.GetMember(teamID, userID)
|
|
|
|
}
|
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
func (da *pluginAPIAdapter) GetChannelByID(channelID string) (*mm_model.Channel, error) {
|
2021-09-20 23:32:47 +02:00
|
|
|
return da.client.Channel.Get(channelID)
|
|
|
|
}
|
2021-12-03 19:33:59 +01:00
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
func (da *pluginAPIAdapter) GetChannelMember(channelID string, userID string) (*mm_model.ChannelMember, error) {
|
2021-12-03 19:33:59 +01:00
|
|
|
return da.client.Channel.GetMember(channelID, userID)
|
|
|
|
}
|
2022-05-04 22:10:26 +02:00
|
|
|
|
2022-05-16 17:53:41 +02:00
|
|
|
func (da *pluginAPIAdapter) CreateMember(teamID string, userID string) (*mm_model.TeamMember, error) {
|
2022-05-04 22:10:26 +02:00
|
|
|
return da.client.Team.CreateMember(teamID, userID)
|
|
|
|
}
|
2022-05-16 17:53:41 +02:00
|
|
|
|
|
|
|
type appIface interface {
|
|
|
|
CreateSubscription(sub *model.Subscription) (*model.Subscription, error)
|
|
|
|
AddMemberToBoard(member *model.BoardMember) (*model.BoardMember, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// appAPI provides app and store APIs for notification services. Where appropriate calls are made to the
|
|
|
|
// app layer to leverage the additional websocket notification logic present there, and other times the
|
|
|
|
// store APIs are called directly.
|
|
|
|
type appAPI struct {
|
|
|
|
store store.Store
|
|
|
|
app appIface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) init(store store.Store, app appIface) {
|
|
|
|
a.store = store
|
|
|
|
a.app = app
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) GetBlockHistory(blockID string, opts model.QueryBlockHistoryOptions) ([]model.Block, error) {
|
|
|
|
return a.store.GetBlockHistory(blockID, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) GetSubTree2(boardID, blockID string, opts model.QuerySubtreeOptions) ([]model.Block, error) {
|
|
|
|
return a.store.GetSubTree2(boardID, blockID, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) GetBoardAndCardByID(blockID string) (board *model.Board, card *model.Block, err error) {
|
|
|
|
return a.store.GetBoardAndCardByID(blockID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) GetUserByID(userID string) (*model.User, error) {
|
|
|
|
return a.store.GetUserByID(userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) CreateSubscription(sub *model.Subscription) (*model.Subscription, error) {
|
|
|
|
return a.app.CreateSubscription(sub)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) GetSubscribersForBlock(blockID string) ([]*model.Subscriber, error) {
|
|
|
|
return a.store.GetSubscribersForBlock(blockID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) UpdateSubscribersNotifiedAt(blockID string, notifyAt int64) error {
|
|
|
|
return a.store.UpdateSubscribersNotifiedAt(blockID, notifyAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) UpsertNotificationHint(hint *model.NotificationHint, notificationFreq time.Duration) (*model.NotificationHint, error) {
|
|
|
|
return a.store.UpsertNotificationHint(hint, notificationFreq)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) GetNextNotificationHint(remove bool) (*model.NotificationHint, error) {
|
|
|
|
return a.store.GetNextNotificationHint(remove)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) GetMemberForBoard(boardID, userID string) (*model.BoardMember, error) {
|
|
|
|
return a.store.GetMemberForBoard(boardID, userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appAPI) AddMemberToBoard(member *model.BoardMember) (*model.BoardMember, error) {
|
|
|
|
return a.app.AddMemberToBoard(member)
|
|
|
|
}
|