2021-05-24 19:06:11 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-06-21 12:08:33 +02:00
|
|
|
"path"
|
2021-05-24 19:06:11 +02:00
|
|
|
"sync"
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
"github.com/mattermost/focalboard/server/auth"
|
2021-05-24 19:06:11 +02:00
|
|
|
"github.com/mattermost/focalboard/server/server"
|
|
|
|
"github.com/mattermost/focalboard/server/services/config"
|
2021-09-13 21:36:36 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/notify"
|
2021-06-25 16:49:06 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
|
|
|
"github.com/mattermost/focalboard/server/services/store/mattermostauthlayer"
|
|
|
|
"github.com/mattermost/focalboard/server/services/store/sqlstore"
|
2021-08-27 10:59:14 +02:00
|
|
|
"github.com/mattermost/focalboard/server/ws"
|
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
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
mmModel "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
|
|
|
)
|
|
|
|
|
|
|
|
// Plugin implements the interface expected by the Mattermost server to communicate between the server and plugin processes.
|
|
|
|
type Plugin struct {
|
|
|
|
plugin.MattermostPlugin
|
|
|
|
|
|
|
|
// configurationLock synchronizes access to the configuration.
|
|
|
|
configurationLock sync.RWMutex
|
|
|
|
|
|
|
|
// configuration is the active plugin configuration. Consult getConfiguration and
|
|
|
|
// setConfiguration for usage.
|
|
|
|
configuration *configuration
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
server *server.Server
|
2021-09-16 21:31:02 +02:00
|
|
|
wsPluginAdapter ws.PluginAdapterInterface
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) OnActivate() error {
|
|
|
|
mmconfig := p.API.GetUnsanitizedConfig()
|
|
|
|
filesS3Config := config.AmazonS3Config{}
|
|
|
|
if mmconfig.FileSettings.AmazonS3AccessKeyId != nil {
|
2021-07-09 03:09:02 +02:00
|
|
|
filesS3Config.AccessKeyID = *mmconfig.FileSettings.AmazonS3AccessKeyId
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3SecretAccessKey != nil {
|
|
|
|
filesS3Config.SecretAccessKey = *mmconfig.FileSettings.AmazonS3SecretAccessKey
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3Bucket != nil {
|
|
|
|
filesS3Config.Bucket = *mmconfig.FileSettings.AmazonS3Bucket
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3PathPrefix != nil {
|
|
|
|
filesS3Config.PathPrefix = *mmconfig.FileSettings.AmazonS3PathPrefix
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3Region != nil {
|
|
|
|
filesS3Config.Region = *mmconfig.FileSettings.AmazonS3Region
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3Endpoint != nil {
|
|
|
|
filesS3Config.Endpoint = *mmconfig.FileSettings.AmazonS3Endpoint
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3SSL != nil {
|
|
|
|
filesS3Config.SSL = *mmconfig.FileSettings.AmazonS3SSL
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3SignV2 != nil {
|
|
|
|
filesS3Config.SignV2 = *mmconfig.FileSettings.AmazonS3SignV2
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3SSE != nil {
|
|
|
|
filesS3Config.SSE = *mmconfig.FileSettings.AmazonS3SSE
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3Trace != nil {
|
|
|
|
filesS3Config.Trace = *mmconfig.FileSettings.AmazonS3Trace
|
|
|
|
}
|
|
|
|
|
2021-06-25 16:49:06 +02:00
|
|
|
client := pluginapi.NewClient(p.API, p.Driver)
|
|
|
|
sqlDB, err := client.Store.GetMasterDB()
|
|
|
|
if err != nil {
|
2021-07-09 12:59:44 +02:00
|
|
|
return fmt.Errorf("error initializing the DB: %w", err)
|
2021-06-25 16:49:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-15 15:01:05 +02:00
|
|
|
logger, _ := mlog.NewLogger()
|
|
|
|
pluginTargetFactory := newPluginTargetFactory(&client.Log)
|
|
|
|
factories := &mlog.Factories{
|
|
|
|
TargetFactory: pluginTargetFactory.createTarget,
|
|
|
|
}
|
|
|
|
cfgJSON := defaultLoggingConfig()
|
|
|
|
err = logger.Configure("", cfgJSON, factories)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-07 06:43:06 +02:00
|
|
|
baseURL := ""
|
|
|
|
if mmconfig.ServiceSettings.SiteURL != nil {
|
|
|
|
baseURL = *mmconfig.ServiceSettings.SiteURL
|
|
|
|
}
|
|
|
|
|
2021-09-01 23:53:27 +02:00
|
|
|
serverID := client.System.GetDiagnosticID()
|
|
|
|
|
|
|
|
enableTelemetry := false
|
|
|
|
if mmconfig.LogSettings.EnableDiagnostics != nil {
|
|
|
|
enableTelemetry = *mmconfig.LogSettings.EnableDiagnostics
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:31:02 +02:00
|
|
|
enablePublicSharedBoards := false
|
|
|
|
if mmconfig.PluginSettings.Plugins["focalboard"]["enablepublicsharedboards"] == true {
|
|
|
|
enablePublicSharedBoards = true
|
|
|
|
}
|
|
|
|
|
2021-06-25 16:49:06 +02:00
|
|
|
cfg := &config.Configuration{
|
2021-09-16 21:31:02 +02:00
|
|
|
ServerRoot: baseURL + "/plugins/focalboard",
|
|
|
|
Port: -1,
|
|
|
|
DBType: *mmconfig.SqlSettings.DriverName,
|
|
|
|
DBConfigString: *mmconfig.SqlSettings.DataSource,
|
|
|
|
DBTablePrefix: "focalboard_",
|
|
|
|
UseSSL: false,
|
|
|
|
SecureCookie: true,
|
|
|
|
WebPath: path.Join(*mmconfig.PluginSettings.Directory, "focalboard", "pack"),
|
|
|
|
FilesDriver: *mmconfig.FileSettings.DriverName,
|
|
|
|
FilesPath: *mmconfig.FileSettings.Directory,
|
|
|
|
FilesS3Config: filesS3Config,
|
|
|
|
Telemetry: enableTelemetry,
|
|
|
|
TelemetryID: serverID,
|
|
|
|
WebhookUpdate: []string{},
|
|
|
|
SessionExpireTime: 2592000,
|
|
|
|
SessionRefreshTime: 18000,
|
|
|
|
LocalOnly: false,
|
|
|
|
EnableLocalMode: false,
|
|
|
|
LocalModeSocketLocation: "",
|
|
|
|
AuthMode: "mattermost",
|
|
|
|
EnablePublicSharedBoards: enablePublicSharedBoards,
|
2021-06-25 16:49:06 +02:00
|
|
|
}
|
|
|
|
var db store.Store
|
2021-09-08 06:52:03 +02:00
|
|
|
db, err = sqlstore.New(cfg.DBType, cfg.DBConfigString, cfg.DBTablePrefix, logger, sqlDB, true)
|
2021-06-25 16:49:06 +02:00
|
|
|
if err != nil {
|
2021-07-09 12:59:44 +02:00
|
|
|
return fmt.Errorf("error initializing the DB: %w", err)
|
2021-06-25 16:49:06 +02:00
|
|
|
}
|
|
|
|
if cfg.AuthMode == server.MattermostAuthMod {
|
2021-07-06 22:44:11 +02:00
|
|
|
layeredStore, err2 := mattermostauthlayer.New(cfg.DBType, sqlDB, db, logger)
|
2021-06-25 16:49:06 +02:00
|
|
|
if err2 != nil {
|
2021-07-09 12:59:44 +02:00
|
|
|
return fmt.Errorf("error initializing the DB: %w", err2)
|
2021-06-25 16:49:06 +02:00
|
|
|
}
|
|
|
|
db = layeredStore
|
|
|
|
}
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
p.wsPluginAdapter = ws.NewPluginAdapter(p.API, auth.New(cfg, db))
|
2021-08-06 21:03:56 +02:00
|
|
|
|
2021-10-15 20:23:35 +02:00
|
|
|
mentionsBackend, err := createMentionsNotifyBackend(client, baseURL+"/boards", logger)
|
2021-09-13 21:36:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating mentions notifications backend: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := server.Params{
|
|
|
|
Cfg: cfg,
|
|
|
|
SingleUserToken: "",
|
|
|
|
DBStore: db,
|
|
|
|
Logger: logger,
|
|
|
|
ServerID: serverID,
|
|
|
|
WSAdapter: p.wsPluginAdapter,
|
|
|
|
NotifyBackends: []notify.Backend{mentionsBackend},
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := server.New(params)
|
2021-05-24 19:06:11 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("ERROR INITIALIZING THE SERVER", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.server = server
|
|
|
|
return server.Start()
|
|
|
|
}
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
func (p *Plugin) OnWebSocketConnect(webConnID, userID string) {
|
|
|
|
p.wsPluginAdapter.OnWebSocketConnect(webConnID, userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) OnWebSocketDisconnect(webConnID, userID string) {
|
|
|
|
p.wsPluginAdapter.OnWebSocketDisconnect(webConnID, userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) WebSocketMessageHasBeenPosted(webConnID, userID string, req *mmModel.WebSocketRequest) {
|
|
|
|
p.wsPluginAdapter.WebSocketMessageHasBeenPosted(webConnID, userID, req)
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) OnDeactivate() error {
|
|
|
|
return p.server.Shutdown()
|
|
|
|
}
|
|
|
|
|
2021-09-16 12:18:11 +02:00
|
|
|
func (p *Plugin) OnPluginClusterEvent(_ *plugin.Context, ev mmModel.PluginClusterEvent) {
|
|
|
|
p.wsPluginAdapter.HandleClusterEvent(ev)
|
|
|
|
}
|
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
// ServeHTTP demonstrates a plugin that handles HTTP requests by greeting the world.
|
2021-07-09 12:59:44 +02:00
|
|
|
func (p *Plugin) ServeHTTP(_ *plugin.Context, w http.ResponseWriter, r *http.Request) {
|
2021-05-24 19:06:11 +02:00
|
|
|
router := p.server.GetRootRouter()
|
|
|
|
router.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
}
|