-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
webhook_local.go
113 lines (93 loc) · 3.24 KB
/
webhook_local.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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"encoding/json"
"net/http"
"github.com/mattermost/mattermost-server/v6/audit"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
)
func (api *API) InitWebhookLocal() {
api.BaseRoutes.IncomingHooks.Handle("", api.APILocal(localCreateIncomingHook)).Methods("POST")
api.BaseRoutes.IncomingHooks.Handle("", api.APILocal(getIncomingHooks)).Methods("GET")
api.BaseRoutes.IncomingHook.Handle("", api.APILocal(getIncomingHook)).Methods("GET")
api.BaseRoutes.IncomingHook.Handle("", api.APILocal(updateIncomingHook)).Methods("PUT")
api.BaseRoutes.IncomingHook.Handle("", api.APILocal(deleteIncomingHook)).Methods("DELETE")
api.BaseRoutes.OutgoingHooks.Handle("", api.APILocal(localCreateOutgoingHook)).Methods("POST")
api.BaseRoutes.OutgoingHooks.Handle("", api.APILocal(getOutgoingHooks)).Methods("GET")
api.BaseRoutes.OutgoingHook.Handle("", api.APILocal(getOutgoingHook)).Methods("GET")
api.BaseRoutes.OutgoingHook.Handle("", api.APILocal(updateOutgoingHook)).Methods("PUT")
api.BaseRoutes.OutgoingHook.Handle("", api.APILocal(deleteOutgoingHook)).Methods("DELETE")
}
func localCreateIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) {
var hook model.IncomingWebhook
if jsonErr := json.NewDecoder(r.Body).Decode(&hook); jsonErr != nil {
c.SetInvalidParam("incoming_webhook")
return
}
if hook.UserId == "" {
c.SetInvalidParam("user_id")
return
}
channel, err := c.App.GetChannel(hook.ChannelId)
if err != nil {
c.Err = err
return
}
if _, err = c.App.GetUser(hook.UserId); err != nil {
c.Err = err
return
}
auditRec := c.MakeAuditRecord("localCreateIncomingHook", audit.Fail)
defer c.LogAuditRec(auditRec)
auditRec.AddMeta("channel", channel)
c.LogAudit("attempt")
incomingHook, err := c.App.CreateIncomingWebhookForChannel(hook.UserId, channel, &hook)
if err != nil {
c.Err = err
return
}
auditRec.Success()
auditRec.AddMeta("hook", incomingHook)
c.LogAudit("success")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(incomingHook); err != nil {
mlog.Warn("Error while writing response", mlog.Err(err))
}
}
func localCreateOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) {
var hook model.OutgoingWebhook
if jsonErr := json.NewDecoder(r.Body).Decode(&hook); jsonErr != nil {
c.SetInvalidParam("outgoing_webhook")
return
}
auditRec := c.MakeAuditRecord("createOutgoingHook", audit.Fail)
defer c.LogAuditRec(auditRec)
auditRec.AddMeta("hook_id", hook.Id)
c.LogAudit("attempt")
if hook.CreatorId == "" {
c.SetInvalidParam("creator_id")
return
}
_, err := c.App.GetUser(hook.CreatorId)
if err != nil {
c.Err = err
return
}
rhook, err := c.App.CreateOutgoingWebhook(&hook)
if err != nil {
c.LogAudit("fail")
c.Err = err
return
}
auditRec.Success()
auditRec.AddMeta("hook_display", rhook.DisplayName)
auditRec.AddMeta("channel_id", rhook.ChannelId)
auditRec.AddMeta("team_id", rhook.TeamId)
c.LogAudit("success")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(rhook); err != nil {
mlog.Warn("Error while writing response", mlog.Err(err))
}
}