diff options
author | Chris <ccbrown112@gmail.com> | 2017-08-29 16:14:59 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-29 16:14:59 -0500 |
commit | 213a072b38d29d3c3ec8e150584685b1144a7d6a (patch) | |
tree | 1da10a494e49914b0f6641db79e7dcf8ad3886f6 /app/post.go | |
parent | 59798c137584a0b7e008ec713b489929dd83a690 (diff) | |
download | chat-213a072b38d29d3c3ec8e150584685b1144a7d6a.tar.gz chat-213a072b38d29d3c3ec8e150584685b1144a7d6a.tar.bz2 chat-213a072b38d29d3c3ec8e150584685b1144a7d6a.zip |
PLT-6403: Interactive messages (#7274)
* wip
* finish first pass
* requested changes
* add DoPostAction to Client4
Diffstat (limited to 'app/post.go')
-rw-r--r-- | app/post.go | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/app/post.go b/app/post.go index 5b83ab7a2..c852a90d2 100644 --- a/app/post.go +++ b/app/post.go @@ -4,8 +4,11 @@ package app import ( + "encoding/json" + "fmt" "net/http" "regexp" + "strings" l4g "github.com/alecthomas/log4go" "github.com/dyatlov/go-opengraph/opengraph" @@ -210,7 +213,7 @@ func parseSlackLinksToMarkdown(text string) string { return linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})") } -func SendEphemeralPost(teamId, userId string, post *model.Post) *model.Post { +func SendEphemeralPost(userId string, post *model.Post) *model.Post { post.Type = model.POST_EPHEMERAL // fill in fields which haven't been specified which have sensible defaults @@ -638,3 +641,65 @@ func GetOpenGraphMetadata(url string) *opengraph.OpenGraph { return og } + +func DoPostAction(postId string, actionId string, userId string) *model.AppError { + pchan := Srv.Store.Post().GetSingle(postId) + + var post *model.Post + if result := <-pchan; result.Err != nil { + return result.Err + } else { + post = result.Data.(*model.Post) + } + + action := post.GetAction(actionId) + if action == nil || action.Integration == nil { + return model.NewAppError("DoPostAction", "api.post.do_action.action_id.app_error", nil, fmt.Sprintf("action=%v", action), http.StatusNotFound) + } + + request := &model.PostActionIntegrationRequest{ + UserId: userId, + Context: action.Integration.Context, + } + + req, _ := http.NewRequest("POST", action.Integration.URL, strings.NewReader(request.ToJson())) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + resp, err := utils.HttpClient(false).Do(req) + if err != nil { + return model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, "err="+err.Error(), http.StatusBadRequest) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, fmt.Sprintf("status=%v", resp.StatusCode), http.StatusBadRequest) + } + + var response model.PostActionIntegrationResponse + if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { + return model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, "err="+err.Error(), http.StatusBadRequest) + } + + if response.Update != nil { + response.Update.Id = postId + response.Update.AddProp("from_webhook", "true") + if _, err := UpdatePost(response.Update, false); err != nil { + return err + } + } + + if response.EphemeralText != "" { + ephemeralPost := &model.Post{} + ephemeralPost.Message = parseSlackLinksToMarkdown(response.EphemeralText) + ephemeralPost.ChannelId = post.ChannelId + ephemeralPost.RootId = post.RootId + if ephemeralPost.RootId == "" { + ephemeralPost.RootId = post.Id + } + ephemeralPost.UserId = userId + ephemeralPost.AddProp("from_webhook", "true") + SendEphemeralPost(userId, ephemeralPost) + } + + return nil +} |