* ensure plugin is always shutdown Once we call `.client.Client()` the plugin has started, and must be shut down. `newSupervisor` sometimes returned with an error (and without a reference to the supervisor), leaving the client running indefinitely. * Clarify the documentation to explain that plugin hooks will not trigger until `OnActivate` returns successfully, and will stop triggering just before `OnDeactivate` is called. * test for plugin deadlock * plugin/environment.go: switch to sync.Map From: https://golang.org/pkg/sync/#Map > If a goroutine holds a RWMutex for reading and another goroutine might call Lock, no goroutine should expect to be able to acquire a read lock until the initial read lock is released. In particular, this prohibits recursive read locking. This is to ensure that the lock eventually becomes available; a blocked Lock call excludes new readers from acquiring the lock. The previous `RWMutex` was not safe given that we effectively acquired read locks recursively (hook -> api -> hook). This worked up until we activated or deactivated plugins, tried to acquire a write lock, and the plugin used the API to effectively trigger another hook. Switching to sync.Map avoids this by divesting the need to lock at all, avoiding the potential for a recursive lock in the first place.
129 строки
6.2 KiB
Go
129 строки
6.2 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package plugin
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
)
|
|
|
|
// These assignments are part of the wire protocol used to trigger hook events in plugins.
|
|
//
|
|
// 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
|
|
TotalHooksId = iota
|
|
)
|
|
|
|
// Hooks describes the methods a plugin may implement to automatically receive the corresponding
|
|
// event.
|
|
//
|
|
// A plugin only need implement the hooks it cares about. The MattermostPlugin provides some
|
|
// default implementations for convenience but may be overridden.
|
|
type Hooks interface {
|
|
// OnActivate is invoked when the plugin is activated. If an error is returned, the plugin
|
|
// will be terminated. The plugin will not receive hooks until after OnActivate returns
|
|
// without error.
|
|
OnActivate() error
|
|
|
|
// Implemented returns a list of hooks that are implemented by the plugin.
|
|
// Plugins do not need to provide an implementation. Any given will be ignored.
|
|
Implemented() ([]string, error)
|
|
|
|
// OnDeactivate is invoked when the plugin is deactivated. This is the plugin's last chance to
|
|
// use the API, and the plugin will be terminated shortly after this invocation. The plugin
|
|
// will stop receiving hooks just prior to this method being called.
|
|
OnDeactivate() error
|
|
|
|
// OnConfigurationChange is invoked when configuration changes may have been made.
|
|
OnConfigurationChange() error
|
|
|
|
// ServeHTTP allows the plugin to implement the http.Handler interface. Requests destined for
|
|
// the /plugins/{id} path will be routed to the plugin.
|
|
//
|
|
// The Mattermost-User-Id header will be present if (and only if) the request is by an
|
|
// authenticated user.
|
|
ServeHTTP(c *Context, w http.ResponseWriter, r *http.Request)
|
|
|
|
// ExecuteCommand executes a command that has been previously registered via the RegisterCommand
|
|
// API.
|
|
ExecuteCommand(c *Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError)
|
|
|
|
// MessageWillBePosted is invoked when a message is posted by a user before it is committed
|
|
// to the database. If you also want to act on edited posts, see MessageWillBeUpdated.
|
|
// Return values should be the modified post or nil if rejected and an explanation for the user.
|
|
//
|
|
// If you don't need to modify or reject posts, use MessageHasBeenPosted instead.
|
|
//
|
|
// Note that this method will be called for posts created by plugins, including the plugin that
|
|
// created the post.
|
|
MessageWillBePosted(c *Context, post *model.Post) (*model.Post, string)
|
|
|
|
// MessageWillBeUpdated is invoked when a message is updated by a user before it is committed
|
|
// to the database. If you also want to act on new posts, see MessageWillBePosted.
|
|
// Return values should be the modified post or nil if rejected and an explanation for the user.
|
|
// On rejection, the post will be kept in its previous state.
|
|
//
|
|
// If you don't need to modify or rejected updated posts, use MessageHasBeenUpdated instead.
|
|
//
|
|
// Note that this method will be called for posts updated by plugins, including the plugin that
|
|
// updated the post.
|
|
MessageWillBeUpdated(c *Context, newPost, oldPost *model.Post) (*model.Post, string)
|
|
|
|
// MessageHasBeenPosted is invoked after the message has been committed to the database.
|
|
// If you need to modify or reject the post, see MessageWillBePosted
|
|
// Note that this method will be called for posts created by plugins, including the plugin that
|
|
// created the post.
|
|
MessageHasBeenPosted(c *Context, post *model.Post)
|
|
|
|
// MessageHasBeenUpdated is invoked after a message is updated and has been updated in the database.
|
|
// If you need to modify or reject the post, see MessageWillBeUpdated
|
|
// Note that this method will be called for posts created by plugins, including the plugin that
|
|
// created the post.
|
|
MessageHasBeenUpdated(c *Context, newPost, oldPost *model.Post)
|
|
|
|
// ChannelHasBeenCreated is invoked after the channel has been committed to the database.
|
|
ChannelHasBeenCreated(c *Context, channel *model.Channel)
|
|
|
|
// UserHasJoinedChannel is invoked after the membership has been committed to the database.
|
|
// If actor is not nil, the user was invited to the channel by the actor.
|
|
UserHasJoinedChannel(c *Context, channelMember *model.ChannelMember, actor *model.User)
|
|
|
|
// UserHasLeftChannel is invoked after the membership has been removed from the database.
|
|
// If actor is not nil, the user was removed from the channel by the actor.
|
|
UserHasLeftChannel(c *Context, channelMember *model.ChannelMember, actor *model.User)
|
|
|
|
// UserHasJoinedTeam is invoked after the membership has been committed to the database.
|
|
// If actor is not nil, the user was added to the team by the actor.
|
|
UserHasJoinedTeam(c *Context, teamMember *model.TeamMember, actor *model.User)
|
|
|
|
// UserHasLeftTeam is invoked after the membership has been removed from the database.
|
|
// If actor is not nil, the user was removed from the team by the actor.
|
|
UserHasLeftTeam(c *Context, teamMember *model.TeamMember, actor *model.User)
|
|
|
|
// FileWillBeUploaded is invoked when a file is uploaded, but before it is committed to backing store.
|
|
// Read from file to retrieve the body of the uploaded file. You may modify the body of the file by writing to output.
|
|
// Returned FileInfo will be used instead of input FileInfo. Return nil to reject the file upload and include a text reason as the second argument.
|
|
// Note that this method will be called for files uploaded by plugins, including the plugin that uploaded the post.
|
|
// FileInfo.Size will be automatically set properly if you modify the file.
|
|
FileWillBeUploaded(c *Context, info *model.FileInfo, file io.Reader, output io.Writer) (*model.FileInfo, string)
|
|
}
|