-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathmessage_hooks.go
304 lines (265 loc) · 9.28 KB
/
message_hooks.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"fmt"
"strconv"
"strings"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
)
// 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. Return values
// should be the modified post or nil if rejected and an explanation for the user.
//
// 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.
//
// This demo implementation rejects posts in the demo channel, as well as posts that @-mention
// the demo plugin user.
func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
configuration := p.getConfiguration()
if configuration.disabled {
return post, ""
}
// Always allow posts by the demo plugin user and demo plugin bot.
if post.UserId == p.botID || post.UserId == configuration.demoUserID {
return post, ""
}
// Reject posts by other users in the demo channels, effectively making it read-only.
for _, channelID := range configuration.demoChannelIDs {
if channelID == post.ChannelId {
p.API.SendEphemeralPost(post.UserId, &model.Post{
UserId: configuration.demoUserID,
ChannelId: channelID,
Message: "Posting is not allowed in this channel.",
})
return nil, "disallowing post in demo channel"
}
}
// Reject posts mentioning the demo plugin user.
if strings.Contains(post.Message, fmt.Sprintf("@%s", configuration.Username)) {
p.API.SendEphemeralPost(post.UserId, &model.Post{
UserId: configuration.demoUserID,
ChannelId: post.ChannelId,
Message: "Shh! You must not talk about the demo plugin user.",
})
return nil, plugin.DismissPostError
}
// Otherwise, allow the post through.
return post, ""
}
// 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.
//
// This demo implementation rejects posts that @-mention the demo plugin user.
func (p *Plugin) MessageWillBeUpdated(c *plugin.Context, newPost, oldPost *model.Post) (*model.Post, string) {
configuration := p.getConfiguration()
if configuration.disabled {
return newPost, ""
}
// Reject posts mentioning the demo plugin user.
if strings.Contains(newPost.Message, fmt.Sprintf("@%s", configuration.Username)) {
p.API.SendEphemeralPost(newPost.UserId, &model.Post{
UserId: configuration.demoUserID,
ChannelId: newPost.ChannelId,
Message: "You must not talk about the demo plugin user.",
})
return nil, "disallowing mention of demo plugin user"
}
// Otherwise, allow the post through.
return newPost, ""
}
// 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.
//
// This demo implementation logs a message to the demo channel whenever a message is posted,
// unless by the demo plugin user itself.
func (p *Plugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {
configuration := p.getConfiguration()
if configuration.disabled {
return
}
// Ignore posts by the demo plugin user and demo plugin bot.
if post.UserId == p.botID || post.UserId == configuration.demoUserID {
return
}
user, err := p.API.GetUser(post.UserId)
if err != nil {
p.API.LogError(
"Failed to query user",
"user_id", post.UserId,
"error", err.Error(),
)
return
}
channel, err := p.API.GetChannel(post.ChannelId)
if err != nil {
p.API.LogError(
"Failed to query channel",
"channel_id", post.ChannelId,
"error", err.Error(),
)
return
}
msg := fmt.Sprintf("MessageHasBeenPosted: @%s, ~%s", user.Username, channel.Name)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"Failed to post MessageHasBeenPosted message",
"channel_id", channel.Id,
"user_id", user.Id,
"error", err.Error(),
)
}
// Check if the Random Secret was posted
if strings.Contains(post.Message, configuration.RandomSecret) {
msg = fmt.Sprintf("The random secret %q has been entered by @%s!\n%s",
configuration.RandomSecret, user.Username, configuration.SecretMessage,
)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"Failed to post random secret message",
"channel_id", channel.Id,
"user_id", user.Id,
"error", err.Error(),
)
}
}
if strings.Contains(post.Message, strconv.Itoa(configuration.SecretNumber)) {
msg = fmt.Sprintf("The random number %d has been entered by @%s!",
configuration.SecretNumber, user.Username)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"Failed to post random secret message",
"channel_id", channel.Id,
"user_id", user.Id,
"error", err.Error(),
)
}
}
}
// 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.
//
// This demo implementation logs a message to the demo channel whenever a message is updated,
// unless by the demo plugin user itself.
func (p *Plugin) MessageHasBeenUpdated(c *plugin.Context, newPost, oldPost *model.Post) {
configuration := p.getConfiguration()
if configuration.disabled {
return
}
// Ignore updates by the demo plugin user.
if newPost.UserId == configuration.demoUserID {
return
}
user, err := p.API.GetUser(newPost.UserId)
if err != nil {
p.API.LogError(
"Failed to query user",
"user_id", newPost.UserId,
"error", err.Error(),
)
return
}
channel, err := p.API.GetChannel(newPost.ChannelId)
if err != nil {
p.API.LogError(
"Failed to query channel",
"channel_id", newPost.ChannelId,
"error", err.Error(),
)
return
}
msg := fmt.Sprintf("MessageHasBeenUpdated: @%s, ~%s", user.Username, channel.Name)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"Failed to post MessageHasBeenUpdated message",
"channel_id", channel.Id,
"user_id", user.Id,
"error", err.Error(),
)
}
// Check if the Random Secret was posted
if strings.Contains(newPost.Message, configuration.RandomSecret) {
msg = fmt.Sprintf("The random secret %q has been entered by @%s!\n%s",
configuration.RandomSecret, user.Username, configuration.SecretMessage,
)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"Failed to post random secret message",
"channel_id", channel.Id,
"user_id", user.Id,
"error", err.Error(),
)
}
}
}
// MessageHasBeenDeleted is invoked after a message is mark as deleted in the
// database. If you need to modify or reject the post, see MessageWillBeUpdated
// Note that this method will be called for posts deleted by plugins, including
// the plugin that deleted the post.
//
// This demo implementation logs a message to the demo channel whenever a message is deleted.
func (p *Plugin) MessageHasBeenDeleted(c *plugin.Context, post *model.Post) {
configuration := p.getConfiguration()
if configuration.disabled {
return
}
// Ignore updates by the demo plugin user.
if post.UserId == configuration.demoUserID {
return
}
user, err := p.API.GetUser(post.UserId)
if err != nil {
p.API.LogError(
"Failed to query user",
"user_id", post.UserId,
"error", err.Error(),
)
return
}
channel, err := p.API.GetChannel(post.ChannelId)
if err != nil {
p.API.LogError(
"Failed to query channel",
"channel_id", post.ChannelId,
"error", err.Error(),
)
return
}
msg := fmt.Sprintf("MessageHasBeenDeleted: @%s, ~%s", user.Username, channel.Name)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"Failed to post MessageHasBeenDeleted message",
"channel_id", channel.Id,
"user_id", user.Id,
"error", err.Error(),
)
}
}
// MessagesWillBeConsumed is invoked before a message is sent to the client. It allows plugins to
// modify the message before it is sent to the client. Note that this method will be called for
// posts created by plugins, including the plugin that created the post.
//
// This demo implementation replaces "SECURE" prefix in the messages with "ENCRYPTED" prefix.
func (p *Plugin) MessagesWillBeConsumed(posts []*model.Post) []*model.Post {
configuration := p.getConfiguration()
if configuration.disabled {
return posts
}
for _, post := range posts {
// Replaces posts that include "SECURE" prefix with "ENCRYPTED" prefix.
if strings.HasPrefix(post.Message, "[SECURE]") {
post.Message = strings.Replace(post.Message, "[SECURE]", "[ENCRYPTED]", 1)
}
}
return posts
}