forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.js
64 lines (51 loc) · 1.92 KB
/
hooks.js
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
export function runMessageWillBePostedHooks(originalPost) {
return async (dispatch, getState) => {
const hooks = getState().plugins.components.MessageWillBePosted;
if (!hooks || hooks.length === 0) {
return {data: originalPost};
}
let post = originalPost;
for (const hook of hooks) {
const result = await hook.hook(post); // eslint-disable-line no-await-in-loop
if (result) {
if (result.error) {
return {
error: result.error,
};
}
post = result.post;
}
}
return {data: post};
};
}
export function runSlashCommandWillBePostedHooks(originalMessage, originalArgs) {
return async (dispatch, getState) => {
const hooks = getState().plugins.components.SlashCommandWillBePosted;
if (!hooks || hooks.length === 0) {
return {data: {message: originalMessage, args: originalArgs}};
}
let message = originalMessage;
let args = originalArgs;
for (const hook of hooks) {
const result = await hook.hook(message, args); // eslint-disable-line no-await-in-loop
if (result) {
if (result.error) {
return {
error: result.error,
};
}
message = result.message;
args = result.args;
// The first plugin to consume the slash command by returning an empty object
// should terminate the processing by subsequent plugins.
if (Object.keys(result).length === 0) {
break;
}
}
}
return {data: {message, args}};
};
}