diff options
Diffstat (limited to 'app/webhook.go')
-rw-r--r-- | app/webhook.go | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/app/webhook.go b/app/webhook.go index 29f642ea8..f84086d94 100644 --- a/app/webhook.go +++ b/app/webhook.go @@ -102,7 +102,7 @@ func TriggerWebhook(payload *model.OutgoingWebhookPayload, hook *model.OutgoingW req, _ := http.NewRequest("POST", url, body) req.Header.Set("Content-Type", contentType) req.Header.Set("Accept", "application/json") - if resp, err := utils.HttpClient().Do(req); err != nil { + if resp, err := utils.HttpClient(false).Do(req); err != nil { l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.event_post.error"), err.Error()) } else { defer CloseBody(resp) @@ -533,3 +533,54 @@ func HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest) *mo return nil } + +func CreateCommandWebhook(commandId string, args *model.CommandArgs) (*model.CommandWebhook, *model.AppError) { + hook := &model.CommandWebhook{ + CommandId: commandId, + UserId: args.UserId, + ChannelId: args.ChannelId, + RootId: args.RootId, + ParentId: args.ParentId, + } + + if result := <-Srv.Store.CommandWebhook().Save(hook); result.Err != nil { + return nil, result.Err + } else { + return result.Data.(*model.CommandWebhook), nil + } +} + +func HandleCommandWebhook(hookId string, response *model.CommandResponse) *model.AppError { + if response == nil { + return model.NewAppError("HandleCommandWebhook", "web.command_webhook.parse.app_error", nil, "", http.StatusBadRequest) + } + + var hook *model.CommandWebhook + if result := <-Srv.Store.CommandWebhook().Get(hookId); result.Err != nil { + return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode) + } else { + hook = result.Data.(*model.CommandWebhook) + } + + var cmd *model.Command + if result := <-Srv.Store.Command().Get(hook.CommandId); result.Err != nil { + return model.NewAppError("HandleCommandWebhook", "web.command_webhook.command.app_error", nil, "err="+result.Err.Message, http.StatusBadRequest) + } else { + cmd = result.Data.(*model.Command) + } + + args := &model.CommandArgs{ + UserId: hook.UserId, + ChannelId: hook.ChannelId, + TeamId: cmd.TeamId, + RootId: hook.RootId, + ParentId: hook.ParentId, + } + + if result := <-Srv.Store.CommandWebhook().TryUse(hook.Id, 5); result.Err != nil { + return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode) + } + + _, err := HandleCommandResponse(cmd, args, response, false) + return err +} |