From d7976549a0b45a42c04ac043a15677b7ca1228e9 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Wed, 27 Jun 2018 08:46:38 -0400 Subject: MM-9674 Add plugin API for publishing custom WebSocket events (#8999) * Add plugin API for publishing custom WebSocket events * Add clearer payload comment * Update comment --- app/plugin_api.go | 9 +++++++++ plugin/api.go | 6 ++++++ plugin/client_rpc_generated.go | 27 +++++++++++++++++++++++++++ plugin/plugintest/api.go | 7 ++++++- plugin/plugintest/hooks.go | 2 +- 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/app/plugin_api.go b/app/plugin_api.go index ffb8cccd6..fc786202e 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 ed2bfa733..842cef4f6 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 b32a3e36e..9880814b2 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 11c5a9c59..145fcbf9d 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 790a5a993..0d335a626 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`. -- cgit v1.2.3-1-g7c22