Unverified Commit d7976549 authored by Joram Wilander's avatar Joram Wilander Committed by GitHub

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
parent 1e5c432e
......@@ -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,
})
}
......@@ -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_<pluginid>_"
// 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{
......
......@@ -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
}
// 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)
......
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v1.0.0
// Regenerate this file using `make plugin-mocks`.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment