summaryrefslogtreecommitdiffstats
path: root/plugin/hooks.go
blob: b5cff6f6b87565bb3736117a38b685dd5f10c81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package plugin

import (
	"io"
	"net/http"

	"github.com/mattermost/mattermost-server/model"
)

// These assignments are part of the wire protocol used to trigger hook events in plugins.
//
// Feel free to add more, but do not change existing assignments. Follow the naming convention of
// <HookName>Id as the autogenerated glue code depends on that.
const (
	OnActivateId            = 0
	OnDeactivateId          = 1
	ServeHTTPId             = 2
	OnConfigurationChangeId = 3
	ExecuteCommandId        = 4
	MessageWillBePostedId   = 5
	MessageWillBeUpdatedId  = 6
	MessageHasBeenPostedId  = 7
	MessageHasBeenUpdatedId = 8
	UserHasJoinedChannelId  = 9
	UserHasLeftChannelId    = 10
	UserHasJoinedTeamId     = 11
	UserHasLeftTeamId       = 12
	ChannelHasBeenCreatedId = 13
	FileWillBeUploadedId    = 14
	UserWillLogInId         = 15
	UserHasLoggedInId       = 16
	TotalHooksId            = iota
)

// Hooks describes the methods a plugin may implement to automatically receive the corresponding
// event.
//
// A plugin only need implement the hooks it cares about. The MattermostPlugin provides some
// default implementations for convenience but may be overridden.
type Hooks interface {
	// OnActivate is invoked when the plugin is activated. If an error is returned, the plugin
	// will be terminated. The plugin will not receive hooks until after OnActivate returns
	// without error.
	OnActivate() error

	// Implemented returns a list of hooks that are implemented by the plugin.
	// Plugins do not need to provide an implementation. Any given will be ignored.
	Implemented() ([]string, error)

	// 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. The plugin
	// will stop receiving hooks just prior to this method being called.
	OnDeactivate() error

	// OnConfigurationChange is invoked when configuration changes may have been made. Any
	// returned error is logged, but does not stop the plugin. You must be prepared to handle
	// a configuration failure gracefully.
	OnConfigurationChange() error

	// 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.
	ServeHTTP(c *Context, w http.ResponseWriter, r *http.Request)

	// ExecuteCommand executes a command that has been previously registered via the RegisterCommand
	// API.
	ExecuteCommand(c *Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError)

	// 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.
	//
	// To reject a post, return an non-empty string describing why the post was rejected.
	// To modify the post, return the replacement, non-nil *model.Post and an empty string.
	// To allow the post without modification, return a nil *model.Post and an empty string.
	//
	// 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.
	MessageWillBePosted(c *Context, post *model.Post) (*model.Post, string)

	// 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.
	MessageWillBeUpdated(c *Context, newPost, oldPost *model.Post) (*model.Post, string)

	// 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.
	MessageHasBeenPosted(c *Context, post *model.Post)

	// 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.
	MessageHasBeenUpdated(c *Context, newPost, oldPost *model.Post)

	// ChannelHasBeenCreated is invoked after the channel has been committed to the database.
	ChannelHasBeenCreated(c *Context, channel *model.Channel)

	// 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.
	UserHasJoinedChannel(c *Context, channelMember *model.ChannelMember, actor *model.User)

	// 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.
	UserHasLeftChannel(c *Context, channelMember *model.ChannelMember, actor *model.User)

	// 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.
	UserHasJoinedTeam(c *Context, teamMember *model.TeamMember, actor *model.User)

	// 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.
	UserHasLeftTeam(c *Context, teamMember *model.TeamMember, actor *model.User)

	// UserWillLogIn before the login of the user is returned. Returning a non empty string will reject the login event.
	// If you don't need to reject the login event, see UserHasLoggedIn
	UserWillLogIn(c *Context, user *model.User) string

	// UserHasLoggedIn is invoked after a user has logged in.
	UserHasLoggedIn(c *Context, user *model.User)

	// FileWillBeUploaded is invoked when a file is uploaded, but before it is committed to backing store.
	// Read from file to retrieve the body of the uploaded file.
	//
	// To reject a file upload, return an non-empty string describing why the file was rejected.
	// To modify the file, write to the output and/or return a non-nil *model.FileInfo, as well as an empty string.
	// To allow the file without modification, do not write to the output and return a nil *model.FileInfo and an empty string.
	//
	// Note that this method will be called for files uploaded by plugins, including the plugin that uploaded the post.
	// FileInfo.Size will be automatically set properly if you modify the file.
	FileWillBeUploaded(c *Context, info *model.FileInfo, file io.Reader, output io.Writer) (*model.FileInfo, string)
}