github.com/crspeller/mattermost-server@v0.0.0-20190328001957-a200beb3d111/plugin/example_help_test.go (about)

     1  package plugin_test
     2  
     3  import (
     4  	"strings"
     5  	"sync"
     6  
     7  	"github.com/pkg/errors"
     8  
     9  	"github.com/crspeller/mattermost-server/model"
    10  	"github.com/crspeller/mattermost-server/plugin"
    11  )
    12  
    13  // configuration represents the configuration for this plugin as exposed via the Mattermost
    14  // server configuration.
    15  type configuration struct {
    16  	TeamName    string
    17  	ChannelName string
    18  
    19  	// channelId is resolved when the public configuration fields above change
    20  	channelId string
    21  }
    22  
    23  type HelpPlugin struct {
    24  	plugin.MattermostPlugin
    25  
    26  	// configurationLock synchronizes access to the configuration.
    27  	configurationLock sync.RWMutex
    28  
    29  	// configuration is the active plugin configuration. Consult getConfiguration and
    30  	// setConfiguration for usage.
    31  	configuration *configuration
    32  }
    33  
    34  // getConfiguration retrieves the active configuration under lock, making it safe to use
    35  // concurrently. The active configuration may change underneath the client of this method, but
    36  // the struct returned by this API call is considered immutable.
    37  func (p *HelpPlugin) getConfiguration() *configuration {
    38  	p.configurationLock.RLock()
    39  	defer p.configurationLock.RUnlock()
    40  
    41  	if p.configuration == nil {
    42  		return &configuration{}
    43  	}
    44  
    45  	return p.configuration
    46  }
    47  
    48  // setConfiguration replaces the active configuration under lock.
    49  //
    50  // Do not call setConfiguration while holding the configurationLock, as sync.Mutex is not
    51  // reentrant.
    52  func (p *HelpPlugin) setConfiguration(configuration *configuration) {
    53  	// Replace the active configuration under lock.
    54  	p.configurationLock.Lock()
    55  	defer p.configurationLock.Unlock()
    56  	p.configuration = configuration
    57  }
    58  
    59  // OnConfigurationChange updates the active configuration for this plugin under lock.
    60  func (p *HelpPlugin) OnConfigurationChange() error {
    61  	var configuration = new(configuration)
    62  
    63  	// Load the public configuration fields from the Mattermost server configuration.
    64  	if err := p.API.LoadPluginConfiguration(configuration); err != nil {
    65  		return errors.Wrap(err, "failed to load plugin configuration")
    66  	}
    67  
    68  	team, err := p.API.GetTeamByName(configuration.TeamName)
    69  	if err != nil {
    70  		return errors.Wrapf(err, "failed to find team %s", configuration.TeamName)
    71  	}
    72  
    73  	channel, err := p.API.GetChannelByName(configuration.ChannelName, team.Id, false)
    74  	if err != nil {
    75  		return errors.Wrapf(err, "failed to find channel %s", configuration.ChannelName)
    76  	}
    77  
    78  	configuration.channelId = channel.Id
    79  
    80  	p.setConfiguration(configuration)
    81  
    82  	return nil
    83  }
    84  
    85  func (p *HelpPlugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {
    86  	configuration := p.getConfiguration()
    87  
    88  	// Ignore posts not in the configured channel
    89  	if post.ChannelId != configuration.channelId {
    90  		return
    91  	}
    92  
    93  	// Ignore posts this plugin made.
    94  	if sentByPlugin, _ := post.Props["sent_by_plugin"].(bool); sentByPlugin {
    95  		return
    96  	}
    97  
    98  	// Ignore posts without a plea for help.
    99  	if !strings.Contains(post.Message, "help") {
   100  		return
   101  	}
   102  
   103  	p.API.SendEphemeralPost(post.UserId, &model.Post{
   104  		ChannelId: configuration.channelId,
   105  		Message:   "You asked for help? Checkout https://about.mattermost.com/help/",
   106  		Props: map[string]interface{}{
   107  			"sent_by_plugin": true,
   108  		},
   109  	})
   110  }
   111  
   112  func Example_helpPlugin() {
   113  	plugin.ClientMain(&HelpPlugin{})
   114  }