diff --git a/app/plugin_api.go b/app/plugin_api.go index ffb8cccd67..fc786202e7 100644 --- a/app/plugin_api.go +++ b/app/plugin_api.go @@ -5,6 +5,7 @@ package app import ( "encoding/json" + "fmt" "github.com/mattermost/mattermost-server/model" ) @@ -176,3 +177,11 @@ func (api *PluginAPI) KVGet(key string) ([]byte, *model.AppError) { func (api *PluginAPI) KVDelete(key string) *model.AppError { return api.app.DeletePluginKey(api.id, key) } + +func (api *PluginAPI) PublishWebSocketEvent(event string, payload map[string]interface{}, broadcast *model.WebsocketBroadcast) { + api.app.Publish(&model.WebSocketEvent{ + Event: fmt.Sprintf("custom_%v_%v", api.id, event), + Data: payload, + Broadcast: broadcast, + }) +} diff --git a/plugin/api.go b/plugin/api.go index ed2bfa7335..842cef4f69 100644 --- a/plugin/api.go +++ b/plugin/api.go @@ -113,6 +113,12 @@ type API interface { // Delete will remove a key-value pair. Returns nil for non-existent keys. KVDelete(key string) *model.AppError + + // PublishWebSocketEvent sends an event to WebSocket connections. + // event is the type and will be prepended with "custom__" + // payload is the data sent with the event. Interface values must be primitive Go types or mattermost-server/model types + // broadcast determines to which users to send the event + PublishWebSocketEvent(event string, payload map[string]interface{}, broadcast *model.WebsocketBroadcast) } var Handshake = plugin.HandshakeConfig{ diff --git a/plugin/client_rpc_generated.go b/plugin/client_rpc_generated.go index b32a3e36ea..9880814b21 100644 --- a/plugin/client_rpc_generated.go +++ b/plugin/client_rpc_generated.go @@ -1101,3 +1101,30 @@ func (s *APIRPCServer) KVDelete(args *KVDeleteArgs, returns *KVDeleteReturns) er } return nil } + +type PublishWebSocketEventArgs struct { + A string + B map[string]interface{} + C *model.WebsocketBroadcast +} + +type PublishWebSocketEventReturns struct { +} + +func (g *APIRPCClient) PublishWebSocketEvent(event string, payload map[string]interface{}, broadcast *model.WebsocketBroadcast) { + _args := &PublishWebSocketEventArgs{event, payload, broadcast} + _returns := &PublishWebSocketEventReturns{} + if err := g.client.Call("Plugin.PublishWebSocketEvent", _args, _returns); err != nil { + g.log.Error("RPC call to PublishWebSocketEvent API failed.", mlog.Err(err)) + } + return +} + +func (s *APIRPCServer) PublishWebSocketEvent(args *PublishWebSocketEventArgs, returns *PublishWebSocketEventReturns) error { + if hook, ok := s.impl.(interface { + PublishWebSocketEvent(event string, payload map[string]interface{}, broadcast *model.WebsocketBroadcast) + }); ok { + hook.PublishWebSocketEvent(args.A, args.B, args.C) + } + return nil +} diff --git a/plugin/plugintest/api.go b/plugin/plugintest/api.go index 11c5a9c592..145fcbf9d4 100644 --- a/plugin/plugintest/api.go +++ b/plugin/plugintest/api.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 // Regenerate this file using `make plugin-mocks`. @@ -563,6 +563,11 @@ func (_m *API) LoadPluginConfiguration(dest interface{}) error { return r0 } +// PublishWebSocketEvent provides a mock function with given fields: event, payload, broadcast +func (_m *API) PublishWebSocketEvent(event string, payload map[string]interface{}, broadcast *model.WebsocketBroadcast) { + _m.Called(event, payload, broadcast) +} + // RegisterCommand provides a mock function with given fields: command func (_m *API) RegisterCommand(command *model.Command) error { ret := _m.Called(command) diff --git a/plugin/plugintest/hooks.go b/plugin/plugintest/hooks.go index 790a5a9932..0d335a6261 100644 --- a/plugin/plugintest/hooks.go +++ b/plugin/plugintest/hooks.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 // Regenerate this file using `make plugin-mocks`.