focalboard/server/ws/plugin_adapter_cluster.go
Doug Lauder 605c0079eb
Multi product architecture (#3309)
* skeleton lifecycle

* bare minimum to satisfy mm-server import

* added boards_imports.go

* move boards_imports.go to correct package

* bump mmserver version; remove replace in go.mod; use module workspaces; remove logger service

* rename product.go --> boards.go

* add FileInfoStore and Cloud services for product; create minimal pluginAPI interfaces for all packages

* rename Boards -> BoardsProduct

* compile success

* remove hooks service; guard for nil BoardsApp

* update to latest mmserver ver

* upgrade mmserver to master tip

* upgrade mmserver to master tip

* bump plugin-api to master tip

* fix users service

* fix OnActivate crash; normalize AppError returns

* fileBackend interface for server/app

* feature flag

* bump mmserver version

* fix linter errors

* make go.work when linting

* fix go.work creation for CI

* add execute flag for script

* fix more linter errors

* always create a go.work

* fix ci go.work

* OS agnostic go.work generator

* fix path

* fix path again

* partially disable cypress test

* fix case Id --> ID

* bump mmserver version

* include  in go.work for dev

* addressed review comments.

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
2022-07-15 07:51:50 +02:00

73 lines
1.9 KiB
Go

package ws
import (
"encoding/json"
mmModel "github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
)
type ClusterMessage struct {
TeamID string
BoardID string
Payload map[string]interface{}
EnsureUsers []string
}
func (pa *PluginAdapter) sendMessageToCluster(id string, clusterMessage *ClusterMessage) {
b, err := json.Marshal(clusterMessage)
if err != nil {
pa.logger.Error("couldn't get JSON bytes from cluster message",
mlog.String("id", id),
mlog.Err(err),
)
return
}
event := mmModel.PluginClusterEvent{Id: id, Data: b}
opts := mmModel.PluginClusterEventSendOptions{
SendType: mmModel.PluginClusterEventSendTypeReliable,
}
if err := pa.api.PublishPluginClusterEvent(event, opts); err != nil {
pa.logger.Error("error publishing cluster event",
mlog.String("id", id),
mlog.Err(err),
)
}
}
func (pa *PluginAdapter) HandleClusterEvent(ev mmModel.PluginClusterEvent) {
pa.logger.Debug("received cluster event", mlog.String("id", ev.Id))
var clusterMessage ClusterMessage
if err := json.Unmarshal(ev.Data, &clusterMessage); err != nil {
pa.logger.Error("cannot unmarshal cluster message data",
mlog.String("id", ev.Id),
mlog.Err(err),
)
return
}
if clusterMessage.BoardID != "" {
pa.sendBoardMessageSkipCluster(clusterMessage.TeamID, clusterMessage.BoardID, clusterMessage.Payload, clusterMessage.EnsureUsers...)
return
}
var action string
if actionRaw, ok := clusterMessage.Payload["action"]; ok {
if s, ok := actionRaw.(string); ok {
action = s
}
}
if action == "" {
// no action was specified in the event; assume block change and warn.
pa.logger.Warn("cannot determine action from cluster message data",
mlog.String("id", ev.Id),
mlog.Map("payload", clusterMessage.Payload),
)
return
}
pa.sendTeamMessageSkipCluster(action, clusterMessage.TeamID, clusterMessage.Payload)
}