github.com/levb/mattermost-server@v5.3.1+incompatible/plugin/hooks.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package plugin
     5  
     6  import (
     7  	"io"
     8  	"net/http"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  )
    12  
    13  // These assignments are part of the wire protocol used to trigger hook events in plugins.
    14  //
    15  // Feel free to add more, but do not change existing assignments. Follow the naming convention of
    16  // <HookName>Id as the autogenerated glue code depends on that.
    17  const (
    18  	OnActivateId            = 0
    19  	OnDeactivateId          = 1
    20  	ServeHTTPId             = 2
    21  	OnConfigurationChangeId = 3
    22  	ExecuteCommandId        = 4
    23  	MessageWillBePostedId   = 5
    24  	MessageWillBeUpdatedId  = 6
    25  	MessageHasBeenPostedId  = 7
    26  	MessageHasBeenUpdatedId = 8
    27  	UserHasJoinedChannelId  = 9
    28  	UserHasLeftChannelId    = 10
    29  	UserHasJoinedTeamId     = 11
    30  	UserHasLeftTeamId       = 12
    31  	ChannelHasBeenCreatedId = 13
    32  	FileWillBeUploadedId    = 14
    33  	UserWillLogInId         = 15
    34  	UserHasLoggedInId       = 16
    35  	TotalHooksId            = iota
    36  )
    37  
    38  // Hooks describes the methods a plugin may implement to automatically receive the corresponding
    39  // event.
    40  //
    41  // A plugin only need implement the hooks it cares about. The MattermostPlugin provides some
    42  // default implementations for convenience but may be overridden.
    43  type Hooks interface {
    44  	// OnActivate is invoked when the plugin is activated. If an error is returned, the plugin
    45  	// will be terminated. The plugin will not receive hooks until after OnActivate returns
    46  	// without error.
    47  	OnActivate() error
    48  
    49  	// Implemented returns a list of hooks that are implemented by the plugin.
    50  	// Plugins do not need to provide an implementation. Any given will be ignored.
    51  	Implemented() ([]string, error)
    52  
    53  	// OnDeactivate is invoked when the plugin is deactivated. This is the plugin's last chance to
    54  	// use the API, and the plugin will be terminated shortly after this invocation. The plugin
    55  	// will stop receiving hooks just prior to this method being called.
    56  	OnDeactivate() error
    57  
    58  	// OnConfigurationChange is invoked when configuration changes may have been made.
    59  	OnConfigurationChange() error
    60  
    61  	// ServeHTTP allows the plugin to implement the http.Handler interface. Requests destined for
    62  	// the /plugins/{id} path will be routed to the plugin.
    63  	//
    64  	// The Mattermost-User-Id header will be present if (and only if) the request is by an
    65  	// authenticated user.
    66  	ServeHTTP(c *Context, w http.ResponseWriter, r *http.Request)
    67  
    68  	// ExecuteCommand executes a command that has been previously registered via the RegisterCommand
    69  	// API.
    70  	ExecuteCommand(c *Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError)
    71  
    72  	// MessageWillBePosted is invoked when a message is posted by a user before it is committed
    73  	// to the database. If you also want to act on edited posts, see MessageWillBeUpdated.
    74  	//
    75  	// To reject a post, return an non-empty string describing why the post was rejected.
    76  	// To modify the post, return the replacement, non-nil *model.Post and an empty string.
    77  	// To allow the post without modification, return a nil *model.Post and an empty string.
    78  	//
    79  	// If you don't need to modify or reject posts, use MessageHasBeenPosted instead.
    80  	//
    81  	// Note that this method will be called for posts created by plugins, including the plugin that
    82  	// created the post.
    83  	MessageWillBePosted(c *Context, post *model.Post) (*model.Post, string)
    84  
    85  	// MessageWillBeUpdated is invoked when a message is updated by a user before it is committed
    86  	// to the database. If you also want to act on new posts, see MessageWillBePosted.
    87  	// Return values should be the modified post or nil if rejected and an explanation for the user.
    88  	// On rejection, the post will be kept in its previous state.
    89  	//
    90  	// If you don't need to modify or rejected updated posts, use MessageHasBeenUpdated instead.
    91  	//
    92  	// Note that this method will be called for posts updated by plugins, including the plugin that
    93  	// updated the post.
    94  	MessageWillBeUpdated(c *Context, newPost, oldPost *model.Post) (*model.Post, string)
    95  
    96  	// MessageHasBeenPosted is invoked after the message has been committed to the database.
    97  	// If you need to modify or reject the post, see MessageWillBePosted
    98  	// Note that this method will be called for posts created by plugins, including the plugin that
    99  	// created the post.
   100  	MessageHasBeenPosted(c *Context, post *model.Post)
   101  
   102  	// MessageHasBeenUpdated is invoked after a message is updated and has been updated in the database.
   103  	// If you need to modify or reject the post, see MessageWillBeUpdated
   104  	// Note that this method will be called for posts created by plugins, including the plugin that
   105  	// created the post.
   106  	MessageHasBeenUpdated(c *Context, newPost, oldPost *model.Post)
   107  
   108  	// ChannelHasBeenCreated is invoked after the channel has been committed to the database.
   109  	ChannelHasBeenCreated(c *Context, channel *model.Channel)
   110  
   111  	// UserHasJoinedChannel is invoked after the membership has been committed to the database.
   112  	// If actor is not nil, the user was invited to the channel by the actor.
   113  	UserHasJoinedChannel(c *Context, channelMember *model.ChannelMember, actor *model.User)
   114  
   115  	// UserHasLeftChannel is invoked after the membership has been removed from the database.
   116  	// If actor is not nil, the user was removed from the channel by the actor.
   117  	UserHasLeftChannel(c *Context, channelMember *model.ChannelMember, actor *model.User)
   118  
   119  	// UserHasJoinedTeam is invoked after the membership has been committed to the database.
   120  	// If actor is not nil, the user was added to the team by the actor.
   121  	UserHasJoinedTeam(c *Context, teamMember *model.TeamMember, actor *model.User)
   122  
   123  	// UserHasLeftTeam is invoked after the membership has been removed from the database.
   124  	// If actor is not nil, the user was removed from the team by the actor.
   125  	UserHasLeftTeam(c *Context, teamMember *model.TeamMember, actor *model.User)
   126  
   127  	// UserWillLogIn before the login of the user is returned. Returning a non empty string will reject the login event.
   128  	// If you don't need to reject the login event, see UserHasLoggedIn
   129  	UserWillLogIn(c *Context, user *model.User) string
   130  
   131  	// UserHasLoggedIn is invoked after a user has logged in.
   132  	UserHasLoggedIn(c *Context, user *model.User)
   133  
   134  	// FileWillBeUploaded is invoked when a file is uploaded, but before it is committed to backing store.
   135  	// Read from file to retrieve the body of the uploaded file.
   136  	//
   137  	// To reject a file upload, return an non-empty string describing why the file was rejected.
   138  	// To modify the file, write to the output and/or return a non-nil *model.FileInfo, as well as an empty string.
   139  	// To allow the file without modification, do not write to the output and return a nil *model.FileInfo and an empty string.
   140  	//
   141  	// Note that this method will be called for files uploaded by plugins, including the plugin that uploaded the post.
   142  	// FileInfo.Size will be automatically set properly if you modify the file.
   143  	FileWillBeUploaded(c *Context, info *model.FileInfo, file io.Reader, output io.Writer) (*model.FileInfo, string)
   144  }