2019-11-29 06:59:40 -05:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
package plugin_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
2018-10-03 13:13:19 -04:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-07-13 10:29:50 -04:00
|
|
|
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
|
|
|
"github.com/mattermost/mattermost/server/public/plugin"
|
2018-07-13 10:29:50 -04:00
|
|
|
)
|
|
|
|
|
|
2018-10-03 13:13:19 -04:00
|
|
|
// configuration represents the configuration for this plugin as exposed via the Mattermost
|
|
|
|
|
// server configuration.
|
|
|
|
|
type configuration struct {
|
2018-07-13 10:29:50 -04:00
|
|
|
TeamName string
|
|
|
|
|
ChannelName string
|
|
|
|
|
|
2019-12-05 14:31:53 -05:00
|
|
|
// channelID is resolved when the public configuration fields above change
|
|
|
|
|
channelID string
|
2018-07-13 10:29:50 -04:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 14:22:48 -05:00
|
|
|
// HelpPlugin implements the interface expected by the Mattermost server to communicate
|
|
|
|
|
// between the server and plugin processes.
|
2018-10-03 13:13:19 -04:00
|
|
|
type HelpPlugin struct {
|
|
|
|
|
plugin.MattermostPlugin
|
|
|
|
|
|
|
|
|
|
// configurationLock synchronizes access to the configuration.
|
|
|
|
|
configurationLock sync.RWMutex
|
|
|
|
|
|
|
|
|
|
// configuration is the active plugin configuration. Consult getConfiguration and
|
|
|
|
|
// setConfiguration for usage.
|
|
|
|
|
configuration *configuration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getConfiguration retrieves the active configuration under lock, making it safe to use
|
|
|
|
|
// concurrently. The active configuration may change underneath the client of this method, but
|
|
|
|
|
// the struct returned by this API call is considered immutable.
|
|
|
|
|
func (p *HelpPlugin) getConfiguration() *configuration {
|
|
|
|
|
p.configurationLock.RLock()
|
|
|
|
|
defer p.configurationLock.RUnlock()
|
|
|
|
|
|
|
|
|
|
if p.configuration == nil {
|
|
|
|
|
return &configuration{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return p.configuration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setConfiguration replaces the active configuration under lock.
|
|
|
|
|
//
|
|
|
|
|
// Do not call setConfiguration while holding the configurationLock, as sync.Mutex is not
|
|
|
|
|
// reentrant.
|
|
|
|
|
func (p *HelpPlugin) setConfiguration(configuration *configuration) {
|
|
|
|
|
// Replace the active configuration under lock.
|
|
|
|
|
p.configurationLock.Lock()
|
|
|
|
|
defer p.configurationLock.Unlock()
|
|
|
|
|
p.configuration = configuration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OnConfigurationChange updates the active configuration for this plugin under lock.
|
2018-07-13 10:29:50 -04:00
|
|
|
func (p *HelpPlugin) OnConfigurationChange() error {
|
2018-10-03 13:13:19 -04:00
|
|
|
var configuration = new(configuration)
|
|
|
|
|
|
|
|
|
|
// Load the public configuration fields from the Mattermost server configuration.
|
|
|
|
|
if err := p.API.LoadPluginConfiguration(configuration); err != nil {
|
|
|
|
|
return errors.Wrap(err, "failed to load plugin configuration")
|
2018-07-13 10:29:50 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-03 13:13:19 -04:00
|
|
|
team, err := p.API.GetTeamByName(configuration.TeamName)
|
2018-07-13 10:29:50 -04:00
|
|
|
if err != nil {
|
2018-10-03 13:13:19 -04:00
|
|
|
return errors.Wrapf(err, "failed to find team %s", configuration.TeamName)
|
2018-07-13 10:29:50 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-23 17:07:33 -05:00
|
|
|
channel, err := p.API.GetChannelByName(team.Id, configuration.ChannelName, false)
|
2018-07-13 10:29:50 -04:00
|
|
|
if err != nil {
|
2018-10-03 13:13:19 -04:00
|
|
|
return errors.Wrapf(err, "failed to find channel %s", configuration.ChannelName)
|
2018-07-13 10:29:50 -04:00
|
|
|
}
|
|
|
|
|
|
2019-12-05 14:31:53 -05:00
|
|
|
configuration.channelID = channel.Id
|
2018-10-03 13:13:19 -04:00
|
|
|
|
|
|
|
|
p.setConfiguration(configuration)
|
2018-07-13 10:29:50 -04:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 14:22:48 -05:00
|
|
|
// MessageHasBeenPosted automatically replies to posts that plea for help.
|
2018-07-13 10:29:50 -04:00
|
|
|
func (p *HelpPlugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {
|
2018-10-03 13:13:19 -04:00
|
|
|
configuration := p.getConfiguration()
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
// Ignore posts not in the configured channel
|
2019-12-05 14:31:53 -05:00
|
|
|
if post.ChannelId != configuration.channelID {
|
2018-07-13 10:29:50 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore posts this plugin made.
|
2020-03-13 16:12:20 -04:00
|
|
|
if sentByPlugin, _ := post.GetProp("sent_by_plugin").(bool); sentByPlugin {
|
2018-07-13 10:29:50 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore posts without a plea for help.
|
|
|
|
|
if !strings.Contains(post.Message, "help") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.API.SendEphemeralPost(post.UserId, &model.Post{
|
2019-12-05 14:31:53 -05:00
|
|
|
ChannelId: configuration.channelID,
|
2022-03-16 07:47:57 -04:00
|
|
|
Message: "You asked for help? Checkout https://support.mattermost.com/hc/en-us",
|
2022-07-05 02:46:50 -04:00
|
|
|
Props: map[string]any{
|
2018-07-13 10:29:50 -04:00
|
|
|
"sent_by_plugin": true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Example_helpPlugin() {
|
|
|
|
|
plugin.ClientMain(&HelpPlugin{})
|
|
|
|
|
}
|