2021-09-16 12:18:11 +02:00
|
|
|
package ws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
mmModel "github.com/mattermost/mattermost-server/v6/model"
|
2022-07-18 19:21:57 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2021-09-16 12:18:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ClusterMessage struct {
|
2022-03-22 16:24:34 +02:00
|
|
|
TeamID string
|
|
|
|
BoardID string
|
2022-08-10 12:44:37 +02:00
|
|
|
UserID string
|
2021-09-16 12:18:11 +02:00
|
|
|
Payload map[string]interface{}
|
2022-03-22 16:24:34 +02:00
|
|
|
EnsureUsers []string
|
2021-09-16 12:18:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-23 09:13:40 +02:00
|
|
|
func (pa *PluginAdapter) sendMessageToCluster(clusterMessage *ClusterMessage) {
|
|
|
|
const id = "websocket_message"
|
2021-09-16 12:18:11 +02:00
|
|
|
b, err := json.Marshal(clusterMessage)
|
|
|
|
if err != nil {
|
2022-07-18 19:21:57 +02:00
|
|
|
pa.logger.Error("couldn't get JSON bytes from cluster message",
|
|
|
|
mlog.String("id", id),
|
|
|
|
mlog.Err(err),
|
2021-09-16 12:18:11 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event := mmModel.PluginClusterEvent{Id: id, Data: b}
|
|
|
|
opts := mmModel.PluginClusterEventSendOptions{
|
|
|
|
SendType: mmModel.PluginClusterEventSendTypeReliable,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pa.api.PublishPluginClusterEvent(event, opts); err != nil {
|
2022-07-18 19:21:57 +02:00
|
|
|
pa.logger.Error("error publishing cluster event",
|
|
|
|
mlog.String("id", id),
|
|
|
|
mlog.Err(err),
|
2021-09-16 12:18:11 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *PluginAdapter) HandleClusterEvent(ev mmModel.PluginClusterEvent) {
|
2022-07-18 19:21:57 +02:00
|
|
|
pa.logger.Debug("received cluster event", mlog.String("id", ev.Id))
|
2021-09-16 12:18:11 +02:00
|
|
|
|
|
|
|
var clusterMessage ClusterMessage
|
|
|
|
if err := json.Unmarshal(ev.Data, &clusterMessage); err != nil {
|
2022-07-18 19:21:57 +02:00
|
|
|
pa.logger.Error("cannot unmarshal cluster message data",
|
|
|
|
mlog.String("id", ev.Id),
|
|
|
|
mlog.Err(err),
|
2021-09-16 12:18:11 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
if clusterMessage.BoardID != "" {
|
|
|
|
pa.sendBoardMessageSkipCluster(clusterMessage.TeamID, clusterMessage.BoardID, clusterMessage.Payload, clusterMessage.EnsureUsers...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-10 17:46:37 +02:00
|
|
|
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.
|
2022-07-18 19:21:57 +02:00
|
|
|
pa.logger.Warn("cannot determine action from cluster message data",
|
|
|
|
mlog.String("id", ev.Id),
|
|
|
|
mlog.Map("payload", clusterMessage.Payload),
|
2021-12-10 17:46:37 +02:00
|
|
|
)
|
2022-05-19 21:59:04 +02:00
|
|
|
return
|
2021-12-10 17:46:37 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 12:44:37 +02:00
|
|
|
if clusterMessage.UserID != "" {
|
|
|
|
pa.sendUserMessageSkipCluster(action, clusterMessage.Payload, clusterMessage.UserID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-06 10:42:13 +02:00
|
|
|
pa.sendTeamMessageSkipCluster(action, clusterMessage.TeamID, clusterMessage.Payload)
|
2021-09-16 12:18:11 +02:00
|
|
|
}
|