The plugin architecture consists of an API, hooks, and plugin helpers.
When a plugin invokes an API method, it makes an RPC call to the Mattermost server and waits for a response. When the Mattermost server invokes a hook method, it makes an RPC call to the plugin and waits for a response:
// OnActivate is a hook called by the server when the plugin is started.
func (p *Plugin) OnActivate() error {
// CreatePost is an API called by the plugin to create a post.
post, err := p.API.CreatePost(&model.Post{...})
if err != nil {
return err
}
return nil
}
By contrast, a plugin helper is just Go code that wraps the existing API and hook methods without introducing any new RPC calls:
func (p *Plugin) OnActivate() error {
var enabled bool
// KVGetJSON is a plugin helper that wraps KVGet to simplify reading JSON data.
_, err := p.Helpers.KVGetJSON("enabled", enabled)
if err != nil {
return err
}
if enabled {
// ...
}
}
Although plugin helpers are defined in the github.com/mattermost/mattermost-server/plugin package, they aren’t part of the RPC exchange with the Mattermost server. This distinction has a number of advantages:
In general, always write a plugin helper when the functionality can be expressed using the existing API or hooks.
Even if this is not possible, consider whether the helper functionality naturally decomposes into smaller, general purpose API calls and hooks to enable future plugin helpers to build upon your changes.
Don’t be afraid to extend the API or hooks to support brand new functionality. Consider accepting an options struct instead of a list of parameters to simplify extending the API in the future:
// GetUsers a list of users based on search options.
//
// Minimum server version: 5.10
GetUsers(options *model.UserGetOptions) ([]*model.User, *model.AppError)
Old servers won’t do anything with new, unrecognized fields, but also won’t break if they are present. When adding new options that will only be supported by some Mattermost server versions, consider wrapping the functionality with a plugin helper to help plugin authors safely use the API.
To add a helper to the official github.com/mattermost/mattermost-server/plugin package:
Helpers interface with the new method.HelpersImpl struct in a new or existing helpers_* file:func (p *HelpersImpl) NewHelper(param1 string) (error) {
// ...
return nil
}