2022-07-15 07:51:50 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-07-15 07:51:50 +02:00
|
|
|
"errors"
|
2021-05-24 19:06:11 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
"github.com/mattermost/focalboard/mattermost-plugin/server/boards"
|
2021-06-04 08:43:32 +02:00
|
|
|
|
2021-06-25 16:49:06 +02:00
|
|
|
pluginapi "github.com/mattermost/mattermost-plugin-api"
|
2021-07-06 22:44:11 +02:00
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
mm_model "github.com/mattermost/mattermost-server/v6/model"
|
2021-08-24 12:13:58 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/plugin"
|
2021-08-25 22:08:01 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2021-05-24 19:06:11 +02:00
|
|
|
)
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
var ErrPluginNotAllowed = errors.New("boards plugin not allowed while Boards product enabled")
|
2021-10-22 18:13:31 +02:00
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
// Plugin implements the interface expected by the Mattermost server to communicate between the server and plugin processes.
|
|
|
|
type Plugin struct {
|
|
|
|
plugin.MattermostPlugin
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
boardsApp *boards.BoardsApp
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) OnActivate() error {
|
2022-07-15 07:51:50 +02:00
|
|
|
if p.API.GetConfig().FeatureFlags.BoardsProduct {
|
|
|
|
p.API.LogError(ErrPluginNotAllowed.Error())
|
|
|
|
return ErrPluginNotAllowed
|
|
|
|
}
|
2021-11-02 03:12:43 +02:00
|
|
|
|
|
|
|
client := pluginapi.NewClient(p.API, p.Driver)
|
|
|
|
|
|
|
|
logger, _ := mlog.NewLogger()
|
|
|
|
pluginTargetFactory := newPluginTargetFactory(&client.Log)
|
|
|
|
factories := &mlog.Factories{
|
|
|
|
TargetFactory: pluginTargetFactory.createTarget,
|
|
|
|
}
|
|
|
|
cfgJSON := defaultLoggingConfig()
|
2022-07-15 07:51:50 +02:00
|
|
|
err := logger.Configure("", cfgJSON, factories)
|
2021-11-02 03:12:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
adapter := newServiceAPIAdapter(p.API, client.Store, logger)
|
2021-12-10 17:46:37 +02:00
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
boardsApp, err := boards.NewBoardsApp(adapter)
|
2021-11-02 03:12:43 +02:00
|
|
|
if err != nil {
|
2022-07-15 07:51:50 +02:00
|
|
|
return fmt.Errorf("cannot activate plugin: %w", err)
|
2021-11-02 03:12:43 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
p.boardsApp = boardsApp
|
|
|
|
return p.boardsApp.Start()
|
2021-11-02 03:12:43 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
// OnConfigurationChange is invoked when configuration changes may have been made.
|
|
|
|
func (p *Plugin) OnConfigurationChange() error {
|
|
|
|
// Have we been setup by OnActivate?
|
|
|
|
if p.boardsApp == nil {
|
|
|
|
return nil
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
return p.boardsApp.OnConfigurationChange()
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
func (p *Plugin) OnWebSocketConnect(webConnID, userID string) {
|
|
|
|
p.boardsApp.OnWebSocketConnect(webConnID, userID)
|
|
|
|
}
|
2021-12-10 17:46:37 +02:00
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
func (p *Plugin) OnWebSocketDisconnect(webConnID, userID string) {
|
|
|
|
p.boardsApp.OnWebSocketDisconnect(webConnID, userID)
|
2021-12-10 17:46:37 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
func (p *Plugin) WebSocketMessageHasBeenPosted(webConnID, userID string, req *mm_model.WebSocketRequest) {
|
|
|
|
p.boardsApp.WebSocketMessageHasBeenPosted(webConnID, userID, req)
|
2021-12-10 17:46:37 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
func (p *Plugin) OnDeactivate() error {
|
|
|
|
return p.boardsApp.Stop()
|
2021-11-09 16:56:04 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
func (p *Plugin) OnPluginClusterEvent(ctx *plugin.Context, ev mm_model.PluginClusterEvent) {
|
|
|
|
p.boardsApp.OnPluginClusterEvent(ctx, ev)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
func (p *Plugin) MessageWillBePosted(ctx *plugin.Context, post *mm_model.Post) (*mm_model.Post, string) {
|
|
|
|
return p.boardsApp.MessageWillBePosted(ctx, post)
|
2021-08-27 10:59:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
func (p *Plugin) MessageWillBeUpdated(ctx *plugin.Context, newPost, oldPost *mm_model.Post) (*mm_model.Post, string) {
|
|
|
|
return p.boardsApp.MessageWillBeUpdated(ctx, newPost, oldPost)
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
func (p *Plugin) OnCloudLimitsUpdated(limits *mm_model.ProductLimits) {
|
|
|
|
p.boardsApp.OnCloudLimitsUpdated(limits)
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:50 +02:00
|
|
|
func (p *Plugin) RunDataRetention(nowTime, batchSize int64) (int64, error) {
|
|
|
|
return p.boardsApp.RunDataRetention(nowTime, batchSize)
|
2021-09-16 12:18:11 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
// ServeHTTP demonstrates a plugin that handles HTTP requests by greeting the world.
|
2022-07-15 07:51:50 +02:00
|
|
|
func (p *Plugin) ServeHTTP(ctx *plugin.Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
p.boardsApp.ServeHTTP(ctx, w, r)
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 18:07:30 +02:00
|
|
|
func defaultLoggingConfig() string {
|
|
|
|
return `
|
|
|
|
{
|
|
|
|
"def": {
|
2021-09-15 15:01:05 +02:00
|
|
|
"type": "focalboard_plugin_adapter",
|
|
|
|
"options": {},
|
2021-07-02 18:07:30 +02:00
|
|
|
"format": "plain",
|
|
|
|
"format_options": {
|
|
|
|
"delim": " ",
|
2021-07-27 18:57:29 +02:00
|
|
|
"min_level_len": 0,
|
|
|
|
"min_msg_len": 0,
|
|
|
|
"enable_color": false,
|
|
|
|
"enable_caller": true
|
2021-07-02 18:07:30 +02:00
|
|
|
},
|
|
|
|
"levels": [
|
|
|
|
{"id": 5, "name": "debug"},
|
|
|
|
{"id": 4, "name": "info", "color": 36},
|
|
|
|
{"id": 3, "name": "warn"},
|
|
|
|
{"id": 2, "name": "error", "color": 31},
|
|
|
|
{"id": 1, "name": "fatal", "stacktrace": true},
|
|
|
|
{"id": 0, "name": "panic", "stacktrace": true}
|
|
|
|
]
|
2021-12-10 17:46:37 +02:00
|
|
|
},
|
|
|
|
"errors_file": {
|
|
|
|
"Type": "file",
|
|
|
|
"Format": "plain",
|
|
|
|
"Levels": [
|
|
|
|
{"ID": 2, "Name": "error", "Stacktrace": true}
|
|
|
|
],
|
|
|
|
"Options": {
|
|
|
|
"Compress": true,
|
|
|
|
"Filename": "focalboard_errors.log",
|
|
|
|
"MaxAgeDays": 0,
|
|
|
|
"MaxBackups": 5,
|
2022-02-28 13:28:16 +02:00
|
|
|
"MaxSizeMB": 10
|
2021-12-10 17:46:37 +02:00
|
|
|
},
|
|
|
|
"MaxQueueSize": 1000
|
2022-02-28 13:28:16 +02:00
|
|
|
}
|
2021-07-02 18:07:30 +02:00
|
|
|
}`
|
|
|
|
}
|