github.com/trigonella/mattermost-server@v5.11.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 UserHasBeenCreatedId = 17 36 TotalHooksId = iota 37 ) 38 39 // Hooks describes the methods a plugin may implement to automatically receive the corresponding 40 // event. 41 // 42 // A plugin only need implement the hooks it cares about. The MattermostPlugin provides some 43 // default implementations for convenience but may be overridden. 44 type Hooks interface { 45 // OnActivate is invoked when the plugin is activated. If an error is returned, the plugin 46 // will be terminated. The plugin will not receive hooks until after OnActivate returns 47 // without error. 48 OnActivate() error 49 50 // Implemented returns a list of hooks that are implemented by the plugin. 51 // Plugins do not need to provide an implementation. Any given will be ignored. 52 Implemented() ([]string, error) 53 54 // OnDeactivate is invoked when the plugin is deactivated. This is the plugin's last chance to 55 // use the API, and the plugin will be terminated shortly after this invocation. The plugin 56 // will stop receiving hooks just prior to this method being called. 57 OnDeactivate() error 58 59 // OnConfigurationChange is invoked when configuration changes may have been made. Any 60 // returned error is logged, but does not stop the plugin. You must be prepared to handle 61 // a configuration failure gracefully. 62 OnConfigurationChange() error 63 64 // ServeHTTP allows the plugin to implement the http.Handler interface. Requests destined for 65 // the /plugins/{id} path will be routed to the plugin. 66 // 67 // The Mattermost-User-Id header will be present if (and only if) the request is by an 68 // authenticated user. 69 ServeHTTP(c *Context, w http.ResponseWriter, r *http.Request) 70 71 // ExecuteCommand executes a command that has been previously registered via the RegisterCommand 72 // API. 73 ExecuteCommand(c *Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) 74 75 // UserHasBeenCreated is invoked after a user was created. 76 // 77 // Minimum server version: 5.10 78 UserHasBeenCreated(c *Context, user *model.User) 79 80 // UserWillLogIn before the login of the user is returned. Returning a non empty string will reject the login event. 81 // If you don't need to reject the login event, see UserHasLoggedIn 82 UserWillLogIn(c *Context, user *model.User) string 83 84 // UserHasLoggedIn is invoked after a user has logged in. 85 UserHasLoggedIn(c *Context, user *model.User) 86 87 // MessageWillBePosted is invoked when a message is posted by a user before it is committed 88 // to the database. If you also want to act on edited posts, see MessageWillBeUpdated. 89 // 90 // To reject a post, return an non-empty string describing why the post was rejected. 91 // To modify the post, return the replacement, non-nil *model.Post and an empty string. 92 // To allow the post without modification, return a nil *model.Post and an empty string. 93 // 94 // If you don't need to modify or reject posts, use MessageHasBeenPosted instead. 95 // 96 // Note that this method will be called for posts created by plugins, including the plugin that 97 // created the post. 98 MessageWillBePosted(c *Context, post *model.Post) (*model.Post, string) 99 100 // MessageWillBeUpdated is invoked when a message is updated by a user before it is committed 101 // to the database. If you also want to act on new posts, see MessageWillBePosted. 102 // Return values should be the modified post or nil if rejected and an explanation for the user. 103 // On rejection, the post will be kept in its previous state. 104 // 105 // If you don't need to modify or rejected updated posts, use MessageHasBeenUpdated instead. 106 // 107 // Note that this method will be called for posts updated by plugins, including the plugin that 108 // updated the post. 109 MessageWillBeUpdated(c *Context, newPost, oldPost *model.Post) (*model.Post, string) 110 111 // MessageHasBeenPosted is invoked after the message has been committed to the database. 112 // If you need to modify or reject the post, see MessageWillBePosted 113 // Note that this method will be called for posts created by plugins, including the plugin that 114 // created the post. 115 MessageHasBeenPosted(c *Context, post *model.Post) 116 117 // MessageHasBeenUpdated is invoked after a message is updated and has been updated in the database. 118 // If you need to modify or reject the post, see MessageWillBeUpdated 119 // Note that this method will be called for posts created by plugins, including the plugin that 120 // created the post. 121 MessageHasBeenUpdated(c *Context, newPost, oldPost *model.Post) 122 123 // ChannelHasBeenCreated is invoked after the channel has been committed to the database. 124 ChannelHasBeenCreated(c *Context, channel *model.Channel) 125 126 // UserHasJoinedChannel is invoked after the membership has been committed to the database. 127 // If actor is not nil, the user was invited to the channel by the actor. 128 UserHasJoinedChannel(c *Context, channelMember *model.ChannelMember, actor *model.User) 129 130 // UserHasLeftChannel is invoked after the membership has been removed from the database. 131 // If actor is not nil, the user was removed from the channel by the actor. 132 UserHasLeftChannel(c *Context, channelMember *model.ChannelMember, actor *model.User) 133 134 // UserHasJoinedTeam is invoked after the membership has been committed to the database. 135 // If actor is not nil, the user was added to the team by the actor. 136 UserHasJoinedTeam(c *Context, teamMember *model.TeamMember, actor *model.User) 137 138 // UserHasLeftTeam is invoked after the membership has been removed from the database. 139 // If actor is not nil, the user was removed from the team by the actor. 140 UserHasLeftTeam(c *Context, teamMember *model.TeamMember, actor *model.User) 141 142 // FileWillBeUploaded is invoked when a file is uploaded, but before it is committed to backing store. 143 // Read from file to retrieve the body of the uploaded file. 144 // 145 // To reject a file upload, return an non-empty string describing why the file was rejected. 146 // To modify the file, write to the output and/or return a non-nil *model.FileInfo, as well as an empty string. 147 // To allow the file without modification, do not write to the output and return a nil *model.FileInfo and an empty string. 148 // 149 // Note that this method will be called for files uploaded by plugins, including the plugin that uploaded the post. 150 // FileInfo.Size will be automatically set properly if you modify the file. 151 FileWillBeUploaded(c *Context, info *model.FileInfo, file io.Reader, output io.Writer) (*model.FileInfo, string) 152 }