summaryrefslogtreecommitdiffstats
path: root/api4/webhook.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-03-13 10:40:43 -0400
committerGitHub <noreply@github.com>2017-03-13 10:40:43 -0400
commit19c67d7fe35f92ae8a288dcdb9877d3bede41a61 (patch)
tree1119f74c30564ce53d5e7dc566009594e348f494 /api4/webhook.go
parent3ebfb369530e28ca3246c5cd2833e666edce9c90 (diff)
downloadchat-19c67d7fe35f92ae8a288dcdb9877d3bede41a61.tar.gz
chat-19c67d7fe35f92ae8a288dcdb9877d3bede41a61.tar.bz2
chat-19c67d7fe35f92ae8a288dcdb9877d3bede41a61.zip
Implement GET and POST /hooks/outgoing endpoints for APIv4 (#5645)
Diffstat (limited to 'api4/webhook.go')
-rw-r--r--api4/webhook.go72
1 files changed, 70 insertions, 2 deletions
diff --git a/api4/webhook.go b/api4/webhook.go
index 19a851390..923f66ad3 100644
--- a/api4/webhook.go
+++ b/api4/webhook.go
@@ -17,15 +17,17 @@ func InitWebhook() {
BaseRoutes.IncomingHooks.Handle("", ApiSessionRequired(createIncomingHook)).Methods("POST")
BaseRoutes.IncomingHooks.Handle("", ApiSessionRequired(getIncomingHooks)).Methods("GET")
-
BaseRoutes.IncomingHook.Handle("", ApiSessionRequired(getIncomingHook)).Methods("GET")
BaseRoutes.IncomingHook.Handle("", ApiSessionRequired(deleteIncomingHook)).Methods("DELETE")
+
+ BaseRoutes.OutgoingHooks.Handle("", ApiSessionRequired(createOutgoingHook)).Methods("POST")
+ BaseRoutes.OutgoingHooks.Handle("", ApiSessionRequired(getOutgoingHooks)).Methods("GET")
}
func createIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) {
hook := model.IncomingWebhookFromJson(r.Body)
if hook == nil {
- c.SetInvalidParam("webhook")
+ c.SetInvalidParam("incoming_webhook")
return
}
@@ -53,6 +55,7 @@ func createIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) {
return
} else {
c.LogAudit("success")
+ w.WriteHeader(http.StatusCreated)
w.Write([]byte(incomingHook.ToJson()))
}
}
@@ -158,3 +161,68 @@ func deleteIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
}
+
+func createOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) {
+ hook := model.OutgoingWebhookFromJson(r.Body)
+ if hook == nil {
+ c.SetInvalidParam("outgoing_webhook")
+ return
+ }
+
+ c.LogAudit("attempt")
+
+ hook.CreatorId = c.Session.UserId
+
+ if !app.SessionHasPermissionToTeam(c.Session, hook.TeamId, model.PERMISSION_MANAGE_WEBHOOKS) {
+ c.SetPermissionError(model.PERMISSION_MANAGE_WEBHOOKS)
+ return
+ }
+
+ if rhook, err := app.CreateOutgoingWebhook(hook); err != nil {
+ c.LogAudit("fail")
+ c.Err = err
+ return
+ } else {
+ c.LogAudit("success")
+ w.WriteHeader(http.StatusCreated)
+ w.Write([]byte(rhook.ToJson()))
+ }
+}
+
+func getOutgoingHooks(c *Context, w http.ResponseWriter, r *http.Request) {
+ channelId := r.URL.Query().Get("channel_id")
+ teamId := r.URL.Query().Get("team_id")
+
+ var hooks []*model.OutgoingWebhook
+ var err *model.AppError
+
+ if len(channelId) > 0 {
+ if !app.SessionHasPermissionToChannel(c.Session, channelId, model.PERMISSION_MANAGE_WEBHOOKS) {
+ c.SetPermissionError(model.PERMISSION_MANAGE_WEBHOOKS)
+ return
+ }
+
+ hooks, err = app.GetOutgoingWebhooksForChannelPage(channelId, c.Params.Page, c.Params.PerPage)
+ } else if len(teamId) > 0 {
+ if !app.SessionHasPermissionToTeam(c.Session, teamId, model.PERMISSION_MANAGE_WEBHOOKS) {
+ c.SetPermissionError(model.PERMISSION_MANAGE_WEBHOOKS)
+ return
+ }
+
+ hooks, err = app.GetOutgoingWebhooksForTeamPage(teamId, c.Params.Page, c.Params.PerPage)
+ } else {
+ if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_WEBHOOKS) {
+ c.SetPermissionError(model.PERMISSION_MANAGE_WEBHOOKS)
+ return
+ }
+
+ hooks, err = app.GetOutgoingWebhooksPage(c.Params.Page, c.Params.PerPage)
+ }
+
+ if err != nil {
+ c.Err = err
+ return
+ }
+
+ w.Write([]byte(model.OutgoingWebhookListToJson(hooks)))
+}