Add plugin websocket hooks (#18151)
* Add plugin websocket hooks * Improve sending message mechanism and filter out of the router plugin specific messages * Return and manage error if the request clone fails * Wording change to avoid repetition Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
b66fd1f89e
Коммит
0ae681464e
@@ -566,6 +566,109 @@ func (s *hooksRPCServer) OnPluginClusterEvent(args *Z_OnPluginClusterEventArgs,
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
hookNameToId["OnWebSocketConnect"] = OnWebSocketConnectID
|
||||
}
|
||||
|
||||
type Z_OnWebSocketConnectArgs struct {
|
||||
A string
|
||||
B string
|
||||
}
|
||||
|
||||
type Z_OnWebSocketConnectReturns struct {
|
||||
}
|
||||
|
||||
func (g *hooksRPCClient) OnWebSocketConnect(webConnID, userID string) {
|
||||
_args := &Z_OnWebSocketConnectArgs{webConnID, userID}
|
||||
_returns := &Z_OnWebSocketConnectReturns{}
|
||||
if g.implemented[OnWebSocketConnectID] {
|
||||
if err := g.client.Call("Plugin.OnWebSocketConnect", _args, _returns); err != nil {
|
||||
g.log.Error("RPC call OnWebSocketConnect to plugin failed.", mlog.Err(err))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *hooksRPCServer) OnWebSocketConnect(args *Z_OnWebSocketConnectArgs, returns *Z_OnWebSocketConnectReturns) error {
|
||||
if hook, ok := s.impl.(interface {
|
||||
OnWebSocketConnect(webConnID, userID string)
|
||||
}); ok {
|
||||
hook.OnWebSocketConnect(args.A, args.B)
|
||||
} else {
|
||||
return encodableError(fmt.Errorf("Hook OnWebSocketConnect called but not implemented."))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
hookNameToId["OnWebSocketDisconnect"] = OnWebSocketDisconnectID
|
||||
}
|
||||
|
||||
type Z_OnWebSocketDisconnectArgs struct {
|
||||
A string
|
||||
B string
|
||||
}
|
||||
|
||||
type Z_OnWebSocketDisconnectReturns struct {
|
||||
}
|
||||
|
||||
func (g *hooksRPCClient) OnWebSocketDisconnect(webConnID, userID string) {
|
||||
_args := &Z_OnWebSocketDisconnectArgs{webConnID, userID}
|
||||
_returns := &Z_OnWebSocketDisconnectReturns{}
|
||||
if g.implemented[OnWebSocketDisconnectID] {
|
||||
if err := g.client.Call("Plugin.OnWebSocketDisconnect", _args, _returns); err != nil {
|
||||
g.log.Error("RPC call OnWebSocketDisconnect to plugin failed.", mlog.Err(err))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *hooksRPCServer) OnWebSocketDisconnect(args *Z_OnWebSocketDisconnectArgs, returns *Z_OnWebSocketDisconnectReturns) error {
|
||||
if hook, ok := s.impl.(interface {
|
||||
OnWebSocketDisconnect(webConnID, userID string)
|
||||
}); ok {
|
||||
hook.OnWebSocketDisconnect(args.A, args.B)
|
||||
} else {
|
||||
return encodableError(fmt.Errorf("Hook OnWebSocketDisconnect called but not implemented."))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
hookNameToId["WebSocketMessageHasBeenPosted"] = WebSocketMessageHasBeenPostedID
|
||||
}
|
||||
|
||||
type Z_WebSocketMessageHasBeenPostedArgs struct {
|
||||
A string
|
||||
B string
|
||||
C *model.WebSocketRequest
|
||||
}
|
||||
|
||||
type Z_WebSocketMessageHasBeenPostedReturns struct {
|
||||
}
|
||||
|
||||
func (g *hooksRPCClient) WebSocketMessageHasBeenPosted(webConnID, userID string, req *model.WebSocketRequest) {
|
||||
_args := &Z_WebSocketMessageHasBeenPostedArgs{webConnID, userID, req}
|
||||
_returns := &Z_WebSocketMessageHasBeenPostedReturns{}
|
||||
if g.implemented[WebSocketMessageHasBeenPostedID] {
|
||||
if err := g.client.Call("Plugin.WebSocketMessageHasBeenPosted", _args, _returns); err != nil {
|
||||
g.log.Error("RPC call WebSocketMessageHasBeenPosted to plugin failed.", mlog.Err(err))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *hooksRPCServer) WebSocketMessageHasBeenPosted(args *Z_WebSocketMessageHasBeenPostedArgs, returns *Z_WebSocketMessageHasBeenPostedReturns) error {
|
||||
if hook, ok := s.impl.(interface {
|
||||
WebSocketMessageHasBeenPosted(webConnID, userID string, req *model.WebSocketRequest)
|
||||
}); ok {
|
||||
hook.WebSocketMessageHasBeenPosted(args.A, args.B, args.C)
|
||||
} else {
|
||||
return encodableError(fmt.Errorf("Hook WebSocketMessageHasBeenPosted called but not implemented."))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Z_RegisterCommandArgs struct {
|
||||
A *model.Command
|
||||
}
|
||||
|
||||
@@ -15,28 +15,31 @@ import (
|
||||
// Feel free to add more, but do not change existing assignments. Follow the naming convention of
|
||||
// <HookName>ID as the autogenerated glue code depends on that.
|
||||
const (
|
||||
OnActivateID = 0
|
||||
OnDeactivateID = 1
|
||||
ServeHTTPID = 2
|
||||
OnConfigurationChangeID = 3
|
||||
ExecuteCommandID = 4
|
||||
MessageWillBePostedID = 5
|
||||
MessageWillBeUpdatedID = 6
|
||||
MessageHasBeenPostedID = 7
|
||||
MessageHasBeenUpdatedID = 8
|
||||
UserHasJoinedChannelID = 9
|
||||
UserHasLeftChannelID = 10
|
||||
UserHasJoinedTeamID = 11
|
||||
UserHasLeftTeamID = 12
|
||||
ChannelHasBeenCreatedID = 13
|
||||
FileWillBeUploadedID = 14
|
||||
UserWillLogInID = 15
|
||||
UserHasLoggedInID = 16
|
||||
UserHasBeenCreatedID = 17
|
||||
ReactionHasBeenAddedID = 18
|
||||
ReactionHasBeenRemovedID = 19
|
||||
OnPluginClusterEventID = 20
|
||||
TotalHooksID = iota
|
||||
OnActivateID = 0
|
||||
OnDeactivateID = 1
|
||||
ServeHTTPID = 2
|
||||
OnConfigurationChangeID = 3
|
||||
ExecuteCommandID = 4
|
||||
MessageWillBePostedID = 5
|
||||
MessageWillBeUpdatedID = 6
|
||||
MessageHasBeenPostedID = 7
|
||||
MessageHasBeenUpdatedID = 8
|
||||
UserHasJoinedChannelID = 9
|
||||
UserHasLeftChannelID = 10
|
||||
UserHasJoinedTeamID = 11
|
||||
UserHasLeftTeamID = 12
|
||||
ChannelHasBeenCreatedID = 13
|
||||
FileWillBeUploadedID = 14
|
||||
UserWillLogInID = 15
|
||||
UserHasLoggedInID = 16
|
||||
UserHasBeenCreatedID = 17
|
||||
ReactionHasBeenAddedID = 18
|
||||
ReactionHasBeenRemovedID = 19
|
||||
OnPluginClusterEventID = 20
|
||||
OnWebSocketConnectID = 21
|
||||
OnWebSocketDisconnectID = 22
|
||||
WebSocketMessageHasBeenPostedID = 23
|
||||
TotalHooksID = iota
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -219,4 +222,25 @@ type Hooks interface {
|
||||
//
|
||||
// Minimum server version: 5.36
|
||||
OnPluginClusterEvent(c *Context, ev model.PluginClusterEvent)
|
||||
|
||||
// OnWebSocketConnect is invoked when a new websocket connection is opened.
|
||||
//
|
||||
// This is used to track which users have connections opened with the Mattermost
|
||||
// websocket.
|
||||
//
|
||||
// Minimum server version: 6.0
|
||||
OnWebSocketConnect(webConnID, userID string)
|
||||
|
||||
// OnWebSocketDisconnect is invoked when a websocket connection is closed.
|
||||
//
|
||||
// This is used to track which users have connections opened with the Mattermost
|
||||
// websocket.
|
||||
//
|
||||
// Minimum server version: 6.0
|
||||
OnWebSocketDisconnect(webConnID, userID string)
|
||||
|
||||
// WebSocketMessageHasBeenPosted is invoked when a websocket message is received.
|
||||
//
|
||||
// Minimum server version: 6.0
|
||||
WebSocketMessageHasBeenPosted(webConnID, userID string, req *model.WebSocketRequest)
|
||||
}
|
||||
|
||||
@@ -168,3 +168,21 @@ func (hooks *hooksTimerLayer) OnPluginClusterEvent(c *Context, ev model.PluginCl
|
||||
hooks.hooksImpl.OnPluginClusterEvent(c, ev)
|
||||
hooks.recordTime(startTime, "OnPluginClusterEvent", true)
|
||||
}
|
||||
|
||||
func (hooks *hooksTimerLayer) OnWebSocketConnect(webConnID, userID string) {
|
||||
startTime := timePkg.Now()
|
||||
hooks.hooksImpl.OnWebSocketConnect(webConnID, userID)
|
||||
hooks.recordTime(startTime, "OnWebSocketConnect", true)
|
||||
}
|
||||
|
||||
func (hooks *hooksTimerLayer) OnWebSocketDisconnect(webConnID, userID string) {
|
||||
startTime := timePkg.Now()
|
||||
hooks.hooksImpl.OnWebSocketDisconnect(webConnID, userID)
|
||||
hooks.recordTime(startTime, "OnWebSocketDisconnect", true)
|
||||
}
|
||||
|
||||
func (hooks *hooksTimerLayer) WebSocketMessageHasBeenPosted(webConnID, userID string, req *model.WebSocketRequest) {
|
||||
startTime := timePkg.Now()
|
||||
hooks.hooksImpl.WebSocketMessageHasBeenPosted(webConnID, userID, req)
|
||||
hooks.recordTime(startTime, "WebSocketMessageHasBeenPosted", true)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user