strip out sample bits (moved to demo repository)
|
@ -8,8 +8,7 @@
|
||||||
"linux-amd64": "server/dist/plugin-linux-amd64",
|
"linux-amd64": "server/dist/plugin-linux-amd64",
|
||||||
"darwin-amd64": "server/dist/plugin-darwin-amd64",
|
"darwin-amd64": "server/dist/plugin-darwin-amd64",
|
||||||
"windows-amd64": "server/dist/plugin-windows-amd64.exe"
|
"windows-amd64": "server/dist/plugin-windows-amd64.exe"
|
||||||
},
|
}
|
||||||
"executable": ""
|
|
||||||
},
|
},
|
||||||
"webapp": {
|
"webapp": {
|
||||||
"bundle_path": "webapp/dist/main.js"
|
"bundle_path": "webapp/dist/main.js"
|
||||||
|
|
100
server/README.md
|
@ -1,100 +0,0 @@
|
||||||
# Sample Plugin: Server
|
|
||||||
|
|
||||||
The server component of this sample plugin is written in Go and [net/rpc](https://golang.org/pkg/net/rpc/). It relies on a configured `ChannelName` and `Username` in [plugin.json](../plugin.json) to implement each of the supported hooks.
|
|
||||||
|
|
||||||
Each of the included files or folders is outlined below.
|
|
||||||
|
|
||||||
## [go.mod](go.mod), [go.sum](go.sum)
|
|
||||||
|
|
||||||
These are metadata files managed by [vgo](https://github.com/golang/vgo) for dependency management. While vgo is currently in beta, it will launch as part of the standard Go 1.11 tooling and stabilize in subsequent releases. It was preferred for this project over [dep](https://github.com/golang/dep) since it does not require locating your plugin in the `$GOPATH`.
|
|
||||||
|
|
||||||
## [main.go](main.go)
|
|
||||||
|
|
||||||
This is the entry point of your plugin binary, that in turn invokes [plugin.ClientMain](https://godoc.org/github.com/mattermost/mattermost-server/plugin#ClientMain) to wire up RPC communication between your plugin and the Mattermost Server.
|
|
||||||
|
|
||||||
## [plugin\_id.go](plugin_id.go)
|
|
||||||
|
|
||||||
This is a file generated by the [build/manifest](../build/manifest) tool that captures the plugin id from [plugin.json](../plugin.json). It simplifies the need to hard-code the plugin id in multiple places by exporting a constant for use instead.
|
|
||||||
|
|
||||||
## [plugin.go](plugin.go)
|
|
||||||
|
|
||||||
This file defines the `Plugin` struct, embedding [plugin.MattermostPlugin](https://godoc.org/github.com/mattermost/mattermost-server/plugin#MattermostPlugin) to automatically handle the wiring up the [API](https://godoc.org/github.com/mattermost/mattermost-server/plugin#API) when the plugin starts. It contains public fields that are automatically unmarshalled from [plugin.json](../plugin.json) as part of the `OnConfiguration` hook in [configuration.go](configuration.go).
|
|
||||||
|
|
||||||
## [activate\_hooks.go](activate_hooks.go)
|
|
||||||
|
|
||||||
### OnActivate
|
|
||||||
|
|
||||||
This sample implementation logs a message to the sample channel whenever the plugin is activated.
|
|
||||||
|
|
||||||
### OnDeactivate
|
|
||||||
|
|
||||||
This sample implementation logs a debug message to the server logs whenever the plugin is activated.
|
|
||||||
|
|
||||||
## [configuration.go](configuration.go)
|
|
||||||
|
|
||||||
### OnConfigurationChange
|
|
||||||
|
|
||||||
This sample implementation ensures the configured sample user and channel are created for use
|
|
||||||
by the plugin.
|
|
||||||
|
|
||||||
## [channel\_hooks.go](channel_hooks.go)
|
|
||||||
|
|
||||||
### ChannelHasBeenCreated
|
|
||||||
|
|
||||||
This sample implementation logs a message to the sample channel whenever a channel is created.
|
|
||||||
|
|
||||||
### UserHasJoinedChannel
|
|
||||||
|
|
||||||
This sample implementation logs a message to the sample channel whenever a user joins a channel.
|
|
||||||
|
|
||||||
### UserHasLeftChannel
|
|
||||||
|
|
||||||
This sample implementation logs a message to the sample channel whenever a user leaves a channel.
|
|
||||||
|
|
||||||
## [command\_hooks.go](command_hooks.go)
|
|
||||||
|
|
||||||
### ExecuteCommand
|
|
||||||
|
|
||||||
This sample implementation responds to a `/sample_plugin` command, allowing the user to enable
|
|
||||||
or disable the sample plugin's hooks functionality (but leave the command and webapp enabled).
|
|
||||||
|
|
||||||
## [http\_hooks.go](http_hooks.go)
|
|
||||||
|
|
||||||
### ServeHTTP
|
|
||||||
|
|
||||||
This sample implementation sends back whether or not the plugin hooks are currently enabled. It
|
|
||||||
is used by the web app to recover from a network reconnection and synchronize the state of the
|
|
||||||
plugin's hooks.
|
|
||||||
|
|
||||||
## [message\_hooks.go](message_hooks.go)
|
|
||||||
|
|
||||||
### MessageWillBePosted
|
|
||||||
|
|
||||||
This sample implementation rejects posts in the sample channel, as well as posts that @-mention
|
|
||||||
the sample plugin user.
|
|
||||||
|
|
||||||
### MessageWillBeUpdated
|
|
||||||
|
|
||||||
This sample implementation rejects posts that @-mention the sample plugin user.
|
|
||||||
|
|
||||||
### MessageHasBeenPosted
|
|
||||||
|
|
||||||
This sample implementation logs a message to the sample channel whenever a message is posted,
|
|
||||||
unless by the sample plugin user itself.
|
|
||||||
|
|
||||||
### MessageHasBeenUpdated
|
|
||||||
|
|
||||||
This sample implementation logs a message to the sample channel whenever a message is updated,
|
|
||||||
unless by the sample plugin user itself.
|
|
||||||
|
|
||||||
## [team\_hooks.go](team.go)
|
|
||||||
|
|
||||||
### UserHasJoinedTeam
|
|
||||||
|
|
||||||
This sample implementation logs a message to the sample channel in the team whenever a user
|
|
||||||
joins the team.
|
|
||||||
|
|
||||||
### UserHasLeftTeam
|
|
||||||
|
|
||||||
This sample implementation logs a message to the sample channel in the team whenever a user
|
|
||||||
leaves the team.
|
|
|
@ -1,70 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/model"
|
|
||||||
)
|
|
||||||
|
|
||||||
// OnActivate is invoked when the plugin is activated.
|
|
||||||
//
|
|
||||||
// This sample implementation logs a message to the sample channel whenever the plugin is
|
|
||||||
// activated.
|
|
||||||
func (p *Plugin) OnActivate() error {
|
|
||||||
// It's necessary to do this asynchronously, so as to avoid CreatePost triggering a call
|
|
||||||
// to MessageWillBePosted and deadlocking the plugin.
|
|
||||||
//
|
|
||||||
// See https://mattermost.atlassian.net/browse/MM-11431
|
|
||||||
go func() {
|
|
||||||
teams, err := p.API.GetTeams()
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to query teams OnActivate",
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, team := range teams {
|
|
||||||
if _, err := p.API.CreatePost(&model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: p.sampleChannelIds[team.Id],
|
|
||||||
Message: fmt.Sprintf(
|
|
||||||
"OnActivate: %s", PluginId,
|
|
||||||
),
|
|
||||||
Type: "custom_sample_plugin",
|
|
||||||
Props: map[string]interface{}{
|
|
||||||
"username": p.Username,
|
|
||||||
"channel_name": p.ChannelName,
|
|
||||||
},
|
|
||||||
}); err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to post OnActivate message",
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := p.registerCommand(team.Id); err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to register command",
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// This sample implementation logs a debug message to the server logs whenever the plugin is
|
|
||||||
// activated.
|
|
||||||
func (p *Plugin) OnDeactivate() error {
|
|
||||||
// Ideally, we'd post an on deactivate message like in OnActivate, but this is hampered by
|
|
||||||
// https://mattermost.atlassian.net/browse/MM-11431?filter=15018
|
|
||||||
p.API.LogDebug("OnDeactivate")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,98 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/model"
|
|
||||||
"github.com/mattermost/mattermost-server/plugin"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ChannelHasBeenCreated is invoked after the channel has been committed to the database.
|
|
||||||
//
|
|
||||||
// This sample implementation logs a message to the sample channel whenever a channel is created.
|
|
||||||
func (p *Plugin) ChannelHasBeenCreated(c *plugin.Context, channel *model.Channel) {
|
|
||||||
if p.disabled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := p.API.CreatePost(&model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: p.sampleChannelIds[channel.TeamId],
|
|
||||||
Message: fmt.Sprintf("ChannelHasBeenCreated: ~%s", channel.Name),
|
|
||||||
}); err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to post ChannelHasBeenCreated message",
|
|
||||||
"channel_id", channel.Id,
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// This sample implementation logs a message to the sample channel whenever a user joins a channel.
|
|
||||||
func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) {
|
|
||||||
if p.disabled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := p.API.GetUser(channelMember.UserId)
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError("failed to query user", "user_id", channelMember.UserId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
channel, err := p.API.GetChannel(channelMember.ChannelId)
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError("failed to query channel", "channel_id", channelMember.ChannelId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = p.API.CreatePost(&model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: p.sampleChannelIds[channel.TeamId],
|
|
||||||
Message: fmt.Sprintf("UserHasJoinedChannel: @%s, ~%s", user.Username, channel.Name),
|
|
||||||
}); err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to post UserHasJoinedChannel message",
|
|
||||||
"user_id", channelMember.UserId,
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// This sample implementation logs a message to the sample channel whenever a user leaves a
|
|
||||||
// channel.
|
|
||||||
func (p *Plugin) UserHasLeftChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) {
|
|
||||||
if p.disabled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := p.API.GetUser(channelMember.UserId)
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError("failed to query user", "user_id", channelMember.UserId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
channel, err := p.API.GetChannel(channelMember.ChannelId)
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError("failed to query channel", "channel_id", channelMember.ChannelId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = p.API.CreatePost(&model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: p.sampleChannelIds[channel.TeamId],
|
|
||||||
Message: fmt.Sprintf("UserHasLeftChannel: @%s, ~%s", user.Username, channel.Name),
|
|
||||||
}); err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to post UserHasLeftChannel message",
|
|
||||||
"user_id", channelMember.UserId,
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,88 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/model"
|
|
||||||
"github.com/mattermost/mattermost-server/plugin"
|
|
||||||
)
|
|
||||||
|
|
||||||
const CommandTrigger = "sample_plugin"
|
|
||||||
|
|
||||||
func (p *Plugin) registerCommand(teamId string) error {
|
|
||||||
if err := p.API.RegisterCommand(&model.Command{
|
|
||||||
TeamId: teamId,
|
|
||||||
Trigger: CommandTrigger,
|
|
||||||
AutoComplete: true,
|
|
||||||
AutoCompleteHint: "(true|false)",
|
|
||||||
AutoCompleteDesc: "Enables or disables the sample plugin hooks.",
|
|
||||||
DisplayName: "Sample Plugin Command",
|
|
||||||
Description: "A command used to enable or disable the sample plugin hooks.",
|
|
||||||
}); err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to register command",
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Plugin) emitStatusChange() {
|
|
||||||
p.API.PublishWebSocketEvent("status_change", map[string]interface{}{
|
|
||||||
"enabled": !p.disabled,
|
|
||||||
}, &model.WebsocketBroadcast{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExecuteCommand executes a command that has been previously registered via the RegisterCommand
|
|
||||||
// API.
|
|
||||||
//
|
|
||||||
// This sample implementation responds to a /sample_plugin command, allowing the user to enable
|
|
||||||
// or disable the sample plugin's hooks functionality (but leave the command and webapp enabled).
|
|
||||||
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
|
|
||||||
if !strings.HasPrefix(args.Command, "/"+CommandTrigger) {
|
|
||||||
return &model.CommandResponse{
|
|
||||||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
|
|
||||||
Text: fmt.Sprintf("Unknown command: " + args.Command),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasSuffix(args.Command, "true") {
|
|
||||||
if !p.disabled {
|
|
||||||
return &model.CommandResponse{
|
|
||||||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
|
|
||||||
Text: "The sample plugin hooks are already enabled.",
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
p.disabled = false
|
|
||||||
p.emitStatusChange()
|
|
||||||
|
|
||||||
return &model.CommandResponse{
|
|
||||||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
|
|
||||||
Text: "Enabled sample plugin hooks.",
|
|
||||||
}, nil
|
|
||||||
|
|
||||||
} else if strings.HasSuffix(args.Command, "false") {
|
|
||||||
if p.disabled {
|
|
||||||
return &model.CommandResponse{
|
|
||||||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
|
|
||||||
Text: "The sample plugin hooks are already disabled.",
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
p.disabled = true
|
|
||||||
p.emitStatusChange()
|
|
||||||
|
|
||||||
return &model.CommandResponse{
|
|
||||||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
|
|
||||||
Text: "Disabled sample plugin hooks.",
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return &model.CommandResponse{
|
|
||||||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
|
|
||||||
Text: fmt.Sprintf("Unknown command action: " + args.Command),
|
|
||||||
}, nil
|
|
||||||
}
|
|
|
@ -1,112 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/model"
|
|
||||||
)
|
|
||||||
|
|
||||||
// OnConfigurationChange is invoked when configuration changes may have been made.
|
|
||||||
//
|
|
||||||
// This sample implementation ensures the configured sample user and channel are created for use
|
|
||||||
// by the plugin.
|
|
||||||
func (p *Plugin) OnConfigurationChange() error {
|
|
||||||
// Leverage the default implementation on the embedded plugin.Mattermost. This
|
|
||||||
// automatically attempts to unmarshal the plugin config block of the server's
|
|
||||||
// configuration onto the public members of Plugin, such as Username and ChannelName.
|
|
||||||
//
|
|
||||||
// Feel free to skip this and implement your own handler if you have more complex needs.
|
|
||||||
if err := p.MattermostPlugin.OnConfigurationChange(); err != nil {
|
|
||||||
p.API.LogError(err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := p.ensureSampleUser(); err != nil {
|
|
||||||
p.API.LogError(err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := p.ensureSampleChannels(); err != nil {
|
|
||||||
p.API.LogError(err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Plugin) ensureSampleUser() *model.AppError {
|
|
||||||
var err *model.AppError
|
|
||||||
|
|
||||||
// Check for the configured user. Ignore any error, since it's hard to distinguish runtime
|
|
||||||
// errors from a user simply not existing.
|
|
||||||
user, _ := p.API.GetUserByUsername(p.Username)
|
|
||||||
|
|
||||||
// Ensure the configured user exists.
|
|
||||||
if user == nil {
|
|
||||||
user, err = p.API.CreateUser(&model.User{
|
|
||||||
Username: p.Username,
|
|
||||||
Password: "sample",
|
|
||||||
// AuthData *string `json:"auth_data,omitempty"`
|
|
||||||
// AuthService string `json:"auth_service"`
|
|
||||||
Email: fmt.Sprintf("%s@example.com", p.Username),
|
|
||||||
Nickname: "Sam",
|
|
||||||
FirstName: "Sample",
|
|
||||||
LastName: "Plugin User",
|
|
||||||
Position: "Bot",
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
teams, err := p.API.GetTeams()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, team := range teams {
|
|
||||||
// Ignore any error.
|
|
||||||
p.API.CreateTeamMember(team.Id, p.sampleUserId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the id for later use.
|
|
||||||
p.sampleUserId = user.Id
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Plugin) ensureSampleChannels() *model.AppError {
|
|
||||||
teams, err := p.API.GetTeams()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
p.sampleChannelIds = make(map[string]string)
|
|
||||||
for _, team := range teams {
|
|
||||||
// Check for the configured channel. Ignore any error, since it's hard to
|
|
||||||
// distinguish runtime errors from a channel simply not existing.
|
|
||||||
channel, _ := p.API.GetChannelByNameForTeamName(team.Name, p.ChannelName)
|
|
||||||
|
|
||||||
// Ensure the configured channel exists.
|
|
||||||
if channel == nil {
|
|
||||||
channel, err = p.API.CreateChannel(&model.Channel{
|
|
||||||
TeamId: team.Id,
|
|
||||||
Type: model.CHANNEL_OPEN,
|
|
||||||
DisplayName: "Sample Plugin",
|
|
||||||
Name: p.ChannelName,
|
|
||||||
Header: "The channel used by the sample plugin.",
|
|
||||||
Purpose: "This channel was created by a plugin for testing.",
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the ids for later use.
|
|
||||||
p.sampleChannelIds[team.Id] = channel.Id
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,29 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/plugin"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// This sample implementation sends back whether or not the plugin hooks are currently enabled. It
|
|
||||||
// is used by the web app to recover from a network reconnection and synchronize the state of the
|
|
||||||
// plugin's hooks.
|
|
||||||
func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
|
|
||||||
var response = struct {
|
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
}{
|
|
||||||
Enabled: !p.disabled,
|
|
||||||
}
|
|
||||||
|
|
||||||
responseJSON, _ := json.Marshal(response)
|
|
||||||
|
|
||||||
w.Write(responseJSON)
|
|
||||||
}
|
|
|
@ -1,180 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/model"
|
|
||||||
"github.com/mattermost/mattermost-server/plugin"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// This sample implementation rejects posts in the sample channel, as well as posts that @-mention
|
|
||||||
// the sample plugin user.
|
|
||||||
func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
|
||||||
if p.disabled {
|
|
||||||
return post, ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always allow posts by the sample plugin user.
|
|
||||||
if post.UserId == p.sampleUserId {
|
|
||||||
return post, ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reject posts by other users in the sample channels, effectively making it read-only.
|
|
||||||
for _, channelId := range p.sampleChannelIds {
|
|
||||||
if channelId == post.ChannelId {
|
|
||||||
p.API.SendEphemeralPost(post.UserId, &model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: channelId,
|
|
||||||
Message: "Posting is not allowed in this channel.",
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil, "disallowing post in sample channel"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reject posts mentioning the sample plugin user.
|
|
||||||
if strings.Contains(post.Message, fmt.Sprintf("@%s", p.Username)) {
|
|
||||||
p.API.SendEphemeralPost(post.UserId, &model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: post.ChannelId,
|
|
||||||
Message: "You must not talk about the sample plugin user.",
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil, "disallowing mention of sample plugin user"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, allow the post through.
|
|
||||||
return post, ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// This sample implementation rejects posts that @-mention the sample plugin user.
|
|
||||||
func (p *Plugin) MessageWillBeUpdated(c *plugin.Context, newPost, oldPost *model.Post) (*model.Post, string) {
|
|
||||||
if p.disabled {
|
|
||||||
return newPost, ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reject posts mentioning the sample plugin user.
|
|
||||||
if strings.Contains(newPost.Message, fmt.Sprintf("@%s", p.Username)) {
|
|
||||||
p.API.SendEphemeralPost(newPost.UserId, &model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: newPost.ChannelId,
|
|
||||||
Message: "You must not talk about the sample plugin user.",
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil, "disallowing mention of sample plugin user"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, allow the post through.
|
|
||||||
return newPost, ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// This sample implementation logs a message to the sample channel whenever a message is posted,
|
|
||||||
// unless by the sample plugin user itself.
|
|
||||||
func (p *Plugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {
|
|
||||||
if p.disabled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore posts by the sample plugin user.
|
|
||||||
if post.UserId == p.sampleUserId {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := p.API.GetUser(post.UserId)
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError("failed to query user", "user_id", post.UserId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
channel, err := p.API.GetChannel(post.ChannelId)
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError("failed to query channel", "channel_id", post.ChannelId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := p.API.CreatePost(&model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: p.sampleChannelIds[channel.TeamId],
|
|
||||||
Message: fmt.Sprintf(
|
|
||||||
"MessageHasBeenPosted in ~%s by @%s",
|
|
||||||
channel.Name,
|
|
||||||
user.Username,
|
|
||||||
),
|
|
||||||
}); err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to post MessageHasBeenPosted message",
|
|
||||||
"channel_id", channel.Id,
|
|
||||||
"user_id", user.Id,
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// This sample implementation logs a message to the sample channel whenever a message is updated,
|
|
||||||
// unless by the sample plugin user itself.
|
|
||||||
func (p *Plugin) MessageHasBeenUpdated(c *plugin.Context, newPost, oldPost *model.Post) {
|
|
||||||
if p.disabled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore updates by the sample plugin user.
|
|
||||||
if newPost.UserId == p.sampleUserId {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := p.API.GetUser(newPost.UserId)
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError("failed to query user", "user_id", newPost.UserId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
channel, err := p.API.GetChannel(newPost.ChannelId)
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError("failed to query channel", "channel_id", newPost.ChannelId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := p.API.CreatePost(&model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: p.sampleChannelIds[channel.TeamId],
|
|
||||||
Message: fmt.Sprintf(
|
|
||||||
"MessageHasBeenUpdated in ~%s by @%s",
|
|
||||||
channel.Name,
|
|
||||||
user.Username,
|
|
||||||
),
|
|
||||||
}); err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to post MessageHasBeenUpdated message",
|
|
||||||
"channel_id", channel.Id,
|
|
||||||
"user_id", user.Id,
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -6,19 +6,6 @@ import (
|
||||||
|
|
||||||
type Plugin struct {
|
type Plugin struct {
|
||||||
plugin.MattermostPlugin
|
plugin.MattermostPlugin
|
||||||
|
|
||||||
// The user to use as part of the sample plugin, created automatically if it does not exist.
|
|
||||||
Username string
|
|
||||||
|
|
||||||
// The channel to use as part of the sample plugin, created for each team automatically if it does not exist.
|
|
||||||
ChannelName string
|
|
||||||
|
|
||||||
// disabled tracks whether or not the plugin has been disabled after activation. It always starts enabled.
|
|
||||||
disabled bool
|
|
||||||
|
|
||||||
// sampleUserId is the id of the user specified above.
|
|
||||||
sampleUserId string
|
|
||||||
|
|
||||||
// sampleChannelIds maps team ids to the channels created for each using the channel name above.
|
|
||||||
sampleChannelIds map[string]string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See https://developers.mattermost.com/extend/plugins/server/reference/
|
||||||
|
|
|
@ -1,66 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/model"
|
|
||||||
"github.com/mattermost/mattermost-server/plugin"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// This sample implementation logs a message to the sample channel in the team whenever a user
|
|
||||||
// joins the team.
|
|
||||||
func (p *Plugin) UserHasJoinedTeam(c *plugin.Context, teamMember *model.TeamMember, actor *model.User) {
|
|
||||||
if p.disabled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := p.API.GetUser(teamMember.UserId)
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError("failed to query user", "user_id", teamMember.UserId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = p.API.CreatePost(&model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: p.sampleChannelIds[teamMember.TeamId],
|
|
||||||
Message: fmt.Sprintf("UserHasJoinedTeam: @%s", user.Username),
|
|
||||||
}); err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to post UserHasJoinedTeam message",
|
|
||||||
"user_id", teamMember.UserId,
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// This sample implementation logs a message to the sample channel in the team whenever a user
|
|
||||||
// leaves the team.
|
|
||||||
func (p *Plugin) UserHasLeftTeam(c *plugin.Context, teamMember *model.TeamMember, actor *model.User) {
|
|
||||||
if p.disabled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := p.API.GetUser(teamMember.UserId)
|
|
||||||
if err != nil {
|
|
||||||
p.API.LogError("failed to query user", "user_id", teamMember.UserId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = p.API.CreatePost(&model.Post{
|
|
||||||
UserId: p.sampleUserId,
|
|
||||||
ChannelId: p.sampleChannelIds[teamMember.TeamId],
|
|
||||||
Message: fmt.Sprintf("UserHasLeftTeam: @%s", user.Username),
|
|
||||||
}); err != nil {
|
|
||||||
p.API.LogError(
|
|
||||||
"failed to post UserHasLeftTeam message",
|
|
||||||
"user_id", teamMember.UserId,
|
|
||||||
"error", err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
105
webapp/README.md
|
@ -1,105 +0,0 @@
|
||||||
# Sample Plugin: Web App
|
|
||||||
|
|
||||||
The web app component of this sample plugin is written in Javascript, and leverages [React](https://reactjs.org/) and [Redux](https://redux.js.org/). It registers a component type for all of the supported registration calls, parses a custom webhook to detect when the server plugin's hooks status changes, and pings the server on network reconnect to synchronize state.
|
|
||||||
|
|
||||||
Each of the included files or folders is outlined below.
|
|
||||||
|
|
||||||
## [package.json](package.json)
|
|
||||||
|
|
||||||
See the NPM documentation on [package.json](https://docs.npmjs.com/files/package.json). It defines a `build` script to invoke webpack and generate a bundle, a `lint` script to run the `src/` directory through the [eslint](https://eslint.org/) checker, and a `fix` script that both lints and automatically tries to fix issues.
|
|
||||||
|
|
||||||
## [package-lock.json](package-lock.json)
|
|
||||||
|
|
||||||
See the NPM documentation on [package-lock.json](https://docs.npmjs.com/files/package-lock.json).
|
|
||||||
|
|
||||||
## [webpack.config.js](webpack.config.js)
|
|
||||||
|
|
||||||
See the Webpack documentation on [configuration](https://webpack.js.org/configuration/). Notably, this configuration specifies external dependencies on React, Redux and React Redux to avoid bundling these libraries and duplicating the versions already part of the Mattermost Web App.
|
|
||||||
|
|
||||||
## [.eslintrc.json](.eslintrc.json)
|
|
||||||
|
|
||||||
This defines rules to configure [eslint](https://eslint.org/) as part of invoking the `lint` and `fix` scripts. The styles are based on the rules used by the Mattermost Webapp.
|
|
||||||
|
|
||||||
## [node\_modules](node_modules)
|
|
||||||
|
|
||||||
This is the [location](https://docs.npmjs.com/files/folders#node-modules) in which [npm](https://www.npmjs.com/) installs any necessary Javascript dependencies.
|
|
||||||
|
|
||||||
## [src/index.js](src/index.js)
|
|
||||||
|
|
||||||
This is the entry point of the web app. When the plugin is loaded, this file is executed, registering the plugin with the Mattermost Webapp.
|
|
||||||
|
|
||||||
## [src/plugin\_id.js](src/plugin_id.js)
|
|
||||||
|
|
||||||
This is a file generated by the [build/manifest](../build/manifest) tool that captures the plugin id from [plugin.json](../plugin.json). It simplifies the need to hard-code the plugin id in multiple places by exporting a constant for use instead.
|
|
||||||
|
|
||||||
## [src/plugin.jsx](src/plugin.jsx)
|
|
||||||
|
|
||||||
This defines the Plugin class requires by the Mattermost Webapp, registering all the components and callbacks used by the plugin on `initialize` and logging a console message on `uninitialize`.
|
|
||||||
|
|
||||||
## [src/reducer.js](src/reducer.js)
|
|
||||||
|
|
||||||
This exports a [reducer](https://redux.js.org/basics/reducers) tracking the plugin hook's status. It is part of the global state of the Mattermost Webapp, and accessible at `store['plugins' + PluginId]`.
|
|
||||||
|
|
||||||
## [src/selectors.js](src/selectors.js)
|
|
||||||
|
|
||||||
This defines selectors into the Redux state managed by the plugin to determine if the plugin is enabled or disabled.
|
|
||||||
|
|
||||||
## [src/action\_types.js](src/action_types.js)
|
|
||||||
|
|
||||||
This exports constants used by the Redux [actions](https://redux.js.org/basics/actions) in [action\_types.js](src/action_types.js). It's important to namespace any action types to avoid unintentional collisions with action types from the Mattermost Webapp or other plugins.
|
|
||||||
|
|
||||||
## [src/actions.js](src/actions.js)
|
|
||||||
|
|
||||||
This exports Redux [actions](https://redux.js.org/basics/actions) for triggering the root component, as well as querying the server for the current plugin hooks status and responding to websocket events emitted by the server for the plugin.
|
|
||||||
|
|
||||||
## [components](components)
|
|
||||||
|
|
||||||
This folder exports a number of components illustrating plugin functionality.
|
|
||||||
|
|
||||||
## Root
|
|
||||||
|
|
||||||
This plugin registers a modal-like root component that displays above all other components, and is triggered by interacting with other plugin components on the page:
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## User Attributes
|
|
||||||
|
|
||||||
This plugin registers a user attributes components displaying a static string:
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## User Actions
|
|
||||||
|
|
||||||
This plugin registers a user actions components displaying a static string followed by a simple `<button>` that triggers the root component:
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Left Sidebar Header
|
|
||||||
|
|
||||||
This plugin registers a left sidebar header component displaying the current status of the plugin hooks:
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Bottom Team Sidebar
|
|
||||||
|
|
||||||
This plugin registers a bottom team sidebar component displaying a plugin icon:
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Channel Header Button Action
|
|
||||||
|
|
||||||
This plugin registers a channel header button action displaying a plugin icon that, when clicked, triggers the root component:
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Main Menu Action
|
|
||||||
|
|
||||||
This plugin registers a main menu action that, when clicked, triggers the root component:
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
# Post Type
|
|
||||||
|
|
||||||
This plugin renders a custom post type as part of handling the `OnActivate` hook in the server, dumping the current plugin configuration:
|
|
||||||
|
|
||||||

|
|
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 23 KiB |
Before Width: | Height: | Size: 64 KiB |
Before Width: | Height: | Size: 31 KiB |
Before Width: | Height: | Size: 38 KiB |
Before Width: | Height: | Size: 49 KiB |
Before Width: | Height: | Size: 49 KiB |
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "webapp",
|
"name": "webapp",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"description": "This plugin serves as a reference guide for best practices and build scripts when writing Mattermost plugins.",
|
"description": "This plugin serves as a starting point for writing a Mattermost plugin.",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "webpack --mode=production",
|
"build": "webpack --mode=production",
|
||||||
|
@ -9,7 +9,7 @@
|
||||||
"fix": "eslint --ignore-pattern node_modules --ignore-pattern dist --ext .js --ext .jsx . --quiet --fix"
|
"fix": "eslint --ignore-pattern node_modules --ignore-pattern dist --ext .js --ext .jsx . --quiet --fix"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babel-core": "^6.26.3",
|
"babel-core": "^6.26.3",
|
||||||
"babel-eslint": "^8.2.6",
|
"babel-eslint": "^8.2.6",
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
import PluginId from './plugin_id';
|
|
||||||
|
|
||||||
// Namespace your actions to avoid collisions.
|
|
||||||
export const STATUS_CHANGE = PluginId + '_status_change';
|
|
||||||
|
|
||||||
export const OPEN_ROOT_MODAL = PluginId + '_open_root_modal';
|
|
||||||
export const CLOSE_ROOT_MODAL = PluginId + '_close_root_modal';
|
|
|
@ -1,31 +0,0 @@
|
||||||
import PluginId from './plugin_id';
|
|
||||||
import {STATUS_CHANGE, OPEN_ROOT_MODAL, CLOSE_ROOT_MODAL} from './action_types';
|
|
||||||
|
|
||||||
export const openRootModal = () => (dispatch) => {
|
|
||||||
dispatch({
|
|
||||||
type: OPEN_ROOT_MODAL,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const closeRootModal = () => (dispatch) => {
|
|
||||||
dispatch({
|
|
||||||
type: CLOSE_ROOT_MODAL,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const mainMenuAction = openRootModal;
|
|
||||||
export const channelHeaderButtonAction = openRootModal;
|
|
||||||
|
|
||||||
export const getStatus = () => (dispatch) => {
|
|
||||||
fetch('/plugins/' + PluginId + '/').then((r) => r.json()).then((r) => {
|
|
||||||
dispatch({
|
|
||||||
type: STATUS_CHANGE,
|
|
||||||
data: r.enabled,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const websocketStatusChange = (message) => (dispatch) => dispatch({
|
|
||||||
type: STATUS_CHANGE,
|
|
||||||
data: message.data.enabled,
|
|
||||||
});
|
|
|
@ -1,10 +0,0 @@
|
||||||
import React from 'react';
|
|
||||||
|
|
||||||
const BottomTeamSidebarComponent = () => (
|
|
||||||
<i
|
|
||||||
className='icon fa fa-plug'
|
|
||||||
style={{color: 'white'}}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
export default BottomTeamSidebarComponent;
|
|
|
@ -1,9 +0,0 @@
|
||||||
import React from 'react';
|
|
||||||
|
|
||||||
export const MainMenuMobileIcon = () => (
|
|
||||||
<i className='icon fa fa-plug'/>
|
|
||||||
);
|
|
||||||
|
|
||||||
export const ChannelHeaderButtonIcon = () => (
|
|
||||||
<i className='icon fa fa-plug'/>
|
|
||||||
);
|
|
|
@ -1,11 +0,0 @@
|
||||||
import {connect} from 'react-redux';
|
|
||||||
|
|
||||||
import {isEnabled} from 'selectors';
|
|
||||||
|
|
||||||
import LeftSidebarHeader from './left_sidebar_header';
|
|
||||||
|
|
||||||
const mapStateToProps = (state) => ({
|
|
||||||
enabled: isEnabled(state),
|
|
||||||
});
|
|
||||||
|
|
||||||
export default connect(mapStateToProps)(LeftSidebarHeader);
|
|
|
@ -1,32 +0,0 @@
|
||||||
import React from 'react';
|
|
||||||
import PropTypes from 'prop-types';
|
|
||||||
|
|
||||||
// LeftSidebarHeader is a pure component, later connected to the Redux store so as to
|
|
||||||
// show the plugin's enabled / disabled status.
|
|
||||||
export default class LeftSidebarHeader extends React.PureComponent {
|
|
||||||
static propTypes = {
|
|
||||||
enabled: PropTypes.bool.isRequired,
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
const iconStyle = {
|
|
||||||
display: 'inline-block',
|
|
||||||
margin: '0 7px 0 1px',
|
|
||||||
};
|
|
||||||
const style = {
|
|
||||||
margin: '.5em 0 .5em',
|
|
||||||
padding: '0 12px 0 15px',
|
|
||||||
color: 'rgba(255,255,255,0.6)',
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div style={style}>
|
|
||||||
<i
|
|
||||||
className='icon fa fa-plug'
|
|
||||||
style={iconStyle}
|
|
||||||
/>
|
|
||||||
{'Sample Plugin: '} {this.props.enabled ? <span>{ 'Enabled' }</span> : <span>{ 'Disabled' }</span>}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,36 +0,0 @@
|
||||||
import React from 'react';
|
|
||||||
import PropTypes from 'prop-types';
|
|
||||||
|
|
||||||
const {formatText, messageHtmlToComponent} = window.PostUtils;
|
|
||||||
|
|
||||||
export default class PostType extends React.PureComponent {
|
|
||||||
static propTypes = {
|
|
||||||
post: PropTypes.object.isRequired,
|
|
||||||
theme: PropTypes.object.isRequired,
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
const style = getStyle(this.props.theme);
|
|
||||||
const post = {...this.props.post};
|
|
||||||
const message = post.message || '';
|
|
||||||
|
|
||||||
const formattedText = messageHtmlToComponent(formatText(message));
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
{formattedText}
|
|
||||||
<pre style={style.configuration}>
|
|
||||||
{JSON.stringify(post.props, null, 4)}
|
|
||||||
</pre>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const getStyle = (theme) => ({
|
|
||||||
configuration: {
|
|
||||||
padding: '1em',
|
|
||||||
color: theme.centerChannelBg,
|
|
||||||
backgroundColor: theme.centerChannelColor,
|
|
||||||
},
|
|
||||||
});
|
|
|
@ -1,17 +0,0 @@
|
||||||
import {connect} from 'react-redux';
|
|
||||||
import {bindActionCreators} from 'redux';
|
|
||||||
|
|
||||||
import {closeRootModal} from 'actions';
|
|
||||||
import {isRootModalVisible} from 'selectors';
|
|
||||||
|
|
||||||
import Root from './root';
|
|
||||||
|
|
||||||
const mapStateToProps = (state) => ({
|
|
||||||
visible: isRootModalVisible(state),
|
|
||||||
});
|
|
||||||
|
|
||||||
const mapDispatchToProps = (dispatch) => bindActionCreators({
|
|
||||||
close: closeRootModal,
|
|
||||||
}, dispatch);
|
|
||||||
|
|
||||||
export default connect(mapStateToProps, mapDispatchToProps)(Root);
|
|
|
@ -1,54 +0,0 @@
|
||||||
import React from 'react';
|
|
||||||
import PropTypes from 'prop-types';
|
|
||||||
|
|
||||||
const Root = ({visible, close, theme}) => {
|
|
||||||
if (!visible) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const style = getStyle(theme);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
style={style.backdrop}
|
|
||||||
onClick={close}
|
|
||||||
>
|
|
||||||
<div style={style.modal}>
|
|
||||||
{ 'You have triggered the root component of the sample plugin.' }
|
|
||||||
<br/>
|
|
||||||
<br/>
|
|
||||||
{ 'Click anywhere to close.' }
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
Root.propTypes = {
|
|
||||||
visible: PropTypes.bool.isRequired,
|
|
||||||
close: PropTypes.func.isRequired,
|
|
||||||
theme: PropTypes.object.isRequired,
|
|
||||||
};
|
|
||||||
|
|
||||||
const getStyle = (theme) => ({
|
|
||||||
backdrop: {
|
|
||||||
position: 'absolute',
|
|
||||||
display: 'flex',
|
|
||||||
top: 0,
|
|
||||||
left: 0,
|
|
||||||
right: 0,
|
|
||||||
bottom: 0,
|
|
||||||
backgroundColor: 'rgba(0, 0, 0, 0.50)',
|
|
||||||
zIndex: 2000,
|
|
||||||
alignItems: 'center',
|
|
||||||
justifyContent: 'center',
|
|
||||||
},
|
|
||||||
modal: {
|
|
||||||
height: '250px',
|
|
||||||
width: '400px',
|
|
||||||
padding: '1em',
|
|
||||||
color: theme.centerChannelColor,
|
|
||||||
backgroundColor: theme.centerChannelBg,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export default Root;
|
|
|
@ -1,12 +0,0 @@
|
||||||
import {connect} from 'react-redux';
|
|
||||||
import {bindActionCreators} from 'redux';
|
|
||||||
|
|
||||||
import {openRootModal} from 'actions';
|
|
||||||
|
|
||||||
import UserActions from './user_actions';
|
|
||||||
|
|
||||||
const mapDispatchToProps = (dispatch) => bindActionCreators({
|
|
||||||
openRootModal,
|
|
||||||
}, dispatch);
|
|
||||||
|
|
||||||
export default connect(null, mapDispatchToProps)(UserActions);
|
|
|
@ -1,36 +0,0 @@
|
||||||
import React from 'react';
|
|
||||||
import PropTypes from 'prop-types';
|
|
||||||
|
|
||||||
export default class UserActionsComponent extends React.PureComponent {
|
|
||||||
static propTypes = {
|
|
||||||
openRootModal: PropTypes.func.isRequired,
|
|
||||||
theme: PropTypes.object.isRequired,
|
|
||||||
}
|
|
||||||
|
|
||||||
onClick = () => {
|
|
||||||
this.props.openRootModal();
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
const style = getStyle(this.props.theme);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
{ 'Sample Plugin: '}
|
|
||||||
<button
|
|
||||||
style={style.button}
|
|
||||||
onClick={this.onClick}
|
|
||||||
>
|
|
||||||
{'Action'}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const getStyle = (theme) => ({
|
|
||||||
button: {
|
|
||||||
color: theme.buttonColor,
|
|
||||||
backgroundColor: theme.buttonBg,
|
|
||||||
},
|
|
||||||
});
|
|
|
@ -1,10 +0,0 @@
|
||||||
import React from 'react';
|
|
||||||
|
|
||||||
export default class UserAttributes extends React.PureComponent {
|
|
||||||
render() {
|
|
||||||
return (
|
|
||||||
<div>{'Sample Plugin: User Attributes'}</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
import Plugin from './plugin';
|
|
||||||
import PluginId from './plugin_id';
|
import PluginId from './plugin_id';
|
||||||
|
|
||||||
|
export default class Plugin {
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
initialize(registry, store) {
|
||||||
|
// @see https://developers.mattermost.com/extend/plugins/webapp/reference/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
window.registerPlugin(PluginId, new Plugin());
|
window.registerPlugin(PluginId, new Plugin());
|
||||||
|
|
|
@ -1,69 +0,0 @@
|
||||||
import React from 'react';
|
|
||||||
|
|
||||||
import PluginId from './plugin_id';
|
|
||||||
|
|
||||||
import Root from './components/root';
|
|
||||||
import BottomTeamSidebar from './components/bottom_team_sidebar';
|
|
||||||
import LeftSidebarHeader from './components/left_sidebar_header';
|
|
||||||
import UserAttributes from './components/user_attributes';
|
|
||||||
import UserActions from './components/user_actions';
|
|
||||||
import PostType from './components/post_type';
|
|
||||||
import {
|
|
||||||
MainMenuMobileIcon,
|
|
||||||
ChannelHeaderButtonIcon,
|
|
||||||
} from './components/icons';
|
|
||||||
import {
|
|
||||||
mainMenuAction,
|
|
||||||
channelHeaderButtonAction,
|
|
||||||
websocketStatusChange,
|
|
||||||
getStatus,
|
|
||||||
} from './actions';
|
|
||||||
import reducer from './reducer';
|
|
||||||
|
|
||||||
export default class SamplePlugin {
|
|
||||||
initialize(registry, store) {
|
|
||||||
registry.registerRootComponent(Root);
|
|
||||||
registry.registerPopoverUserAttributesComponent(UserAttributes);
|
|
||||||
registry.registerPopoverUserActionsComponent(UserActions);
|
|
||||||
registry.registerLeftSidebarHeaderComponent(LeftSidebarHeader);
|
|
||||||
registry.registerBottomTeamSidebarComponent(
|
|
||||||
BottomTeamSidebar,
|
|
||||||
);
|
|
||||||
|
|
||||||
registry.registerChannelHeaderButtonAction(
|
|
||||||
<ChannelHeaderButtonIcon/>,
|
|
||||||
() => store.dispatch(channelHeaderButtonAction()),
|
|
||||||
'Sample Plugin',
|
|
||||||
);
|
|
||||||
|
|
||||||
registry.registerPostTypeComponent('custom_sample_plugin', PostType);
|
|
||||||
|
|
||||||
registry.registerMainMenuAction(
|
|
||||||
'Sample Plugin',
|
|
||||||
() => store.dispatch(mainMenuAction()),
|
|
||||||
<MainMenuMobileIcon/>,
|
|
||||||
);
|
|
||||||
|
|
||||||
registry.registerWebSocketEventHandler(
|
|
||||||
'custom_' + PluginId + '_status_change',
|
|
||||||
(message) => {
|
|
||||||
store.dispatch(websocketStatusChange(message));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
registry.registerReducer(reducer);
|
|
||||||
|
|
||||||
// Immediately fetch the current plugin status.
|
|
||||||
store.dispatch(getStatus());
|
|
||||||
|
|
||||||
// Fetch the current status whenever we recover an internet connection.
|
|
||||||
registry.registerReconnectHandler(() => {
|
|
||||||
store.dispatch(getStatus());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
uninitialize() {
|
|
||||||
//eslint-disable-next-line no-console
|
|
||||||
console.log(PluginId + '::uninitialize()');
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,30 +0,0 @@
|
||||||
import {combineReducers} from 'redux';
|
|
||||||
|
|
||||||
import {STATUS_CHANGE, OPEN_ROOT_MODAL, CLOSE_ROOT_MODAL} from './action_types';
|
|
||||||
|
|
||||||
const enabled = (state = false, action) => {
|
|
||||||
switch (action.type) {
|
|
||||||
case STATUS_CHANGE:
|
|
||||||
return action.data;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const rootModalVisible = (state = false, action) => {
|
|
||||||
switch (action.type) {
|
|
||||||
case OPEN_ROOT_MODAL:
|
|
||||||
return true;
|
|
||||||
case CLOSE_ROOT_MODAL:
|
|
||||||
return false;
|
|
||||||
default:
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export default combineReducers({
|
|
||||||
enabled,
|
|
||||||
rootModalVisible,
|
|
||||||
});
|
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
import PluginId from './plugin_id';
|
|
||||||
|
|
||||||
const getPluginState = (state) => state['plugins-' + PluginId] || {};
|
|
||||||
|
|
||||||
export const isEnabled = (state) => getPluginState(state).enabled;
|
|
||||||
|
|
||||||
export const isRootModalVisible = (state) => getPluginState(state).rootModalVisible;
|
|